From 9cab3ea2cdf78e815714febcf364317fb7072f60 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 8 Jul 2016 19:35:13 +0200 Subject: [PATCH] http_stat_code: mpm prefilter engine --- src/detect-engine-hscd.c | 62 ++++++++++++++++++---------------------- src/detect-engine-hscd.h | 4 +-- src/detect-engine-mpm.c | 4 ++- src/detect.c | 8 ------ 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/detect-engine-hscd.c b/src/detect-engine-hscd.c index 8d0cbbee5a..205005570f 100644 --- a/src/detect-engine-hscd.c +++ b/src/detect-engine-hscd.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2016 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -25,6 +25,7 @@ * \file * * \author Anoop Saldanha + * \author Victor Julien */ #include "suricata-common.h" @@ -38,6 +39,7 @@ #include "detect-parse.h" #include "detect-engine-state.h" #include "detect-engine-content-inspection.h" +#include "detect-engine-prefilter.h" #include "flow-util.h" #include "util-debug.h" @@ -55,52 +57,44 @@ #include "app-layer-protos.h" #include "util-validate.h" -/** - * \brief Http stat code match -- searches for one pattern per signature. - * - * \param det_ctx Detection engine thread ctx. - * \param stat_code Stat code to inspect. - * \param stat_code_len Stat code length. +/** \brief HTTP Status Code Mpm prefilter callback * - * \retval ret Number of matches. + * \param det_ctx detection engine thread ctx + * \param p packet to inspect + * \param f flow to inspect + * \param txv tx to inspect + * \param pectx inspection context */ -static inline uint32_t HttpStatCodePatternSearch(DetectEngineThreadCtx *det_ctx, - const uint8_t *stat_code, const uint32_t stat_code_len) +static void PrefilterTxHttpStatCode(DetectEngineThreadCtx *det_ctx, + const void *pectx, + Packet *p, Flow *f, void *txv, + const uint64_t idx, const uint8_t flags) { SCEnter(); - uint32_t ret = 0; + const MpmCtx *mpm_ctx = (MpmCtx *)pectx; + htp_tx_t *tx = (htp_tx_t *)txv; - DEBUG_VALIDATE_BUG_ON(det_ctx->sgh->mpm_hscd_ctx_tc == NULL); + if (tx->response_status == NULL) + return; - if (stat_code_len >= det_ctx->sgh->mpm_hscd_ctx_tc->minlen) { - ret = mpm_table[det_ctx->sgh->mpm_hscd_ctx_tc->mpm_type]. - Search(det_ctx->sgh->mpm_hscd_ctx_tc, &det_ctx->mtcu, - &det_ctx->pmq, stat_code, stat_code_len); - } + const uint32_t buffer_len = bstr_len(tx->response_status); + const uint8_t *buffer = bstr_ptr(tx->response_status); - SCReturnUInt(ret); + if (buffer != NULL && buffer_len >= mpm_ctx->minlen) { + (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx, + &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len); + } } -/** - * \brief Run the mpm against http stat code. - * - * \retval cnt Number of matches reported by the mpm algo. - */ -int DetectEngineRunHttpStatCodeMpm(DetectEngineThreadCtx *det_ctx, void *txv) +int PrefilterTxHttpStatCodeRegister(SigGroupHead *sgh, MpmCtx *mpm_ctx) { SCEnter(); - uint32_t cnt = 0; - htp_tx_t *tx = (htp_tx_t *)txv; - if (tx->response_status == NULL) - goto end; - - cnt = HttpStatCodePatternSearch(det_ctx, - (const uint8_t *)bstr_ptr(tx->response_status), - bstr_len(tx->response_status)); -end: - SCReturnInt(cnt); + return PrefilterAppendTxEngine(sgh, PrefilterTxHttpStatCode, + ALPROTO_HTTP, + HTP_RESPONSE_LINE+1, /* inspect when response line completely parsed */ + mpm_ctx, NULL); } /** diff --git a/src/detect-engine-hscd.h b/src/detect-engine-hscd.h index d85921d3eb..73977eccec 100644 --- a/src/detect-engine-hscd.h +++ b/src/detect-engine-hscd.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2016 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -25,7 +25,7 @@ #include "app-layer-htp.h" -int DetectEngineRunHttpStatCodeMpm(DetectEngineThreadCtx *det_ctx, void *txv); +int PrefilterTxHttpStatCodeRegister(SigGroupHead *sgh, MpmCtx *mpm_ctx); int DetectEngineInspectHttpStatCode(ThreadVars *tv, DetectEngineCtx *de_ctx, diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index a8a3a3de3d..a231fdcf70 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -57,6 +57,7 @@ #include "detect-engine-hhhd.h" #include "detect-engine-hrhhd.h" #include "detect-engine-hsmd.h" +#include "detect-engine-hscd.h" #include "detect-engine-dns.h" #include "stream.h" @@ -112,7 +113,8 @@ AppLayerMpms app_mpms[] = { { "http_stat_msg", 0, SIG_FLAG_TOCLIENT, DETECT_SM_LIST_HSMDMATCH, SIG_GROUP_HEAD_MPM_HSMD, PrefilterTxHttpStatMsgRegister, 10}, - { "http_stat_code", 0, SIG_FLAG_TOCLIENT, DETECT_SM_LIST_HSCDMATCH, SIG_GROUP_HEAD_MPM_HSCD, NULL, 11}, + { "http_stat_code", 0, SIG_FLAG_TOCLIENT, DETECT_SM_LIST_HSCDMATCH, + SIG_GROUP_HEAD_MPM_HSCD, PrefilterTxHttpStatCodeRegister, 11}, { "http_client_body", 0, SIG_FLAG_TOSERVER, DETECT_SM_LIST_HCBDMATCH, SIG_GROUP_HEAD_MPM_HCBD, NULL, 12}, diff --git a/src/detect.c b/src/detect.c index 7216425cec..701eca169c 100644 --- a/src/detect.c +++ b/src/detect.c @@ -928,14 +928,6 @@ static inline void DetectMpmPrefilter(DetectEngineCtx *de_ctx, } else { /* implied FLOW_PKT_TOCLIENT */ tx_progress = AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags); - if (tx_progress > HTP_RESPONSE_LINE) { - if (det_ctx->sgh->flags & SIG_GROUP_HEAD_MPM_HSCD) { - PACKET_PROFILING_DETECT_START(p, PROF_DETECT_MPM_HSCD); - DetectEngineRunHttpStatCodeMpm(det_ctx, tx); - PACKET_PROFILING_DETECT_END(p, PROF_DETECT_MPM_HSCD); - } - } - if (tx_progress >= HTP_RESPONSE_HEADERS) { if (det_ctx->sgh->flags & SIG_GROUP_HEAD_MPM_HHD) { PACKET_PROFILING_DETECT_START(p, PROF_DETECT_MPM_HHD);