Skip to content

Commit a2a4779

Browse files
committed
Adds support to the collection SESSION and setsid action
1 parent 33a704e commit a2a4779

File tree

11 files changed

+186
-12
lines changed

11 files changed

+186
-12
lines changed

headers/modsecurity/collection/collections.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace collection {
4545
class Collections :
4646
public std::unordered_map<std::string, Collection *> {
4747
public:
48-
Collections(Collection *global, Collection *ip);
48+
Collections(Collection *global, Collection *ip, Collection *session);
4949
~Collections();
5050

5151
void store(std::string key, std::string value);
@@ -86,9 +86,11 @@ class Collections :
8686

8787
std::string m_global_collection_key;
8888
std::string m_ip_collection_key;
89+
std::string m_session_collection_key;
8990

9091
Collection *m_global_collection;
9192
Collection *m_ip_collection;
93+
Collection *m_session_collection;
9294
};
9395

9496
} // namespace collection

headers/modsecurity/modsecurity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class ModSecurity {
224224

225225
collection::Collection *m_global_collection;
226226
collection::Collection *m_ip_collection;
227+
collection::Collection *m_session_collection;
227228

228229
private:
229230
std::string m_connector;

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ACTIONS = \
7979
actions/rev.cc \
8080
actions/rule_id.cc \
8181
actions/severity.cc \
82+
actions/set_sid.cc \
8283
actions/set_var.cc \
8384
actions/status.cc \
8485
actions/skip_after.cc \

src/actions/set_sid.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 "actions/set_sid.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+
#include "src/utils.h"
25+
26+
namespace modsecurity {
27+
namespace actions {
28+
29+
30+
bool SetSID::init(std::string *error) {
31+
m_collection_key = std::string(action, 0, action.length());
32+
33+
if (m_collection_key.empty()) {
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
41+
bool SetSID::evaluate(Rule *rule, Transaction *t) {
42+
std::string colNameExpanded = MacroExpansion::expand(m_collection_key, t);
43+
44+
#ifndef NO_LOGS
45+
t->debug(8, "Session ID initiated with value: \'"
46+
+ colNameExpanded + "\'.");
47+
#endif
48+
49+
t->m_collections.m_session_collection_key = colNameExpanded;
50+
51+
return true;
52+
}
53+
54+
} // namespace actions
55+
} // namespace modsecurity

src/actions/set_sid.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 "actions/action.h"
19+
20+
#ifndef SRC_ACTIONS_SET_SID_H_
21+
#define SRC_ACTIONS_SET_SID_H_
22+
23+
class Transaction;
24+
25+
namespace modsecurity {
26+
class Transaction;
27+
namespace actions {
28+
29+
30+
class SetSID : public Action {
31+
public:
32+
explicit SetSID(std::string _action)
33+
: Action(_action) { }
34+
35+
bool evaluate(Rule *rule, Transaction *transaction) override;
36+
bool init(std::string *error) override;
37+
private:
38+
std::string m_collection_key;
39+
};
40+
41+
42+
} // namespace actions
43+
} // namespace modsecurity
44+
45+
#endif // SRC_ACTIONS_SET_SID_H_

src/collection/collections.cc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ namespace collection {
3434

3535

3636
Collections::Collections(Collection *global,
37-
Collection *ip)
37+
Collection *ip, Collection *session)
3838
: m_global_collection_key(""),
3939
m_ip_collection_key(""),
4040
m_global_collection(global),
4141
m_ip_collection(ip),
42+
m_session_collection(session),
4243
m_transient(new backend::InMemoryPerProcess()) {
4344
/* Create collection TX */
4445
this->emplace("TX", new backend::InMemoryPerProcess());
@@ -69,6 +70,13 @@ void Collections::storeOrUpdateFirst(const std::string& collectionName,
6970
return;
7071
}
7172

73+
if (tolower(collectionName) == "session"
74+
&& !m_session_collection_key.empty()) {
75+
m_session_collection->storeOrUpdateFirst(collectionName + ":"
76+
+ variableName, m_session_collection_key, targetValue);
77+
return;
78+
}
79+
7280
try {
7381
Collection *collection;
7482
collection = this->at(collectionName);
@@ -137,6 +145,12 @@ std::string* Collections::resolveFirst(const std::string& collectionName,
137145
+ ":" + var, m_global_collection_key);
138146
}
139147

148+
if (tolower(collectionName) == "session"
149+
&& !m_session_collection_key.empty()) {
150+
return m_session_collection->resolveFirst(toupper(collectionName)
151+
+ ":" + var, m_session_collection_key);
152+
}
153+
140154
for (auto &a : *this) {
141155
if (tolower(a.first) == tolower(collectionName)) {
142156
std::string *res = a.second->resolveFirst(toupper(a.first)
@@ -175,6 +189,13 @@ void Collections::resolveSingleMatch(const std::string& var,
175189
return;
176190
}
177191

192+
if (tolower(collection) == "session"
193+
&& !m_session_collection_key.empty()) {
194+
m_session_collection->resolveSingleMatch(var,
195+
m_session_collection_key, l);
196+
return;
197+
}
198+
178199
try {
179200
this->at(collection)->resolveSingleMatch(var, l);
180201
} catch (...) { }
@@ -203,6 +224,13 @@ void Collections::resolveMultiMatches(const std::string& var,
203224
return;
204225
}
205226

227+
if (tolower(collection) == "session"
228+
&& !m_session_collection_key.empty()) {
229+
m_session_collection->resolveMultiMatches(var,
230+
m_session_collection_key, l);
231+
return;
232+
}
233+
206234
try {
207235
this->at(collection)->resolveMultiMatches(var, l);
208236
} catch (...) { }
@@ -231,6 +259,13 @@ void Collections::resolveRegularExpression(const std::string& var,
231259
return;
232260
}
233261

262+
if (tolower(collection) == "session"
263+
&& !m_session_collection_key.empty()) {
264+
m_session_collection->resolveRegularExpression(toupper(collection)
265+
+ ":" + var, m_session_collection_key, l);
266+
return;
267+
}
268+
234269
try {
235270
this->at(collection)->resolveRegularExpression(var, l);
236271
} catch (...) { }

src/modsecurity.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ModSecurity::ModSecurity()
4848
: m_connector(""),
4949
m_global_collection(new collection::backend::InMemoryPerProcess()),
5050
m_ip_collection(new collection::backend::InMemoryPerProcess()),
51+
m_session_collection(new collection::backend::InMemoryPerProcess()),
5152
m_logCb(NULL) {
5253
UniqueId::uniqueId();
5354
srand(time(NULL));

src/parser/seclang-parser.yy

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Driver;
2424
#include "actions/audit_log.h"
2525
#include "actions/ctl_audit_log_parts.h"
2626
#include "actions/init_col.h"
27+
#include "actions/set_sid.h"
2728
#include "actions/set_var.h"
2829
#include "actions/severity.h"
2930
#include "actions/skip_after.h"
@@ -66,6 +67,7 @@ using modsecurity::actions::Accuracy;
6667
using modsecurity::actions::Action;
6768
using modsecurity::actions::CtlAuditLogParts;
6869
using modsecurity::actions::InitCol;
70+
using modsecurity::actions::SetSID;
6971
using modsecurity::actions::SetVar;
7072
using modsecurity::actions::Severity;
7173
using modsecurity::actions::Tag;
@@ -906,7 +908,7 @@ act:
906908
TODO: setEnv is not implemented yet.
907909
908910
std::string error;
909-
SetEnv *setEnv = new SetEnv($1);
911+
SetEnv *setEnv = new s($1);
910912
911913
if (setEnv->init(&error) == false) {
912914
driver.parserError << error;
@@ -919,21 +921,15 @@ act:
919921
}
920922
| ACTION_SETSID
921923
{
922-
/*
923-
924-
TODO: setSID is not implemented yet.
925-
926924
std::string error;
927-
SetEnv *setSID = new SetSID($1);
925+
SetSID *setSID = new SetSID($1);
928926

929927
if (setSID->init(&error) == false) {
930928
driver.parserError << error;
931929
YYERROR;
932930
}
933931

934932
$$ = setSID;
935-
*/
936-
$$ = Action::instantiate($1);
937933
}
938934
| ACTION_SETUID
939935
{

src/transaction.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ Transaction::Transaction(ModSecurity *ms, Rules *rules, void *logCbData)
112112
m_creationTimeStamp(cpu_seconds()),
113113
m_logCbData(logCbData),
114114
m_ms(ms),
115-
m_collections(ms->m_global_collection, ms->m_ip_collection) {
115+
m_collections(ms->m_global_collection, ms->m_ip_collection,
116+
ms->m_session_collection) {
116117
m_id = std::to_string(this->m_timeStamp) + \
117118
std::to_string(generate_transaction_unique_id());
118119
m_rules->incrementReferenceCount();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"enabled":1,
4+
"version_min":300000,
5+
"title":"Testing setsid action",
6+
"expected":{
7+
"debug_log": "Saving variable: SESSION:score with value: 5"
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+
"SecDebugLog \/tmp\/modsec_debug.log",
31+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:'900018',phase:1,t:none,t:sha1,t:hexEncode,setsid:%{REQUEST_COOKIES:PHPSESSID}%,nolog,pass\"",
32+
"SecRule REQUEST_HEADERS \".*\" \"id:'900021',phase:1,setvar:SESSION.score=+10\"",
33+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:'900068',phase:1,t:none,t:sha1,t:hexEncode,setsid:%{REQUEST_COOKIES:PHPSESSID}2,nolog,pass\"",
34+
"SecRule REQUEST_HEADERS \".*\" \"id:'900022',phase:1,setvar:SESSION.score=+5\""
35+
]
36+
}
37+
]

0 commit comments

Comments
 (0)