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"
40 #include "detect-engine-buffer.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-helper.h"
54 #include "util-spm.h"
55 
56 #include "app-layer.h"
57 #include "app-layer-parser.h"
58 
59 #include "app-layer-htp.h"
60 #include "stream-tcp.h"
62 
63 static int DetectHttpResponseLineSetup(DetectEngineCtx *, Signature *, const char *);
64 #ifdef UNITTESTS
65 static void DetectHttpResponseLineRegisterTests(void);
66 #endif
67 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms,
69  Flow *_f, const uint8_t _flow_flags,
70  void *txv, const int list_id);
71 static int g_http_response_line_id = 0;
72 static int g_http2_thread_id = 0;
73 
74 static 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 = SCInspectionBufferGet(det_ctx, list_id);
81  if (buffer->inspect == NULL) {
82  uint32_t b_len = 0;
83  const uint8_t *b = NULL;
84 
85  void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
86  if (thread_buf == NULL)
87  return NULL;
88  if (SCHttp2TxGetResponseLine(txv, &b, &b_len, thread_buf) != 1)
89  return NULL;
90  if (b == NULL || b_len == 0)
91  return NULL;
92 
93  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
94  }
95 
96  return buffer;
97 }
98 
99 /**
100  * \brief Registers the keyword handlers for the "http_response_line" keyword.
101  */
103 {
104  sigmatch_table[DETECT_HTTP_RESPONSE_LINE].name = "http.response_line";
105  sigmatch_table[DETECT_HTTP_RESPONSE_LINE].alias = "http_response_line";
107  "content modifier to match only on the HTTP response line";
108  sigmatch_table[DETECT_HTTP_RESPONSE_LINE].url = "/rules/http-keywords.html#http-response-line";
109  sigmatch_table[DETECT_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
110 #ifdef UNITTESTS
111  sigmatch_table[DETECT_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
112 #endif
114 
116  HTP_RESPONSE_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
117 
118  DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2,
119  PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_LINE);
120 
122  SIG_FLAG_TOCLIENT, HTTP2TxTypeStream, HTTP2ProgData, DetectEngineInspectBufferGeneric,
123  GetData2);
124  DetectAppLayerMpmRegisterSubState("http_response_line", SIG_FLAG_TOCLIENT, 2,
125  PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2TxTypeStream, HTTP2ProgData);
126 
127  DetectBufferTypeSetDescriptionByName("http_response_line",
128  "http response line");
129 
130  g_http_response_line_id = DetectBufferTypeGetByName("http_response_line");
131  g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs(
132  "http.response_line", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree);
133 }
134 
135 /**
136  * \brief The setup function for the http_response_line keyword for a signature.
137  *
138  * \param de_ctx Pointer to the detection engine context.
139  * \param s Pointer to the signature for the current Signature being
140  * parsed from the rules.
141  * \param m Pointer to the head of the SigMatch for the current rule
142  * being parsed.
143  * \param arg Pointer to the string holding the keyword value.
144  *
145  * \retval 0 On success
146  * \retval -1 On failure
147  */
148 static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
149 {
150  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_response_line_id) < 0)
151  return -1;
152 
154  return -1;
155 
156  return 0;
157 }
158 
159 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
160  const DetectEngineTransforms *transforms,
161  Flow *_f, const uint8_t _flow_flags,
162  void *txv, const int list_id)
163 {
164  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
165  if (buffer->inspect == NULL) {
166  htp_tx_t *tx = (htp_tx_t *)txv;
167  if (unlikely(htp_tx_response_line(tx) == NULL)) {
168  return NULL;
169  }
170  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_response_line(tx));
171  const uint8_t *data = bstr_ptr(htp_tx_response_line(tx));
172 
174  det_ctx, list_id, buffer, data, data_len, transforms);
175  }
176  return buffer;
177 }
178 
179 /************************************Unittests*********************************/
180 
181 #ifdef UNITTESTS
182 
183 #include "stream-tcp-reassemble.h"
184 
185 /**
186  * \test Test that a signature containing a http_response_line is correctly parsed
187  * and the keyword is registered.
188  */
189 static int DetectHttpResponseLineTest01(void)
190 {
193 
194  de_ctx->flags |= DE_QUIET;
195  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
196  "(http_response_line; content:\"200 OK\"; sid:1;)");
198 
200  PASS;
201 }
202 
203 void DetectHttpResponseLineRegisterTests(void)
204 {
205  UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
206 }
207 #endif /* UNITTESTS */
208 /**
209  * @}
210  */
SigTableElmt_::url
const char * url
Definition: detect.h:1548
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:337
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1547
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
DetectEngineInspectBufferGeneric
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.
Definition: detect-engine.c:2268
flow-util.h
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1545
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:366
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1536
SCDetectThreadCtxGetGlobalKeywordThreadCtx
void * SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:4136
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:360
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:966
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(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
Definition: detect-engine-inspect-buffer.c:197
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2913
DE_QUIET
#define DE_QUIET
Definition: detect.h:305
stream-tcp-reassemble.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2693
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:246
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1527
detect-pcre.h
DetectAppLayerMpmRegisterSubState
void DetectAppLayerMpmRegisterSubState(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t sub_state, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:167
detect-engine-prefilter.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:276
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1453
app-layer-htp.h
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1295
detect-http-response-line.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1707
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3698
app-layer-parser.h
DETECT_HTTP_RESPONSE_LINE
@ DETECT_HTTP_RESPONSE_LINE
Definition: detect-engine-register.h:200
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
detect-engine-content-inspection.h
DetectHttpResponseLineRegister
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
Definition: detect-http-response-line.c:102
SigTableElmt_::alias
const char * alias
Definition: detect.h:1546
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:976
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:299
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:663
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:77
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:359
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2874
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:968
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1550
flow.h
SCDetectRegisterThreadCtxGlobalFuncs
int SCDetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:4092
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1534
app-layer.h