Skip to content

Commit 2672db1

Browse files
martinhsvzimmerle
authored andcommitted
Add support for new operator rxGlobal
1 parent 785958f commit 2672db1

File tree

14 files changed

+7070
-6740
lines changed

14 files changed

+7070
-6740
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- Add support for new operator rxGlobal
5+
[@martinhsv]
46
- Fix maxminddb link on FreeBSD
57
[Issue #2131 - @granalberto, @zimmerle]
68
- Fix IP address logging in Section A

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ TESTS+=test/test-cases/regression/operator-inpectFile.json
171171
TESTS+=test/test-cases/regression/operator-ipMatchFromFile.json
172172
TESTS+=test/test-cases/regression/operator-pm.json
173173
TESTS+=test/test-cases/regression/operator-rx.json
174+
TESTS+=test/test-cases/regression/operator-rxGlobal.json
174175
TESTS+=test/test-cases/regression/operator-UnconditionalMatch.json
175176
TESTS+=test/test-cases/regression/operator-validate-byte-range.json
176177
TESTS+=test/test-cases/regression/operator-verifycc.json
@@ -290,6 +291,7 @@ TESTS+=test/test-cases/secrules-language-tests/operators/noMatch.json
290291
TESTS+=test/test-cases/secrules-language-tests/operators/pmFromFile.json
291292
TESTS+=test/test-cases/secrules-language-tests/operators/pm.json
292293
TESTS+=test/test-cases/secrules-language-tests/operators/rx.json
294+
TESTS+=test/test-cases/secrules-language-tests/operators/rxGlobal.json
293295
TESTS+=test/test-cases/secrules-language-tests/operators/streq.json
294296
TESTS+=test/test-cases/secrules-language-tests/operators/strmatch.json
295297
TESTS+=test/test-cases/secrules-language-tests/operators/unconditionalMatch.json

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ OPERATORS = \
221221
operators/rbl.cc \
222222
operators/rsub.cc \
223223
operators/rx.cc \
224+
operators/rx_global.cc \
224225
operators/str_eq.cc \
225226
operators/str_match.cc \
226227
operators/validate_byte_range.cc \

src/operators/operator.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "src/operators/rbl.h"
4848
#include "src/operators/rsub.h"
4949
#include "src/operators/rx.h"
50+
#include "src/operators/rx_global.h"
5051
#include "src/operators/str_eq.h"
5152
#include "src/operators/str_match.h"
5253
#include "src/operators/validate_byte_range.h"
@@ -169,6 +170,7 @@ Operator *Operator::instantiate(std::string op, std::string param_str) {
169170
IF_MATCH(rbl) { return new Rbl(std::move(param)); }
170171
IF_MATCH(rsub) { return new Rsub(std::move(param)); }
171172
IF_MATCH(rx) { return new Rx(std::move(param)); }
173+
IF_MATCH(rxglobal) { return new RxGlobal(std::move(param)); }
172174
IF_MATCH(streq) { return new StrEq(std::move(param)); }
173175
IF_MATCH(strmatch) { return new StrMatch(std::move(param)); }
174176
IF_MATCH(validatebyterange) {

src/operators/rx_global.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2020 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address [email protected].
13+
*
14+
*/
15+
16+
#include "src/operators/rx_global.h"
17+
18+
#include <string>
19+
#include <list>
20+
#include <memory>
21+
22+
#include "src/operators/operator.h"
23+
#include "modsecurity/rule.h"
24+
#include "modsecurity/rule_message.h"
25+
26+
namespace modsecurity {
27+
namespace operators {
28+
29+
30+
bool RxGlobal::init(const std::string &arg, std::string *error) {
31+
if (m_string->m_containsMacro == false) {
32+
m_re = new Regex(m_param);
33+
}
34+
35+
return true;
36+
}
37+
38+
39+
bool RxGlobal::evaluate(Transaction *transaction, RuleWithActions *rule,
40+
const std::string& input, std::shared_ptr<RuleMessage> ruleMessage) {
41+
Regex *re;
42+
43+
if (m_param.empty() && !m_string->m_containsMacro) {
44+
return true;
45+
}
46+
47+
if (m_string->m_containsMacro) {
48+
std::string eparam(m_string->evaluate(transaction));
49+
re = new Regex(eparam);
50+
} else {
51+
re = m_re;
52+
}
53+
54+
std::vector<Utils::SMatchCapture> captures;
55+
re->searchGlobal(input, captures);
56+
57+
if (rule && rule->hasCaptureAction() && transaction) {
58+
for (const Utils::SMatchCapture& capture : captures) {
59+
const std::string capture_substring(input.substr(capture.m_offset,capture.m_length));
60+
transaction->m_collections.m_tx_collection->storeOrUpdateFirst(
61+
std::to_string(capture.m_group), capture_substring);
62+
ms_dbg_a(transaction, 7, "Added regex subexpression TX." +
63+
std::to_string(capture.m_group) + ": " + capture_substring);
64+
transaction->m_matched.push_back(capture_substring);
65+
}
66+
}
67+
68+
for (const auto & capture : captures) {
69+
logOffset(ruleMessage, capture.m_offset, capture.m_length);
70+
}
71+
72+
if (m_string->m_containsMacro) {
73+
delete re;
74+
}
75+
76+
if (captures.size() > 0) {
77+
return true;
78+
}
79+
80+
return false;
81+
}
82+
83+
84+
} // namespace operators
85+
} // namespace modsecurity

src/operators/rx_global.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2020 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address [email protected].
13+
*
14+
*/
15+
16+
#ifndef SRC_OPERATORS_RX_GLOBAL_H_
17+
#define SRC_OPERATORS_RX_GLOBAL_H_
18+
19+
#include <string>
20+
//#include <list>
21+
#include <memory>
22+
#include <utility>
23+
24+
#include "src/operators/operator.h"
25+
#include "src/utils/regex.h"
26+
27+
28+
namespace modsecurity {
29+
using Utils::SMatch;
30+
using Utils::regex_search;
31+
using Utils::Regex;
32+
33+
namespace operators {
34+
35+
36+
class RxGlobal : public Operator {
37+
public:
38+
/** @ingroup ModSecurity_Operator */
39+
explicit RxGlobal(std::unique_ptr<RunTimeString> param)
40+
: m_re(nullptr),
41+
Operator("RxGlobal", std::move(param)) {
42+
m_couldContainsMacro = true;
43+
}
44+
45+
~RxGlobal() {
46+
if (m_string->m_containsMacro == false && m_re != NULL) {
47+
delete m_re;
48+
m_re = NULL;
49+
}
50+
}
51+
52+
bool evaluate(Transaction *transaction, RuleWithActions *rule,
53+
const std::string& input,
54+
std::shared_ptr<RuleMessage> ruleMessage) override;
55+
56+
bool init(const std::string &arg, std::string *error) override;
57+
58+
private:
59+
Regex *m_re;
60+
};
61+
62+
63+
} // namespace operators
64+
} // namespace modsecurity
65+
66+
67+
#endif // SRC_OPERATORS_RX_GLOBAL_H_

0 commit comments

Comments
 (0)