mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-15 23:55:03 +03:00
Adds support to the validateByteRange operator
This commit is contained in:
parent
c2d33823f5
commit
a05fa8287b
@ -22,21 +22,107 @@
|
|||||||
namespace ModSecurity {
|
namespace ModSecurity {
|
||||||
namespace operators {
|
namespace operators {
|
||||||
|
|
||||||
bool ValidateByteRange::evaluate(Assay *assay) {
|
bool ValidateByteRange::getRange(const std::string &rangeRepresentation,
|
||||||
/**
|
const char **error) {
|
||||||
* @todo Implement the operator ValidateByteRange.
|
size_t pos = param.find_first_of("-");
|
||||||
* Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#validateByteRange
|
int start;
|
||||||
*/
|
int end;
|
||||||
|
|
||||||
|
if (pos == std::string::npos) {
|
||||||
|
try {
|
||||||
|
start = std::stoi(rangeRepresentation);
|
||||||
|
} catch(...) {
|
||||||
|
*error = ("Not able to convert '" + rangeRepresentation + "' into a number").c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
table[start >> 3] = (table[start >> 3] | (1 << (start & 0x7)));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
start = std::stoi(std::string(rangeRepresentation, 0, pos));
|
||||||
|
} catch (...) {
|
||||||
|
*error = ("Not able to convert '" + std::string(rangeRepresentation, 0, pos) + "' into a number").c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
end = std::stoi(std::string(rangeRepresentation, pos + 1,
|
||||||
|
rangeRepresentation.length() - (pos + 1)));
|
||||||
|
} catch (...) {
|
||||||
|
*error = ("Not able to convert '" + std::string(rangeRepresentation, pos + 1,
|
||||||
|
rangeRepresentation.length() - (pos + 1)) + "' into a number").c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((start < 0) || (start > 255)) {
|
||||||
|
*error = ("Invalid range start value: " + std::to_string(start)).c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((end < 0) || (end > 255)) {
|
||||||
|
*error = ("Invalid range end value: " + std::to_string(end)).c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (start > end) {
|
||||||
|
*error = ("Invalid range: " + std::to_string(start) + "-" +
|
||||||
|
std::to_string(end)).c_str();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(start <= end) {
|
||||||
|
table[start >> 3] = (table[start >> 3] | (1 << (start & 0x7)));
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ValidateByteRange::ValidateByteRange(std::string op, std::string param,
|
bool ValidateByteRange::init(const char **error) {
|
||||||
bool negation)
|
size_t pos = param.find_first_of(",");
|
||||||
: Operator() {
|
|
||||||
this->op = op;
|
if (pos == std::string::npos) {
|
||||||
this->param = param;
|
getRange(param, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (pos != std::string::npos) {
|
||||||
|
size_t next_pos = param.find_first_of(",", pos + 1);
|
||||||
|
if (next_pos == std::string::npos) {
|
||||||
|
getRange(std::string(param, pos + 1, param.length() - (pos + 1)), error);
|
||||||
|
} else {
|
||||||
|
getRange(std::string(param, pos + 1, next_pos), error);
|
||||||
|
}
|
||||||
|
pos = next_pos;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool ValidateByteRange::evaluate(Assay *assay, const std::string &input) {
|
||||||
|
bool ret = true;
|
||||||
|
|
||||||
|
size_t count = 0;
|
||||||
|
for(int i = 0; i < input.length(); i++) {
|
||||||
|
int x = input.at(i);
|
||||||
|
if (!(table[x >> 3] & (1 << (x & 0x7)))) {
|
||||||
|
//debug(9, "Value " + std::to_string(x) + " in " + input + " ouside range: " + param);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = (count != 0);
|
||||||
|
//if (count == 0) return 0;
|
||||||
|
|
||||||
|
//debug(9, "Found %d byte(s) in %s outside range: %s.",
|
||||||
|
//count, var->name, rule->op_param);
|
||||||
|
|
||||||
|
if (negation) {
|
||||||
|
return !ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace operators
|
} // namespace operators
|
||||||
} // namespace ModSecurity
|
} // namespace ModSecurity
|
||||||
|
@ -27,8 +27,17 @@ namespace operators {
|
|||||||
class ValidateByteRange : public Operator {
|
class ValidateByteRange : public Operator {
|
||||||
public:
|
public:
|
||||||
/** @ingroup ModSecurity_Operator */
|
/** @ingroup ModSecurity_Operator */
|
||||||
ValidateByteRange(std::string o, std::string p, bool i);
|
ValidateByteRange(std::string op, std::string param, bool negation)
|
||||||
bool evaluate(Assay *assay);
|
: Operator(op, param, negation) { }
|
||||||
|
|
||||||
|
~ValidateByteRange() override { }
|
||||||
|
|
||||||
|
bool evaluate(Assay *assay, const std::string &input) override;
|
||||||
|
bool getRange(const std::string &rangeRepresentation, const char **error);
|
||||||
|
bool init(const char **error) override;
|
||||||
|
private:
|
||||||
|
std::vector<std::string> ranges;
|
||||||
|
char table[32];
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace operators
|
} // namespace operators
|
||||||
|
Loading…
x
Reference in New Issue
Block a user