Skip to content

Commit e52bd7d

Browse files
author
Felipe Zimmerle
committed
Adds support to SecRuleScript directive
1 parent cb3363c commit e52bd7d

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
v3.0.????? - ?
33
---------------------------
44

5+
- Adds support to SecRuleScript directive.
6+
[Issue #994 - @zimmerle]
57
- Adds support for the exec action.
68
[Issue #1050 - @zimmerle]
79
- Adds support for transformations inside Lua engine

headers/modsecurity/rule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Rule {
5252
explicit Rule(std::string marker);
5353
~Rule();
5454

55-
bool evaluate(Transaction *transaction, std::shared_ptr<RuleMessage> rm);
55+
virtual bool evaluate(Transaction *transaction, std::shared_ptr<RuleMessage> rm);
5656
bool evaluateActions(Transaction *transaction);
5757
std::vector<std::unique_ptr<collection::Variable>>
5858
getFinalVars(Transaction *trasn);

src/parser/driver.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ int Driver::addSecAction(Rule *rule) {
6363
return true;
6464
}
6565

66+
67+
int Driver::addSecRuleScript(RuleScript *rule) {
68+
m_rules[rule->m_phase].push_back(rule);
69+
return true;
70+
}
71+
72+
6673
int Driver::addSecRule(Rule *rule) {
6774
if (rule->m_phase > modsecurity::Phases::NUMBER_OF_PHASES) {
6875
m_parserError << "Unknown phase: " << std::to_string(rule->m_phase);

src/parser/driver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "modsecurity/rules.h"
2929
#include "modsecurity/rules_properties.h"
3030
#include "modsecurity/audit_log.h"
31-
31+
#include "src/rule_script.h"
3232
#include "src/parser/seclang-parser.hh"
3333

3434
using modsecurity::Rule;
@@ -58,6 +58,7 @@ class Driver : public RulesProperties {
5858
int addSecRule(Rule *rule);
5959
int addSecAction(Rule *rule);
6060
int addSecMarker(std::string marker);
61+
int addSecRuleScript(RuleScript *rule);
6162

6263
bool scan_begin();
6364
void scan_end();

src/rule_script.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 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 security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include "src/rule_script.h"
17+
18+
19+
namespace modsecurity {
20+
21+
bool RuleScript::init(std::string *err) {
22+
return m_lua.load(m_name, err);
23+
}
24+
25+
bool RuleScript::evaluate(Transaction *trans,
26+
std::shared_ptr<RuleMessage> ruleMessage) {
27+
trans->debug(4, " Executing script: " + m_name + ".");
28+
bool containsDisruptive = false;
29+
30+
if (ruleMessage == NULL) {
31+
ruleMessage = std::shared_ptr<RuleMessage>(
32+
new RuleMessage(this, trans));
33+
}
34+
35+
executeActionsIndependentOfChainedRuleResult(trans,
36+
&containsDisruptive, ruleMessage);
37+
38+
bool ret = m_lua.run(trans);
39+
if (ret) {
40+
executeActionsAfterFullMatch(trans, containsDisruptive, ruleMessage);
41+
}
42+
43+
return ret;
44+
}
45+
46+
} // namespace modsecurity

src/rule_script.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/*
3+
* ModSecurity, http://www.modsecurity.org/
4+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
5+
*
6+
* You may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* If any of the files related to licensing are missing or if you have any
12+
* other questions related to licensing please contact Trustwave Holdings, Inc.
13+
* directly using the email address security@modsecurity.org.
14+
*
15+
*/
16+
17+
#include <string>
18+
#include "modsecurity/rule.h"
19+
#include "src/engine/lua.h"
20+
#include "src/operators/operator.h"
21+
#include "modsecurity/actions/action.h"
22+
#include "modsecurity/modsecurity.h"
23+
#include "src/actions/transformations/none.h"
24+
#include "src/actions/tag.h"
25+
#include "src/utils/string.h"
26+
#include "modsecurity/rules.h"
27+
#include "modsecurity/rule_message.h"
28+
#include "src/macro_expansion.h"
29+
#include "src/actions/msg.h"
30+
#include "src/actions/log_data.h"
31+
#include "src/actions/severity.h"
32+
#include "src/variables/variable.h"
33+
34+
#ifndef SRC_RULE_SCRIPT_H_
35+
#define SRC_RULE_SCRIPT_H_
36+
37+
38+
namespace modsecurity {
39+
40+
using actions::Action;
41+
42+
/** @ingroup ModSecurity_CPP_API */
43+
class RuleScript : public Rule {
44+
public:
45+
RuleScript(std::string name,
46+
std::vector<Action *> *actions,
47+
std::string fileName,
48+
int lineNumber
49+
) : Rule (NULL, NULL, actions, fileName, lineNumber),
50+
m_name(name) { };
51+
52+
bool init(std::string *err);
53+
bool evaluate(Transaction *trans,
54+
std::shared_ptr<RuleMessage> ruleMessage) override;
55+
56+
57+
std::string m_name;
58+
engine::Lua m_lua;
59+
};
60+
61+
} // namespace modsecurity
62+
63+
#endif // SRC_RULE_SCRIPT_H_
64+

0 commit comments

Comments
 (0)