Adds support to the TX collection and setvar action

This commit is contained in:
Felipe Zimmerle
2015-08-06 23:41:46 -03:00
parent a9e0fbb41e
commit e12d95b10d
11 changed files with 707 additions and 4 deletions

View File

@@ -35,11 +35,17 @@ class Action {
explicit Action(const std::string& _action)
: action_kind(2),
action(_action),
temporaryAction(false) { }
name(_action),
temporaryAction(false) {
name.erase(0, 2);
}
explicit Action(const std::string& _action, int kind)
: action_kind(kind),
action(_action),
temporaryAction(false) { }
name(_action),
temporaryAction(false) {
name.erase(0, 2);
}
virtual ~Action() { }
/**

140
src/actions/set_var.cc Normal file
View File

@@ -0,0 +1,140 @@
/*
* 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 "actions/set_var.h"
#include <iostream>
#include <string>
#include "modsecurity/assay.h"
#include "src/rule.h"
namespace ModSecurity {
namespace actions {
SetVar::SetVar(std::string action)
: Action(action, RunTimeOnlyIfMatchKind) {
}
bool SetVar::init(std::string *error) {
size_t pos = std::string::npos;
// Resolv operation
operation = setToOne;
pos = action.find("=");
if (pos != std::string::npos) {
operation = setOperation;
}
pos = action.find("=+");
if (pos != std::string::npos) {
operation = sumAndSetOperation;
}
pos = action.find("=-");
if (pos != std::string::npos) {
operation = substractAndSetOperation;
}
// Collection name
pos = action.find(".");
if (pos != std::string::npos) {
collectionName = std::string(action, 0, pos);
} else {
error->assign("Missing the collection and/or variable name");
return false;
}
// Variable name
if (operation == setToOne) {
variableName = std::string(action, pos + 1, action.length()
- (pos + 1));
} else {
size_t pos2 = action.find("=");
variableName = std::string(action, pos + 1, pos2 - (pos + 1));
if (pos2 + 2 > action.length()) {
error->assign("Something wrong with the input format");
return false;
}
if (operation == setOperation) {
predicate = std::string(action, pos2 + 1, action.length() - (pos2));
} else {
predicate = std::string(action, pos2 + 2, action.length()
- (pos2 + 1));
}
}
if (collectionName.empty() || variableName.empty()) {
error->assign("Something wrong with the input format");
return false;
}
return true;
}
void SetVar::dump() {
std::cout << " Operation: " << std::to_string(operation) << std::endl;
std::cout << "Collection: " << collectionName << std::endl;
std::cout << " Variable: " << variableName << std::endl;
std::cout << " Predicate: " << predicate << std::endl;
}
bool SetVar::evaluate(Rule *rule, Assay *assay) {
std::string targetValue;
int value = 0;
try {
std::string *resolvedValue =
assay->resolve_variable_first(collectionName, variableName);
if (resolvedValue == NULL) {
value = 0;
} else {
value = stoi(*resolvedValue);
}
} catch (...) {
value = 0;
}
int pre = 0;
try {
pre = stoi(predicate);
} catch (...) {
/* perform a macro expansion. */
pre = 0;
}
switch (operation) {
case setOperation:
/* perform a macro expansion. */
targetValue = predicate;
break;
case sumAndSetOperation:
targetValue = std::to_string(value + pre);
break;
case substractAndSetOperation:
targetValue = std::to_string(value - pre);
break;
case setToOne:
targetValue = std::string("1");
break;
}
assay->setCollection(collectionName, variableName, targetValue);
return true;
}
} // namespace actions
} // namespace ModSecurity

60
src/actions/set_var.h Normal file
View File

@@ -0,0 +1,60 @@
/*
* 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 <string>
#include "actions/action.h"
#ifndef SRC_ACTIONS_SET_VAR_H_
#define SRC_ACTIONS_SET_VAR_H_
namespace ModSecurity {
class Assay;
class Rule;
namespace actions {
class SetVar : public Action {
public:
explicit SetVar(std::string action);
bool evaluate(Rule *rule, Assay *assay) override;
void dump();
bool init(std::string *error);
std::string collectionName;
std::string variableName;
std::string predicate;
enum SetVarOperation {
/* Set variable to something */
setOperation,
/* read variable, sum predicate and set */
sumAndSetOperation,
/* read variable, substract predicate and set */
substractAndSetOperation,
/* set variable to 1 */
setToOne
};
SetVarOperation operation;
};
} // namespace actions
} // namespace ModSecurity
#endif // SRC_ACTIONS_SET_VAR_H_