suricata
detect-http-request-line.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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 Victor Julien <victor@inliniac.net>
29  *
30  * Implements support for the http_request_line keyword.
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
44 #include "detect-content.h"
45 #include "detect-pcre.h"
46 
47 #include "flow.h"
48 #include "flow-var.h"
49 #include "flow-util.h"
50 
51 #include "util-debug.h"
52 #include "util-unittest.h"
53 #include "util-unittest-helper.h"
54 #include "util-spm.h"
55 
56 #include "app-layer.h"
57 #include "app-layer-parser.h"
58 
59 #include "app-layer-htp.h"
60 #include "stream-tcp.h"
62 
63 static int DetectHttpRequestLineSetup(DetectEngineCtx *, Signature *, const char *);
64 static void DetectHttpRequestLineRegisterTests(void);
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 int g_http_request_line_buffer_id = 0;
70 
71 /**
72  * \brief Registers the keyword handlers for the "http_request_line" keyword.
73  */
75 {
76  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].name = "http.request_line";
77  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].alias = "http_request_line";
78  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].desc = "sticky buffer to match on the HTTP request line";
79  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-request-line";
81  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].Setup = DetectHttpRequestLineSetup;
82  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].RegisterTests = DetectHttpRequestLineRegisterTests;
83 
85 
86  DetectAppLayerInspectEngineRegister2("http_request_line",
87  ALPROTO_HTTP, SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
89  GetData);
90 
91  DetectAppLayerMpmRegister2("http_request_line", SIG_FLAG_TOSERVER, 2,
93  ALPROTO_HTTP, HTP_REQUEST_LINE);
94 
95  DetectBufferTypeSetDescriptionByName("http_request_line",
96  "http request line");
97 
98  g_http_request_line_buffer_id = DetectBufferTypeGetByName("http_request_line");
99 }
100 
101 /**
102  * \brief The setup function for the http_request_line keyword for a signature.
103  *
104  * \param de_ctx Pointer to the detection engine context.
105  * \param s Pointer to the signature for the current Signature being
106  * parsed from the rules.
107  * \param m Pointer to the head of the SigMatch for the current rule
108  * being parsed.
109  * \param arg Pointer to the string holding the keyword value.
110  *
111  * \retval 0 On success
112  * \retval -1 On failure
113  */
114 static int DetectHttpRequestLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
115 {
116  if (DetectBufferSetActiveList(s, g_http_request_line_buffer_id) < 0)
117  return -1;
118 
120  return -1;
121 
122  return 0;
123 }
124 
125 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
126  const DetectEngineTransforms *transforms,
127  Flow *_f, const uint8_t _flow_flags,
128  void *txv, const int list_id)
129 {
130  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
131  if (buffer->inspect == NULL) {
132  htp_tx_t *tx = (htp_tx_t *)txv;
133  if (unlikely(tx->request_line == NULL)) {
134  return NULL;
135  }
136  const uint32_t data_len = bstr_len(tx->request_line);
137  const uint8_t *data = bstr_ptr(tx->request_line);
138 
139  InspectionBufferSetup(buffer, data, data_len);
140  InspectionBufferApplyTransforms(buffer, transforms);
141  }
142  return buffer;
143 }
144 
145 /************************************Unittests*********************************/
146 
147 #ifdef UNITTESTS
148 
149 #include "stream-tcp-reassemble.h"
150 
151 /**
152  * \test Test that a signature containting a http_request_line is correctly parsed
153  * and the keyword is registered.
154  */
155 static int DetectHttpRequestLineTest01(void)
156 {
158  FAIL_IF_NULL(de_ctx);
159 
160  de_ctx->flags |= DE_QUIET;
161  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
162  "(http_request_line; content:\"GET /\"; sid:1;)");
163  FAIL_IF_NULL(de_ctx->sig_list);
164 
165  DetectEngineCtxFree(de_ctx);
166  PASS;
167 }
168 
169 
170 /**
171  *\test Test that the http_request_line content matches against a http request
172  * which holds the content.
173  */
174 static int DetectHttpRequestLineTest02(void)
175 {
176  TcpSession ssn;
177  Packet *p = NULL;
178  ThreadVars th_v;
179  DetectEngineCtx *de_ctx = NULL;
180  DetectEngineThreadCtx *det_ctx = NULL;
181  HtpState *http_state = NULL;
182  Flow f;
183  uint8_t http_buf[] =
184  "GET /index.html HTTP/1.0\r\n"
185  "Host: www.openinfosecfoundation.org\r\n"
186  "User-Agent: This is dummy message body\r\n"
187  "Content-Type: text/html\r\n"
188  "\r\n";
189  uint32_t http_len = sizeof(http_buf) - 1;
190 
192  FAIL_IF_NULL(alp_tctx);
193 
194  memset(&th_v, 0, sizeof(th_v));
195  memset(&f, 0, sizeof(f));
196  memset(&ssn, 0, sizeof(ssn));
197 
198  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
199  FAIL_IF_NULL(p);
200 
201  FLOW_INITIALIZE(&f);
202  f.protoctx = (void *)&ssn;
203  f.proto = IPPROTO_TCP;
204  f.flags |= FLOW_IPV4;
205 
206  p->flow = &f;
210  f.alproto = ALPROTO_HTTP;
211 
213 
214  de_ctx = DetectEngineCtxInit();
215  FAIL_IF_NULL(de_ctx);
216 
217  de_ctx->flags |= DE_QUIET;
218 
219  de_ctx->sig_list = SigInit(de_ctx,"alert http any any -> any any "
220  "(http_request_line; content:\"GET /index.html HTTP/1.0\"; "
221  "sid:1;)");
222  FAIL_IF_NULL(de_ctx->sig_list);
223 
224  SigGroupBuild(de_ctx);
225  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
226 
227  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
228  FAIL_IF(r != 0);
229 
230  http_state = f.alstate;
231  FAIL_IF_NULL(http_state);
232 
233  /* do detect */
234  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
235 
236  FAIL_IF(!(PacketAlertCheck(p, 1)));
237 
238  AppLayerParserThreadCtxFree(alp_tctx);
239  DetectEngineCtxFree(de_ctx);
240 
242  FLOW_DESTROY(&f);
243  UTHFreePackets(&p, 1);
244  PASS;
245 }
246 
247 static int DetectHttpRequestLineWrapper(const char *sig, const int expectation)
248 {
249  TcpSession ssn;
250  Packet *p = NULL;
251  ThreadVars th_v;
252  DetectEngineCtx *de_ctx = NULL;
253  DetectEngineThreadCtx *det_ctx = NULL;
254  HtpState *http_state = NULL;
255  Flow f;
256  uint8_t http_buf[] =
257  "GET /index.html HTTP/1.0\r\n"
258  "Host: www.openinfosecfoundation.org\r\n"
259  "User-Agent: This is dummy message body\r\n"
260  "Content-Type: text/html\r\n"
261  "\r\n";
262  uint32_t http_len = sizeof(http_buf) - 1;
263 
265  FAIL_IF_NULL(alp_tctx);
266 
267  memset(&th_v, 0, sizeof(th_v));
268  memset(&f, 0, sizeof(f));
269  memset(&ssn, 0, sizeof(ssn));
270 
271  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
272  FAIL_IF_NULL(p);
273 
274  FLOW_INITIALIZE(&f);
275  f.protoctx = (void *)&ssn;
276  f.proto = IPPROTO_TCP;
277  f.flags |= FLOW_IPV4;
278 
279  p->flow = &f;
283  f.alproto = ALPROTO_HTTP;
284 
286 
287  de_ctx = DetectEngineCtxInit();
288  FAIL_IF_NULL(de_ctx);
289 
290  de_ctx->flags |= DE_QUIET;
291 
292  de_ctx->sig_list = SigInit(de_ctx, sig);
293  FAIL_IF_NULL(de_ctx->sig_list);
294  int sid = de_ctx->sig_list->id;
295 
296  SigGroupBuild(de_ctx);
297  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
298 
299  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
300  FAIL_IF(r != 0);
301 
302  http_state = f.alstate;
303  FAIL_IF_NULL(http_state);
304 
305  /* do detect */
306  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
307 
308  r = PacketAlertCheck(p, sid);
309  FAIL_IF_NOT(r == expectation);
310 
311  AppLayerParserThreadCtxFree(alp_tctx);
312  DetectEngineCtxFree(de_ctx);
313 
315  FLOW_DESTROY(&f);
316  UTHFreePackets(&p, 1);
317  PASS;
318 }
319 
320 static int DetectHttpRequestLineTest03(void)
321 {
322  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:>10; sid:1;)", true));
323  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:<100; sid:2;)", true));
324  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:10<>100; sid:3;)", true));
325  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:>100; sid:3;)", false));
326  PASS;
327 }
328 
329 #endif /* UNITTESTS */
330 
331 static void DetectHttpRequestLineRegisterTests(void)
332 {
333 #ifdef UNITTESTS
334  UtRegisterTest("DetectHttpRequestLineTest01", DetectHttpRequestLineTest01);
335  UtRegisterTest("DetectHttpRequestLineTest02", DetectHttpRequestLineTest02);
336  UtRegisterTest("DetectHttpRequestLineTest03", DetectHttpRequestLineTest03);
337 #endif /* UNITTESTS */
338 
339  return;
340 }
341 /**
342  * @}
343  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
struct Flow_ * flow
Definition: decode.h:446
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
uint8_t proto
Definition: flow.h:344
uint32_t id
Definition: detect.h:555
#define PASS
Pass the test.
#define unlikely(expr)
Definition: util-optimize.h:35
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
void DetectHttpRequestLineRegister(void)
Registers the keyword handlers for the "http_request_line" keyword.
Signature * sig_list
Definition: detect.h:767
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:203
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:668
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
#define TRUE
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
void * protoctx
Definition: flow.h:400
main detection engine ctx
Definition: detect.h:761
void * alstate
Definition: flow.h:438
#define DE_QUIET
Definition: detect.h:292
int DetectBufferTypeGetByName(const char *name)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
uint8_t flags
Definition: detect.h:762
Data structures and function prototypes for keeping state for the detection engine.
#define FLOW_DESTROY(f)
Definition: flow-util.h:121
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void SigMatchSignatures(ThreadVars *th_v, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1670
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:364
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
uint8_t flowflags
Definition: decode.h:440
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
#define FLOW_PKT_TOSERVER
Definition: flow.h:201
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol&#39;s parser thread context.
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1170
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)
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
#define SIGMATCH_NOOPT
Definition: detect.h:1369
const char * url
Definition: detect.h:1203
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
#define STREAM_TOSERVER
Definition: stream.h:31
#define PKT_HAS_FLOW
Definition: decode.h:1094
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
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)
Per thread variable structure.
Definition: threadvars.h:57
AppProto alproto
application level protocol
Definition: flow.h:409
uint32_t flags
Definition: decode.h:444
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1194
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself...
Flow data structure.
Definition: flow.h:325
#define FLOW_IPV4
Definition: flow.h:94
uint32_t flags
Definition: flow.h:379
#define PKT_STREAM_EST
Definition: decode.h:1092
void(* RegisterTests)(void)
Definition: detect.h:1192
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
DetectEngineCtx * DetectEngineCtxInit(void)