Adds support to & (count) and ! (exclusion) as variables variations

This commit is contained in:
Felipe Zimmerle
2015-08-06 14:12:12 -03:00
parent 4308ee0280
commit 88c53575be
9 changed files with 368 additions and 135 deletions

View File

@@ -21,6 +21,9 @@
#include <list>
#include "modsecurity/assay.h"
#include "variations/exclusion.h"
using ModSecurity::Variables::Variations::Exclusion;
namespace ModSecurity {
namespace Variables {
@@ -33,8 +36,18 @@ std::list<std::pair<std::string, std::string>>
std::string Variable::to_s(
std::vector<Variable *> *variables) {
std::string ret;
std::string except("");
for (int i = 0; i < variables->size() ; i++) {
std::string name = variables->at(i)->name;
Exclusion *e = dynamic_cast<Exclusion *>(variables->at(i));
if (e != NULL) {
if (except.empty()) {
except = except + name;
} else {
except = except + "|" + name;
}
continue;
}
if (i == 0) {
ret = ret + name;
@@ -43,6 +56,9 @@ std::string Variable::to_s(
}
}
if (except.empty() == false) {
ret = ret + ", except for: " + except;
}
return ret;
}

View File

@@ -0,0 +1,55 @@
/*
* 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 "variables/variations/count.h"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include "modsecurity/assay.h"
#include "src/utils.h"
namespace ModSecurity {
namespace Variables {
namespace Variations {
std::list<std::pair<std::string, std::string>>
Count::evaluate(Assay *assay) {
std::list<std::pair<std::string, std::string>> reslIn;
std::list<std::pair<std::string, std::string>> reslOut;
std::pair<std::string, std::string> pair;
int count = 0;
reslIn = var->evaluate(assay);
for (auto &a : reslIn) {
count++;
}
std::string res = std::to_string(count);
pair = std::make_pair(std::string(var->name), std::string(res));
reslOut.push_back(pair);
return reslOut;
}
} // namespace Variations
} // namespace Variables
} // namespace ModSecurity

View File

@@ -0,0 +1,49 @@
/*
* 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 <vector>
#include <string>
#include <list>
#include <utility>
#ifndef SRC_VARIABLES_VARIATIONS_COUNT_H_
#define SRC_VARIABLES_VARIATIONS_COUNT_H_
#include "variables/variable.h"
namespace ModSecurity {
class Assay;
namespace Variables {
namespace Variations {
class Count : public Variable {
public:
explicit Count(Variable *v)
: Variable("count(" + v->name + ")"),
var(v) { }
std::list<std::pair<std::string, std::string>>
evaluate(Assay *assay) override;
Variable *var;
};
} // namespace Variations
} // namespace Variables
} // namespace ModSecurity
#endif // SRC_VARIABLES_VARIATIONS_COUNT_H_

View File

@@ -0,0 +1,40 @@
/*
* 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 "variables/variations/exclusion.h"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include "modsecurity/assay.h"
#include "src/utils.h"
namespace ModSecurity {
namespace Variables {
namespace Variations {
std::list<std::pair<std::string, std::string>>
Exclusion::evaluate(Assay *assay) {
return assay->resolve_variable(this->name);
}
} // namespace Variations
} // namespace Variables
} // namespace ModSecurity

View File

@@ -0,0 +1,50 @@
/*
* 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 <vector>
#include <string>
#include <list>
#include <utility>
#include <iostream>
#ifndef SRC_VARIABLES_VARIATIONS_EXCLUSION_H_
#define SRC_VARIABLES_VARIATIONS_EXCLUSION_H_
#include "variables/variable.h"
namespace ModSecurity {
class Assay;
namespace Variables {
namespace Variations {
class Exclusion : public Variable {
public:
explicit Exclusion(Variable *v)
: Variable(v->name),
var(v) { }
std::list<std::pair<std::string, std::string>>
evaluate(Assay *assay) override;
Variable *var;
};
} // namespace Variations
} // namespace Variables
} // namespace ModSecurity
#endif // SRC_VARIABLES_VARIATIONS_EXCLUSION_H_