mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-29 19:24:29 +03:00
First version of global' and
ip' collections
This commit is contained in:
@@ -36,6 +36,7 @@ libmodsecurity_includesub_HEADERS = \
|
||||
../headers/modsecurity/transaction/collections.h \
|
||||
../headers/modsecurity/transaction/variable.h \
|
||||
../headers/modsecurity/transaction/variables.h
|
||||
../headers/modsecurity/transaction/global_variables.h
|
||||
|
||||
|
||||
|
||||
@@ -188,6 +189,7 @@ libmodsecurity_la_SOURCES = \
|
||||
utils.cc \
|
||||
collections.cc \
|
||||
variables.cc \
|
||||
global_variables.cc \
|
||||
debug_log.cc \
|
||||
debug_log_writer.cc \
|
||||
debug_log_writer_agent.cc \
|
||||
|
@@ -46,15 +46,30 @@ bool InitCol::init(std::string *error) {
|
||||
m_collection_key = std::string(action, posInit, posEquals - posInit);
|
||||
m_collection_value = std::string(action, posEquals + 1);
|
||||
|
||||
if (m_collection_key != "ip" && m_collection_key != "global") {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool InitCol::evaluate(Rule *rule, Transaction *transaction) {
|
||||
bool InitCol::evaluate(Rule *rule, Transaction *t) {
|
||||
std::string collectionName;
|
||||
collectionName = MacroExpansion::expand(m_collection_value, transaction);
|
||||
std::cout << "Collection is not implemented yet, here is the ";
|
||||
std::cout << "collection name: " << collectionName << std::endl;
|
||||
collectionName = MacroExpansion::expand(m_collection_value, t);
|
||||
|
||||
|
||||
if (m_collection_key == "ip") {
|
||||
t->m_collections.m_ip_collection_key = collectionName;
|
||||
} else if (m_collection_key == "global") {
|
||||
t->m_collections.m_global_collection_key = collectionName;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
t->debug(5, "Collection `" + m_collection_key + "' initialized with " \
|
||||
"value: " + collectionName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -31,7 +31,12 @@ namespace modsecurity {
|
||||
namespace transaction {
|
||||
|
||||
|
||||
Collections::Collections() {
|
||||
Collections::Collections(transaction::GlobalVariables *global,
|
||||
transaction::GlobalVariables *ip)
|
||||
: m_global_collection_key(""),
|
||||
m_ip_collection_key(""),
|
||||
m_global_collection(global),
|
||||
m_ip_collection(ip) {
|
||||
/* Create collection TX */
|
||||
this->emplace("TX", new Collection("TX", ""));
|
||||
}
|
||||
@@ -53,6 +58,20 @@ void Collections::init(const std::string& name, const std::string& key) {
|
||||
void Collections::storeOrUpdateFirst(const std::string& collectionName,
|
||||
const std::string& variableName,
|
||||
const std::string& targetValue) {
|
||||
if (tolower(collectionName) == "ip"
|
||||
&& !m_ip_collection_key.empty()) {
|
||||
m_ip_collection->storeOrUpdateFirst(collectionName + ":"
|
||||
+ variableName, m_ip_collection_key, targetValue);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tolower(collectionName) == "global"
|
||||
&& !m_global_collection_key.empty()) {
|
||||
m_global_collection->storeOrUpdateFirst(collectionName + ":"
|
||||
+ variableName, m_global_collection_key, targetValue);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
transaction::Variables *collection;
|
||||
collection = this->at(collectionName);
|
||||
@@ -99,7 +118,7 @@ std::string* Collections::resolveFirst(const std::string& var) {
|
||||
for (auto &a : *this) {
|
||||
auto range = a.second->equal_range(var);
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
return &it->second;
|
||||
return & it->second;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +128,19 @@ std::string* Collections::resolveFirst(const std::string& var) {
|
||||
|
||||
std::string* Collections::resolveFirst(const std::string& collectionName,
|
||||
const std::string& var) {
|
||||
|
||||
if (tolower(collectionName) == "ip"
|
||||
&& !m_ip_collection_key.empty()) {
|
||||
return m_ip_collection->resolveFirst(toupper(collectionName)
|
||||
+ ":" + var, m_ip_collection_key);
|
||||
}
|
||||
|
||||
if (tolower(collectionName) == "global"
|
||||
&& !m_global_collection_key.empty()) {
|
||||
return m_global_collection->resolveFirst(toupper(collectionName)
|
||||
+ ":" + var, m_global_collection_key);
|
||||
}
|
||||
|
||||
for (auto &a : *this) {
|
||||
if (tolower(a.first) == tolower(collectionName)) {
|
||||
transaction::Variables *t = a.second;
|
||||
@@ -135,6 +167,18 @@ void Collections::resolveSingleMatch(const std::string& var,
|
||||
const std::string& collection,
|
||||
std::vector<const transaction::Variable *> *l) {
|
||||
|
||||
if (tolower(collection) == "ip"
|
||||
&& !m_ip_collection_key.empty()) {
|
||||
m_ip_collection->resolveSingleMatch(var, m_ip_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tolower(collection) == "global"
|
||||
&& !m_global_collection_key.empty()) {
|
||||
m_global_collection->resolveSingleMatch(var, m_global_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this->at(collection)->resolveSingleMatch(var, l);
|
||||
} catch (...) { }
|
||||
@@ -150,6 +194,18 @@ void Collections::resolveMultiMatches(const std::string& var,
|
||||
void Collections::resolveMultiMatches(const std::string& var,
|
||||
const std::string& collection,
|
||||
std::vector<const transaction::Variable *> *l) {
|
||||
if (tolower(collection) == "ip"
|
||||
&& !m_ip_collection_key.empty()) {
|
||||
m_ip_collection->resolveMultiMatches(var, m_ip_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tolower(collection) == "global"
|
||||
&& !m_global_collection_key.empty()) {
|
||||
m_global_collection->resolveMultiMatches(var, m_global_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this->at(collection)->resolveMultiMatches(var, l);
|
||||
} catch (...) { }
|
||||
@@ -164,6 +220,19 @@ void Collections::resolveRegularExpression(const std::string& var,
|
||||
void Collections::resolveRegularExpression(const std::string& var,
|
||||
const std::string& collection,
|
||||
std::vector<const transaction::Variable *> *l) {
|
||||
if (tolower(collection) == "ip"
|
||||
&& !m_ip_collection_key.empty()) {
|
||||
m_ip_collection->resolveRegularExpression(toupper(collection)
|
||||
+ ":" + var, m_ip_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tolower(collection) == "global"
|
||||
&& !m_global_collection_key.empty()) {
|
||||
m_global_collection->resolveRegularExpression(toupper(collection)
|
||||
+ ":" + var, m_global_collection_key, l);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this->at(collection)->resolveRegularExpression(var, l);
|
||||
|
126
src/global_variables.cc
Normal file
126
src/global_variables.cc
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* ModSecurity, http://www.modsecurity.org/
|
||||
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
|
||||
*
|
||||
* You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* If any of the files related to licensing are missing or if you have any
|
||||
* other questions related to licensing please contact Trustwave Holdings, Inc.
|
||||
* directly using the email address security@modsecurity.org.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "modsecurity/transaction/global_variables.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#endif
|
||||
|
||||
#include "modsecurity/transaction/variable.h"
|
||||
#include "src/utils.h"
|
||||
|
||||
namespace modsecurity {
|
||||
namespace transaction {
|
||||
|
||||
|
||||
GlobalVariables::GlobalVariables() {
|
||||
this->reserve(1000);
|
||||
}
|
||||
|
||||
GlobalVariables::~GlobalVariables() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void GlobalVariables::store(std::string key, std::string compartment, std::string value) {
|
||||
this->emplace(new CollectionKey(key, compartment), value);
|
||||
}
|
||||
|
||||
|
||||
bool GlobalVariables::storeOrUpdateFirst(const std::string &key,
|
||||
std::string compartment, const std::string &value) {
|
||||
if (updateFirst(key, compartment, value) == false) {
|
||||
store(key, compartment, value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool GlobalVariables::updateFirst(const std::string &key, std::string compartment, const std::string &value) {
|
||||
auto range = this->equal_range(new CollectionKey(key, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
it->second = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void GlobalVariables::del(const std::string& key, std::string compartment) {
|
||||
this->erase(new CollectionKey(key, compartment));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GlobalVariables::resolveSingleMatch(const std::string& var,
|
||||
std::string compartment, std::vector<const transaction::Variable *> *l) {
|
||||
auto range = this->equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
l->push_back(new transaction::Variable(var, it->second));
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalVariables::resolveMultiMatches(const std::string& var,
|
||||
std::string compartment, std::vector<const transaction::Variable *> *l) {
|
||||
size_t keySize = var.size();
|
||||
l->reserve(15);
|
||||
|
||||
auto range = this->equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
l->insert(l->begin(), new transaction::Variable(var, it->second));
|
||||
}
|
||||
|
||||
for (const auto& x : *this) {
|
||||
if (x.first->m_name.size() <= keySize + 1) {
|
||||
continue;
|
||||
}
|
||||
if (x.first->m_name.at(keySize) != ':') {
|
||||
continue;
|
||||
}
|
||||
if (x.first->m_name.compare(0, keySize, var) != 0) {
|
||||
continue;
|
||||
}
|
||||
l->insert(l->begin(), new transaction::Variable(x.first->m_name, x.second));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GlobalVariables::resolveRegularExpression(const std::string& var,
|
||||
std::string compartment, std::vector<const transaction::Variable *> *l) {
|
||||
/* Not ready */
|
||||
}
|
||||
|
||||
|
||||
std::string* GlobalVariables::resolveFirst(const std::string& var,
|
||||
std::string compartment) {
|
||||
auto range = equal_range(new CollectionKey(var, compartment));
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
} // namespace transaction
|
||||
} // namespace modsecurity
|
@@ -111,7 +111,10 @@ Transaction::Transaction(ModSecurity *ms, Rules *rules, void *logCbData)
|
||||
m_marker(""),
|
||||
m_creationTimeStamp(cpu_seconds()),
|
||||
m_logCbData(logCbData),
|
||||
m_ms(ms) {
|
||||
m_ms(ms),
|
||||
m_collections(&ms->m_global_collection, &ms->m_ip_collection)
|
||||
{
|
||||
|
||||
m_id = std::to_string(this->m_timeStamp) + \
|
||||
std::to_string(generate_transaction_unique_id());
|
||||
m_rules->incrementReferenceCount();
|
||||
|
Reference in New Issue
Block a user