Skip to content

Commit 027d50b

Browse files
zimmerleFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Adds first version of `processContentOffset'
This commit also includes an example application on how to use the `processContentOffset' method.
1 parent 7aae5dc commit 027d50b

File tree

11 files changed

+325
-95
lines changed

11 files changed

+325
-95
lines changed

configure.ac

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,12 @@ AM_COND_IF([TEST_UTILITIES],
316316
[AC_CONFIG_FILES([test/Makefile test/benchmark/Makefile])])
317317

318318
AM_COND_IF([EXAMPLES],
319-
[AC_CONFIG_FILES([examples/Makefile \
319+
[AC_CONFIG_FILES([ \
320+
examples/Makefile \
320321
examples/simple_example_using_c/Makefile \
321-
examples/multiprocess_c/Makefile])])
322+
examples/multiprocess_c/Makefile \
323+
examples/reading_logs_with_offset/Makefile \
324+
])])
322325

323326
AM_COND_IF([AFL_FUZZER],
324327
[AC_CONFIG_FILES([test/fuzzer/Makefile])])

examples/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ ACLOCAL_AMFLAGS = -I build
44

55
SUBDIRS = \
66
simple_example_using_c \
7-
multiprocess_c
7+
multiprocess_c \
8+
reading_logs_with_offset
89

910
# make clean
1011
CLEANFILES =
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
noinst_PROGRAMS = read
4+
5+
read_SOURCES = \
6+
read.cc
7+
8+
read_LDADD = \
9+
$(top_builddir)/src/.libs/libmodsecurity.a \
10+
$(CURL_LDADD) \
11+
$(GEOIP_LDFLAGS) $(GEOIP_LDADD) \
12+
$(PCRE_LDADD) \
13+
$(YAJL_LDFLAGS) $(YAJL_LDADD) \
14+
$(LMDB_LDFLAGS) $(LMDB_LDADD) \
15+
$(LIBXML2_LDADD) \
16+
$(GLOBAL_LDADD)
17+
18+
19+
read_CPPFLAGS = \
20+
$(GLOBAL_CFLAGS) \
21+
-std=c++11 \
22+
-I$(top_builddir)/headers \
23+
-I$(top_builddir) \
24+
-g \
25+
-I../others \
26+
-fPIC \
27+
-O3 \
28+
$(GEOIP_CFLAGS) \
29+
$(GLOBAL_CPPFLAGS) \
30+
$(MODSEC_NO_LOGS) \
31+
$(YAJL_CFLAGS) \
32+
$(LMDB_CFLAGS) \
33+
$(PCRE_CFLAGS) \
34+
$(LIBXML2_CFLAGS)
35+
36+
37+
MAINTAINERCLEANFILES = \
38+
Makefile.in
39+
40+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include <modsecurity/modsecurity.h>
6+
7+
8+
// Variable offset - REQUEST_HEADERS_NAMES
9+
10+
const char *request = "" \
11+
"GET /index.html?param1=value1&param2=value1&param3=value1 HTTP/\n" \
12+
"AuThOrIzAtIoN: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n" \
13+
"Host: localhost\n" \
14+
"Content-Length: 27\n" \
15+
"Content-Type: application/x-www-form-urlencoded\n";
16+
17+
18+
int main() {
19+
modsecurity::ModSecurity msc;
20+
std::string json("");
21+
const char *err = NULL;
22+
int ret = 0;
23+
24+
ret = msc.processContentOffset(request, strlen(request),
25+
"o0,4v64,13v114,4v130,14v149,12t:lowercase", &json, &err);
26+
27+
if (ret >= 0) {
28+
std::cout << json << std::endl;
29+
} else {
30+
std::cout << err << std::endl;
31+
}
32+
33+
return ret;
34+
}

examples/simple_example_using_c/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*/
1515

16-
#include <modsecurity/transaction.h>
16+
1717
#include <stdio.h>
1818
#include <stdlib.h>
1919

