Support PCRE2

This commit is contained in:
Martin Vierula
2022-04-13 09:37:54 -07:00
parent 5519f6cfae
commit f84614fe06
9 changed files with 380 additions and 18 deletions

View File

@@ -15,24 +15,28 @@
#include "src/operators/verify_cc.h"
#include <pcre.h>
#include <iostream>
#include <cstring>
#include <vector>
#include "src/operators/operator.h"
#ifndef WITH_PCRE2
#if PCRE_HAVE_JIT
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
#else
#define pcre_study_opt 0
#endif
#endif
namespace modsecurity {
namespace operators {
VerifyCC::~VerifyCC() {
#if WITH_PCRE2
pcre2_code_free(m_pc);
#else
if (m_pc != NULL) {
pcre_free(m_pc);
m_pc = NULL;
@@ -45,6 +49,7 @@ VerifyCC::~VerifyCC() {
#endif
m_pce = NULL;
}
#endif
}
/**
@@ -90,6 +95,22 @@ int VerifyCC::luhnVerify(const char *ccnumber, int len) {
bool VerifyCC::init(const std::string &param2, std::string *error) {
#ifdef WITH_PCRE2
PCRE2_SPTR pcre2_pattern = reinterpret_cast<PCRE2_SPTR>(m_param.c_str());
uint32_t pcre2_options = (PCRE2_DOTALL|PCRE2_MULTILINE);
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
pcre2_options, &errornumber, &erroroffset, NULL);
if (m_pc == NULL) {
return false;
} else {
m_match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
if (m_match_data == NULL) {
return false;
}
}
#else
const char *errptr = NULL;
int erroffset = 0;
@@ -112,6 +133,7 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
error->assign(errptr);
return false;
}
#endif
return true;
}
@@ -119,11 +141,25 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
const std::string& i, std::shared_ptr<RuleMessage> ruleMessage) {
#ifdef WITH_PCRE2
PCRE2_SIZE offset = 0;
size_t target_length = i.length();
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
for (offset = 0; offset < target_length; offset++) {
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, m_match_data, NULL);
/* If there was no match, then we are done. */
if (ret < 0) {
break;
}
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
#else
int offset = 0;
int target_length = i.length();
for (offset = 0; offset < target_length; offset++) {
std::string match;
int ovector[33];
memset(ovector, 0, sizeof(ovector));
int ret = pcre_exec(m_pc, m_pce, i.c_str(), i.size(), offset,
@@ -136,8 +172,9 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
if (ret < 0) {
return false;
}
#endif
if (ret > 0) {
match = std::string(i, ovector[0], ovector[1] - ovector[0]);
std::string match = std::string(i, ovector[0], ovector[1] - ovector[0]);
int is_cc = luhnVerify(match.c_str(), match.size());
if (is_cc) {
if (t) {

View File

@@ -16,7 +16,14 @@
#ifndef SRC_OPERATORS_VERIFY_CC_H_
#define SRC_OPERATORS_VERIFY_CC_H_
#if WITH_PCRE2
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#else
#include <pcre.h>
#endif
#include <string>
#include <memory>
#include <utility>
@@ -32,7 +39,11 @@ class VerifyCC : public Operator {
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
: Operator("VerifyCC", std::move(param)),
m_pc(NULL),
#if WITH_PCRE2
m_match_data(NULL) { }
#else
m_pce(NULL) { }
#endif
~VerifyCC();
bool evaluate(Transaction *t, RuleWithActions *rule,
@@ -40,8 +51,13 @@ class VerifyCC : public Operator {
std::shared_ptr<RuleMessage> ruleMessage) override;
bool init(const std::string &param, std::string *error) override;
private:
#if WITH_PCRE2
pcre2_code *m_pc;
pcre2_match_data *m_match_data;
#else
pcre *m_pc;
pcre_extra *m_pce;
#endif
static int luhnVerify(const char *ccnumber, int len);
};