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"
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
61static void DetectHttpUriRegisterTests(void);
62#endif
63static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx, Signature *s);
64static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
65 const DetectEngineTransforms *transforms,
66 Flow *_f, const uint8_t _flow_flags,
67 void *txv, const int list_id);
68static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
69 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
70 const int list_id);
71static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
72static int DetectHttpRawUriSetup(DetectEngineCtx *, Signature *, const char *);
73static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx, Signature *s);
74static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
75 const DetectEngineTransforms *transforms,
76 Flow *_f, const uint8_t _flow_flags,
77 void *txv, const int list_id);
78static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
79
80static int g_http_raw_uri_buffer_id = 0;
81static 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
186static 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 */
202static 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
211static 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
234static 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 */
266static 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
272static 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 */
288static 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
297static 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 */
321#endif /* UNITTESTS */
322
323/**
324 * @}
325 */
@ ALPROTO_HTTP2
@ ALPROTO_HTTP
@ ALPROTO_HTTP1
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
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
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_HTTP_RAW_URI
@ DETECT_HTTP_URI_CM
@ DETECT_HTTP_URI_RAW
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
int DetectBufferTypeGetByName(const char *name)
int DetectHttpUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
this function setups the http_uri modifier keyword used in the rule
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
SigTableElmt * sigmatch_table
bool DetectUrilenValidateContent(const Signature *s, const char **sigerror, const DetectBufferType *dbt)
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition detect.h:1674
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t alternative
Definition detect.h:1457
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t id
Definition detect.h:713
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define unlikely(expr)