headers/modsecurity/modsecurity.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ class ModSecurity {
221221
void serverLog(void *data, const std::string& msg);
222222
const std::string& getConnectorInformation();
223223

224+
int processContentOffset(const char *content, size_t len,
225+
const char *matchString, std::string *json, const char **err);
226+
224227
collection::Collection *m_global_collection;
225228
collection::Collection *m_resource_collection;
226229
collection::Collection *m_ip_collection;

src/actions/transformations/lower_case.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,10 @@ std::string LowerCase::evaluate(std::string value,
3535
Transaction *transaction) {
3636
std::locale loc;
3737

38-
if (LowerCaseInstantCache::getInstance().count(value) > 0) {
39-
return LowerCaseInstantCache::getInstance().at(value);
40-
}
41-
42-
std::string orig_value = value;
43-
4438
for (std::string::size_type i=0; i < value.length(); ++i) {
4539
value[i] = std::tolower(value[i], loc);
4640
}
4741

48-
LowerCaseInstantCache::getInstance().cache(orig_value, value);
49-
5042
return value;
5143
}
5244

src/actions/transformations/transformation.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
#include "src/actions/transformations/url_encode.h"
5959
#include "src/actions/transformations/utf8_to_unicode.h"
6060

61+
62+
#define IF_MATCH(b) \
63+
if (a.compare(2, std::strlen(#b), #b) == 0)
64+
65+
6166
namespace modsecurity {
6267
namespace actions {
6368
namespace transformations {
@@ -68,6 +73,49 @@ std::string Transformation::evaluate(std::string value,
6873
return value;
6974
}
7075

76+
Transformation* Transformation::instantiate(std::string a) {
77+
IF_MATCH(base64DecodeExt) { return new Base64DecodeExt(a); }
78+
IF_MATCH(base64Decode) { return new Base64Decode(a); }
79+
IF_MATCH(base64Encode) { return new Base64Encode(a); }
80+
IF_MATCH(cmd_line) { return new CmdLine(a); }
81+
IF_MATCH(compress_whitespace) { return new CompressWhitespace(a); }
82+
IF_MATCH(cssDecode) { return new CssDecode(a); }
83+
IF_MATCH(escapeSeqDecode) { return new EscapeSeqDecode(a); }
84+
IF_MATCH(hexDecode) { return new HexDecode(a); }
85+
IF_MATCH(hexEncode) { return new HexEncode(a); }
86+
IF_MATCH(htmlEntityDecode) { return new HtmlEntityDecode(a); }
87+
IF_MATCH(jsDecode) { return new JsDecode(a); }
88+
IF_MATCH(length) { return new Length(a); }
89+
IF_MATCH(lowercase) { return new LowerCase(a); }
90+
IF_MATCH(md5) { return new Md5(a); }
91+
IF_MATCH(none) { return new None(a); }
92+
IF_MATCH(normalizePathWin) { return new NormalisePathWin(a); }
93+
IF_MATCH(normalisePathWin) { return new NormalisePathWin(a); }
94+
IF_MATCH(normalizePath) { return new NormalisePath(a); }
95+
IF_MATCH(normalisePath) { return new NormalisePath(a); }
96+
IF_MATCH(parityEven7bit) { return new ParityEven7bit(a); }
97+
IF_MATCH(parityOdd7bit) { return new ParityOdd7bit(a); }
98+
IF_MATCH(parityZero7bit) { return new ParityZero7bit(a); }
99+
IF_MATCH(removeCommentsChar) { return new RemoveCommentsChar(a); }
100+
IF_MATCH(removeComments) { return new RemoveComments(a); }
101+
IF_MATCH(removeNulls) { return new RemoveNulls(a); }
102+
IF_MATCH(removeWhitespace) { return new RemoveWhitespace(a); }
103+
IF_MATCH(compressWhitespace) { return new CompressWhitespace(a); }
104+
IF_MATCH(replaceComments) { return new ReplaceComments(a); }
105+
IF_MATCH(replaceNulls) { return new ReplaceNulls(a); }
106+
IF_MATCH(sha1) { return new Sha1(a); }
107+
IF_MATCH(sqlHexDecode) { return new SqlHexDecode(a); }
108+
IF_MATCH(transformation) { return new Transformation(a); }
109+
IF_MATCH(trimLeft) { return new TrimLeft(a); }
110+
IF_MATCH(trimRight) { return new TrimRight(a); }
111+
IF_MATCH(trim) { return new Trim(a); }
112+
IF_MATCH(urlDecodeUni) { return new UrlDecodeUni(a); }
113+
IF_MATCH(urlDecode) { return new UrlDecode(a); }
114+
IF_MATCH(urlEncode) { return new UrlEncode(a); }
115+
IF_MATCH(utf8ToUnicode) { return new Utf8ToUnicode(a); }
116+
117+
return new Transformation(a);
118+
}
71119

72120
} // namespace transformations
73121
} // namespace actions

src/actions/transformations/transformation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef SRC_ACTIONS_TRANSFORMATIONS_TRANSFORMATION_H_
2121
#define SRC_ACTIONS_TRANSFORMATIONS_TRANSFORMATION_H_
2222

23+
2324
namespace modsecurity {
2425
class Transaction;
2526

@@ -36,6 +37,9 @@ class Transformation : public Action {
3637

3738
std::string evaluate(std::string exp,
3839
Transaction *transaction) override;
40+
41+
static Transformation* instantiate(std::string a);
42+
3943
};
4044

4145
} // namespace transformations

0 commit comments

Comments
 (0)