Initial support for Lua script engine

This commit is contained in:
Felipe Zimmerle
2017-11-05 10:42:40 -03:00
parent 1866a3a9eb
commit a676f313c3
19 changed files with 1270 additions and 59 deletions

View File

@@ -28,6 +28,7 @@ namespace operators {
bool InspectFile::init(const std::string &param2, std::string *error) {
std::istream *iss;
std::string err;
std::string err_lua;
m_file = utils::find_resource(m_param, param2, &err);
iss = new std::ifstream(m_file, std::ios::in);
@@ -38,37 +39,45 @@ bool InspectFile::init(const std::string &param2, std::string *error) {
return false;
}
if (engine::Lua::isCompatible(m_file, &m_lua, &err_lua) == true) {
m_isScript = true;
}
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;
if (m_isScript) {
return m_lua.run(transaction);
} else {
FILE *in;
char buff[512];
std::stringstream s;
std::string res;
std::string openstr;
openstr.append(m_param);
openstr.append(" ");
openstr.append(str);
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;
}
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;
}

View File

@@ -19,6 +19,7 @@
#include <string>
#include "src/operators/operator.h"
#include "src/engine/lua.h"
namespace modsecurity {
@@ -29,15 +30,19 @@ class InspectFile : public Operator {
/** @ingroup ModSecurity_Operator */
InspectFile(std::string o, std::string p, bool n)
: Operator(o, p, n),
m_file("") { }
m_file(""),
m_isScript(false) { }
explicit InspectFile(std::string param)
: Operator("InspectFile", param),
m_file("") { }
m_file(""),
m_isScript(false) { }
bool init(const std::string &param, std::string *error) override;
bool evaluate(Transaction *transaction, const std::string &str) override;
private:
std::string m_file;
bool m_isScript;
engine::Lua m_lua;
};
} // namespace operators