Skip to content

Commit 9cb3f23

Browse files
author
Felipe Zimmerle
committedJun 9, 2017
Adds support to setrsc action
1 parent 616a95b commit 9cb3f23

File tree

10 files changed

+4672
-4383
lines changed

10 files changed

+4672
-4383
lines changed
 

‎Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ TESTS+=test/test-cases/regression/issue-960.json
126126
TESTS+=test/test-cases/regression/request-body-parser-xml-validade-dtd.json
127127
TESTS+=test/test-cases/regression/variable-TIME_MIN.json
128128
TESTS+=test/test-cases/regression/action-setuid.json
129+
TESTS+=test/test-cases/regression/action-setrsc.json
129130
TESTS+=test/test-cases/regression/issue-1152.json
130131
TESTS+=test/test-cases/regression/config-calling_phases_by_name.json
131132
TESTS+=test/test-cases/regression/variable-USERID.json

‎src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ ACTIONS = \
128128
actions/rev.cc \
129129
actions/rule_id.cc \
130130
actions/severity.cc \
131+
actions/set_rsc.cc \
131132
actions/set_sid.cc \
132133
actions/set_uid.cc \
133134
actions/set_var.cc \

‎src/actions/set_rsc.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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/actions/set_rsc.h"
17+
18+
#include <iostream>
19+
#include <string>
20+
21+
#include "modsecurity/transaction.h"
22+
#include "modsecurity/rule.h"
23+
#include "src/macro_expansion.h"
24+
25+
26+
namespace modsecurity {
27+
namespace actions {
28+
29+
30+
bool SetRSC::init(std::string *error) {
31+
m_collection_key = std::string(m_parser_payload, 0,
32+
m_parser_payload.length());
33+
34+
if (m_collection_key.empty()) {
35+
error->assign("Missing collection key");
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
42+
43+
bool SetRSC::evaluate(Rule *rule, Transaction *t) {
44+
std::string colNameExpanded = MacroExpansion::expand(m_collection_key, t);
45+
46+
#ifndef NO_LOGS
47+
t->debug(8, "RESOURCE initiated with value: \'"
48+
+ colNameExpanded + "\'.");
49+
#endif
50+
51+
t->m_collections.m_resource_collection_key = colNameExpanded;
52+
t->m_variableResource.set(colNameExpanded, t->m_variableOffset);
53+
54+
return true;
55+
}
56+
57+
} // namespace actions
58+
} // namespace modsecurity

‎src/actions/set_rsc.h

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 <string>
17+
18+
#include "modsecurity/actions/action.h"
19+
20+
#ifndef SRC_ACTIONS_SET_RSC_H_
21+
#define SRC_ACTIONS_SET_RSC_H_
22+
23+
class Transaction;
24+
25+
namespace modsecurity {
26+
class Transaction;
27+
namespace actions {
28+
29+
30+
class SetRSC : public Action {
31+
public:
32+
explicit SetRSC(std::string _action)
33+
: Action(_action) { }
34+
35+
bool evaluate(Rule *rule, Transaction *transaction) override;
36+
bool init(std::string *error) override;
37+
38+
private:
39+
std::string m_collection_key;
40+
};
41+
42+
43+
} // namespace actions
44+
} // namespace modsecurity
45+
46+
#endif // SRC_ACTIONS_SET_RSC_H_

‎src/parser/seclang-parser.cc

Lines changed: 382 additions & 382 deletions
Large diffs are not rendered by default.

‎src/parser/seclang-parser.hh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Driver;
8080
#include "src/actions/phase.h"
8181
#include "src/actions/rev.h"
8282
#include "src/actions/rule_id.h"
83+
#include "src/actions/set_rsc.h"
8384
#include "src/actions/set_sid.h"
8485
#include "src/actions/set_uid.h"
8586
#include "src/actions/set_var.h"
@@ -366,7 +367,7 @@ using modsecurity::operators::Operator;
366367

367368

368369

369-
#line 370 "seclang-parser.hh" // lalr1.cc:377
370+
#line 371 "seclang-parser.hh" // lalr1.cc:377
370371

371372
# include <cassert>
372373
# include <cstdlib> // std::abort
@@ -443,7 +444,7 @@ using modsecurity::operators::Operator;
443444

444445

445446
namespace yy {
446-
#line 447 "seclang-parser.hh" // lalr1.cc:377
447+
#line 448 "seclang-parser.hh" // lalr1.cc:377
447448

448449

449450

@@ -654,7 +655,7 @@ namespace yy {
654655
// "SanatiseRequestHeader"
655656
// "SanatiseResponseHeader"
656657
// "SetEnv"
657-
// "SetSrc"
658+
// "SetRsc"
658659
// "SetSid"
659660
// "SetUID"
660661
// "Severity"
@@ -2752,7 +2753,7 @@ namespace yy {
27522753
case 134: // "SanatiseRequestHeader"
27532754
case 135: // "SanatiseResponseHeader"
27542755
case 136: // "SetEnv"
2755-
case 137: // "SetSrc"
2756+
case 137: // "SetRsc"
27562757
case 138: // "SetSid"
27572758
case 139: // "SetUID"
27582759
case 140: // "Severity"
@@ -2990,7 +2991,7 @@ namespace yy {
29902991
case 134: // "SanatiseRequestHeader"
29912992
case 135: // "SanatiseResponseHeader"
29922993
case 136: // "SetEnv"
2993-
case 137: // "SetSrc"
2994+
case 137: // "SetRsc"
29942995
case 138: // "SetSid"
29952996
case 139: // "SetUID"
29962997
case 140: // "Severity"
@@ -3294,7 +3295,7 @@ namespace yy {
32943295
case 134: // "SanatiseRequestHeader"
32953296
case 135: // "SanatiseResponseHeader"
32963297
case 136: // "SetEnv"
3297-
case 137: // "SetSrc"
3298+
case 137: // "SetRsc"
32983299
case 138: // "SetSid"
32993300
case 139: // "SetUID"
33003301
case 140: // "Severity"
@@ -3538,7 +3539,7 @@ namespace yy {
35383539
case 134: // "SanatiseRequestHeader"
35393540
case 135: // "SanatiseResponseHeader"
35403541
case 136: // "SetEnv"
3541-
case 137: // "SetSrc"
3542+
case 137: // "SetRsc"
35423543
case 138: // "SetSid"
35433544
case 139: // "SetUID"
35443545
case 140: // "Severity"
@@ -5522,7 +5523,7 @@ namespace yy {
55225523

55235524

55245525
} // yy
5525-
#line 5526 "seclang-parser.hh" // lalr1.cc:377
5526+
#line 5527 "seclang-parser.hh" // lalr1.cc:377
55265527

55275528

55285529

‎src/parser/seclang-parser.yy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Driver;
4646
#include "src/actions/phase.h"
4747
#include "src/actions/rev.h"
4848
#include "src/actions/rule_id.h"
49+
#include "src/actions/set_rsc.h"
4950
#include "src/actions/set_sid.h"
5051
#include "src/actions/set_uid.h"
5152
#include "src/actions/set_var.h"
@@ -487,7 +488,7 @@ using modsecurity::operators::Operator;
487488
ACTION_SANATISE_REQUEST_HEADER "SanatiseRequestHeader"
488489
ACTION_SANATISE_RESPONSE_HEADER "SanatiseResponseHeader"
489490
ACTION_SETENV "SetEnv"
490-
ACTION_SETRSC "SetSrc"
491+
ACTION_SETRSC "SetRsc"
491492
ACTION_SETSID "SetSid"
492493
ACTION_SETUID "SetUID"
493494
ACTION_SEVERITY "Severity"
@@ -2182,7 +2183,7 @@ act:
21822183
}
21832184
| ACTION_SETRSC
21842185
{
2185-
ACTION_NOT_SUPPORTED("SetRSC", @0);
2186+
ACTION_CONTAINER($$, new actions::SetRSC($1));
21862187
}
21872188
| ACTION_SETSID
21882189
{

‎src/parser/seclang-scanner.cc

Lines changed: 4128 additions & 3990 deletions
Large diffs are not rendered by default.

‎src/parser/seclang-scanner.ll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,10 @@ EQUALS_MINUS (?i:=\-)
398398
{ACTION_SANATISE_MATCHED_BYTES} { return p::make_ACTION_SANATISE_MATCHED_BYTES(yytext, *driver.loc.back()); }
399399
{ACTION_SANATISE_REQUEST_HEADER} { return p::make_ACTION_SANATISE_REQUEST_HEADER(yytext, *driver.loc.back()); }
400400
{ACTION_SANATISE_RESPONSE_HEADER} { return p::make_ACTION_SANATISE_RESPONSE_HEADER(yytext, *driver.loc.back()); }
401-
{ACTION_SETRSC} { return p::make_ACTION_SETRSC(yytext, *driver.loc.back()); }
401+
{ACTION_SETRSC}:{VAR_FREE_TEXT_SPACE_COMMA} { return p::make_ACTION_SETRSC(yytext, *driver.loc.back()); }
402+
{ACTION_SETRSC}:'{VAR_FREE_TEXT_QUOTE}' { return p::make_ACTION_SETRSC(yytext, *driver.loc.back()); }
403+
{ACTION_SETRSC}:{VAR_FREE_TEXT_SPACE_COMMA} { return p::make_ACTION_SETRSC(yytext, *driver.loc.back()); }
404+
402405
{ACTION_STATUS} { return p::make_ACTION_STATUS(yytext, *driver.loc.back()); }
403406
{ACTION_ACCURACY}:'{FREE_TEXT_QUOTE}' { return p::make_ACTION_ACCURACY(yytext, *driver.loc.back()); }
404407
{ACTION_ACCURACY}:{FREE_TEXT_QUOTE} { return p::make_ACTION_ACCURACY(yytext, *driver.loc.back()); }
@@ -509,6 +512,10 @@ EQUALS_MINUS (?i:=\-)
509512
{VARIABLE_SESSION}(\:{DICT_ELEMENT_TWO})? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
510513
{VARIABLE_SESSION}(\.[\']{DICT_ELEMENT_TWO}[\'])? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
511514
{VARIABLE_SESSION}(\.{DICT_ELEMENT_TWO})? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
515+
{VARIABLE_RESOURCE}(\:[\']{DICT_ELEMENT_TWO}[\'])? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
516+
{VARIABLE_RESOURCE}(\:{DICT_ELEMENT_TWO})? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
517+
{VARIABLE_RESOURCE}(\.[\']{DICT_ELEMENT_TWO}[\'])? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
518+
{VARIABLE_RESOURCE}(\.{DICT_ELEMENT_TWO})? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
512519
{VARIABLE_IP}(\:[\']{DICT_ELEMENT_TWO}[\'])? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
513520
{VARIABLE_IP}(\:{DICT_ELEMENT_TWO})? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
514521
{VARIABLE_IP}(\.[\']{DICT_ELEMENT_TWO}[\'])? { BEGIN(SETVAR_ACTION_WAITING_OPERATION); return p::make_VARIABLE(yytext, *driver.loc.back()); }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"enabled":1,
4+
"version_min":300000,
5+
"title":"Testing setrsc action",
6+
"expected":{
7+
"debug_log": "Saving variable: RESOURCE:score with value: "
8+
},
9+
"client":{
10+
"ip":"200.249.12.31",
11+
"port":123
12+
},
13+
"request":{
14+
"headers":{
15+
"Host":"localhost",
16+
"User-Agent":"curl/7.38.0",
17+
"Accept":"*/*",
18+
"User-Agent":"My sweet little browser",
19+
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120"
20+
},
21+
"uri":"/?key=value&key=other_value",
22+
"method":"GET"
23+
},
24+
"server":{
25+
"ip":"200.249.12.31",
26+
"port":80
27+
},
28+
"rules":[
29+
"SecRuleEngine On",
30+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:'900018',phase:1,t:none,t:sha1,t:hexEncode,setrsc:'test',nolog,pass\"",
31+
"SecRule REQUEST_HEADERS \".*\" \"id:'900021',phase:1,setvar:RESOURCE.score=+10\"",
32+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:'900068',phase:1,t:none,t:sha1,t:hexEncode,setrsc:%{REQUEST_COOKIES:PHPSESSID}2,nolog,pass\"",
33+
"SecRule REQUEST_HEADERS \".*\" \"id:'900022',phase:1,setvar:RESOURCE.score=+5\""
34+
]
35+
}
36+
]

0 commit comments

Comments
 (0)
Please sign in to comment.