mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-29 19:24:29 +03:00
Adds support for the @inspectFile operator
This commit is contained in:
@@ -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 ¶m2, 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
|
||||
|
@@ -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 ¶m, std::string *error) override;
|
||||
bool evaluate(Transaction *transaction, const std::string &str) override;
|
||||
private:
|
||||
std::string m_file;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
|
Reference in New Issue
Block a user