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 "debug.h"
35 #include "decode.h"
36 #include "detect.h"
37 
38 #include "detect-parse.h"
39 #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 "detect-uricontent.h"
59 #include "stream-tcp.h"
60 
61 #ifdef UNITTESTS
62 static void DetectHttpUriRegisterTests(void);
63 #endif
64 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
65  Signature *s);
66 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror);
67 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms,
69  Flow *_f, const uint8_t _flow_flags,
70  void *txv, 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,
74  Signature *s);
75 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **);
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 */
92  sigmatch_table[DETECT_AL_HTTP_URI].desc = "content modifier to match specifically and only on the HTTP uri-buffer";
93  sigmatch_table[DETECT_AL_HTTP_URI].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-uri-and-http-raw-uri";
95 #ifdef UNITTESTS
96  sigmatch_table[DETECT_AL_HTTP_URI].RegisterTests = DetectHttpUriRegisterTests;
97 #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 = DOC_URL DOC_VERSION "/rules/tls-keywords.html#http-uri";
105  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
107 
109  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
111 
114  HTP_REQUEST_LINE);
115 
117  "http request uri");
118 
120  DetectHttpUriSetupCallback);
121 
123  DetectHttpUriValidateCallback);
124 
125  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
126 
127  /* http_raw_uri content modifier */
128  sigmatch_table[DETECT_AL_HTTP_RAW_URI].name = "http_raw_uri";
129  sigmatch_table[DETECT_AL_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
130  sigmatch_table[DETECT_AL_HTTP_RAW_URI].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http_uri-and-http_raw-uri";
131  sigmatch_table[DETECT_AL_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
133 
134  /* http.uri.raw sticky buffer */
135  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
136  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
137  sigmatch_table[DETECT_HTTP_URI_RAW].url = DOC_URL DOC_VERSION "/rules/tls-keywords.html#http-uri";
138  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
140 
142  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
144 
147  HTP_REQUEST_LINE);
148 
150  "raw http uri");
151 
153  DetectHttpRawUriSetupCallback);
154 
156  DetectHttpRawUriValidateCallback);
157 
158  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
159 }
160 
161 /**
162  * \brief this function setups the http_uri modifier keyword used in the rule
163  *
164  * \param de_ctx Pointer to the Detection Engine Context
165  * \param s Pointer to the Signature to which the current keyword belongs
166  * \param str Should hold an empty string always
167  *
168  * \retval 0 On success
169  * \retval -1 On failure
170  */
171 
172 int DetectHttpUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
173 {
174  return DetectEngineContentModifierBufferSetup(de_ctx, s, str,
176  g_http_uri_buffer_id,
177  ALPROTO_HTTP);
178 }
179 
180 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror)
181 {
182  return DetectUrilenValidateContent(s, g_http_uri_buffer_id, sigerror);
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  DetectBufferSetActiveList(s, g_http_uri_buffer_id);
204  s->alproto = ALPROTO_HTTP;
205  return 0;
206 }
207 
208 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
209  const DetectEngineTransforms *transforms, Flow *_f,
210  const uint8_t _flow_flags, void *txv, const int list_id)
211 {
212  SCEnter();
213 
214  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
215  if (buffer->inspect == NULL) {
216  htp_tx_t *tx = (htp_tx_t *)txv;
217  HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
218 
219  if (tx_ud == NULL || tx_ud->request_uri_normalized == NULL) {
220  SCLogDebug("no tx_id or uri");
221  return NULL;
222  }
223 
224  const uint32_t data_len = bstr_len(tx_ud->request_uri_normalized);
225  const uint8_t *data = bstr_ptr(tx_ud->request_uri_normalized);
226 
227  InspectionBufferSetup(buffer, data, data_len);
228  InspectionBufferApplyTransforms(buffer, transforms);
229  }
230 
231  return buffer;
232 }
233 
234 /**
235  * \brief Sets up the http_raw_uri modifier keyword.
236  *
237  * \param de_ctx Pointer to the Detection Engine Context.
238  * \param s Pointer to the Signature to which the current keyword belongs.
239  * \param arg Should hold an empty string always.
240  *
241  * \retval 0 On success.
242  * \retval -1 On failure.
243  */
244 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
245 {
246  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
248  g_http_raw_uri_buffer_id,
249  ALPROTO_HTTP);
250 }
251 
252 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **sigerror)
253 {
254  return DetectUrilenValidateContent(s, g_http_raw_uri_buffer_id, sigerror);
255 }
256 
257 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
258  Signature *s)
259 {
260  SCLogDebug("callback invoked by %u", s->id);
261  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
262 }
263 
264 /**
265  * \brief this function setup the http.uri.raw keyword used in the rule
266  *
267  * \param de_ctx Pointer to the Detection Engine Context
268  * \param s Pointer to the Signature to which the current keyword belongs
269  * \param str Should hold an empty string always
270  *
271  * \retval 0 On success
272  */
273 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
274 {
275  DetectBufferSetActiveList(s, g_http_raw_uri_buffer_id);
276  s->alproto = ALPROTO_HTTP;
277  return 0;
278 }
279 
280 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
281  const DetectEngineTransforms *transforms, Flow *_f,
282  const uint8_t _flow_flags, void *txv, const int list_id)
283 {
284  SCEnter();
285 
286  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
287  if (buffer->inspect == NULL) {
288  htp_tx_t *tx = (htp_tx_t *)txv;
289  if (unlikely(tx->request_uri == NULL)) {
290  return NULL;
291  }
292  const uint32_t data_len = bstr_len(tx->request_uri);
293  const uint8_t *data = bstr_ptr(tx->request_uri);
294 
295  InspectionBufferSetup(buffer, data, data_len);
296  InspectionBufferApplyTransforms(buffer, transforms);
297  }
298 
299  return buffer;
300 }
301 
302 #ifdef UNITTESTS /* UNITTESTS */
303 #include "tests/detect-http-uri.c"
304 #endif /* UNITTESTS */
305 
306 /**
307  * @}
308  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectHttpUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
this function setups the http_uri modifier keyword used in the rule
uint32_t id
Definition: detect.h:525
#define unlikely(expr)
Definition: util-optimize.h:35
bstr * request_uri_normalized
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
main detection engine ctx
Definition: detect.h:720
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
#define SCEnter(...)
Definition: util-debug.h:337
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
AppProto alproto
Definition: detect.h:496
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
int 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.
const char * desc
Definition: detect.h:1162
const char * alias
Definition: detect.h:1161
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1328
bool DetectUrilenValidateContent(const Signature *s, int list, const char **sigerror)
const char * url
Definition: detect.h:1163
void DetectHttpUriRegister(void)
Registration function for keywords: http_uri and http.uri.
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:349
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeRegisterValidateCallback(const char *name, _Bool(*ValidateCallback)(const Signature *, const char **sigerror))
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
Flow data structure.
Definition: flow.h:327
void(* RegisterTests)(void)
Definition: detect.h:1152
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine