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
100 
101  /* http.uri sticky buffer */
102  sigmatch_table[DETECT_HTTP_URI].name = "http.uri";
103  sigmatch_table[DETECT_HTTP_URI].alias = "http.uri.normalized";
104  sigmatch_table[DETECT_HTTP_URI].desc = "sticky buffer to match specifically and only on the normalized HTTP URI buffer";
105  sigmatch_table[DETECT_HTTP_URI].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-uri-and-http-raw-uri";
106  sigmatch_table[DETECT_HTTP_URI].Setup = DetectHttpUriSetupSticky;
108 
110  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
112 
115  HTP_REQUEST_LINE);
116 
118  "http request uri");
119 
121  DetectHttpUriSetupCallback);
122 
124  DetectHttpUriValidateCallback);
125 
126  g_http_uri_buffer_id = DetectBufferTypeGetByName("http_uri");
127 
128  /* http_raw_uri content modifier */
129  sigmatch_table[DETECT_AL_HTTP_RAW_URI].name = "http_raw_uri";
130  sigmatch_table[DETECT_AL_HTTP_RAW_URI].desc = "content modifier to match on the raw HTTP uri";
131  sigmatch_table[DETECT_AL_HTTP_RAW_URI].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http_uri-and-http_raw-uri";
132  sigmatch_table[DETECT_AL_HTTP_RAW_URI].Setup = DetectHttpRawUriSetup;
135 
136  /* http.uri.raw sticky buffer */
137  sigmatch_table[DETECT_HTTP_URI_RAW].name = "http.uri.raw";
138  sigmatch_table[DETECT_HTTP_URI_RAW].desc = "sticky buffer to match specifically and only on the raw HTTP URI buffer";
139  sigmatch_table[DETECT_HTTP_URI_RAW].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-uri-and-http-raw-uri";
140  sigmatch_table[DETECT_HTTP_URI_RAW].Setup = DetectHttpRawUriSetupSticky;
142 
144  SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
146 
149  HTP_REQUEST_LINE);
150 
152  "raw http uri");
153 
155  DetectHttpRawUriSetupCallback);
156 
158  DetectHttpRawUriValidateCallback);
159 
160  g_http_raw_uri_buffer_id = DetectBufferTypeGetByName("http_raw_uri");
161 }
162 
163 /**
164  * \brief this function setups the http_uri modifier keyword used in the rule
165  *
166  * \param de_ctx Pointer to the Detection Engine Context
167  * \param s Pointer to the Signature to which the current keyword belongs
168  * \param str Should hold an empty string always
169  *
170  * \retval 0 On success
171  * \retval -1 On failure
172  */
173 
174 int DetectHttpUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
175 {
176  return DetectEngineContentModifierBufferSetup(de_ctx, s, str,
178  g_http_uri_buffer_id,
179  ALPROTO_HTTP);
180 }
181 
182 static bool DetectHttpUriValidateCallback(const Signature *s, const char **sigerror)
183 {
184  return DetectUrilenValidateContent(s, g_http_uri_buffer_id, sigerror);
185 }
186 
187 static void DetectHttpUriSetupCallback(const DetectEngineCtx *de_ctx,
188  Signature *s)
189 {
190  SCLogDebug("callback invoked by %u", s->id);
191  DetectUrilenApplyToContent(s, g_http_uri_buffer_id);
192 }
193 
194 /**
195  * \brief this function setup the http.uri keyword used in the rule
196  *
197  * \param de_ctx Pointer to the Detection Engine Context
198  * \param s Pointer to the Signature to which the current keyword belongs
199  * \param str Should hold an empty string always
200  *
201  * \retval 0 On success
202  */
203 static int DetectHttpUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
204 {
205  if (DetectBufferSetActiveList(s, g_http_uri_buffer_id) < 0)
206  return -1;
208  return -1;
209  return 0;
210 }
211 
212 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
213  const DetectEngineTransforms *transforms, Flow *_f,
214  const uint8_t _flow_flags, void *txv, const int list_id)
215 {
216  SCEnter();
217 
218  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
219  if (buffer->inspect == NULL) {
220  htp_tx_t *tx = (htp_tx_t *)txv;
221  HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
222 
223  if (tx_ud == NULL || tx_ud->request_uri_normalized == NULL) {
224  SCLogDebug("no tx_id or uri");
225  return NULL;
226  }
227 
228  const uint32_t data_len = bstr_len(tx_ud->request_uri_normalized);
229  const uint8_t *data = bstr_ptr(tx_ud->request_uri_normalized);
230 
231  InspectionBufferSetup(buffer, data, data_len);
232  InspectionBufferApplyTransforms(buffer, transforms);
233  }
234 
235  return buffer;
236 }
237 
238 /**
239  * \brief Sets up the http_raw_uri modifier keyword.
240  *
241  * \param de_ctx Pointer to the Detection Engine Context.
242  * \param s Pointer to the Signature to which the current keyword belongs.
243  * \param arg Should hold an empty string always.
244  *
245  * \retval 0 On success.
246  * \retval -1 On failure.
247  */
248 static int DetectHttpRawUriSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
249 {
250  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
252  g_http_raw_uri_buffer_id,
253  ALPROTO_HTTP);
254 }
255 
256 static bool DetectHttpRawUriValidateCallback(const Signature *s, const char **sigerror)
257 {
258  return DetectUrilenValidateContent(s, g_http_raw_uri_buffer_id, sigerror);
259 }
260 
261 static void DetectHttpRawUriSetupCallback(const DetectEngineCtx *de_ctx,
262  Signature *s)
263 {
264  SCLogDebug("callback invoked by %u", s->id);
265  DetectUrilenApplyToContent(s, g_http_raw_uri_buffer_id);
266 }
267 
268 /**
269  * \brief this function setup the http.uri.raw keyword used in the rule
270  *
271  * \param de_ctx Pointer to the Detection Engine Context
272  * \param s Pointer to the Signature to which the current keyword belongs
273  * \param str Should hold an empty string always
274  *
275  * \retval 0 On success
276  */
277 static int DetectHttpRawUriSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
278 {
279  if (DetectBufferSetActiveList(s, g_http_raw_uri_buffer_id) < 0)
280  return -1;
282  return -1;
283  return 0;
284 }
285 
286 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
287  const DetectEngineTransforms *transforms, Flow *_f,
288  const uint8_t _flow_flags, void *txv, const int list_id)
289 {
290  SCEnter();
291 
292  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
293  if (buffer->inspect == NULL) {
294  htp_tx_t *tx = (htp_tx_t *)txv;
295  if (unlikely(tx->request_uri == NULL)) {
296  return NULL;
297  }
298  const uint32_t data_len = bstr_len(tx->request_uri);
299  const uint8_t *data = bstr_ptr(tx->request_uri);
300 
301  InspectionBufferSetup(buffer, data, data_len);
302  InspectionBufferApplyTransforms(buffer, transforms);
303  }
304 
305  return buffer;
306 }
307 
308 #ifdef UNITTESTS /* UNITTESTS */
309 #include "tests/detect-http-uri.c"
310 #endif /* UNITTESTS */
311 
312 /**
313  * @}
314  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#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
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint32_t id
Definition: detect.h:555
#define unlikely(expr)
Definition: util-optimize.h:35
bstr * request_uri_normalized
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1391
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:1200
Signature container.
Definition: detect.h:522
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
main detection engine ctx
Definition: detect.h:761
void DetectUrilenApplyToContent(Signature *s, int list)
set prefilter dsize pair
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
#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
uint16_t alternative
Definition: detect.h:1198
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:1202
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
const char * alias
Definition: detect.h:1201
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:1369
bool DetectUrilenValidateContent(const Signature *s, int list, const char **sigerror)
const char * url
Definition: detect.h:1203
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:343
#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:1194
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1192