suricata
detect-http-uri.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2018 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 Gerardo Iglesias <iglesiasg@gmail.com>
29  * \author Victor Julien <victor@inliniac.net>
30  */
31 
32 #include "suricata-common.h"
33 #include "threads.h"
34 #include "debug.h"
35 #include "decode.h"
36 #include "detect.h"
37 
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
42 #include "detect-content.h"
43 #include "detect-pcre.h"
44 #include "detect-urilen.h"
45 
46 #include "flow.h"
47 #include "flow-var.h"
48 
49 #include "util-debug.h"
50 #include "util-unittest.h"
51 #include "util-spm.h"
52 #include "util-print.h"
53 
54 #include "app-layer.h"
55 
56 #include "app-layer-htp.h"
57 #include "detect-http-uri.h"
58 #include "detect-uricontent.h"
59 #include "stream-tcp.h"
60 
61 #ifdef UNITTESTS
62 static void DetectHttpUriRegisterTests(void);
63 #endif
64 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
65  Signature *s);
66 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror);
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 InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
72  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
73  const int list_id);
74 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
75 static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
76 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
77  Signature *s);
78 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **);
79 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
80  const DetectEngineTransforms *transforms,
81  Flow *_f, const uint8_t _flow_flags,
82  void *txv, const int list_id);
83 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
84 
85 static int g_http_raw_uri_buffer_id = 0;
86 static int g_http_uri_buffer_id = 0;
87 
88 /**
89  * \brief Registration function for keywords: http_uri and http.uri
90  */
92 {
93  /* http_uri content modifier */
95  sigmatch_table[DETECT_AL_HTTP_URI].desc = "content modifier to match specifically and only on the HTTP uri-buffer";
96  sigmatch_table[DETECT_AL_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
98 #ifdef UNITTESTS
99  sigmatch_table[DETECT_AL_HTTP_URI].RegisterTests = DetectHttpUriRegisterTests;
100 #endif
103 
104  /* http.uri sticky buffer */
105  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
106  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
107  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
108  sigmatch_table[DETECT_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
109  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
111 
113  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
114 
116  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
117 
119  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
120 
122  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
123 
125  "http request uri");
126 
128  DetectHttpUriSetupCallback);
129 
131  DetectHttpUriValidateCallback);
132 
133  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
134 
135  /* http_raw_uri content modifier */
136  sigmatch_table[DETECT_AL_HTTP_RAW_URI].name = "http_raw_uri";
137  sigmatch_table[DETECT_AL_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
138  sigmatch_table[DETECT_AL_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
139  sigmatch_table[DETECT_AL_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
142 
143  /* http.uri.raw sticky buffer */
144  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
145  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
146  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
147  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
149 
151  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetRawData);
152 
154  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
155 
156  // no difference between raw and decoded uri for HTTP2
158  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
159 
161  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
162 
164  "raw http uri");
165 
167  DetectHttpRawUriSetupCallback);
168 
170  DetectHttpRawUriValidateCallback);
171 
172  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
173 }
174 
175 /**
176  * \brief this function setups the http_uri modifier keyword used in the rule
177  *
178  * \param de_ctx Pointer to the Detection Engine Context
179  * \param s Pointer to the Signature to which the current keyword belongs
180  * \param str Should hold an empty string always
181  *
182  * \retval 0 On success
183  * \retval -1 On failure
184  */
185 
187 {
189  de_ctx, s, str, DETECT_AL_HTTP_URI, g_http_uri_buffer_id, ALPROTO_HTTP1);
190 }
191 
192 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror)
193 {
194  return DetectUrilenValidateContent(s, g_http_uri_buffer_id, sigerror);
195 }
196 
197 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
198  Signature *s)
199 {
200  SCLogDebug("callback invoked by %u", s->id);
201  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
202 }
203 
204 /**
205  * \brief this function setup the http.uri keyword used in the rule
206  *
207  * \param de_ctx Pointer to the Detection Engine Context
208  * \param s Pointer to the Signature to which the current keyword belongs
209  * \param str Should hold an empty string always
210  *
211  * \retval 0 On success
212  */
213 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
214 {
215  if (DetectBufferSetActiveList(s, g_http_uri_buffer_id) < 0)
216  return -1;
218  return -1;
219  return 0;
220 }
221 
222 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
223  const DetectEngineTransforms *transforms, Flow *_f,
224  const uint8_t _flow_flags, void *txv, const int list_id)
225 {
226  SCEnter();
227 
228  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
229  if (buffer->inspect == NULL) {
230  htp_tx_t *tx = (htp_tx_t *)txv;
231  HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
232 
233  if (tx_ud == NULL || tx_ud->request_uri_normalized == NULL) {
234  SCLogDebug("no tx_id or uri");
235  return NULL;
236  }
237 
238  const uint32_t data_len = bstr_len(tx_ud->request_uri_normalized);
239  const uint8_t *data = bstr_ptr(tx_ud->request_uri_normalized);
240 
241  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
242  InspectionBufferApplyTransforms(buffer, transforms);
243  }
244 
245  return buffer;
246 }
247 
248 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
249  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
250  const int list_id)
251 {
252  SCEnter();
253 
254  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
255  if (buffer->inspect == NULL) {
256  uint32_t b_len = 0;
257  const uint8_t *b = NULL;
258 
259  if (rs_http2_tx_get_uri(txv, &b, &b_len) != 1)
260  return NULL;
261  if (b == NULL || b_len == 0)
262  return NULL;
263 
264  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
265  InspectionBufferApplyTransforms(buffer, transforms);
266  }
267 
268  return buffer;
269 }
270 
271 /**
272  * \brief Sets up the http_raw_uri modifier keyword.
273  *
274  * \param de_ctx Pointer to the Detection Engine Context.
275  * \param s Pointer to the Signature to which the current keyword belongs.
276  * \param arg Should hold an empty string always.
277  *
278  * \retval 0 On success.
279  * \retval -1 On failure.
280  */
281 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
282 {
284  de_ctx, s, arg, DETECT_AL_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
285 }
286 
287 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **sigerror)
288 {
289  return DetectUrilenValidateContent(s, g_http_raw_uri_buffer_id, sigerror);
290 }
291 
292 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
293  Signature *s)
294 {
295  SCLogDebug("callback invoked by %u", s->id);
296  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
297 }
298 
299 /**
300  * \brief this function setup the http.uri.raw keyword used in the rule
301  *
302  * \param de_ctx Pointer to the Detection Engine Context
303  * \param s Pointer to the Signature to which the current keyword belongs
304  * \param str Should hold an empty string always
305  *
306  * \retval 0 On success
307  */
308 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
309 {
310  if (DetectBufferSetActiveList(s, g_http_raw_uri_buffer_id) < 0)
311  return -1;
313  return -1;
314  return 0;
315 }
316 
317 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
318  const DetectEngineTransforms *transforms, Flow *_f,
319  const uint8_t _flow_flags, void *txv, const int list_id)
320 {
321  SCEnter();
322 
323  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
324  if (buffer->inspect == NULL) {
325  htp_tx_t *tx = (htp_tx_t *)txv;
326  if (unlikely(tx->request_uri == NULL)) {
327  return NULL;
328  }
329  const uint32_t data_len = bstr_len(tx->request_uri);
330  const uint8_t *data = bstr_ptr(tx->request_uri);
331 
332  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
333  InspectionBufferApplyTransforms(buffer, transforms);
334  }
335 
336  return buffer;
337 }
338 
339 #ifdef UNITTESTS /* UNITTESTS */
340 #include "tests/detect-http-uri.c"
341 #endif /* UNITTESTS */
342 
343 /**
344  * @}
345  */
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1475
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:379
DetectHttpUriRegister
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
Definition: detect-http-uri.c:91
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:1253
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:753
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
HtpTxUserData_::request_uri_normalized
bstr * request_uri_normalized
Definition: app-layer-htp.h:232
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
detect-pcre.h
DetectUrilenApplyToContent
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
Definition: detect-urilen.c:294
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1360
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
DetectHttpUriSetup
int DetectHttpUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
this function setups the http_uri modifier keyword used in the rule
Definition: detect-http-uri.c:186
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
app-layer-htp.h
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:1271
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
detect-http-uri.h
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1265
DetectEngineInspectBufferGeneric
int 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:1983
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:225
DETECT_HTTP_URI_RAW
@ DETECT_HTTP_URI_RAW
Definition: detect-engine-register.h:147
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:62
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SigTableElmt_::alias
const char * alias
Definition: detect.h:1268
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1543
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
detect-http-uri.c
HtpTxUserData_
Definition: app-layer-htp.h:214
DETECT_AL_HTTP_RAW_URI
@ DETECT_AL_HTTP_RAW_URI
Definition: detect-engine-register.h:148
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
str
#define str(s)
Definition: suricata-common.h:272
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:1441
Signature_::id
uint32_t id
Definition: detect.h:582
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DETECT_AL_HTTP_URI
@ DETECT_AL_HTTP_URI
Definition: detect-engine-register.h:145
detect-urilen.h
detect-uricontent.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
DetectUrilenValidateContent
bool DetectUrilenValidateContent(const Signature *s, int list, const char **sigerror)
Definition: detect-urilen.c:347
flow.h
flow-var.h
DETECT_HTTP_URI
@ DETECT_HTTP_URI
Definition: detect-engine-register.h:146
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
app-layer.h