bunkerweb/test/optimization/optimization.cc
Théophile Diot cfc32af85c Squashed 'src/deps/src/modsecurity/' changes from ccc2d9b536..bbde9381cb
bbde9381cb Change release version to v3.0.11
35969946ca Merge pull request #3024 from martinhsv/v3/master
4c7a9bd312 Add WRDE_NOCMD to wordexp call
5b094c0ce9 Merge pull request #3014 from martinhsv/v3/master
c11b28292d Fix: validateDTD compile fails if when libxml2 not installed
cb4d7ae371 Adjust some copyright dates
0311da9f1b Merge pull request #3008 from martinhsv/v3/master
beaa452302 Fix memory leak of validateDTD's dtd object
a9edee3dbe const-ify some references in test folder
de2d053d6e Remove unneeded cppcheck suppression
36adc58ea3 const-ify some references (satisfy cppcheck)
b9836bcaa4 Merge pull request #3005 from martinhsv/v3/master
b180de53bf Fix memory leaks in ValidateSchema
fd67c6eb1d Remove unneeded heap allocation in AnchoredSetVariable::set
dc6cce5f0c refactoring and remove dead code in lmdb
3951ba0e48 CHANGES entry for previous PR
2fcd373107 Merge pull request #3001 from SpiderLabs/v3/dev/action_expirevar
c63b5bea1d Change linux workflow to Ubuntu 22.04
34809d8064 Add expirevar support for lmdb
118e1b3a44 Support expirevar for in-memory collection
135d1fa42b Merge pull request #2985 from martinhsv/v3/master
af45ccd53f Fix: lmdb regex match on non-null-terminated string
375519d5f2 Merge pull request #2983 from martinhsv/v3/master
dc2e38e242 Fix memory leaks in lmdb code (new'd strings)
8594cb8a7d Correct json for one regression test file
60f802e480 Merge pull request #2939 from martinhsv/v3/master
31cbd7309a Configure: add additional name to pcre2 pkg-config list
cbe2d61174 CHANGES: Preparing for next version

git-subtree-dir: src/deps/src/modsecurity
git-subtree-split: bbde9381cbccb49ea73f6194b08b478adc53f3bc
2023-12-07 14:46:12 +01:00

136 lines
4 KiB
C++

/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2023 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/rules_set_properties.h"
#include "modsecurity/rules_set.h"
#include "modsecurity/modsecurity.h"
#include "src/utils/system.h"
#include "src/parser/driver.h"
#include "src/utils/https_client.h"
#include "modsecurity/transaction.h"
#include "modsecurity/rule_unconditional.h"
#include "modsecurity/rule_with_operator.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::RulesSet *modsecRules = new modsecurity::RulesSet();
std::list<std::string> files;
int total = 0;
int p = 1;
while (p < argc) {
std::list<std::string> tfiles = modsecurity::utils::expandEnv(
argv[p], 0);
for (const auto &file : tfiles) {
files.insert(files.begin(), file);
}
p++;
}
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;
delete modsecRules;
return -1;
}
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Rules optimization" << std::endl;
std::cout << std::endl;
int nphases = modsecurity::Phases::NUMBER_OF_PHASES;
for (int j = 0; j < nphases; j++) {
Rules *rules = modsecRules->m_rulesSetPhases[j];
if (rules->size() == 0) {
continue;
}
std::cout << "Phase: " << std::to_string(j);
std::cout << " (" << std::to_string(rules->size());
std::cout << " rules)" << std::endl;
std::unordered_map<std::string, int> operators;
for (int i = 0; i < rules->size(); i++) {
auto z = rules->at(i);
if (z == NULL) {
continue;
}
if (dynamic_cast<modsecurity::RuleUnconditional *>(z.get()) != nullptr) {
std::string op = "Unconditional";
if (operators.count(op) > 0) {
operators[op] = 1 + operators[op];
} else {
operators[op] = 1;
}
}
if (dynamic_cast<modsecurity::RuleWithOperator *>(z.get()) != nullptr) {
auto *rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get());
std::string op = rwo->getOperatorName();
if (operators.count(op) > 0) {
operators[op] = 1 + operators[op];
} else {
operators[op] = 1;
}
}
}
if (operators.empty()) {
std::cout << " ~ no SecRule found ~ " << std::endl;
continue;
}
std::cout << " Operators" << std::endl;
for (const auto &z : operators) {
const auto &s = z.second;
std::cout << " " << std::left << std::setw(20) << z.first;
std::cout << std::right << std::setw(4) << 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;
delete modsecRules;
return 0;
}