Skip to content

Commit 758ecb5

Browse files
committed
Adds support to USER collection, setuid action and USERID variable
More details on: #1026, #1024, #1048
1 parent ff9aa5c commit 758ecb5

File tree

10 files changed

+115
-11
lines changed

10 files changed

+115
-11
lines changed

headers/modsecurity/collection/collections.h

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

5152
void store(std::string key, std::string value);
@@ -87,10 +88,12 @@ class Collections :
8788
std::string m_global_collection_key;
8889
std::string m_ip_collection_key;
8990
std::string m_session_collection_key;
91+
std::string m_user_collection_key;
9092

9193
Collection *m_global_collection;
9294
Collection *m_ip_collection;
9395
Collection *m_session_collection;
96+
Collection *m_user_collection;
9497
};
9598

9699
} // namespace collection

headers/modsecurity/modsecurity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class ModSecurity {
225225
collection::Collection *m_global_collection;
226226
collection::Collection *m_ip_collection;
227227
collection::Collection *m_session_collection;
228+
collection::Collection *m_user_collection;
228229

229230
private:
230231
std::string m_connector;

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ ACTIONS = \
8080
actions/rule_id.cc \
8181
actions/severity.cc \
8282
actions/set_sid.cc \
83+
actions/set_uid.cc \
8384
actions/set_var.cc \
8485
actions/status.cc \
8586
actions/skip_after.cc \

src/actions/set_uid.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_uid.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 SetUID::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 SetUID::evaluate(Rule *rule, Transaction *t) {
42+
std::string colNameExpanded = MacroExpansion::expand(m_collection_key, t);
43+
44+
#ifndef NO_LOGS
45+
t->debug(8, "User collection initiated with value: \'"
46+
+ colNameExpanded + "\'.");
47+
#endif
48+
49+
t->m_collections.m_user_collection_key = colNameExpanded;
50+
t->m_collections.storeOrUpdateFirst("USERID", colNameExpanded);
51+
52+
return true;
53+
}
54+
55+
} // namespace actions
56+
} // namespace modsecurity

