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 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_response_line_id = 0;
70 
71 /**
72  * \brief Registers the keyword handlers for the "http_response_line" keyword.
73  */
75 {
76  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].name = "http.response_line";
77  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].alias = "http_response_line";
78  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].desc = "content modifier to match only on the HTTP response line";
79  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-response-line";
80  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
81  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
82 
84 
85  DetectAppLayerInspectEngineRegister2("http_response_line",
86  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
88  GetData);
89 
90  DetectAppLayerMpmRegister2("http_response_line", SIG_FLAG_TOCLIENT, 2,
92  ALPROTO_HTTP, HTP_RESPONSE_LINE);
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  if (DetectBufferSetActiveList(s, g_http_response_line_id) < 0)
116  return -1;
117 
119  return -1;
120 
121  return 0;
122 }
123 
124 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
125  const DetectEngineTransforms *transforms,
126  Flow *_f, const uint8_t _flow_flags,
127  void *txv, const int list_id)
128 {
129  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
130  if (buffer->inspect == NULL) {
131  htp_tx_t *tx = (htp_tx_t *)txv;
132  if (unlikely(tx->response_line == NULL)) {
133  return NULL;
134  }
135  const uint32_t data_len = bstr_len(tx->response_line);
136  const uint8_t *data = bstr_ptr(tx->response_line);
137 
138  InspectionBufferSetup(buffer, data, data_len);
139  InspectionBufferApplyTransforms(buffer, transforms);
140  }
141  return buffer;
142 }
143 
144 /************************************Unittests*********************************/
145 
146 #ifdef UNITTESTS
147 
148 #include "stream-tcp-reassemble.h"
149 
150 /**
151  * \test Test that a signature containting a http_response_line is correctly parsed
152  * and the keyword is registered.
153  */
154 static int DetectHttpResponseLineTest01(void)
155 {
157  FAIL_IF_NULL(de_ctx);
158 
159  de_ctx->flags |= DE_QUIET;
160  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
161  "(http_response_line; content:\"200 OK\"; sid:1;)");
162  FAIL_IF_NULL(de_ctx->sig_list);
163 
164  DetectEngineCtxFree(de_ctx);
165  PASS;
166 }
167 
168 
169 /**
170  *\test Test that the http_response_line content matches against a http request
171  * which holds the content.
172  */
173 static int DetectHttpResponseLineTest02(void)
174 {
175  TcpSession ssn;
176  Packet *p = NULL;
177  ThreadVars th_v;
178  DetectEngineCtx *de_ctx = NULL;
179  DetectEngineThreadCtx *det_ctx = NULL;
180  HtpState *http_state = NULL;
181  Flow f;
182  uint8_t http_buf[] =
183  "GET /index.html HTTP/1.0\r\n"
184  "Host: www.openinfosecfoundation.org\r\n"
185  "User-Agent: This is dummy message body\r\n"
186  "Content-Type: text/html\r\n"
187  "\r\n";
188  uint32_t http_len = sizeof(http_buf) - 1;
189  uint8_t http_buf2[] =
190  "HTTP/1.0 200 OK\r\n"
191  "Content-Type: text/html\r\n"
192  "Content-Length: 7\r\n"
193  "\r\n"
194  "message";
195  uint32_t http_len2 = sizeof(http_buf2) - 1;
196 
198  FAIL_IF_NULL(alp_tctx);
199 
200  memset(&th_v, 0, sizeof(th_v));
201  memset(&f, 0, sizeof(f));
202  memset(&ssn, 0, sizeof(ssn));
203 
204  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
205  FAIL_IF_NULL(p);
206 
207  FLOW_INITIALIZE(&f);
208  f.protoctx = (void *)&ssn;
209  f.proto = IPPROTO_TCP;
210  f.flags |= FLOW_IPV4;
211 
212  p->flow = &f;
215  f.alproto = ALPROTO_HTTP;
216 
218 
219  de_ctx = DetectEngineCtxInit();
220  FAIL_IF_NULL(de_ctx);
221 
222  de_ctx->flags |= DE_QUIET;
223 
224  de_ctx->sig_list = SigInit(de_ctx,"alert http any any -> any any "
225  "(http_response_line; content:\"HTTP/1.0 200 OK\"; "
226  "sid:1;)");
227  FAIL_IF_NULL(de_ctx->sig_list);
228 
229  SigGroupBuild(de_ctx);
230  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
231 
232  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
233  FAIL_IF(r != 0);
234 
235  http_state = f.alstate;
236  FAIL_IF_NULL(http_state);
237 
238  r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOCLIENT, http_buf2, http_len2);
239  FAIL_IF(r != 0);
240 
241  /* do detect */
242  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
243  FAIL_IF(PacketAlertCheck(p, 1));
244 
246 
247  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
248  FAIL_IF(!(PacketAlertCheck(p, 1)));
249 
250  AppLayerParserThreadCtxFree(alp_tctx);
251  DetectEngineCtxFree(de_ctx);
252 
254  FLOW_DESTROY(&f);
255  UTHFreePackets(&p, 1);
256  PASS;
257 }
258 
259 #endif /* UNITTESTS */
260 
261 void DetectHttpResponseLineRegisterTests(void)
262 {
263 #ifdef UNITTESTS
264  UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
265  UtRegisterTest("DetectHttpResponseLineTest02", DetectHttpResponseLineTest02);
266 #endif /* UNITTESTS */
267 
268  return;
269 }
270 /**
271  * @}
272  */
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
#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.
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 SIG_FLAG_TOCLIENT
Definition: detect.h:237
#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
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 STREAM_TOCLIENT
Definition: stream.h:32
#define FLOW_PKT_TOSERVER
Definition: flow.h:201
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol&#39;s parser thread context.
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.
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
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
#define FLOW_PKT_TOCLIENT
Definition: flow.h:202
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
DetectEngineCtx * DetectEngineCtxInit(void)