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 */
93  sigmatch_table[DETECT_AL_HTTP_URI].desc = "content modifier to match specifically and only on the HTTP uri-buffer";
94  sigmatch_table[DETECT_AL_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
96 #ifdef UNITTESTS
97  sigmatch_table[DETECT_AL_HTTP_URI].RegisterTests = DetectHttpUriRegisterTests;
98 #endif
101 
102  /* http.uri sticky buffer */
103  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
104  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
105  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
106  sigmatch_table[DETECT_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
107  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
109 
111  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData);
112 
114  GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
115 
117  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
118 
120  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
121 
123  "http request uri");
124 
126  DetectHttpUriSetupCallback);
127 
129  DetectHttpUriValidateCallback);
130 
131  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
132 
133  /* http_raw_uri content modifier */
134  sigmatch_table[DETECT_AL_HTTP_RAW_URI].name = "http_raw_uri";
135  sigmatch_table[DETECT_AL_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
136  sigmatch_table[DETECT_AL_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
137  sigmatch_table[DETECT_AL_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
140 
141  /* http.uri.raw sticky buffer */
142  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
143  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
144  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
145  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
147 
149  HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetRawData);
150 
152  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_LINE);
153 
154  // no difference between raw and decoded uri for HTTP2
156  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
157 
159  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
160 
162  "raw http uri");
163 
165  DetectHttpRawUriSetupCallback);
166 
168  DetectHttpRawUriValidateCallback);
169 
170  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
171 }
172 
173 /**
174  * \brief this function setups the http_uri modifier keyword used in the rule
175  *
176  * \param de_ctx Pointer to the Detection Engine Context
177  * \param s Pointer to the Signature to which the current keyword belongs
178  * \param str Should hold an empty string always
179  *
180  * \retval 0 On success
181  * \retval -1 On failure
182  */
183 
185 {
187  de_ctx, s, str, DETECT_AL_HTTP_URI, g_http_uri_buffer_id, ALPROTO_HTTP1);
188 }
189 
190 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror)
191 {
192  return DetectUrilenValidateContent(s, g_http_uri_buffer_id, sigerror);
193 }
194 
195 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
196  Signature *s)
197 {
198  SCLogDebug("callback invoked by %u", s->id);
199  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
200 }
201 
202 /**
203  * \brief this function setup the http.uri keyword used in the rule
204  *
205  * \param de_ctx Pointer to the Detection Engine Context
206  * \param s Pointer to the Signature to which the current keyword belongs
207  * \param str Should hold an empty string always
208  *
209  * \retval 0 On success
210  */
211 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
212 {
213  if (DetectBufferSetActiveList(s, g_http_uri_buffer_id) < 0)
214  return -1;
216  return -1;
217  return 0;
218 }
219 
220 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
221  const DetectEngineTransforms *transforms, Flow *_f,
222  const uint8_t _flow_flags, void *txv, const int list_id)
223 {
224  SCEnter();
225 
226  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
227  if (!buffer->initialized) {
228  htp_tx_t *tx = (htp_tx_t *)txv;
229  HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
230 
231  if (tx_ud == NULL || tx_ud->request_uri_normalized == NULL) {
232  SCLogDebug("no tx_id or uri");
233  return NULL;
234  }
235 
236  const uint32_t data_len = bstr_len(tx_ud->request_uri_normalized);
237  const uint8_t *data = bstr_ptr(tx_ud->request_uri_normalized);
238 
239  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
240  InspectionBufferApplyTransforms(buffer, transforms);
241  }
242 
243  return buffer;
244 }
245 
246 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
247  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
248  const int list_id)
249 {
250  SCEnter();
251 
252  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
253  if (!buffer->initialized) {
254  uint32_t b_len = 0;
255  const uint8_t *b = NULL;
256 
257  if (rs_http2_tx_get_uri(txv, &b, &b_len) != 1)
258  return NULL;
259  if (b == NULL || b_len == 0)
260  return NULL;
261 
262  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
263  InspectionBufferApplyTransforms(buffer, 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_AL_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(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(tx->request_uri == NULL)) {
325  return NULL;
326  }
327  const uint32_t data_len = bstr_len(tx->request_uri);
328  const uint8_t *data = bstr_ptr(tx->request_uri);
329 
330  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
331  InspectionBufferApplyTransforms(buffer, 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:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
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:1991
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1445
SigTableElmt_::name
const char * name
Definition: detect.h:1238
InspectionBuffer::initialized
bool initialized
Definition: detect.h:341
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:372
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
InspectionBuffer
Definition: detect.h:337
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:1255
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:749
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
HtpTxUserData_::request_uri_normalized
bstr * request_uri_normalized
Definition: app-layer-htp.h:225
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
detect-pcre.h
DetectUrilenApplyToContent
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
Definition: detect-urilen.c:148
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1364
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
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:184
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
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:1273
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
detect-http-uri.h
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1236
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:149
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:224
DETECT_HTTP_URI_RAW
@ DETECT_HTTP_URI_RAW
Definition: detect-engine-register.h:152
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
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:1239
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1550
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
util-spm.h
detect-http-uri.c
HtpTxUserData_
Definition: app-layer-htp.h:207
DETECT_AL_HTTP_RAW_URI
@ DETECT_AL_HTTP_RAW_URI
Definition: detect-engine-register.h:153
str
#define str(s)
Definition: suricata-common.h:280
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:1446
Signature_::id
uint32_t id
Definition: detect.h:574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DETECT_AL_HTTP_URI
@ DETECT_AL_HTTP_URI
Definition: detect-engine-register.h:150
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1176
DetectUrilenValidateContent
bool DetectUrilenValidateContent(const Signature *s, int list, const char **sigerror)
Definition: detect-urilen.c:211
flow.h
flow-var.h
DETECT_HTTP_URI
@ DETECT_HTTP_URI
Definition: detect-engine-register.h:151
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1230
app-layer.h