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 "detect-http-uri.h"
57 #include "stream-tcp.h"
58 
59 #ifdef UNITTESTS
60 static void DetectHttpUriRegisterTests(void);
61 #endif
62 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
63  Signature *s);
64 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror);
65 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
66  const DetectEngineTransforms *transforms,
67  Flow *_f, const uint8_t _flow_flags,
68  void *txv, const int list_id);
69 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
70  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
71  const int list_id);
72 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
73 static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
74 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
75  Signature *s);
76 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **);
77 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
78  const DetectEngineTransforms *transforms,
79  Flow *_f, const uint8_t _flow_flags,
80  void *txv, const int list_id);
81 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
82 
83 static int g_http_raw_uri_buffer_id = 0;
84 static int g_http_uri_buffer_id = 0;
85 
86 /**
87  * \brief Registration function for keywords: http_uri and http.uri
88  */
90 {
91  /* http_uri content modifier */
94  "content modifier to match specifically and only on the HTTP uri-buffer";
95  sigmatch_table[DETECT_HTTP_URI_CM].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
97 #ifdef UNITTESTS
98  sigmatch_table[DETECT_HTTP_URI_CM].RegisterTests = DetectHttpUriRegisterTests;
99 #endif
102 
103  /* http.uri sticky buffer */
104  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
105  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
106  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
107  sigmatch_table[DETECT_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
108  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
110 
113 
116 
118  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
119 
121  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
122 
124  "http request uri");
125 
127  DetectHttpUriSetupCallback);
128 
130  DetectHttpUriValidateCallback);
131 
132  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
133 
134  /* http_raw_uri content modifier */
135  sigmatch_table[DETECT_HTTP_RAW_URI].name = "http_raw_uri";
136  sigmatch_table[DETECT_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
137  sigmatch_table[DETECT_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
138  sigmatch_table[DETECT_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
141 
142  /* http.uri.raw sticky buffer */
143  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
144  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
145  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
146  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
148 
151 
154 
155  // no difference between raw and decoded uri for HTTP2
157  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
158 
160  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
161 
163  "raw http uri");
164 
166  DetectHttpRawUriSetupCallback);
167 
169  DetectHttpRawUriValidateCallback);
170 
171  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
172 }
173 
174 /**
175  * \brief this function setups the http_uri modifier keyword used in the rule
176  *
177  * \param de_ctx Pointer to the Detection Engine Context
178  * \param s Pointer to the Signature to which the current keyword belongs
179  * \param str Should hold an empty string always
180  *
181  * \retval 0 On success
182  * \retval -1 On failure
183  */
184 
186 {
188  de_ctx, s, str, DETECT_HTTP_URI_CM, g_http_uri_buffer_id, ALPROTO_HTTP1);
189 }
190 
191 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror)
192 {
193  return DetectUrilenValidateContent(s, g_http_uri_buffer_id, sigerror);
194 }
195 
196 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
197  Signature *s)
198 {
199  SCLogDebug("callback invoked by %u", s->id);
200  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
201 }
202 
203 /**
204  * \brief this function setup the http.uri keyword used in the rule
205  *
206  * \param de_ctx Pointer to the Detection Engine Context
207  * \param s Pointer to the Signature to which the current keyword belongs
208  * \param str Should hold an empty string always
209  *
210  * \retval 0 On success
211  */
212 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
213 {
214  if (DetectBufferSetActiveList(de_ctx, s, g_http_uri_buffer_id) < 0)
215  return -1;
217  return -1;
218  return 0;
219 }
220 
221 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
222  const DetectEngineTransforms *transforms, Flow *_f,
223  const uint8_t _flow_flags, void *txv, const int list_id)
224 {
225  SCEnter();
226 
227  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
228  if (!buffer->initialized) {
229  htp_tx_t *tx = (htp_tx_t *)txv;
230  HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
231 
232  if (tx_ud == NULL || tx_ud->request_uri_normalized == NULL) {
233  SCLogDebug("no tx_id or uri");
234  return NULL;
235  }
236 
237  const uint32_t data_len = bstr_len(tx_ud->request_uri_normalized);
238  const uint8_t *data = bstr_ptr(tx_ud->request_uri_normalized);
239 
241  det_ctx, list_id, buffer, data, data_len, transforms);
242  }
243 
244  return buffer;
245 }
246 
247 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
248  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
249  const int list_id)
250 {
251  SCEnter();
252 
253  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
254  if (!buffer->initialized) {
255  uint32_t b_len = 0;
256  const uint8_t *b = NULL;
257 
258  if (rs_http2_tx_get_uri(txv, &b, &b_len) != 1)
259  return NULL;
260  if (b == NULL || b_len == 0)
261  return NULL;
262 
263  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
264  }
265 
266  return buffer;
267 }
268 
269 /**
270  * \brief Sets up the http_raw_uri modifier keyword.
271  *
272  * \param de_ctx Pointer to the Detection Engine Context.
273  * \param s Pointer to the Signature to which the current keyword belongs.
274  * \param arg Should hold an empty string always.
275  *
276  * \retval 0 On success.
277  * \retval -1 On failure.
278  */
279 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
280 {
282  de_ctx, s, arg, DETECT_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
283 }
284 
285 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **sigerror)
286 {
287  return DetectUrilenValidateContent(s, g_http_raw_uri_buffer_id, sigerror);
288 }
289 
290 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
291  Signature *s)
292 {
293  SCLogDebug("callback invoked by %u", s->id);
294  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
295 }
296 
297 /**
298  * \brief this function setup the http.uri.raw keyword used in the rule
299  *
300  * \param de_ctx Pointer to the Detection Engine Context
301  * \param s Pointer to the Signature to which the current keyword belongs
302  * \param str Should hold an empty string always
303  *
304  * \retval 0 On success
305  */
306 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
307 {
308  if (DetectBufferSetActiveList(de_ctx, s, g_http_raw_uri_buffer_id) < 0)
309  return -1;
311  return -1;
312  return 0;
313 }
314 
315 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
316  const DetectEngineTransforms *transforms, Flow *_f,
317  const uint8_t _flow_flags, void *txv, const int list_id)
318 {
319  SCEnter();
320 
321  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
322  if (!buffer->initialized) {
323  htp_tx_t *tx = (htp_tx_t *)txv;
324  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
325  return NULL;
326  }
327  const uint32_t data_len = bstr_len(htp_tx_request_uri(tx));
328  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
329 
331  det_ctx, list_id, buffer, data, data_len, transforms);
332  }
333 
334  return buffer;
335 }
336 
337 #ifdef UNITTESTS /* UNITTESTS */
338 #include "tests/detect-http-uri.c"
339 #endif /* UNITTESTS */
340 
341 /**
342  * @}
343  */
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1527
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2180
htp_tx_request_uri
#define htp_tx_request_uri(tx)
Definition: app-layer-htp-libhtp.h:172
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1525
SigTableElmt_::name
const char * name
Definition: detect.h:1319
InspectionBuffer::initialized
bool initialized
Definition: detect.h:379
InspectionBufferSetupAndApplyTransforms
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
Definition: detect-engine.c:1647
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:410
DetectHttpUriRegister
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
Definition: detect-http-uri.c:89
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:1362
InspectionBuffer
Definition: detect.h:375
DETECT_HTTP_RAW_URI
@ DETECT_HTTP_RAW_URI
Definition: detect-engine-register.h:178
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:1293
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
HtpTxUserData_::request_uri_normalized
bstr * request_uri_normalized
Definition: app-layer-htp.h:228
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:1304
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:1506
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1099
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:185
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
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:1311
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
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:1317
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:220
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:1320
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
detect-http-uri.c
HtpTxUserData_
Definition: app-layer-htp.h:212
str
#define str(s)
Definition: suricata-common.h:300
Signature_::id
uint32_t id
Definition: detect.h:649
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
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:245
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1196
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:1311
app-layer.h