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-buffer.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 "stream-tcp.h"
59 
60 #ifdef UNITTESTS
61 static void DetectHttpUriRegisterTests(void);
62 #endif
63 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx, Signature *s);
64 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
65  const DetectEngineTransforms *transforms,
66  Flow *_f, const uint8_t _flow_flags,
67  void *txv, const int list_id);
68 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
69  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
70  const int list_id);
71 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
72 static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
73 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx, Signature *s);
74 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
75  const DetectEngineTransforms *transforms,
76  Flow *_f, const uint8_t _flow_flags,
77  void *txv, const int list_id);
78 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
79 
80 static int g_http_raw_uri_buffer_id = 0;
81 static int g_http_uri_buffer_id = 0;
82 
83 /**
84  * \brief Registration function for keywords: http_uri and http.uri
85  */
87 {
88  /* http_uri content modifier */
91  "content modifier to match specifically and only on the HTTP uri-buffer";
92  sigmatch_table[DETECT_HTTP_URI_CM].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
94 #ifdef UNITTESTS
95  sigmatch_table[DETECT_HTTP_URI_CM].RegisterTests = DetectHttpUriRegisterTests;
96 #endif
99 
100  /* http.uri sticky buffer */
101  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
102  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
103  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
104  sigmatch_table[DETECT_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
105  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
107 
109  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
110 
112  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
113 
115  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
116 
118  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
119 
121  "http request uri");
122 
124  DetectHttpUriSetupCallback);
125 
127 
128  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
129 
130  /* http_raw_uri content modifier */
131  sigmatch_table[DETECT_HTTP_RAW_URI].name = "http_raw_uri";
132  sigmatch_table[DETECT_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
133  sigmatch_table[DETECT_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
134  sigmatch_table[DETECT_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
137 
138  /* http.uri.raw sticky buffer */
139  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
140  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
141  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
142  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
144 
146  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetRawData);
147 
149  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
150 
151  // no difference between raw and decoded uri for HTTP2
153  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
154 
156  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
157 
159  "raw http uri");
160 
162  DetectHttpRawUriSetupCallback);
163 
165 
166  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
167 }
168 
169 /**
170  * \brief this function setups the http_uri modifier keyword used in the rule
171  *
172  * \param de_ctx Pointer to the Detection Engine Context
173  * \param s Pointer to the Signature to which the current keyword belongs
174  * \param str Should hold an empty string always
175  *
176  * \retval 0 On success
177  * \retval -1 On failure
178  */
179 
181 {
183  de_ctx, s, str, DETECT_HTTP_URI_CM, g_http_uri_buffer_id, ALPROTO_HTTP1);
184 }
185 
186 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
187  Signature *s)
188 {
189  SCLogDebug("callback invoked by %u", s->id);
190  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
191 }
192 
193 /**
194  * \brief this function setup the http.uri keyword used in the rule
195  *
196  * \param de_ctx Pointer to the Detection Engine Context
197  * \param s Pointer to the Signature to which the current keyword belongs
198  * \param str Should hold an empty string always
199  *
200  * \retval 0 On success
201  */
202 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
203 {
204  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_uri_buffer_id) < 0)
205  return -1;
207  return -1;
208  return 0;
209 }
210 
211 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
212  const DetectEngineTransforms *transforms, Flow *_f,
213  const uint8_t _flow_flags, void *txv, const int list_id)
214 {
215  SCEnter();
216 
217  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
218  if (!buffer->initialized) {
219  htp_tx_t *tx = (htp_tx_t *)txv;
220  bstr *request_uri_normalized = (bstr *)htp_tx_normalized_uri(tx);
221  if (request_uri_normalized == NULL)
222  return NULL;
223 
224  const uint32_t data_len = (uint32_t)bstr_len(request_uri_normalized);
225  const uint8_t *data = bstr_ptr(request_uri_normalized);
226 
228  det_ctx, list_id, buffer, data, data_len, transforms);
229  }
230 
231  return buffer;
232 }
233 
234 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
235  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
236  const int list_id)
237 {
238  SCEnter();
239 
240  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
241  if (!buffer->initialized) {
242  uint32_t b_len = 0;
243  const uint8_t *b = NULL;
244 
245  if (SCHttp2TxGetUri(txv, &b, &b_len) != 1)
246  return NULL;
247  if (b == NULL || b_len == 0)
248  return NULL;
249 
250  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
251  }
252 
253  return buffer;
254 }
255 
256 /**
257  * \brief Sets up the http_raw_uri modifier keyword.
258  *
259  * \param de_ctx Pointer to the Detection Engine Context.
260  * \param s Pointer to the Signature to which the current keyword belongs.
261  * \param arg Should hold an empty string always.
262  *
263  * \retval 0 On success.
264  * \retval -1 On failure.
265  */
266 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
267 {
269  de_ctx, s, arg, DETECT_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
270 }
271 
272 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
273  Signature *s)
274 {
275  SCLogDebug("callback invoked by %u", s->id);
276  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
277 }
278 
279 /**
280  * \brief this function setup the http.uri.raw keyword used in the rule
281  *
282  * \param de_ctx Pointer to the Detection Engine Context
283  * \param s Pointer to the Signature to which the current keyword belongs
284  * \param str Should hold an empty string always
285  *
286  * \retval 0 On success
287  */
288 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
289 {
290  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_raw_uri_buffer_id) < 0)
291  return -1;
293  return -1;
294  return 0;
295 }
296 
297 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
298  const DetectEngineTransforms *transforms, Flow *_f,
299  const uint8_t _flow_flags, void *txv, const int list_id)
300 {
301  SCEnter();
302 
303  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
304  if (!buffer->initialized) {
305  htp_tx_t *tx = (htp_tx_t *)txv;
306  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
307  return NULL;
308  }
309  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_uri(tx));
310  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
311 
313  det_ctx, list_id, buffer, data, data_len, transforms);
314  }
315 
316  return buffer;
317 }
318 
319 #ifdef UNITTESTS /* UNITTESTS */
320 #include "tests/detect-http-uri.c"
321 #endif /* UNITTESTS */
322 
323 /**
324  * @}
325  */
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1645
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
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:2103
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1643
SigTableElmt_::name
const char * name
Definition: detect.h:1428
InspectionBuffer::initialized
bool initialized
Definition: detect.h:384
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:1570
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:415
DetectHttpUriRegister
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
Definition: detect-http-uri.c:86
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
InspectionBuffer
Definition: detect.h:380
DETECT_HTTP_RAW_URI
@ DETECT_HTTP_RAW_URI
Definition: detect-engine-register.h:177
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:1351
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1369
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_CM
Definition: detect-engine-register.h:174
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
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:1429
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
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:180
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
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:1547
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1426
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:222
DETECT_HTTP_URI_RAW
@ DETECT_HTTP_URI_RAW
Definition: detect-engine-register.h:176
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
DetectUrilenValidateContent
bool DetectUrilenValidateContent(const Signature *s, const char **sigerror, const DetectBufferType *dbt)
Definition: detect-urilen.c:217
SigTableElmt_::alias
const char * alias
Definition: detect.h:1429
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
detect-engine-buffer.h
detect-http-uri.c
str
#define str(s)
Definition: suricata-common.h:308
Signature_::id
uint32_t id
Definition: detect.h:715
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:75
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
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:1254
flow.h
flow-var.h
DETECT_HTTP_URI
@ DETECT_HTTP_URI
Definition: detect-engine-register.h:175
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1420
app-layer.h