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 #ifdef UNITTESTS
65 static void DetectHttpRequestLineRegisterTests(void);
66 #endif
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 g_http_request_line_buffer_id = 0;
72 
73 /**
74  * \brief Registers the keyword handlers for the "http_request_line" keyword.
75  */
77 {
78  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].name = "http.request_line";
79  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].alias = "http_request_line";
80  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].desc = "sticky buffer to match on the HTTP request line";
81  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].url = "/rules/http-keywords.html#http-request-line";
83  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].Setup = DetectHttpRequestLineSetup;
84 #ifdef UNITTESTS
85  sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].RegisterTests = DetectHttpRequestLineRegisterTests;
86 #endif
88 
89  DetectAppLayerInspectEngineRegister2("http_request_line",
90  ALPROTO_HTTP, SIG_FLAG_TOSERVER, HTP_REQUEST_LINE,
92  GetData);
93 
94  DetectAppLayerMpmRegister2("http_request_line", SIG_FLAG_TOSERVER, 2,
96  ALPROTO_HTTP, HTP_REQUEST_LINE);
97 
98  DetectBufferTypeSetDescriptionByName("http_request_line",
99  "http request line");
100 
101  g_http_request_line_buffer_id = DetectBufferTypeGetByName("http_request_line");
102 }
103 
104 /**
105  * \brief The setup function for the http_request_line keyword for a signature.
106  *
107  * \param de_ctx Pointer to the detection engine context.
108  * \param s Pointer to the signature for the current Signature being
109  * parsed from the rules.
110  * \param m Pointer to the head of the SigMatch for the current rule
111  * being parsed.
112  * \param arg Pointer to the string holding the keyword value.
113  *
114  * \retval 0 On success
115  * \retval -1 On failure
116  */
117 static int DetectHttpRequestLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
118 {
119  if (DetectBufferSetActiveList(s, g_http_request_line_buffer_id) < 0)
120  return -1;
121 
123  return -1;
124 
125  return 0;
126 }
127 
128 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
129  const DetectEngineTransforms *transforms,
130  Flow *_f, const uint8_t _flow_flags,
131  void *txv, const int list_id)
132 {
133  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
134  if (buffer->inspect == NULL) {
135  htp_tx_t *tx = (htp_tx_t *)txv;
136  if (unlikely(tx->request_line == NULL)) {
137  return NULL;
138  }
139  const uint32_t data_len = bstr_len(tx->request_line);
140  const uint8_t *data = bstr_ptr(tx->request_line);
141 
142  InspectionBufferSetup(buffer, data, data_len);
143  InspectionBufferApplyTransforms(buffer, transforms);
144  }
145  return buffer;
146 }
147 
148 /************************************Unittests*********************************/
149 
150 #ifdef UNITTESTS
151 
152 #include "stream-tcp-reassemble.h"
153 
154 /**
155  * \test Test that a signature containting a http_request_line is correctly parsed
156  * and the keyword is registered.
157  */
158 static int DetectHttpRequestLineTest01(void)
159 {
162 
163  de_ctx->flags |= DE_QUIET;
164  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
165  "(http_request_line; content:\"GET /\"; sid:1;)");
167 
169  PASS;
170 }
171 
172 
173 /**
174  *\test Test that the http_request_line content matches against a http request
175  * which holds the content.
176  */
177 static int DetectHttpRequestLineTest02(void)
178 {
179  TcpSession ssn;
180  Packet *p = NULL;
181  ThreadVars th_v;
182  DetectEngineCtx *de_ctx = NULL;
183  DetectEngineThreadCtx *det_ctx = NULL;
184  HtpState *http_state = NULL;
185  Flow f;
186  uint8_t http_buf[] =
187  "GET /index.html HTTP/1.0\r\n"
188  "Host: www.openinfosecfoundation.org\r\n"
189  "User-Agent: This is dummy message body\r\n"
190  "Content-Type: text/html\r\n"
191  "\r\n";
192  uint32_t http_len = sizeof(http_buf) - 1;
193 
196 
197  memset(&th_v, 0, sizeof(th_v));
198  memset(&f, 0, sizeof(f));
199  memset(&ssn, 0, sizeof(ssn));
200 
201  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
202  FAIL_IF_NULL(p);
203 
204  FLOW_INITIALIZE(&f);
205  f.protoctx = (void *)&ssn;
206  f.proto = IPPROTO_TCP;
207  f.flags |= FLOW_IPV4;
208 
209  p->flow = &f;
213  f.alproto = ALPROTO_HTTP;
214 
216 
219 
220  de_ctx->flags |= DE_QUIET;
221 
222  de_ctx->sig_list = SigInit(de_ctx,"alert http any any -> any any "
223  "(http_request_line; content:\"GET /index.html HTTP/1.0\"; "
224  "sid:1;)");
226 
228  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
229 
230  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
231  FAIL_IF(r != 0);
232 
233  http_state = f.alstate;
234  FAIL_IF_NULL(http_state);
235 
236  /* do detect */
237  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
238 
239  FAIL_IF(!(PacketAlertCheck(p, 1)));
240 
243 
245  FLOW_DESTROY(&f);
246  UTHFreePackets(&p, 1);
247  PASS;
248 }
249 
250 static int DetectHttpRequestLineWrapper(const char *sig, const int expectation)
251 {
252  TcpSession ssn;
253  Packet *p = NULL;
254  ThreadVars th_v;
255  DetectEngineCtx *de_ctx = NULL;
256  DetectEngineThreadCtx *det_ctx = NULL;
257  HtpState *http_state = NULL;
258  Flow f;
259  uint8_t http_buf[] =
260  "GET /index.html HTTP/1.0\r\n"
261  "Host: www.openinfosecfoundation.org\r\n"
262  "User-Agent: This is dummy message body\r\n"
263  "Content-Type: text/html\r\n"
264  "\r\n";
265  uint32_t http_len = sizeof(http_buf) - 1;
266 
269 
270  memset(&th_v, 0, sizeof(th_v));
271  memset(&f, 0, sizeof(f));
272  memset(&ssn, 0, sizeof(ssn));
273 
274  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
275  FAIL_IF_NULL(p);
276 
277  FLOW_INITIALIZE(&f);
278  f.protoctx = (void *)&ssn;
279  f.proto = IPPROTO_TCP;
280  f.flags |= FLOW_IPV4;
281 
282  p->flow = &f;
286  f.alproto = ALPROTO_HTTP;
287 
289 
292 
293  de_ctx->flags |= DE_QUIET;
294 
295  de_ctx->sig_list = SigInit(de_ctx, sig);
297  int sid = de_ctx->sig_list->id;
298 
300  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
301 
302  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
303  FAIL_IF(r != 0);
304 
305  http_state = f.alstate;
306  FAIL_IF_NULL(http_state);
307 
308  /* do detect */
309  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
310 
311  r = PacketAlertCheck(p, sid);
312  FAIL_IF_NOT(r == expectation);
313 
316 
318  FLOW_DESTROY(&f);
319  UTHFreePackets(&p, 1);
320  PASS;
321 }
322 
323 static int DetectHttpRequestLineTest03(void)
324 {
325  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:>10; sid:1;)", true));
326  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:<100; sid:2;)", true));
327  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:10<>100; sid:3;)", true));
328  FAIL_IF_NOT(DetectHttpRequestLineWrapper("alert http any any -> any any (http_request_line; bsize:>100; sid:3;)", false));
329  PASS;
330 }
331 
332 static void DetectHttpRequestLineRegisterTests(void)
333 {
334  UtRegisterTest("DetectHttpRequestLineTest01", DetectHttpRequestLineTest01);
335  UtRegisterTest("DetectHttpRequestLineTest02", DetectHttpRequestLineTest02);
336  UtRegisterTest("DetectHttpRequestLineTest03", DetectHttpRequestLineTest03);
337 }
338 #endif /* UNITTESTS */
339 /**
340  * @}
341  */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
detect-content.h
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1404
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1109
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1211
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:376
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_::proto
uint8_t proto
Definition: flow.h:365
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:138
InspectionBuffer
Definition: detect.h:344
Packet_::flags
uint32_t flags
Definition: decode.h:449
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
SigInit
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2051
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:611
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:278
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:219
DE_QUIET
#define DE_QUIET
Definition: detect.h:294
stream-tcp-reassemble.h
UTHBuildPacket
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.
Definition: util-unittest-helper.c:337
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:445
Flow_::protoctx
void * protoctx
Definition: flow.h:441
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
detect-pcre.h
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:95
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1061
HtpState_
Definition: app-layer-htp.h:243
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
app-layer-htp.h
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:19
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DETECT_AL_HTTP_REQUEST_LINE
@ DETECT_AL_HTTP_REQUEST_LINE
Definition: detect-engine-register.h:156
app-layer-parser.h
TRUE
#define TRUE
Definition: suricata-common.h:33
SigMatchSignatures
void SigMatchSignatures(ThreadVars *th_v, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1688
DetectEngineInspectBufferGeneric
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.
Definition: detect-engine.c:1641
Packet_
Definition: decode.h:414
StreamTcpFreeConfig
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:668
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:232
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1179
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1884
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:252
detect-engine-content-inspection.h
DetectAppLayerMpmRegister2
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
Definition: detect-engine-mpm.c:89
Packet_::flow
struct Flow_ * flow
Definition: decode.h:451
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:2797
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
SigTableElmt_::alias
const char * alias
Definition: detect.h:1212
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1178
suricata-common.h
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1120
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1211
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:773
DetectHttpRequestLineRegister
void DetectHttpRequestLineRegister(void)
Registers the keyword handlers for the "http_request_line" keyword.
Definition: detect-http-request-line.c:76
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
detect-http-request-line.h
Flow_::alstate
void * alstate
Definition: flow.h:476
Signature_::id
uint32_t id
Definition: detect.h:561
Flow_::flags
uint32_t flags
Definition: flow.h:421
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:30
StreamTcpInitConfig
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:365
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:221
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:992
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:768
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:87
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:908
TcpSession_
Definition: stream-tcp-private.h:261
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
flow-var.h
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:130
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1107
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
app-layer.h
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:468