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 "decode.h"
35 #include "detect.h"
36 
37 #include "detect-parse.h"
38 #include "detect-engine.h"
39 #include "detect-engine-mpm.h"
41 #include "detect-content.h"
42 #include "detect-pcre.h"
43 #include "detect-urilen.h"
44 
45 #include "flow.h"
46 #include "flow-var.h"
47 
48 #include "util-debug.h"
49 #include "util-unittest.h"
50 #include "util-spm.h"
51 #include "util-print.h"
52 
53 #include "app-layer.h"
54 
55 #include "app-layer-htp.h"
56 #include "app-layer-htp-libhtp.h"
57 #include "detect-http-uri.h"
58 #include "stream-tcp.h"
59 
60 #ifdef UNITTESTS
61 static void DetectHttpUriRegisterTests(void);
62 #endif
63 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
64  Signature *s);
65 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror);
66 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
67  const DetectEngineTransforms *transforms,
68  Flow *_f, const uint8_t _flow_flags,
69  void *txv, const int list_id);
70 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
71  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
72  const int list_id);
73 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
74 static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
75 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
76  Signature *s);
77 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **);
78 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
79  const DetectEngineTransforms *transforms,
80  Flow *_f, const uint8_t _flow_flags,
81  void *txv, const int list_id);
82 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
83 
84 static int g_http_raw_uri_buffer_id = 0;
85 static int g_http_uri_buffer_id = 0;
86 
87 /**
88  * \brief Registration function for keywords: http_uri and http.uri
89  */
91 {
92  /* http_uri content modifier */
95  "content modifier to match specifically and only on the HTTP uri-buffer";
96  sigmatch_table[DETECT_HTTP_URI_CM].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
98 #ifdef UNITTESTS
99  sigmatch_table[DETECT_HTTP_URI_CM].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 
114 
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_HTTP_RAW_URI].name = "http_raw_uri";
137  sigmatch_table[DETECT_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
138  sigmatch_table[DETECT_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
139  sigmatch_table[DETECT_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 
152 
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_HTTP_URI_CM, 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(de_ctx, 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->initialized) {
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->initialized) {
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_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(de_ctx, 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->initialized) {
325  htp_tx_t *tx = (htp_tx_t *)txv;
326  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
327  return NULL;
328  }
329  const uint32_t data_len = bstr_len(htp_tx_request_uri(tx));
330  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
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:1311
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1764
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1516
SigTableElmt_::desc
const char * desc
Definition: detect.h:1310
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:154
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:2155
htp_tx_request_uri
#define htp_tx_request_uri(tx)
Definition: app-layer-htp-libhtp.h:112
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1514
SigTableElmt_::name
const char * name
Definition: detect.h:1308
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:408
DetectHttpUriRegister
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
Definition: detect-http-uri.c:90
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
HTP_REQUEST_PROGRESS_LINE
#define HTP_REQUEST_PROGRESS_LINE
Definition: app-layer-htp-libhtp.h:88
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1357
InspectionBuffer
Definition: detect.h:373
DETECT_HTTP_RAW_URI
@ DETECT_HTTP_RAW_URI
Definition: detect-engine-register.h:178
threads.h
Flow_
Flow data structure.
Definition: flow.h:357
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:1288
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1302
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
HtpTxUserData_::request_uri_normalized
bstr * request_uri_normalized
Definition: app-layer-htp.h:222
DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_CM
Definition: detect-engine-register.h:175
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1293
detect-pcre.h
DetectUrilenApplyToContent
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
Definition: detect-urilen.c:149
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1501
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1094
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:267
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:1306
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1098
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
detect-http-uri.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1306
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
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:221
DETECT_HTTP_URI_RAW
@ DETECT_HTTP_URI_RAW
Definition: detect-engine-register.h:177
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
SigTableElmt_::alias
const char * alias
Definition: detect.h:1309
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1712
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
detect-http-uri.c
HtpTxUserData_
Definition: app-layer-htp.h:206
str
#define str(s)
Definition: suricata-common.h:291
app-layer-htp-libhtp.h
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:1596
Signature_::id
uint32_t id
Definition: detect.h:638
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1492
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
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1191
DetectUrilenValidateContent
bool DetectUrilenValidateContent(const Signature *s, int list, const char **sigerror)
Definition: detect-urilen.c:217
flow.h
flow-var.h
DETECT_HTTP_URI
@ DETECT_HTTP_URI
Definition: detect-engine-register.h:176
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1300
app-layer.h