suricata
detect-http-response-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_response_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 DetectHttpResponseLineSetup(DetectEngineCtx *, Signature *, const char *);
64 static void DetectHttpResponseLineRegisterTests(void);
65 static int PrefilterTxHttpResponseLineRegister(DetectEngineCtx *de_ctx,
66  SigGroupHead *sgh, MpmCtx *mpm_ctx);
67 static int DetectEngineInspectHttpResponseLine(ThreadVars *tv,
68  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
69  const Signature *s, const SigMatchData *smd,
70  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
71 static int g_http_response_line_id = 0;
72 
73 /**
74  * \brief Registers the keyword handlers for the "http_response_line" keyword.
75  */
77 {
78  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].name = "http_response_line";
79  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].desc = "content modifier to match only on the HTTP response line";
80  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-response-line";
82  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
83  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
84 
86 
87  DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2,
88  PrefilterTxHttpResponseLineRegister);
89 
90  DetectAppLayerInspectEngineRegister("http_response_line",
91  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
92  DetectEngineInspectHttpResponseLine);
93 
94  DetectBufferTypeSetDescriptionByName("http_response_line",
95  "http response line");
96 
97  g_http_response_line_id = DetectBufferTypeGetByName("http_response_line");
98 }
99 
100 /**
101  * \brief The setup function for the http_response_line keyword for a signature.
102  *
103  * \param de_ctx Pointer to the detection engine context.
104  * \param s Pointer to the signature for the current Signature being
105  * parsed from the rules.
106  * \param m Pointer to the head of the SigMatch for the current rule
107  * being parsed.
108  * \param arg Pointer to the string holding the keyword value.
109  *
110  * \retval 0 On success
111  * \retval -1 On failure
112  */
113 static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
114 {
115  s->init_data->list = g_http_response_line_id;
116  s->alproto = ALPROTO_HTTP;
117  return 0;
118 }
119 
120 /** \brief HTTP response line Mpm prefilter callback
121  *
122  * \param det_ctx detection engine thread ctx
123  * \param p packet to inspect
124  * \param f flow to inspect
125  * \param txv tx to inspect
126  * \param pectx inspection context
127  */
128 static void PrefilterTxHttpResponseLine(DetectEngineThreadCtx *det_ctx,
129  const void *pectx,
130  Packet *p, Flow *f, void *txv,
131  const uint64_t idx, const uint8_t flags)
132 {
133  SCEnter();
134 
135  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
136  htp_tx_t *tx = (htp_tx_t *)txv;
137 
138  if (tx->response_line == NULL)
139  return;
140 
141  const uint32_t buffer_len = bstr_len(tx->response_line);
142  const uint8_t *buffer = bstr_ptr(tx->response_line);
143 
144  if (buffer_len >= mpm_ctx->minlen) {
145  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
146  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
147  }
148 }
149 
150 static int PrefilterTxHttpResponseLineRegister(DetectEngineCtx *de_ctx,
151  SigGroupHead *sgh, MpmCtx *mpm_ctx)
152 {
153  SCEnter();
154 
155  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpResponseLine,
156  ALPROTO_HTTP, HTP_RESPONSE_LINE,
157  mpm_ctx, NULL, "http_response_line");
158 }
159 
160 /**
161  * \brief Do the content inspection & validation for a signature
162  *
163  * \param de_ctx Detection engine context
164  * \param det_ctx Detection engine thread context
165  * \param s Signature to inspect
166  * \param sm SigMatch to inspect
167  * \param f Flow
168  * \param flags app layer flags
169  * \param state App layer state
170  *
171  * \retval 0 no match.
172  * \retval 1 match.
173  * \retval 2 Sig can't match.
174  */
175 int DetectEngineInspectHttpResponseLine(ThreadVars *tv,
176  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
177  const Signature *s, const SigMatchData *smd,
178  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
179 {
180  htp_tx_t *tx = (htp_tx_t *)txv;
181 
182  if (tx->response_line == NULL) {
183  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, txv, flags) > HTP_RESPONSE_LINE)
185  else
187  }
188 
189  det_ctx->discontinue_matching = 0;
190  det_ctx->buffer_offset = 0;
191  det_ctx->inspection_recursion_counter = 0;
192 
193 #if 0
194  PrintRawDataFp(stdout, (uint8_t *)bstr_ptr(tx->response_line),
195  bstr_len(tx->response_line));
196 #endif
197 
198  /* run the inspection against the buffer */
199  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
200  f,
201  bstr_ptr(tx->response_line),
202  bstr_len(tx->response_line),
205  if (r == 1) {
207  } else {
209  }
210 }
211 
212 /************************************Unittests*********************************/
213 
214 #ifdef UNITTESTS
215 
216 #include "stream-tcp-reassemble.h"
217 
218 /**
219  * \test Test that a signature containting a http_response_line is correctly parsed
220  * and the keyword is registered.
221  */
222 static int DetectHttpResponseLineTest01(void)
223 {
225  FAIL_IF_NULL(de_ctx);
226 
227  de_ctx->flags |= DE_QUIET;
228  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
229  "(http_response_line; content:\"200 OK\"; sid:1;)");
230  FAIL_IF_NULL(de_ctx->sig_list);
231 
232  DetectEngineCtxFree(de_ctx);
233  PASS;
234 }
235 
236 
237 /**
238  *\test Test that the http_response_line content matches against a http request
239  * which holds the content.
240  */
241 static int DetectHttpResponseLineTest02(void)
242 {
243  TcpSession ssn;
244  Packet *p = NULL;
245  ThreadVars th_v;
246  DetectEngineCtx *de_ctx = NULL;
247  DetectEngineThreadCtx *det_ctx = NULL;
248  HtpState *http_state = NULL;
249  Flow f;
250  uint8_t http_buf[] =
251  "GET /index.html HTTP/1.0\r\n"
252  "Host: www.openinfosecfoundation.org\r\n"
253  "User-Agent: This is dummy message body\r\n"
254  "Content-Type: text/html\r\n"
255  "\r\n";
256  uint32_t http_len = sizeof(http_buf) - 1;
257  uint8_t http_buf2[] =
258  "HTTP/1.0 200 OK\r\n"
259  "Content-Type: text/html\r\n"
260  "Content-Length: 7\r\n"
261  "\r\n"
262  "message";
263  uint32_t http_len2 = sizeof(http_buf2) - 1;
264 
266  FAIL_IF_NULL(alp_tctx);
267 
268  memset(&th_v, 0, sizeof(th_v));
269  memset(&f, 0, sizeof(f));
270  memset(&ssn, 0, sizeof(ssn));
271 
272  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
273  FAIL_IF_NULL(p);
274 
275  FLOW_INITIALIZE(&f);
276  f.protoctx = (void *)&ssn;
277  f.proto = IPPROTO_TCP;
278  f.flags |= FLOW_IPV4;
279 
280  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,"alert http any any -> any any "
293  "(http_response_line; content:\"HTTP/1.0 200 OK\"; "
294  "sid:1;)");
295  FAIL_IF_NULL(de_ctx->sig_list);
296 
297  SigGroupBuild(de_ctx);
298  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
299 
300  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
301  FAIL_IF(r != 0);
302 
303  http_state = f.alstate;
304  FAIL_IF_NULL(http_state);
305 
306  r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOCLIENT, http_buf2, http_len2);
307  FAIL_IF(r != 0);
308 
309  /* do detect */
310  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
311  FAIL_IF(PacketAlertCheck(p, 1));
312 
314 
315  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
316  FAIL_IF(!(PacketAlertCheck(p, 1)));
317 
318  AppLayerParserThreadCtxFree(alp_tctx);
319  DetectEngineCtxFree(de_ctx);
320 
322  FLOW_DESTROY(&f);
323  UTHFreePackets(&p, 1);
324  PASS;
325 }
326 
327 #endif /* UNITTESTS */
328 
329 void DetectHttpResponseLineRegisterTests(void)
330 {
331 #ifdef UNITTESTS
332  UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
333  UtRegisterTest("DetectHttpResponseLineTest02", DetectHttpResponseLineTest02);
334 #endif /* UNITTESTS */
335 
336  return;
337 }
338 /**
339  * @}
340  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
SignatureInitData * init_data
Definition: detect.h:560
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
struct Flow_ * flow
Definition: decode.h:444
uint16_t minlen
Definition: util-mpm.h:95
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
uint16_t discontinue_matching
Definition: detect.h:1026
uint8_t proto
Definition: flow.h:346
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx))
register an app layer keyword for mpm
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
#define PASS
Pass the test.
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Signature * sig_list
Definition: detect.h:726
#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:195
Data needed for Match()
Definition: detect.h:333
Container for matching data for a signature group.
Definition: detect.h:1295
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:669
uint32_t buffer_offset
Definition: detect.h:991
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
#define TRUE
void * protoctx
Definition: flow.h:398
main detection engine ctx
Definition: detect.h:720
void * alstate
Definition: flow.h:436
#define DE_QUIET
Definition: detect.h:298
int DetectBufferTypeGetByName(const char *name)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:244
uint8_t flags
Definition: detect.h:721
#define DETECT_ENGINE_INSPECT_SIG_MATCH
uint16_t mpm_type
Definition: util-mpm.h:84
Data structures and function prototypes for keeping state for the detection engine.
#define FLOW_DESTROY(f)
Definition: flow-util.h:115
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:1752
#define SCEnter(...)
Definition: util-debug.h:337
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:365
PrefilterRuleStore pmq
Definition: detect.h:1061
uint8_t flowflags
Definition: decode.h:438
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 STREAM_TOCLIENT
Definition: stream.h:32
#define FLOW_PKT_TOSERVER
Definition: flow.h:193
AppProto alproto
Definition: detect.h:496
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol&#39;s parser thread context.
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
const char * desc
Definition: detect.h:1162
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:165
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
uint16_t tx_id
MpmThreadCtx mtcu
Definition: detect.h:1059
#define SIGMATCH_NOOPT
Definition: detect.h:1328
int(* Match)(ThreadVars *, DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1129
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode, void *data)
Run the actual payload match functions.
const char * url
Definition: detect.h:1163
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
#define STREAM_TOSERVER
Definition: stream.h:31
int inspection_recursion_counter
Definition: detect.h:1038
#define PKT_HAS_FLOW
Definition: decode.h:1101
#define DETECT_CI_FLAGS_SINGLE
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:158
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Per thread variable structure.
Definition: threadvars.h:57
#define FLOW_PKT_TOCLIENT
Definition: flow.h:194
AppProto alproto
application level protocol
Definition: flow.h:407
uint32_t flags
Definition: decode.h:442
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
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:327
#define FLOW_IPV4
Definition: flow.h:93
uint32_t flags
Definition: flow.h:377
#define PKT_STREAM_EST
Definition: decode.h:1099
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
void(* RegisterTests)(void)
Definition: detect.h:1152
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, uint8_t *input, uint32_t input_len)
DetectEngineCtx * DetectEngineCtxInit(void)