mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-29 19:24:29 +03:00
rx:exit after full match; fix TX population after unused group
This commit is contained in:
@@ -38,7 +38,6 @@ bool Rx::init(const std::string &arg, std::string *error) {
|
||||
|
||||
bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
|
||||
const std::string& input, std::shared_ptr<RuleMessage> ruleMessage) {
|
||||
std::list<SMatch> matches;
|
||||
Regex *re;
|
||||
|
||||
if (m_param.empty() && !m_string->m_containsMacro) {
|
||||
@@ -52,29 +51,29 @@ bool Rx::evaluate(Transaction *transaction, RuleWithActions *rule,
|
||||
re = m_re;
|
||||
}
|
||||
|
||||
matches = re->searchAll(input);
|
||||
std::vector<Utils::SMatchCapture> captures;
|
||||
re->searchOneMatch(input, captures);
|
||||
|
||||
if (rule && rule->hasCaptureAction() && transaction) {
|
||||
int i = 0;
|
||||
matches.reverse();
|
||||
for (const SMatch& a : matches) {
|
||||
for (const Utils::SMatchCapture& capture : captures) {
|
||||
const std::string capture_substring(input.substr(capture.m_offset,capture.m_length));
|
||||
transaction->m_collections.m_tx_collection->storeOrUpdateFirst(
|
||||
std::to_string(i), a.str());
|
||||
std::to_string(capture.m_group), capture_substring);
|
||||
ms_dbg_a(transaction, 7, "Added regex subexpression TX." +
|
||||
std::to_string(i) + ": " + a.str());
|
||||
transaction->m_matched.push_back(a.str());
|
||||
i++;
|
||||
std::to_string(capture.m_group) + ": " + capture_substring);
|
||||
transaction->m_matched.push_back(capture_substring);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto & i : matches) {
|
||||
logOffset(ruleMessage, i.offset(), i.str().size());
|
||||
for (const auto & capture : captures) {
|
||||
logOffset(ruleMessage, capture.m_offset, capture.m_length);
|
||||
}
|
||||
|
||||
if (m_string->m_containsMacro) {
|
||||
delete re;
|
||||
}
|
||||
|
||||
if (matches.size() > 0) {
|
||||
if (captures.size() > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -16,10 +16,6 @@
|
||||
#include "src/utils/regex.h"
|
||||
|
||||
#include <pcre.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
@@ -99,6 +95,26 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
|
||||
return retList;
|
||||
}
|
||||
|
||||
bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures) const {
|
||||
const char *subject = s.c_str();
|
||||
int ovector[OVECCOUNT];
|
||||
|
||||
int rc = pcre_exec(m_pc, m_pce, subject, s.size(), 0, 0, ovector, OVECCOUNT);
|
||||
|
||||
for (int i = 0; i < rc; i++) {
|
||||
size_t start = ovector[2*i];
|
||||
size_t end = ovector[2*i+1];
|
||||
size_t len = end - start;
|
||||
if (end > s.size()) {
|
||||
continue;
|
||||
}
|
||||
SMatchCapture capture(i, start, len);
|
||||
captures.push_back(capture);
|
||||
}
|
||||
|
||||
return (rc > 0);
|
||||
}
|
||||
|
||||
int Regex::search(const std::string& s, SMatch *match) const {
|
||||
int ovector[OVECCOUNT];
|
||||
int ret = pcre_exec(m_pc, m_pce, s.c_str(),
|
||||
|
@@ -19,6 +19,7 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#ifndef SRC_UTILS_REGEX_H_
|
||||
#define SRC_UTILS_REGEX_H_
|
||||
@@ -47,6 +48,16 @@ class SMatch {
|
||||
size_t m_offset;
|
||||
};
|
||||
|
||||
struct SMatchCapture {
|
||||
SMatchCapture(size_t group, size_t offset, size_t length) :
|
||||
m_group(group),
|
||||
m_offset(offset),
|
||||
m_length(length) { }
|
||||
|
||||
size_t m_group; // E.g. 0 = full match; 6 = capture group 6
|
||||
size_t m_offset; // offset of match within the analyzed string
|
||||
size_t m_length;
|
||||
};
|
||||
|
||||
class Regex {
|
||||
public:
|
||||
@@ -58,6 +69,7 @@ class Regex {
|
||||
Regex& operator=(const Regex&) = delete;
|
||||
|
||||
std::list<SMatch> searchAll(const std::string& s) const;
|
||||
bool searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures) const;
|
||||
int search(const std::string &s, SMatch *match) const;
|
||||
int search(const std::string &s) const;
|
||||
|
||||
|
Reference in New Issue
Block a user