|
21 | 21 | #define SRC_VARIABLES_RULE_H_
|
22 | 22 |
|
23 | 23 | #include "src/variables/variable.h"
|
| 24 | +#include "src/actions/severity.h" |
| 25 | +#include "src/actions/log_data.h" |
| 26 | +#include "src/actions/msg.h" |
| 27 | + |
24 | 28 |
|
25 | 29 | namespace modsecurity {
|
26 | 30 |
|
27 | 31 | class Transaction;
|
28 | 32 | namespace Variables {
|
29 | 33 |
|
30 | 34 |
|
31 |
| -DEFINE_VARIABLE_DICT(Rule, RULE, m_variableRule) |
| 35 | +class Rule_DictElement : public VariableDictElement { \ |
| 36 | + public: |
| 37 | + explicit Rule_DictElement(std::string dictElement) |
| 38 | + : VariableDictElement("RULE", dictElement) { } |
| 39 | + |
| 40 | + static void id(Transaction *t, |
| 41 | + Rule *rule, |
| 42 | + std::vector<const VariableValue *> *l) { |
| 43 | + if (!rule) { |
| 44 | + return; |
| 45 | + } |
| 46 | + std::unique_ptr<VariableOrigin> origin(new VariableOrigin()); |
| 47 | + std::string *a = new std::string(std::to_string(rule->m_ruleId)); |
| 48 | + VariableValue *var = new VariableValue( |
| 49 | + std::make_shared<std::string>("RULE:id"), |
| 50 | + a |
| 51 | + ); |
| 52 | + delete a; |
| 53 | + origin->m_offset = 0; |
| 54 | + origin->m_length = 0; |
| 55 | + var->m_orign.push_back(std::move(origin)); |
| 56 | + l->push_back(var); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + static void rev(Transaction *t, |
| 61 | + Rule *rule, |
| 62 | + std::vector<const VariableValue *> *l) { |
| 63 | + if (!rule) { |
| 64 | + return; |
| 65 | + } |
| 66 | + std::unique_ptr<VariableOrigin> origin(new VariableOrigin()); |
| 67 | + std::string *a = new std::string(rule->m_rev); |
| 68 | + VariableValue *var = new VariableValue( |
| 69 | + std::make_shared<std::string>("RULE:rev"), |
| 70 | + a |
| 71 | + ); |
| 72 | + delete a; |
| 73 | + origin->m_offset = 0; |
| 74 | + origin->m_length = 0; |
| 75 | + var->m_orign.push_back(std::move(origin)); |
| 76 | + l->push_back(var); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + static void severity(Transaction *t, |
| 81 | + Rule *rule, |
| 82 | + std::vector<const VariableValue *> *l) { |
| 83 | + if (rule && rule->m_severity) { |
| 84 | + std::unique_ptr<VariableOrigin> origin(new VariableOrigin()); |
| 85 | + std::string *a = new std::string(std::to_string(rule->m_severity->m_severity)); |
| 86 | + VariableValue *var = new VariableValue( |
| 87 | + std::make_shared<std::string>("RULE:severity"), |
| 88 | + a |
| 89 | + ); |
| 90 | + delete a; |
| 91 | + origin->m_offset = 0; |
| 92 | + origin->m_length = 0; |
| 93 | + var->m_orign.push_back(std::move(origin)); |
| 94 | + l->push_back(var); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + static void logData(Transaction *t, |
| 100 | + Rule *rule, |
| 101 | + std::vector<const VariableValue *> *l) { |
| 102 | + if (rule && rule->m_logData) { |
| 103 | + std::unique_ptr<VariableOrigin> origin(new VariableOrigin()); |
| 104 | + std::string *a = new std::string(rule->m_logData->data(t)); |
| 105 | + VariableValue *var = new VariableValue( |
| 106 | + std::make_shared<std::string>("RULE:logdata"), |
| 107 | + a |
| 108 | + ); |
| 109 | + delete a; |
| 110 | + origin->m_offset = 0; |
| 111 | + origin->m_length = 0; |
| 112 | + var->m_orign.push_back(std::move(origin)); |
| 113 | + l->push_back(var); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + static void msg(Transaction *t, |
| 118 | + Rule *rule, |
| 119 | + std::vector<const VariableValue *> *l) { |
| 120 | + if (rule && rule->m_msg) { |
| 121 | + std::unique_ptr<VariableOrigin> origin(new VariableOrigin()); |
| 122 | + std::string *a = new std::string(rule->m_msg->data(t)); |
| 123 | + VariableValue *var = new VariableValue( |
| 124 | + std::make_shared<std::string>("RULE:msg"), |
| 125 | + a |
| 126 | + ); |
| 127 | + delete a; |
| 128 | + origin->m_offset = 0; |
| 129 | + origin->m_length = 0; |
| 130 | + var->m_orign.push_back(std::move(origin)); |
| 131 | + l->push_back(var); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + void evaluate(Transaction *t, |
| 136 | + Rule *rule, |
| 137 | + std::vector<const VariableValue *> *l) override { |
| 138 | + if (m_dictElement == "id") { |
| 139 | + id(t, rule, l); |
| 140 | + return; |
| 141 | + } |
| 142 | + if (rule && m_dictElement == "rev") { |
| 143 | + rev(t, rule, l); |
| 144 | + return; |
| 145 | + } |
| 146 | + if (rule && m_dictElement == "severity") { |
| 147 | + severity(t, rule, l); |
| 148 | + return; |
| 149 | + } |
| 150 | + if (m_dictElement == "logdata") { |
| 151 | + logData(t, rule, l); |
| 152 | + return; |
| 153 | + } |
| 154 | + if (m_dictElement == "msg") { |
| 155 | + msg(t, rule, l); |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | + |
| 162 | +class Rule_DictElementRegexp : public VariableRegex { |
| 163 | + public: |
| 164 | + explicit Rule_DictElementRegexp(std::string regex) |
| 165 | + : VariableRegex("RULE", regex) { } |
| 166 | + |
| 167 | + void evaluate(Transaction *t, |
| 168 | + Rule *rule, |
| 169 | + std::vector<const VariableValue *> *l) override { |
| 170 | + if (Utils::regex_search("id", m_r) > 0) { |
| 171 | + Rule_DictElement::id(t, rule, l); |
| 172 | + return; |
| 173 | + } |
| 174 | + if (Utils::regex_search("rev", m_r) > 0) { |
| 175 | + Rule_DictElement::rev(t, rule, l); |
| 176 | + return; |
| 177 | + } |
| 178 | + if (Utils::regex_search("severity", m_r) > 0) { |
| 179 | + Rule_DictElement::severity(t, rule, l); |
| 180 | + return; |
| 181 | + } |
| 182 | + if (Utils::regex_search("logdata", m_r) > 0) { |
| 183 | + Rule_DictElement::logData(t, rule, l); |
| 184 | + return; |
| 185 | + } |
| 186 | + if (Utils::regex_search("msg", m_r) > 0) { |
| 187 | + Rule_DictElement::msg(t, rule, l); |
| 188 | + return; |
| 189 | + } |
| 190 | + } |
| 191 | +}; |
| 192 | + |
| 193 | + |
| 194 | +class Rule_NoDictElement : public Variable { |
| 195 | + public: |
| 196 | + explicit Rule_NoDictElement() |
| 197 | + : Variable("RULE") { } |
| 198 | + |
| 199 | + void evaluate(Transaction *t, |
| 200 | + Rule *rule, |
| 201 | + std::vector<const VariableValue *> *l) override { |
| 202 | + Rule_DictElement::id(t, rule, l); |
| 203 | + Rule_DictElement::rev(t, rule, l); |
| 204 | + Rule_DictElement::severity(t, rule, l); |
| 205 | + Rule_DictElement::logData(t, rule, l); |
| 206 | + Rule_DictElement::msg(t, rule, l); |
| 207 | + } |
| 208 | +}; |
| 209 | + |
| 210 | +// DEFINE_VARIABLE_DICT(Rule, RULE, m_variableRule) |
32 | 211 |
|
33 | 212 |
|
34 | 213 | } // namespace Variables
|
|
0 commit comments