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(
64  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map);
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(
75  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map);
76 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
77  const DetectEngineTransforms *transforms,
78  Flow *_f, const uint8_t _flow_flags,
79  void *txv, const int list_id);
80 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
81 
82 static int g_http_raw_uri_buffer_id = 0;
83 static int g_http_uri_buffer_id = 0;
84 
85 /**
86  * \brief Registration function for keywords: http_uri and http.uri
87  */
89 {
90  /* http_uri content modifier */
93  "content modifier to match specifically and only on the HTTP uri-buffer";
94  sigmatch_table[DETECT_HTTP_URI_CM].url = "/rules/http-keywords.html#http-uri-and-http-uri-raw";
96 #ifdef UNITTESTS
97  sigmatch_table[DETECT_HTTP_URI_CM].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_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
112 
114  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
115 
117  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
118 
120  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
121 
123  "http request uri");
124 
126  DetectHttpUriSetupCallback);
127 
129 
130  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
131 
132  /* http_raw_uri content modifier */
133  sigmatch_table[DETECT_HTTP_RAW_URI].name = "http_raw_uri";
134  sigmatch_table[DETECT_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
135  sigmatch_table[DETECT_HTTP_RAW_URI].url = "/rules/http-keywords.html#http_uri-and-http_raw-uri";
136  sigmatch_table[DETECT_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
139 
140  /* http.uri.raw sticky buffer */
141  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
142  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
143  sigmatch_table[DETECT_HTTP_URI_RAW].url = "/rules/http-keywords.html#http-uri-and-http-raw-uri";
144  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
146 
148  HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetRawData);
149 
151  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
152 
153  // no difference between raw and decoded uri for HTTP2
155  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
156 
158  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
159 
161  "raw http uri");
162 
164  DetectHttpRawUriSetupCallback);
165 
167 
168  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
169 }
170 
171 /**
172  * \brief this function setups the http_uri modifier keyword used in the rule
173  *
174  * \param de_ctx Pointer to the Detection Engine Context
175  * \param s Pointer to the Signature to which the current keyword belongs
176  * \param str Should hold an empty string always
177  *
178  * \retval 0 On success
179  * \retval -1 On failure
180  */
181 
183 {
185  de_ctx, s, str, DETECT_HTTP_URI_CM, g_http_uri_buffer_id, ALPROTO_HTTP1);
186 }
187 
188 static void DetectHttpUriSetupCallback(
189  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
190 {
191  SCLogDebug("callback invoked by %u", s->id);
192  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
193 }
194 
195 /**
196  * \brief this function setup the http.uri keyword used in the rule
197  *
198  * \param de_ctx Pointer to the Detection Engine Context
199  * \param s Pointer to the Signature to which the current keyword belongs
200  * \param str Should hold an empty string always
201  *
202  * \retval 0 On success
203  */
204 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
205 {
206  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_uri_buffer_id) < 0)
207  return -1;
209  return -1;
210  return 0;
211 }
212 
213 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
214  const DetectEngineTransforms *transforms, Flow *_f,
215  const uint8_t _flow_flags, void *txv, const int list_id)
216 {
217  SCEnter();
218 
219  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
220  if (!buffer->initialized) {
221  htp_tx_t *tx = (htp_tx_t *)txv;
222  bstr *request_uri_normalized = (bstr *)htp_tx_normalized_uri(tx);
223  if (request_uri_normalized == NULL)
224  return NULL;
225 
226  const uint32_t data_len = (uint32_t)bstr_len(request_uri_normalized);
227  const uint8_t *data = bstr_ptr(request_uri_normalized);
228 
230  det_ctx, list_id, buffer, data, data_len, transforms);
231  }
232 
233  return buffer;
234 }
235 
236 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
237  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
238  const int list_id)
239 {
240  SCEnter();
241 
242  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
243  if (!buffer->initialized) {
244  uint32_t b_len = 0;
245  const uint8_t *b = NULL;
246 
247  if (SCHttp2TxGetUri(txv, &b, &b_len) != 1)
248  return NULL;
249  if (b == NULL || b_len == 0)
250  return NULL;
251 
252  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
253  }
254 
255  return buffer;
256 }
257 
258 /**
259  * \brief Sets up the http_raw_uri modifier keyword.
260  *
261  * \param de_ctx Pointer to the Detection Engine Context.
262  * \param s Pointer to the Signature to which the current keyword belongs.
263  * \param arg Should hold an empty string always.
264  *
265  * \retval 0 On success.
266  * \retval -1 On failure.
267  */
268 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
269 {
271  de_ctx, s, arg, DETECT_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
272 }
273 
274 static void DetectHttpRawUriSetupCallback(
275  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
276 {
277  SCLogDebug("callback invoked by %u", s->id);
278  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
279 }
280 
281 /**
282  * \brief this function setup the http.uri.raw keyword used in the rule
283  *
284  * \param de_ctx Pointer to the Detection Engine Context
285  * \param s Pointer to the Signature to which the current keyword belongs
286  * \param str Should hold an empty string always
287  *
288  * \retval 0 On success
289  */
290 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
291 {
292  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_raw_uri_buffer_id) < 0)
293  return -1;
295  return -1;
296  return 0;
297 }
298 
299 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
300  const DetectEngineTransforms *transforms, Flow *_f,
301  const uint8_t _flow_flags, void *txv, const int list_id)
302 {
303  SCEnter();
304 
305  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
306  if (!buffer->initialized) {
307  htp_tx_t *tx = (htp_tx_t *)txv;
308  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
309  return NULL;
310  }
311  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_uri(tx));
312  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
313 
315  det_ctx, list_id, buffer, data, data_len, transforms);
316  }
317 
318  return buffer;
319 }
320 
321 #ifdef UNITTESTS /* UNITTESTS */
322 #include "tests/detect-http-uri.c"
323 #endif /* UNITTESTS */
324 
325 /**
326  * @}
327  */
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1675
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
Definition: detect-engine.c:1463
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
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:2049
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1673
SigTableElmt_::name
const char * name
Definition: detect.h:1458
InspectionBuffer::initialized
bool initialized
Definition: detect-engine-inspect-buffer.h:38
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:390
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
DetectHttpUriRegister
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
Definition: detect-http-uri.c:88
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
DETECT_HTTP_RAW_URI
@ DETECT_HTTP_RAW_URI
Definition: detect-engine-register.h:178
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1481
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:448
DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_CM
Definition: detect-engine-register.h:175
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2235
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
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
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
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:182
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:19
DetectEngineThreadCtx_
Definition: detect.h:1245
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
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:1577
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1456
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:152
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
DETECT_HTTP_URI_RAW
@ DETECT_HTTP_URI_RAW
Definition: detect-engine-register.h:177
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:1459
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:713
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
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-inspect-buffer.c:197
detect-urilen.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1650
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:273
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
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:1447
app-layer.h