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, Signature *s);
63 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
64  const DetectEngineTransforms *transforms,
65  Flow *_f, const uint8_t _flow_flags,
66  void *txv, const int list_id);
67 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
69  const int list_id);
70 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
71 static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
72 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx, Signature *s);
73 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
74  const DetectEngineTransforms *transforms,
75  Flow *_f, const uint8_t _flow_flags,
76  void *txv, const int list_id);
77 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
78 
79 static int g_http_raw_uri_buffer_id = 0;
80 static int g_http_uri_buffer_id = 0;
81 
82 /**
83  * \brief Registration function for keywords: http_uri and http.uri
84  */
86 {
87  /* http_uri content modifier */
90  "content modifier to match specifically and only on the HTTP uri-buffer";
91  sigmatch_table[DETECT_HTTP_URI_CM].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
93 #ifdef UNITTESTS
94  sigmatch_table[DETECT_HTTP_URI_CM].RegisterTests = DetectHttpUriRegisterTests;
95 #endif
98 
99  /* http.uri sticky buffer */
100  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
101  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
102  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
103  sigmatch_table[DETECT_HTTP_URI].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
104  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
106 
108  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
109 
111  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
112 
114  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
115 
117  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
118 
120  "http request uri");
121 
123  DetectHttpUriSetupCallback);
124 
126 
127  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
128 
129  /* http_raw_uri content modifier */
130  sigmatch_table[DETECT_HTTP_RAW_URI].name = "http_raw_uri";
131  sigmatch_table[DETECT_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
132  sigmatch_table[DETECT_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
133  sigmatch_table[DETECT_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
136 
137  /* http.uri.raw sticky buffer */
138  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
139  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
140  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
141  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
143 
145  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetRawData);
146 
148  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
149 
150  // no difference between raw and decoded uri for HTTP2
152  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
153 
155  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
156 
158  "raw http uri");
159 
161  DetectHttpRawUriSetupCallback);
162 
164 
165  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
166 }
167 
168 /**
169  * \brief this function setups the http_uri modifier keyword used in the rule
170  *
171  * \param de_ctx Pointer to the Detection Engine Context
172  * \param s Pointer to the Signature to which the current keyword belongs
173  * \param str Should hold an empty string always
174  *
175  * \retval 0 On success
176  * \retval -1 On failure
177  */
178 
180 {
182  de_ctx, s, str, DETECT_HTTP_URI_CM, g_http_uri_buffer_id, ALPROTO_HTTP1);
183 }
184 
185 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
186  Signature *s)
187 {
188  SCLogDebug("callback invoked by %u", s->id);
189  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
190 }
191 
192 /**
193  * \brief this function setup the http.uri keyword used in the rule
194  *
195  * \param de_ctx Pointer to the Detection Engine Context
196  * \param s Pointer to the Signature to which the current keyword belongs
197  * \param str Should hold an empty string always
198  *
199  * \retval 0 On success
200  */
201 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
202 {
203  if (DetectBufferSetActiveList(de_ctx, s, g_http_uri_buffer_id) < 0)
204  return -1;
206  return -1;
207  return 0;
208 }
209 
210 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
211  const DetectEngineTransforms *transforms, Flow *_f,
212  const uint8_t _flow_flags, void *txv, const int list_id)
213 {
214  SCEnter();
215 
216  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
217  if (!buffer->initialized) {
218  htp_tx_t *tx = (htp_tx_t *)txv;
219  bstr *request_uri_normalized = (bstr *)htp_tx_normalized_uri(tx);
220  if (request_uri_normalized == NULL)
221  return NULL;
222 
223  const uint32_t data_len = bstr_len(request_uri_normalized);
224  const uint8_t *data = bstr_ptr(request_uri_normalized);
225 
227  det_ctx, list_id, buffer, data, data_len, transforms);
228  }
229 
230  return buffer;
231 }
232 
233 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
234  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
235  const int list_id)
236 {
237  SCEnter();
238 
239  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
240  if (!buffer->initialized) {
241  uint32_t b_len = 0;
242  const uint8_t *b = NULL;
243 
244  if (rs_http2_tx_get_uri(txv, &b, &b_len) != 1)
245  return NULL;
246  if (b == NULL || b_len == 0)
247  return NULL;
248 
249  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
250  }
251 
252  return buffer;
253 }
254 
255 /**
256  * \brief Sets up the http_raw_uri modifier keyword.
257  *
258  * \param de_ctx Pointer to the Detection Engine Context.
259  * \param s Pointer to the Signature to which the current keyword belongs.
260  * \param arg Should hold an empty string always.
261  *
262  * \retval 0 On success.
263  * \retval -1 On failure.
264  */
265 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
266 {
268  de_ctx, s, arg, DETECT_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
269 }
270 
271 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
272  Signature *s)
273 {
274  SCLogDebug("callback invoked by %u", s->id);
275  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
276 }
277 
278 /**
279  * \brief this function setup the http.uri.raw keyword used in the rule
280  *
281  * \param de_ctx Pointer to the Detection Engine Context
282  * \param s Pointer to the Signature to which the current keyword belongs
283  * \param str Should hold an empty string always
284  *
285  * \retval 0 On success
286  */
287 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
288 {
289  if (DetectBufferSetActiveList(de_ctx, s, g_http_raw_uri_buffer_id) < 0)
290  return -1;
292  return -1;
293  return 0;
294 }
295 
296 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
297  const DetectEngineTransforms *transforms, Flow *_f,
298  const uint8_t _flow_flags, void *txv, const int list_id)
299 {
300  SCEnter();
301 
302  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
303  if (!buffer->initialized) {
304  htp_tx_t *tx = (htp_tx_t *)txv;
305  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
306  return NULL;
307  }
308  const uint32_t data_len = bstr_len(htp_tx_request_uri(tx));
309  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
310 
312  det_ctx, list_id, buffer, data, data_len, transforms);
313  }
314 
315  return buffer;
316 }
317 
318 #ifdef UNITTESTS /* UNITTESTS */
319 #include "tests/detect-http-uri.c"
320 #endif /* UNITTESTS */
321 
322 /**
323  * @}
324  */
SigTableElmt_::url
const char * url
Definition: detect.h:1405
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2218
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1616
SigTableElmt_::desc
const char * desc
Definition: detect.h:1404
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:2252
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1614
SigTableElmt_::name
const char * name
Definition: detect.h:1402
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:1719
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:85
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1422
InspectionBuffer
Definition: detect.h:380
DETECT_HTTP_RAW_URI
@ DETECT_HTTP_RAW_URI
Definition: detect-engine-register.h:176
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:1396
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:920
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1369
DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_CM
Definition: detect-engine-register.h:173
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1387
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:1578
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:179
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:1197
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:1400
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:175
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:1403
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
detect-http-uri.c
str
#define str(s)
Definition: suricata-common.h:300
Signature_::id
uint32_t id
Definition: detect.h:714
detect-parse.h
Signature_
Signature container.
Definition: detect.h:669
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:75
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1592
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:174
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1394
app-layer.h