forked from owasp-modsecurity/ModSecurity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_byte_range.cc
138 lines (113 loc) · 3.81 KB
/
validate_byte_range.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address [email protected].
*
*/
#include "src/operators/validate_byte_range.h"
#include <string>
#include <memory>
#include "src/operators/operator.h"
namespace modsecurity {
namespace operators {
bool ValidateByteRange::getRange(const std::string &rangeRepresentation,
std::string *error) {
size_t pos = rangeRepresentation.find_first_of("-");
int start;
int end;
if (pos == std::string::npos) {
try {
start = std::stoi(rangeRepresentation);
} catch(...) {
error->assign("Not able to convert '" + rangeRepresentation +
"' into a number");
return false;
}
table[start >> 3] = (table[start >> 3] | (1 << (start & 0x7)));
return true;
}
try {
start = std::stoi(std::string(rangeRepresentation, 0, pos));
} catch (...) {
error->assign("Not able to convert '" +
std::string(rangeRepresentation, 0, pos) +
"' into a number");
return false;
}
try {
end = std::stoi(std::string(rangeRepresentation, pos + 1,
rangeRepresentation.length() - (pos + 1)));
} catch (...) {
error->assign("Not able to convert '" + std::string(rangeRepresentation,
pos + 1, rangeRepresentation.length() - (pos + 1)) +
"' into a number");
return false;
}
if ((start < 0) || (start > 255)) {
error->assign("Invalid range start value: " +
std::to_string(start));
return false;
}
if ((end < 0) || (end > 255)) {
error->assign("Invalid range end value: " + std::to_string(end));
return false;
}
if (start > end) {
error->assign("Invalid range: " + std::to_string(start) + "-" +
std::to_string(end));
return false;
}
while (start <= end) {
table[start >> 3] = (table[start >> 3] | (1 << (start & 0x7)));
start++;
}
return true;
}
bool ValidateByteRange::init(const std::string &file,
std::string *error) {
size_t pos = m_param.find_first_of(",");
if (pos == std::string::npos) {
getRange(m_param, error);
} else {
getRange(std::string(m_param, 0, pos), error);
}
while (pos != std::string::npos) {
size_t next_pos = m_param.find_first_of(",", pos + 1);
if (next_pos == std::string::npos) {
getRange(std::string(m_param, pos + 1, m_param.length() -
(pos + 1)), error);
} else {
getRange(std::string(m_param, pos + 1, next_pos - (pos + 1)), error);
}
pos = next_pos;
}
return true;
}
bool ValidateByteRange::evaluate(Transaction *transaction, RuleWithActions *rule,
const std::string &input, std::shared_ptr<RuleMessage> ruleMessage) {
bool ret = true;
size_t count = 0;
for (int i = 0; i < input.length(); i++) {
int x = (unsigned char) input.at(i);
if (!(table[x >> 3] & (1 << (x & 0x7)))) {
// debug(9, "Value " + std::to_string(x) + " in " +
// input + " ouside range: " + param);
logOffset(ruleMessage, i, 1);
count++;
}
}
ret = (count != 0);
// debug(9, "Found %d byte(s) in %s outside range: %s.",
// count, var->name, rule->op_param);
return ret;
}
} // namespace operators
} // namespace modsecurity