suricata
detect-http-request-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_request_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-mpm.h"
41 #include "detect-engine-state.h"
44 #include "detect-content.h"
45 #include "detect-pcre.h"
46 
47 #include "flow.h"
48 #include "flow-var.h"
49 #include "flow-util.h"
50 
51 #include "util-debug.h"
52 #include "util-unittest.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 DetectHttpRequestLineSetup(DetectEngineCtx *, Signature *, const char *);
64 #ifdef UNITTESTS
65 static void DetectHttpRequestLineRegisterTests(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_request_line_buffer_id = 0;
72 
73 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
74  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
75  const int list_id)
76 {
77  SCEnter();
78 
79  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
80  if (buffer->inspect == NULL) {
81  uint32_t b_len = 0;
82  const uint8_t *b = NULL;
83 
84  if (rs_http2_tx_get_request_line(txv, &b, &b_len) != 1)
85  return NULL;
86  if (b == NULL || b_len == 0)
87  return NULL;
88 
89  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
90  InspectionBufferApplyTransforms(buffer, transforms);
91  }
92 
93  return buffer;
94 }
95 
96 /**
97  * \brief Registers the keyword handlers for the "http_request_line" keyword.
98  */
100 {
101  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].name = "http.request_line";
102  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].alias = "http_request_line";
103  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].desc = "sticky buffer to match on the HTTP request line";
104  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].url = "/rules/http-keywords.html#http-request-line";
106  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].Setup = DetectHttpRequestLineSetup;
107 #ifdef UNITTESTS
108  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].RegisterTests = DetectHttpRequestLineRegisterTests;
109 #endif
111 
113  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
114 
115  DetectAppLayerMpmRegister("http_request_line", SIG_FLAG_TOSERVER, 2,
116  PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
117 
119  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
120  DetectAppLayerMpmRegister("http_request_line", SIG_FLAG_TOSERVER, 2,
121  PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
122 
123  DetectBufferTypeSetDescriptionByName("http_request_line",
124  "http request line");
125 
126  g_http_request_line_buffer_id = DetectBufferTypeGetByName("http_request_line");
127 }
128 
129 /**
130  * \brief The setup function for the http_request_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  */
142 static int DetectHttpRequestLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
143 {
144  if (DetectBufferSetActiveList(de_ctx, s, g_http_request_line_buffer_id) < 0)
145  return -1;
146 
148  return -1;
149 
150  return 0;
151 }
152 
153 static 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(tx->request_line == NULL)) {
162  return NULL;
163  }
164  const uint32_t data_len = bstr_len(tx->request_line);
165  const uint8_t *data = bstr_ptr(tx->request_line);
166 
167  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
168  InspectionBufferApplyTransforms(buffer, transforms);
169  }
170  return buffer;
171 }
172 
173 /************************************Unittests*********************************/
174 
175 #ifdef UNITTESTS
176 
177 #include "stream-tcp-reassemble.h"
178 
179 /**
180  * \test Test that a signature containing a http_request_line is correctly parsed
181  * and the keyword is registered.
182  */
183 static int DetectHttpRequestLineTest01(void)
184 {
187 
188  de_ctx->flags |= DE_QUIET;
189  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
190  "(http_request_line; content:\"GET /\"; sid:1;)");
192 
194  PASS;
195 }
196 
197 static void DetectHttpRequestLineRegisterTests(void)
198 {
199  UtRegisterTest("DetectHttpRequestLineTest01", DetectHttpRequestLineTest01);
200 }
201 #endif /* UNITTESTS */
202 /**
203  * @}
204  */
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-content.h
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
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:2154
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:408
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2611
DE_QUIET
#define DE_QUIET
Definition: detect.h:323
stream-tcp-reassemble.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
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:17
DetectEngineThreadCtx_
Definition: detect.h:1093
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
DETECT_AL_HTTP_REQUEST_LINE
@ DETECT_AL_HTTP_REQUEST_LINE
Definition: detect-engine-register.h:184
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2285
DetectAppLayerMpmRegister
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
Definition: detect-engine-mpm.c:151
app-layer-parser.h
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1272
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:64
detect-engine-content-inspection.h
SigTableElmt_::alias
const char * alias
Definition: detect.h:1305
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
util-spm.h
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:849
DetectHttpRequestLineRegister
void DetectHttpRequestLineRegister(void)
Registers the keyword handlers for the "http_request_line" keyword.
Definition: detect-http-request-line.c:99
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1595
detect-http-request-line.h
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:70
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2572
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:843
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
flow.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296
app-layer.h