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  // no doc url for this obsolete keyword, see http.uri
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";
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  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric, GetData2);
118 
120  GetData2, ALPROTO_HTTP2, HTTP2TxTypeStream, HTTP2ProgHeaders);
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  // no doc url for this obsolete keyword, see http.uri.raw
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-raw";
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  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric, GetData2);
156 
158  PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2TxTypeStream,
159  HTTP2ProgHeaders);
160 
162  "raw http uri");
163 
165  DetectHttpRawUriSetupCallback);
166 
168 
169  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
170 }
171 
172 /**
173  * \brief this function setups the http_uri modifier keyword used in the rule
174  *
175  * \param de_ctx Pointer to the Detection Engine Context
176  * \param s Pointer to the Signature to which the current keyword belongs
177  * \param str Should hold an empty string always
178  *
179  * \retval 0 On success
180  * \retval -1 On failure
181  */
182 
184 {
186  de_ctx, s, str, DETECT_HTTP_URI_CM, g_http_uri_buffer_id, ALPROTO_HTTP1);
187 }
188 
189 static void DetectHttpUriSetupCallback(
190  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
191 {
192  SCLogDebug("callback invoked by %u", s->id);
193  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
194 }
195 
196 /**
197  * \brief this function setup the http.uri keyword used in the rule
198  *
199  * \param de_ctx Pointer to the Detection Engine Context
200  * \param s Pointer to the Signature to which the current keyword belongs
201  * \param str Should hold an empty string always
202  *
203  * \retval 0 On success
204  */
205 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
206 {
207  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_uri_buffer_id) < 0)
208  return -1;
210  return -1;
211  return 0;
212 }
213 
214 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
215  const DetectEngineTransforms *transforms, Flow *_f,
216  const uint8_t _flow_flags, void *txv, const int list_id)
217 {
218  SCEnter();
219 
220  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
221  if (!buffer->initialized) {
222  htp_tx_t *tx = (htp_tx_t *)txv;
223  bstr *request_uri_normalized = (bstr *)htp_tx_normalized_uri(tx);
224  if (request_uri_normalized == NULL)
225  return NULL;
226 
227  const uint32_t data_len = (uint32_t)bstr_len(request_uri_normalized);
228  const uint8_t *data = bstr_ptr(request_uri_normalized);
229 
231  det_ctx, list_id, buffer, data, data_len, transforms);
232  }
233 
234  return buffer;
235 }
236 
237 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
238  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
239  const int list_id)
240 {
241  SCEnter();
242 
243  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
244  if (!buffer->initialized) {
245  uint32_t b_len = 0;
246  const uint8_t *b = NULL;
247 
248  if (SCHttp2TxGetUri(txv, &b, &b_len) != 1)
249  return NULL;
250  if (b == NULL || b_len == 0)
251  return NULL;
252 
253  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
254  }
255 
256  return buffer;
257 }
258 
259 /**
260  * \brief Sets up the http_raw_uri modifier keyword.
261  *
262  * \param de_ctx Pointer to the Detection Engine Context.
263  * \param s Pointer to the Signature to which the current keyword belongs.
264  * \param arg Should hold an empty string always.
265  *
266  * \retval 0 On success.
267  * \retval -1 On failure.
268  */
269 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
270 {
272  de_ctx, s, arg, DETECT_HTTP_RAW_URI, g_http_raw_uri_buffer_id, ALPROTO_HTTP1);
273 }
274 
275 static void DetectHttpRawUriSetupCallback(
276  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
277 {
278  SCLogDebug("callback invoked by %u", s->id);
279  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
280 }
281 
282 /**
283  * \brief this function setup the http.uri.raw keyword used in the rule
284  *
285  * \param de_ctx Pointer to the Detection Engine Context
286  * \param s Pointer to the Signature to which the current keyword belongs
287  * \param str Should hold an empty string always
288  *
289  * \retval 0 On success
290  */
291 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
292 {
293  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_raw_uri_buffer_id) < 0)
294  return -1;
296  return -1;
297  return 0;
298 }
299 
300 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
301  const DetectEngineTransforms *transforms, Flow *_f,
302  const uint8_t _flow_flags, void *txv, const int list_id)
303 {
304  SCEnter();
305 
306  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
307  if (!buffer->initialized) {
308  htp_tx_t *tx = (htp_tx_t *)txv;
309  if (unlikely(htp_tx_request_uri(tx) == NULL)) {
310  return NULL;
311  }
312  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_uri(tx));
313  const uint8_t *data = bstr_ptr(htp_tx_request_uri(tx));
314 
316  det_ctx, list_id, buffer, data, data_len, transforms);
317  }
318 
319  return buffer;
320 }
321 
322 #ifdef UNITTESTS /* UNITTESTS */
323 #include "tests/detect-http-uri.c"
324 #endif /* UNITTESTS */
325 
326 /**
327  * @}
328  */
SigTableElmt_::url
const char * url
Definition: detect.h:1521
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:306
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
Definition: detect-engine.c:1640
SigTableElmt_::desc
const char * desc
Definition: detect.h:1520
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:2226
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1518
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:391
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1509
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:186
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:981
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1658
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(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
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:450
DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_CM
Definition: detect-engine-register.h:183
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2461
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1500
detect-pcre.h
DetectUrilenApplyToContent
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
Definition: detect-urilen.c:150
DetectAppLayerMpmRegisterSubState
void DetectAppLayerMpmRegisterSubState(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t sub_state, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:167
detect-engine-prefilter.h
util-unittest.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:276
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1455
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:183
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:22
DetectEngineThreadCtx_
Definition: detect.h:1300
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:1674
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1516
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect-engine-register.h:326
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:185
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:218
SigTableElmt_::alias
const char * alias
Definition: detect.h:1519
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
detect-http-uri.c
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:299
str
#define str(s)
Definition: suricata-common.h:316
Signature_::id
uint32_t id
Definition: detect.h:727
detect-parse.h
Signature_
Signature container.
Definition: detect.h:682
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:77
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:328
detect-urilen.h
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1552
flow.h
flow-var.h
DETECT_HTTP_URI
@ DETECT_HTTP_URI
Definition: detect-engine-register.h:184
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1507
app-layer.h