src/actions/set_uid.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_UID_H_
21+
#define SRC_ACTIONS_SET_UID_H_
22+
23+
class Transaction;
24+
25+
namespace modsecurity {
26+
class Transaction;
27+
namespace actions {
28+
29+
30+
class SetUID : public Action {
31+
public:
32+
explicit SetUID(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_UID_H_

src/collection/collections.cc

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

3535

3636
Collections::Collections(Collection *global,
37-
Collection *ip, Collection *session)
37+
Collection *ip, Collection *session, Collection *user)
3838
: m_global_collection_key(""),
3939
m_ip_collection_key(""),
4040
m_global_collection(global),
4141
m_ip_collection(ip),
4242
m_session_collection(session),
43+
m_user_collection(user),
4344
m_transient(new backend::InMemoryPerProcess()) {
4445
/* Create collection TX */
4546
this->emplace("TX", new backend::InMemoryPerProcess());

src/modsecurity.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ModSecurity::ModSecurity()
4949
m_global_collection(new collection::backend::InMemoryPerProcess()),
5050
m_ip_collection(new collection::backend::InMemoryPerProcess()),
5151
m_session_collection(new collection::backend::InMemoryPerProcess()),
52+
m_user_collection(new collection::backend::InMemoryPerProcess()),
5253
m_logCb(NULL) {
5354
UniqueId::uniqueId();
5455
srand(time(NULL));

src/parser/seclang-parser.yy

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Driver;
2525
#include "actions/ctl_audit_log_parts.h"
2626
#include "actions/init_col.h"
2727
#include "actions/set_sid.h"
28+
#include "actions/set_uid.h"
2829
#include "actions/set_var.h"
2930
#include "actions/severity.h"
3031
#include "actions/skip_after.h"
@@ -68,6 +69,7 @@ using modsecurity::actions::Action;
6869
using modsecurity::actions::CtlAuditLogParts;
6970
using modsecurity::actions::InitCol;
7071
using modsecurity::actions::SetSID;
72+
using modsecurity::actions::SetUID;
7173
using modsecurity::actions::SetVar;
7274
using modsecurity::actions::Severity;
7375
using modsecurity::actions::Tag;
@@ -933,21 +935,15 @@ act:
933935
}
934936
| ACTION_SETUID
935937
{
936-
/*
937-
938-
TODO: setUID is not implemented yet.
939-
940938
std::string error;
941-
SetEnv *setUID = new SetUID($1);
939+
SetUID *setUID = new SetUID($1);
942940

943941
if (setUID->init(&error) == false) {
944942
driver.parserError << error;
945943
YYERROR;
946944
}
947945

948946
$$ = setUID;
949-
*/
950-
$$ = Action::instantiate($1);
951947
}
952948
| ACTION_SETVAR
953949
{

src/parser/seclang-scanner.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ OPERATOR_GEOIP (?i:@geoLookup)
116116
TRANSFORMATION t:(?i:(cmdLine|sha1|hexEncode|lowercase|urlDecodeUni|urlDecode|none|compressWhitespace|removeWhitespace|replaceNulls|removeNulls|htmlEntityDecode|jsDecode|cssDecode|trim|normalizePathWin|normalisePathWin|normalisePath|length|utf8toUnicode|urldecode|removeCommentsChar|removeComments|replaceComments))
117117

118118

119-
VARIABLE (?i:(RESOURCE|ARGS_COMBINED_SIZE|ARGS_GET_NAMES|ARGS_POST_NAMES|FILES_COMBINED_SIZE|FULL_REQUEST_LENGTH|REQUEST_BODY_LENGTH|REQUEST_URI_RAW|UNIQUE_ID|SERVER_PORT|SERVER_ADDR|REMOTE_PORT|REMOTE_HOST|MULTIPART_STRICT_ERROR|PATH_INFO|MULTIPART_CRLF_LF_LINES|MATCHED_VAR_NAME|MATCHED_VAR|INBOUND_DATA_ERROR|OUTBOUND_DATA_ERROR|FULL_REQUEST|AUTH_TYPE|ARGS_NAMES|REMOTE_ADDR|REQUEST_BASENAME|REQUEST_BODY|REQUEST_FILENAME|REQUEST_HEADERS_NAMES|REQUEST_METHOD|REQUEST_PROTOCOL|REQUEST_URI|RESPONSE_BODY|RESPONSE_CONTENT_LENGTH|RESPONSE_CONTENT_TYPE|RESPONSE_HEADERS_NAMES|RESPONSE_PROTOCOL|RESPONSE_STATUS|REQBODY_PROCESSOR|SESSIONID))
119+
VARIABLE (?i:(RESOURCE|ARGS_COMBINED_SIZE|ARGS_GET_NAMES|ARGS_POST_NAMES|FILES_COMBINED_SIZE|FULL_REQUEST_LENGTH|REQUEST_BODY_LENGTH|REQUEST_URI_RAW|UNIQUE_ID|SERVER_PORT|SERVER_ADDR|REMOTE_PORT|REMOTE_HOST|MULTIPART_STRICT_ERROR|PATH_INFO|MULTIPART_CRLF_LF_LINES|MATCHED_VAR_NAME|MATCHED_VAR|INBOUND_DATA_ERROR|OUTBOUND_DATA_ERROR|FULL_REQUEST|AUTH_TYPE|ARGS_NAMES|REMOTE_ADDR|REQUEST_BASENAME|REQUEST_BODY|REQUEST_FILENAME|REQUEST_HEADERS_NAMES|REQUEST_METHOD|REQUEST_PROTOCOL|REQUEST_URI|RESPONSE_BODY|RESPONSE_CONTENT_LENGTH|RESPONSE_CONTENT_TYPE|RESPONSE_HEADERS_NAMES|RESPONSE_PROTOCOL|RESPONSE_STATUS|REQBODY_PROCESSOR|USERID|SESSIONID))
120120
VARIABLE_COL (?i:(SESSION|GLOBAL|ARGS_POST|ARGS_GET|ARGS|FILES_SIZES|FILES_NAMES|FILES_TMP_CONTENT|MULTIPART_FILENAME|MULTIPART_NAME|MATCHED_VARS_NAMES|MATCHED_VARS|FILES|QUERY_STRING|REQUEST_COOKIES|REQUEST_HEADERS|RESPONSE_HEADERS|GEO|IP|XML|REQUEST_COOKIES_NAMES))
121121

122122
VARIABLE_TX (?i:TX)

src/transaction.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Transaction::Transaction(ModSecurity *ms, Rules *rules, void *logCbData)
113113
m_logCbData(logCbData),
114114
m_ms(ms),
115115
m_collections(ms->m_global_collection, ms->m_ip_collection,
116-
ms->m_session_collection) {
116+
ms->m_session_collection, ms->m_user_collection) {
117117
m_id = std::to_string(this->m_timeStamp) + \
118118
std::to_string(generate_transaction_unique_id());
119119
m_rules->incrementReferenceCount();

0 commit comments

Comments
 (0)