suricata
detect-http-response-line.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2022 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \ingroup httplayer
20 *
21 * @{
22 */
23
24
25/**
26 * \file
27 *
28 * \author Victor Julien <victor@inliniac.net>
29 *
30 * Implements support for the http_response_line keyword.
31 */
32
33#include "suricata-common.h"
34#include "threads.h"
35#include "decode.h"
36
37#include "detect.h"
38#include "detect-parse.h"
39#include "detect-engine.h"
41#include "detect-engine-mpm.h"
42#include "detect-engine-state.h"
45#include "detect-content.h"
46#include "detect-pcre.h"
47
48#include "flow.h"
49#include "flow-var.h"
50#include "flow-util.h"
51
52#include "util-debug.h"
53#include "util-unittest.h"
55#include "util-spm.h"
56
57#include "app-layer.h"
58#include "app-layer-parser.h"
59
60#include "app-layer-htp.h"
61#include "stream-tcp.h"
63
64static int DetectHttpResponseLineSetup(DetectEngineCtx *, Signature *, const char *);
65#ifdef UNITTESTS
66static void DetectHttpResponseLineRegisterTests(void);
67#endif
68static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
69 const DetectEngineTransforms *transforms,
70 Flow *_f, const uint8_t _flow_flags,
71 void *txv, const int list_id);
72static int g_http_response_line_id = 0;
73
74static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
75 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
76 const int list_id)
77{
78 SCEnter();
79
80 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
81 if (buffer->inspect == NULL) {
82 uint32_t b_len = 0;
83 const uint8_t *b = NULL;
84
85 if (SCHttp2TxGetResponseLine(txv, &b, &b_len) != 1)
86 return NULL;
87 if (b == NULL || b_len == 0)
88 return NULL;
89
90 InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
91 }
92
93 return buffer;
94}
95
96/**
97 * \brief Registers the keyword handlers for the "http_response_line" keyword.
98 */
100{
101 sigmatch_table[DETECT_HTTP_RESPONSE_LINE].name = "http.response_line";
102 sigmatch_table[DETECT_HTTP_RESPONSE_LINE].alias = "http_response_line";
104 "content modifier to match only on the HTTP response line";
105 sigmatch_table[DETECT_HTTP_RESPONSE_LINE].url = "/rules/http-keywords.html#http-response-line";
106 sigmatch_table[DETECT_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
107#ifdef UNITTESTS
108 sigmatch_table[DETECT_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
109#endif
111
113 HTP_RESPONSE_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
114
115 DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2,
116 PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_LINE);
117
119 HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
120 DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2,
121 PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
122
123 DetectBufferTypeSetDescriptionByName("http_response_line",
124 "http response line");
125
126 g_http_response_line_id = DetectBufferTypeGetByName("http_response_line");
127}
128
129/**
130 * \brief The setup function for the http_response_line keyword for a signature.
131 *
132 * \param de_ctx Pointer to the detection engine context.
133 * \param s Pointer to the signature for the current Signature being
134 * parsed from the rules.
135 * \param m Pointer to the head of the SigMatch for the current rule
136 * being parsed.
137 * \param arg Pointer to the string holding the keyword value.
138 *
139 * \retval 0 On success
140 * \retval -1 On failure
141 */
142static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
143{
144 if (SCDetectBufferSetActiveList(de_ctx, s, g_http_response_line_id) < 0)
145 return -1;
146
148 return -1;
149
150 return 0;
151}
152
153static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
154 const DetectEngineTransforms *transforms,
155 Flow *_f, const uint8_t _flow_flags,
156 void *txv, const int list_id)
157{
158 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
159 if (buffer->inspect == NULL) {
160 htp_tx_t *tx = (htp_tx_t *)txv;
161 if (unlikely(htp_tx_response_line(tx) == NULL)) {
162 return NULL;
163 }
164 const uint32_t data_len = (uint32_t)bstr_len(htp_tx_response_line(tx));
165 const uint8_t *data = bstr_ptr(htp_tx_response_line(tx));
166
168 det_ctx, list_id, buffer, data, data_len, transforms);
169 }
170 return buffer;
171}
172
173/************************************Unittests*********************************/
174
175#ifdef UNITTESTS
176
178
179/**
180 * \test Test that a signature containing a http_response_line is correctly parsed
181 * and the keyword is registered.
182 */
183static int DetectHttpResponseLineTest01(void)
184{
187
189 de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
190 "(http_response_line; content:\"200 OK\"; sid:1;)");
192
194 PASS;
195}
196
197void DetectHttpResponseLineRegisterTests(void)
198{
199 UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
200}
201#endif /* UNITTESTS */
202/**
203 * @}
204 */
@ ALPROTO_HTTP2
@ ALPROTO_HTTP
@ ALPROTO_HTTP1
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_HTTP_RESPONSE_LINE
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Data structures and function prototypes for keeping state for the detection engine.
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define DE_QUIET
Definition detect.h:330
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
main detection engine ctx
Definition detect.h:932
uint8_t flags
Definition detect.h:934
Signature * sig_list
Definition detect.h:941
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define SCEnter(...)
Definition util-debug.h:277
#define unlikely(expr)