mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-30 03:34:29 +03:00
Refactoring on the variable read/store methods
Now it is ready to received two (or more) variables with same key.
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <map>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#endif
|
#endif
|
||||||
@@ -91,7 +92,7 @@ class ModSecurityCollectionsVariables :
|
|||||||
|
|
||||||
|
|
||||||
class ModSecurityStringVariables :
|
class ModSecurityStringVariables :
|
||||||
public std::unordered_map<std::string, std::string> {
|
public std::unordered_multimap<std::string, std::string> {
|
||||||
public:
|
public:
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -135,11 +136,10 @@ class Assay {
|
|||||||
const char *getResponseBody();
|
const char *getResponseBody();
|
||||||
int getResponseBodyLenth();
|
int getResponseBodyLenth();
|
||||||
|
|
||||||
std::string resolve_variable(std::string);
|
std::list<std::string> resolve_variable(std::string var);
|
||||||
|
std::string* resolve_variable_first(std::string);
|
||||||
|
|
||||||
void store_variable(std::string, std::string);
|
void store_variable(std::string, std::string);
|
||||||
void store_variable(std::string,
|
|
||||||
std::unordered_map<std::string, std::string>);
|
|
||||||
|
|
||||||
ModSecurityStringVariables m_variables_strings;
|
ModSecurityStringVariables m_variables_strings;
|
||||||
|
|
||||||
@@ -172,6 +172,9 @@ class Assay {
|
|||||||
const char *m_protocol;
|
const char *m_protocol;
|
||||||
const char *m_httpVersion;
|
const char *m_httpVersion;
|
||||||
|
|
||||||
|
std::string m_namesResponse;
|
||||||
|
std::string m_namesRequest;
|
||||||
|
|
||||||
std::ostringstream m_requestBody;
|
std::ostringstream m_requestBody;
|
||||||
std::ostringstream m_responseBody;
|
std::ostringstream m_responseBody;
|
||||||
ModSecurityCollectionsVariables m_variables_collections;
|
ModSecurityCollectionsVariables m_variables_collections;
|
||||||
|
68
src/assay.cc
68
src/assay.cc
@@ -26,6 +26,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include "modsecurity/modsecurity.h"
|
#include "modsecurity/modsecurity.h"
|
||||||
#include "modsecurity/intervention.h"
|
#include "modsecurity/intervention.h"
|
||||||
@@ -259,18 +260,16 @@ int Assay::processRequestHeaders() {
|
|||||||
*/
|
*/
|
||||||
int Assay::addRequestHeader(const std::string& key,
|
int Assay::addRequestHeader(const std::string& key,
|
||||||
const std::string& value) {
|
const std::string& value) {
|
||||||
|
std::string *names = resolve_variable_first("REQUEST_HEADERS_NAMES");
|
||||||
|
|
||||||
std::string names = resolve_variable("REQUEST_HEADERS_NAMES");
|
if (names == NULL) {
|
||||||
|
this->store_variable("REQUEST_HEADERS_NAMES", m_namesRequest);
|
||||||
this->store_variable("REQUEST_HEADERS:" + key, value);
|
m_namesRequest = key;
|
||||||
|
|
||||||
if (names.length() > 0) {
|
|
||||||
names = names + " " + key;
|
|
||||||
} else {
|
} else {
|
||||||
names = key;
|
m_namesRequest = m_namesRequest + " " + key;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->store_variable("REQUEST_HEADERS_NAMES", names + " " + key);
|
this->store_variable("REQUEST_HEADERS:" + key, value);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -432,17 +431,16 @@ int Assay::processResponseHeaders() {
|
|||||||
*/
|
*/
|
||||||
int Assay::addResponseHeader(const std::string& key,
|
int Assay::addResponseHeader(const std::string& key,
|
||||||
const std::string& value) {
|
const std::string& value) {
|
||||||
std::string names = resolve_variable("RESPONSE_HEADERS_NAMES");
|
std::string *names = resolve_variable_first("RESPONSE_HEADERS_NAMES");
|
||||||
|
|
||||||
this->store_variable("RESPONSE_HEADERS:" + key, value);
|
if (names == NULL) {
|
||||||
|
this->store_variable("RESPONSE_HEADERS_NAMES", m_namesResponse);
|
||||||
if (names.length() > 0) {
|
m_namesRequest = key;
|
||||||
names = names + " " + key;
|
|
||||||
} else {
|
} else {
|
||||||
names = key;
|
m_namesRequest = m_namesRequest + " " + key;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->store_variable("RESPONSE_HEADERS_NAMES", names + " " + key);
|
this->store_variable("RESPONSE_HEADERS:" + key, value);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -699,12 +697,13 @@ std::string Assay::toOldAuditLogFormatIndex(const std::string &filename,
|
|||||||
|
|
||||||
strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo);
|
strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo);
|
||||||
|
|
||||||
ss << dash_if_empty(this->resolve_variable("REQUEST_HEADERS:Host")) << " ";
|
ss << dash_if_empty(
|
||||||
|
*this->resolve_variable_first("REQUEST_HEADERS:Host")) << " ";
|
||||||
ss << dash_if_empty(this->m_clientIpAddress) << " ";
|
ss << dash_if_empty(this->m_clientIpAddress) << " ";
|
||||||
/** TODO: Check variable */
|
/** TODO: Check variable */
|
||||||
ss << dash_if_empty(this->resolve_variable("REMOTE_USER")) << " ";
|
ss << dash_if_empty(*this->resolve_variable_first("REMOTE_USER")) << " ";
|
||||||
/** TODO: Check variable */
|
/** TODO: Check variable */
|
||||||
ss << dash_if_empty(this->resolve_variable("LOCAL_USER")) << " ";
|
ss << dash_if_empty(*this->resolve_variable_first("LOCAL_USER")) << " ";
|
||||||
ss << tstr << " ";
|
ss << tstr << " ";
|
||||||
|
|
||||||
ss << "\"";
|
ss << "\"";
|
||||||
@@ -716,13 +715,14 @@ std::string Assay::toOldAuditLogFormatIndex(const std::string &filename,
|
|||||||
ss << this->httpCodeReturned << " ";
|
ss << this->httpCodeReturned << " ";
|
||||||
ss << this->m_responseBody.tellp();
|
ss << this->m_responseBody.tellp();
|
||||||
/** TODO: Check variable */
|
/** TODO: Check variable */
|
||||||
ss << dash_if_empty(this->resolve_variable("REFERER")) << " ";
|
ss << dash_if_empty(*this->resolve_variable_first("REFERER")) << " ";
|
||||||
ss << "\"";
|
ss << "\"";
|
||||||
ss << dash_if_empty(this->resolve_variable("REQUEST_HEADERS:User-Agent"));
|
ss << dash_if_empty(
|
||||||
|
*this->resolve_variable_first("REQUEST_HEADERS:User-Agent"));
|
||||||
ss << "\" ";
|
ss << "\" ";
|
||||||
ss << this->id << " ";
|
ss << this->id << " ";
|
||||||
/** TODO: Check variable */
|
/** TODO: Check variable */
|
||||||
ss << dash_if_empty(this->resolve_variable("REFERER")) << " ";
|
ss << dash_if_empty(*this->resolve_variable_first("REFERER")) << " ";
|
||||||
|
|
||||||
ss << filename << " ";
|
ss << filename << " ";
|
||||||
ss << "0" << " ";
|
ss << "0" << " ";
|
||||||
@@ -972,19 +972,31 @@ std::string Assay::to_json(int parts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Assay::store_variable(std::string key, std::string value) {
|
void Assay::store_variable(std::string key, std::string value) {
|
||||||
this->m_variables_strings[key] = value;
|
this->m_variables_strings.emplace(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Assay::store_variable(std::string key,
|
std::list<std::string> Assay::resolve_variable(std::string var) {
|
||||||
std::unordered_map<std::string, std::string> value) {
|
std::list<std::string> l;
|
||||||
std::cout << "Storing variable: " << key << ", value is a collection." \
|
auto range = m_variables_strings.equal_range(var);
|
||||||
<< std::endl;
|
|
||||||
|
for (auto it = range.first; it != range.second; ++it) {
|
||||||
|
std::cout << it->first << ' ' << it->second << '\n';
|
||||||
|
l.push_back(it->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string Assay::resolve_variable(std::string var) {
|
std::string* Assay::resolve_variable_first(std::string var) {
|
||||||
return this->m_variables_strings[var];
|
auto range = m_variables_strings.equal_range(var);
|
||||||
|
|
||||||
|
for (auto it = range.first; it != range.second; ++it) {
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include "operators/operator.h"
|
#include "operators/operator.h"
|
||||||
#include "actions/action.h"
|
#include "actions/action.h"
|
||||||
@@ -120,8 +121,12 @@ bool Rule::evaluate(Assay *assay) {
|
|||||||
} else {
|
} else {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
try {
|
try {
|
||||||
ret = this->op->evaluate(assay,
|
std::list<std::string> e = assay->resolve_variable(
|
||||||
assay->m_variables_strings.at(variable.name));
|
variable.name);
|
||||||
|
for (std::string value : e) {
|
||||||
|
ret = this->op->evaluate(assay,
|
||||||
|
value);
|
||||||
|
}
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -58,7 +58,7 @@ double random_number(const double from, const double to) {
|
|||||||
|
|
||||||
|
|
||||||
std::string dash_if_empty(const std::string& str) {
|
std::string dash_if_empty(const std::string& str) {
|
||||||
if (str.empty()) {
|
if (&str == NULL || str.empty()) {
|
||||||
return "-";
|
return "-";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,12 +18,13 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include "modsecurity/assay.h"
|
#include "modsecurity/assay.h"
|
||||||
|
|
||||||
namespace ModSecurity {
|
namespace ModSecurity {
|
||||||
|
|
||||||
std::string Variable::evaluate(Assay *assay) {
|
std::list<std::string> Variable::evaluate(Assay *assay) {
|
||||||
return assay->resolve_variable(this->name);
|
return assay->resolve_variable(this->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#ifndef SRC_VARIABLE_H_
|
#ifndef SRC_VARIABLE_H_
|
||||||
#define SRC_VARIABLE_H_
|
#define SRC_VARIABLE_H_
|
||||||
@@ -29,7 +30,7 @@ class Variable {
|
|||||||
: name(_name) { }
|
: name(_name) { }
|
||||||
|
|
||||||
static std::string to_s(std::vector<Variable> *variables);
|
static std::string to_s(std::vector<Variable> *variables);
|
||||||
std::string evaluate(Assay *assay);
|
std::list<std::string> evaluate(Assay *assay);
|
||||||
std::string name;
|
std::string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user