Skip to content

Commit 2e2417f

Browse files
marshal09zimmerle
authored andcommitted
Add new transformation call phpArgsNames
1 parent ca52e88 commit 2e2417f

File tree

12 files changed

+6832
-6569
lines changed

12 files changed

+6832
-6569
lines changed

CHANGES

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

4-
5-
4+
- EXPERIMENTAL: Add new transformation call phpArgsNames
5+
[Issue #2387 - @marshal09]
66

77

88
v3.0.5 - 2021-Jul-07

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ TESTS+=test/test-cases/secrules-language-tests/transformations/htmlEntityDecode.
319319
TESTS+=test/test-cases/secrules-language-tests/transformations/jsDecode.json
320320
TESTS+=test/test-cases/secrules-language-tests/transformations/length.json
321321
TESTS+=test/test-cases/secrules-language-tests/transformations/lowercase.json
322+
TESTS+=test/test-cases/secrules-language-tests/transformations/phpArgsNames.json
322323
TESTS+=test/test-cases/secrules-language-tests/transformations/md5.json
323324
TESTS+=test/test-cases/secrules-language-tests/transformations/normalisePath.json
324325
TESTS+=test/test-cases/secrules-language-tests/transformations/normalisePathWin.json

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ ACTIONS = \
167167
actions/transformations/js_decode.cc \
168168
actions/transformations/length.cc \
169169
actions/transformations/lower_case.cc \
170+
actions/transformations/php_args_names.cc \
170171
actions/transformations/md5.cc \
171172
actions/transformations/none.cc \
172173
actions/transformations/normalise_path.cc \
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 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/actions/transformations/php_args_names.h"
17+
18+
#include <algorithm>
19+
#include <string>
20+
#include <string.h>
21+
22+
#include "modsecurity/transaction.h"
23+
#include "src/actions/transformations/transformation.h"
24+
#include "modsecurity/actions/action.h"
25+
26+
namespace modsecurity {
27+
namespace actions {
28+
namespace transformations {
29+
30+
31+
PhpArgsNames::PhpArgsNames(const std::string &a)
32+
: Transformation(a) {
33+
}
34+
35+
std::string PhpArgsNames::evaluate(const std::string &val,
36+
Transaction *transaction) {
37+
//Took the logic from php src code:
38+
//https://github.com/php/php-src/blob/master/main/php_variables.c
39+
//Function call PHPAPI void php_register_variable_ex(const char *var_name, zval *val, zval *track_vars_array)
40+
std::string value(val);
41+
std::string ret = "";
42+
if(value[0] == '[' || value[0] == '=') {
43+
return ret;
44+
}
45+
std::string::size_type i = 0;
46+
while(value[i] == ' ') {
47+
i++;
48+
}
49+
std::string::size_type val_size = value.length();
50+
bool is_array = false;
51+
bool is_open_sq_bracket = false;
52+
for (; i < val_size; ++i) {
53+
if(value[i] == '[' && !is_open_sq_bracket) {
54+
if(strchr(&value[i], ']') != NULL) {
55+
is_array = true;
56+
break;
57+
}
58+
59+
ret += '_';
60+
is_open_sq_bracket = true;
61+
}
62+
else if( !is_open_sq_bracket && (value[i] == ' ' || value[i] == '.') ) {
63+
ret += '_';
64+
}
65+
else {
66+
ret += value[i];
67+
}
68+
}
69+
70+
if(is_array) {
71+
char* start = &value[0];
72+
while(true) {
73+
char *tmp = &value[i];
74+
char *close_bra = strchr(tmp, ']');
75+
if(close_bra == NULL) {
76+
return ret;
77+
}
78+
int array_size = (int)(close_bra - start) + 1;
79+
if(array_size - i == 3 && value[i+1] == ' ') {
80+
ret += '[';
81+
i+=2;
82+
}
83+
for(;i < array_size; ++i) {
84+
ret += value[i];
85+
}
86+
if(i >= val_size || value[i] != '[') {
87+
return ret;
88+
}
89+
}
90+
}
91+
return ret;
92+
93+
}
94+
95+
} // namespace transformations
96+
} // namespace actions
97+
} // namespace modsecurity
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 - 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 <string>
17+
#include <unordered_map>
18+
19+
#include "modsecurity/actions/action.h"
20+
#include "src/actions/transformations/transformation.h"
21+
22+
#ifndef SRC_ACTIONS_TRANSFORMATIONS_PHP_ARGS_NAMES_H_
23+
#define SRC_ACTIONS_TRANSFORMATIONS_PHP_ARGS_NAMES_H_
24+
25+
#ifdef __cplusplus
26+
27+
namespace modsecurity {
28+
class Transaction;
29+
namespace actions {
30+
namespace transformations {
31+
32+
33+
class PhpArgsNames : public Transformation {
34+
public:
35+
explicit PhpArgsNames(const std::string &action);
36+
std::string evaluate(const std::string &exp,
37+
Transaction *transaction) override;
38+
};
39+
40+
} // namespace transformations
41+
} // namespace actions
42+
} // namespace modsecurity
43+
44+
#endif
45+
46+
#endif // SRC_ACTIONS_TRANSFORMATIONS_PHP_ARGS_NAMES_H_

src/actions/transformations/transformation.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "src/actions/transformations/js_decode.h"
3636
#include "src/actions/transformations/length.h"
3737
#include "src/actions/transformations/lower_case.h"
38+
#include "src/actions/transformations/php_args_names.h"
3839
#include "src/actions/transformations/md5.h"
3940
#include "src/actions/transformations/none.h"
4041
#include "src/actions/transformations/normalise_path.h"
@@ -88,6 +89,7 @@ Transformation* Transformation::instantiate(std::string a) {
8889
IF_MATCH(jsDecode) { return new JsDecode(a); }
8990
IF_MATCH(length) { return new Length(a); }
9091
IF_MATCH(lowercase) { return new LowerCase(a); }
92+
IF_MATCH(phpArgsNames) { return new PhpArgsNames(a); }
9193
IF_MATCH(md5) { return new Md5(a); }
9294
IF_MATCH(none) { return new None(a); }
9395
IF_MATCH(normalizePathWin) { return new NormalisePathWin(a); }

0 commit comments

Comments
 (0)