mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-14 05:45:59 +03:00
Adds missing file
This commit is contained in:
parent
de79848285
commit
ac61d1c40b
153
test/optimization/optimization.cc
Normal file
153
test/optimization/optimization.cc
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
* 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.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
#include "modsecurity/modsecurity.h"
|
||||
#include "modsecurity/rules.h"
|
||||
#include "src/utils.h"
|
||||
#include "parser/driver.h"
|
||||
#include "utils/https_client.h"
|
||||
#include "modsecurity/assay.h"
|
||||
#include "modsecurity/rules_properties.h"
|
||||
|
||||
void print_help() {
|
||||
std::cout << "Use ./optimization /path/to/files.something" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
ModSecurity::Rules *modsecRules = new ModSecurity::Rules();
|
||||
std::vector<std::string> files;
|
||||
int total = 0;
|
||||
|
||||
int i = 1;
|
||||
while (i < argc) {
|
||||
std::vector<std::string> tfiles = ModSecurity::expandEnv(argv[i] , 0);
|
||||
for (const auto &file : tfiles) {
|
||||
files.insert(files.begin(), file);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
for (auto &x : files) {
|
||||
std::cout << "Loading file: " << x << std::endl;
|
||||
if (modsecRules->loadFromUri(x.c_str()) < 0) {
|
||||
std::cout << "Not able to load the rules" << std::endl;
|
||||
std::cout << modsecRules->getParserError() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Rules optimization" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
int nphases = ModSecurity::ModSecurity::Phases::NUMBER_OF_PHASES;
|
||||
for (int i = 0; i < nphases; i++) {
|
||||
std::vector<Rule *> rules = modsecRules->rules[i];
|
||||
if (rules.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
std::cout << "Phase: " << std::to_string(i);
|
||||
std::cout << " (" << std::to_string(rules.size());
|
||||
std::cout << " rules)" << std::endl;
|
||||
|
||||
std::unordered_map<std::string, int> operators;
|
||||
std::unordered_map<std::string, int> variables;
|
||||
std::unordered_map<std::string, int> op2var;
|
||||
for (auto &z : rules) {
|
||||
std::string key;
|
||||
if (z == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (z->op != NULL) {
|
||||
std::string op = z->op->op;
|
||||
if (operators.count(op) > 0) {
|
||||
operators[op] = 1 + operators[op];
|
||||
} else {
|
||||
operators[op] = 1;
|
||||
}
|
||||
key = op;
|
||||
}
|
||||
if (z->variables != NULL) {
|
||||
std::string var = Variable::to_s(z->variables);
|
||||
if (variables.count(var) > 0) {
|
||||
variables[var] = 1 + variables[var];
|
||||
} else {
|
||||
variables[var] = 1;
|
||||
}
|
||||
key = key + var;
|
||||
}
|
||||
if (z->variables != NULL && z->op != NULL) {
|
||||
if (op2var.count(key) > 0) {
|
||||
op2var[key] = 1 + op2var[key];
|
||||
} else {
|
||||
op2var[key] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operators.size() == 0 &&
|
||||
variables.size() == 0 &&
|
||||
op2var.size() == 0) {
|
||||
std::cout << " ~ no SecRule found ~ " << std::endl;
|
||||
continue;
|
||||
}
|
||||
std::cout << " Operators" << std::endl;
|
||||
for (auto &z : operators) {
|
||||
auto &s = z.second;
|
||||
std::cout << " " << std::left << std::setw(20) << z.first;
|
||||
std::cout << std::right << std::setw(4) << std::to_string(s);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::cout << " Variables" << std::endl;
|
||||
for (auto &z : variables) {
|
||||
auto &s = z.second;
|
||||
std::cout << " " << std::left << std::setw(20) << z.first;
|
||||
std::cout << std::right << std::setw(4) << std::to_string(s);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << " Operators applied to variables" << std::endl;
|
||||
for (auto &z : op2var) {
|
||||
auto &s = z.second;
|
||||
std::cout << " " << std::left << std::setw(40) << z.first;
|
||||
std::cout << std::right << std::setw(4) << std::to_string(s);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
total += rules.size();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Total of: " << std::to_string(total) << " rules.";
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user