Skip to content

Commit 1866a3a

Browse files
author
Felipe Zimmerle
committed
Adds support for the @inspectFile operator
1 parent 1189e9b commit 1866a3a

File tree

7 files changed

+887
-714
lines changed

7 files changed

+887
-714
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
v3.0.????? - ?
33
---------------------------
44

5+
- Adds support for @inspectFile operator.
6+
[Issue #999 - @zimmerle, @victorhora]
57
- Adds support for RESOURCE variable collection.
68
[Issue #1014 - @zimmerle, @victorhora]
79
- Adds support for @fuzzyHash operator.

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,5 @@ TESTS+=test/test-cases/regression/config-update-target-by-id.json
286286
TESTS+=test/test-cases/regression/misc-variable-under-quotes.json
287287
TESTS+=test/test-cases/regression/operator-fuzzyhash.json
288288
TESTS+=test/test-cases/regression/collection-resource.json
289+
TESTS+=test/test-cases/regression/operator-inpectFile.json
289290

src/operators/inspect_file.cc

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,61 @@
1616
#include "src/operators/inspect_file.h"
1717

1818
#include <string>
19+
#include <iostream>
20+
#include <stdio.h>
1921

2022
#include "src/operators/operator.h"
23+
#include "src/utils/system.h"
2124

2225
namespace modsecurity {
2326
namespace operators {
2427

25-
bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
26-
/**
27-
* @todo Implement the operator InspectFile.
28-
* Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#inspectfile
29-
*/
28+
bool InspectFile::init(const std::string &param2, std::string *error) {
29+
std::istream *iss;
30+
std::string err;
31+
32+
m_file = utils::find_resource(m_param, param2, &err);
33+
iss = new std::ifstream(m_file, std::ios::in);
34+
35+
if (((std::ifstream *)iss)->is_open() == false) {
36+
error->assign("Failed to open file: " + m_param + ". " + err);
37+
delete iss;
38+
return false;
39+
}
40+
41+
delete iss;
3042
return true;
3143
}
3244

45+
bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
46+
FILE *in;
47+
char buff[512];
48+
std::stringstream s;
49+
std::string res;
50+
std::string openstr;
51+
52+
openstr.append(m_param);
53+
openstr.append(" ");
54+
openstr.append(str);
55+
56+
if (!(in = popen(openstr.c_str(), "r"))){
57+
return false;
58+
}
59+
60+
while (fgets(buff, sizeof(buff), in) != NULL) {
61+
s << buff;
62+
}
63+
64+
pclose(in);
65+
66+
res.append(s.str());
67+
if (res.size() > 1 && res.at(0) == '1') {
68+
return true;
69+
}
70+
71+
return false;
72+
}
73+
3374

3475
} // namespace operators
3576
} // namespace modsecurity

src/operators/inspect_file.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ class InspectFile : public Operator {
2828
public:
2929
/** @ingroup ModSecurity_Operator */
3030
InspectFile(std::string o, std::string p, bool n)
31-
: Operator(o, p, n) { }
31+
: Operator(o, p, n),
32+
m_file("") { }
3233
explicit InspectFile(std::string param)
33-
: Operator("InspectFile", param) { }
34+
: Operator("InspectFile", param),
35+
m_file("") { }
3436

37+
bool init(const std::string &param, std::string *error) override;
3538
bool evaluate(Transaction *transaction, const std::string &str) override;
39+
private:
40+
std::string m_file;
3641
};
3742

3843
} // namespace operators

0 commit comments

Comments
 (0)