diff --git a/src/operators/verify_cc.cc b/src/operators/verify_cc.cc index b2e431a4..e654228a 100644 --- a/src/operators/verify_cc.cc +++ b/src/operators/verify_cc.cc @@ -15,8 +15,9 @@ #include "operators/verify_cc.h" -#include #include +#include +#include #include "operators/operator.h" @@ -66,16 +67,47 @@ int VerifyCC::luhnVerify(const char *ccnumber, int len) { } + +bool VerifyCC::init(const std::string ¶m2, const char **error) { + std::vector vec; + const char *errptr = NULL; + int erroffset = 0; + + m_pc = pcre_compile(param.c_str(), PCRE_DOTALL|PCRE_MULTILINE, + &errptr, &erroffset, NULL); + m_pce = pcre_study(m_pc, PCRE_STUDY_JIT_COMPILE, &errptr); + + if ((m_pc == NULL) || (m_pce == NULL)) { + *error = errptr; + return false; + } + + return true; +} + + bool VerifyCC::evaluate(Assay *assay, const std::string &i) { int offset = 0; bool is_cc = false; int target_length = i.length(); for (offset = 0; offset < target_length; offset++) { - std::string shiftedString(i, offset, i.length() - offset); std::string match; - pcrecpp::StringPiece input(shiftedString); - while (m_re.FindAndConsume(&input, &match)) { + int ovector[33]; + memset(ovector, 0, sizeof(ovector)); + int ret = pcre_exec(m_pc, m_pce, i.c_str(), i.size(), offset, + 0, ovector, 33) > 0; + + /* If there was no match, then we are done. */ + if (ret == PCRE_ERROR_NOMATCH) { + break; + } + if (ret < 0) { + return false; + } + + if (ret > 0) { + match = std::string(i, ovector[0], ovector[1] - ovector[0]); is_cc = luhnVerify(match.c_str(), match.size()); if (is_cc) { if (assay) { diff --git a/src/operators/verify_cc.h b/src/operators/verify_cc.h index 8adf932b..66a16c0e 100644 --- a/src/operators/verify_cc.h +++ b/src/operators/verify_cc.h @@ -16,9 +16,8 @@ #ifndef SRC_OPERATORS_VERIFY_CC_H_ #define SRC_OPERATORS_VERIFY_CC_H_ -#include - #include +#include #include "operators/operator.h" @@ -29,14 +28,14 @@ class VerifyCC : public Operator { public: /** @ingroup ModSecurity_Operator */ VerifyCC(std::string op, std::string param, bool negation) - : Operator(op, param, negation), - m_re(param, pcrecpp::RE_Options()) { } + : Operator(op, param, negation) { } int luhnVerify(const char *ccnumber, int len); bool evaluate(Assay *assay, const std::string &input) override; - + bool init(const std::string ¶m, const char **error) override; private: - pcrecpp::RE m_re; + pcre *m_pc; + pcre_extra *m_pce; }; } // namespace operators diff --git a/test/test-cases/secrules-language-tests b/test/test-cases/secrules-language-tests index e363aa64..37cf32eb 160000 --- a/test/test-cases/secrules-language-tests +++ b/test/test-cases/secrules-language-tests @@ -1 +1 @@ -Subproject commit e363aa647abebe1be523f9bdd5ba4466ba20256e +Subproject commit 37cf32eb8f939c06923a9ab24dd56a0975c36d4a