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 #ifdef UNITTESTS
65 static void DetectHttpResponseLineRegisterTests(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_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].alias = "http_response_line";
80  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].desc = "content modifier to match only on the HTTP response line";
81  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].url = "/rules/http-keywords.html#http-response-line";
82  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests;
85 #endif
87 
88  DetectAppLayerInspectEngineRegister2("http_response_line",
89  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_LINE,
91  GetData);
92 
93  DetectAppLayerMpmRegister2("http_response_line", SIG_FLAG_TOCLIENT, 2,
95  ALPROTO_HTTP, HTP_RESPONSE_LINE);
96 
97  DetectBufferTypeSetDescriptionByName("http_response_line",
98  "http response line");
99 
100  g_http_response_line_id = DetectBufferTypeGetByName("http_response_line");
101 }
102 
103 /**
104  * \brief The setup function for the http_response_line keyword for a signature.
105  *
106  * \param de_ctx Pointer to the detection engine context.
107  * \param s Pointer to the signature for the current Signature being
108  * parsed from the rules.
109  * \param m Pointer to the head of the SigMatch for the current rule
110  * being parsed.
111  * \param arg Pointer to the string holding the keyword value.
112  *
113  * \retval 0 On success
114  * \retval -1 On failure
115  */
116 static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
117 {
118  if (DetectBufferSetActiveList(s, g_http_response_line_id) < 0)
119  return -1;
120 
122  return -1;
123 
124  return 0;
125 }
126 
127 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
128  const DetectEngineTransforms *transforms,
129  Flow *_f, const uint8_t _flow_flags,
130  void *txv, const int list_id)
131 {
132  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
133  if (buffer->inspect == NULL) {
134  htp_tx_t *tx = (htp_tx_t *)txv;
135  if (unlikely(tx->response_line == NULL)) {
136  return NULL;
137  }
138  const uint32_t data_len = bstr_len(tx->response_line);
139  const uint8_t *data = bstr_ptr(tx->response_line);
140 
141  InspectionBufferSetup(buffer, data, data_len);
142  InspectionBufferApplyTransforms(buffer, transforms);
143  }
144  return buffer;
145 }
146 
147 /************************************Unittests*********************************/
148 
149 #ifdef UNITTESTS
150 
151 #include "stream-tcp-reassemble.h"
152 
153 /**
154  * \test Test that a signature containting a http_response_line is correctly parsed
155  * and the keyword is registered.
156  */
157 static int DetectHttpResponseLineTest01(void)
158 {
161 
162  de_ctx->flags |= DE_QUIET;
163  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
164  "(http_response_line; content:\"200 OK\"; sid:1;)");
166 
168  PASS;
169 }
170 
171 
172 /**
173  *\test Test that the http_response_line content matches against a http request
174  * which holds the content.
175  */
176 static int DetectHttpResponseLineTest02(void)
177 {
178  TcpSession ssn;
179  Packet *p = NULL;
180  ThreadVars th_v;
181  DetectEngineCtx *de_ctx = NULL;
182  DetectEngineThreadCtx *det_ctx = NULL;
183  HtpState *http_state = NULL;
184  Flow f;
185  uint8_t http_buf[] =
186  "GET /index.html HTTP/1.0\r\n"
187  "Host: www.openinfosecfoundation.org\r\n"
188  "User-Agent: This is dummy message body\r\n"
189  "Content-Type: text/html\r\n"
190  "\r\n";
191  uint32_t http_len = sizeof(http_buf) - 1;
192  uint8_t http_buf2[] =
193  "HTTP/1.0 200 OK\r\n"
194  "Content-Type: text/html\r\n"
195  "Content-Length: 7\r\n"
196  "\r\n"
197  "message";
198  uint32_t http_len2 = sizeof(http_buf2) - 1;
199 
202 
203  memset(&th_v, 0, sizeof(th_v));
204  memset(&f, 0, sizeof(f));
205  memset(&ssn, 0, sizeof(ssn));
206 
207  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
208  FAIL_IF_NULL(p);
209 
210  FLOW_INITIALIZE(&f);
211  f.protoctx = (void *)&ssn;
212  f.proto = IPPROTO_TCP;
213  f.flags |= FLOW_IPV4;
214 
215  p->flow = &f;
218  f.alproto = ALPROTO_HTTP;
219 
221 
224 
225  de_ctx->flags |= DE_QUIET;
226 
227  de_ctx->sig_list = SigInit(de_ctx,"alert http any any -> any any "
228  "(http_response_line; content:\"HTTP/1.0 200 OK\"; "
229  "sid:1;)");
231 
233  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
234 
235  int r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_len);
236  FAIL_IF(r != 0);
237 
238  http_state = f.alstate;
239  FAIL_IF_NULL(http_state);
240 
241  r = AppLayerParserParse(&th_v, alp_tctx, &f, ALPROTO_HTTP, STREAM_TOCLIENT, http_buf2, http_len2);
242  FAIL_IF(r != 0);
243 
244  /* do detect */
245  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
246  FAIL_IF(PacketAlertCheck(p, 1));
247 
249 
250  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
251  FAIL_IF(!(PacketAlertCheck(p, 1)));
252 
255 
257  FLOW_DESTROY(&f);
258  UTHFreePackets(&p, 1);
259  PASS;
260 }
261 
262 void DetectHttpResponseLineRegisterTests(void)
263 {
264  UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01);
265  UtRegisterTest("DetectHttpResponseLineTest02", DetectHttpResponseLineTest02);
266 }
267 #endif /* UNITTESTS */
268 /**
269  * @}
270  */
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:2056
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
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
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
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
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
detect-http-response-line.h
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
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.
DETECT_AL_HTTP_RESPONSE_LINE
@ DETECT_AL_HTTP_RESPONSE_LINE
Definition: detect-engine-register.h:157
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:220
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1888
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
DetectHttpResponseLineRegister
void DetectHttpResponseLineRegister(void)
Registers the keyword handlers for the "http_response_line" keyword.
Definition: detect-http-response-line.c:76
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:1203
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
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:773
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
Flow_::alstate
void * alstate
Definition: flow.h:476
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:260
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