Adds support for the @inspectFile operator

This commit is contained in:
Felipe Zimmerle
2017-10-31 09:42:06 -03:00
parent 1189e9b0ef
commit 1866a3a9eb
7 changed files with 897 additions and 724 deletions

View File

@@ -16,20 +16,61 @@
#include "src/operators/inspect_file.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include "src/operators/operator.h"
#include "src/utils/system.h"
namespace modsecurity {
namespace operators {
bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
/**
* @todo Implement the operator InspectFile.
* Reference: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#inspectfile
*/
bool InspectFile::init(const std::string &param2, std::string *error) {
std::istream *iss;
std::string err;
m_file = utils::find_resource(m_param, param2, &err);
iss = new std::ifstream(m_file, std::ios::in);
if (((std::ifstream *)iss)->is_open() == false) {
error->assign("Failed to open file: " + m_param + ". " + err);
delete iss;
return false;
}
delete iss;
return true;
}
bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
FILE *in;
char buff[512];
std::stringstream s;
std::string res;
std::string openstr;
openstr.append(m_param);
openstr.append(" ");
openstr.append(str);
if (!(in = popen(openstr.c_str(), "r"))){
return false;
}
while (fgets(buff, sizeof(buff), in) != NULL) {
s << buff;
}
pclose(in);
res.append(s.str());
if (res.size() > 1 && res.at(0) == '1') {
return true;
}
return false;
}
} // namespace operators
} // namespace modsecurity

View File

@@ -28,11 +28,16 @@ class InspectFile : public Operator {
public:
/** @ingroup ModSecurity_Operator */
InspectFile(std::string o, std::string p, bool n)
: Operator(o, p, n) { }
: Operator(o, p, n),
m_file("") { }
explicit InspectFile(std::string param)
: Operator("InspectFile", param) { }
: Operator("InspectFile", param),
m_file("") { }
bool init(const std::string &param, std::string *error) override;
bool evaluate(Transaction *transaction, const std::string &str) override;
private:
std::string m_file;
};
} // namespace operators