suricata
detect-http-start.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 http_start
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"
47 #include "detect-http-start.h"
48 
49 #include "flow.h"
50 #include "flow-var.h"
51 #include "flow-util.h"
52 
53 #include "util-debug.h"
54 #include "util-unittest.h"
55 #include "util-unittest-helper.h"
56 #include "util-spm.h"
57 #include "util-print.h"
58 
59 #include "app-layer.h"
60 #include "app-layer-parser.h"
61 
62 #include "app-layer-htp.h"
63 #include "detect-http-header.h"
64 #include "stream-tcp.h"
65 
66 #include "util-print.h"
67 
68 #define KEYWORD_NAME "http_start"
69 #define KEYWORD_DOC "http-keywords.html#http-start"
70 #define BUFFER_NAME "http_start"
71 #define BUFFER_DESC "http start: request/response line + headers"
72 static int g_buffer_id = 0;
73 static int g_keyword_thread_id = 0;
74 
75 #define BUFFER_TX_STEP 4
76 #define BUFFER_SIZE_STEP 2048
78 
79 static uint8_t *GetBufferForTX(htp_tx_t *tx, uint64_t tx_id,
80  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
81  Flow *f, HtpState *htp_state, uint8_t flags,
82  uint32_t *buffer_len)
83 {
84  *buffer_len = 0;
85 
86  HttpHeaderThreadData *hdr_td = NULL;
88  tx_id, g_keyword_thread_id, &hdr_td);
89  if (unlikely(buf == NULL)) {
90  return NULL;
91  } else if (buf->len > 0) {
92  /* already filled buf, reuse */
93  *buffer_len = buf->len;
94  return buf->buffer;
95  }
96 
97  bstr *line = NULL;
98  htp_table_t *headers;
99  if (flags & STREAM_TOSERVER) {
100  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
101  HTP_REQUEST_HEADERS)
102  return NULL;
103  line = tx->request_line;
104  headers = tx->request_headers;
105  } else {
106  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
107  HTP_RESPONSE_HEADERS)
108  return NULL;
109  headers = tx->response_headers;
110  line = tx->response_line;
111  }
112  if (line == NULL || headers == NULL)
113  return NULL;
114 
115  size_t line_size = bstr_len(line) + 2;
116  if (line_size + buf->len > buf->size) {
117  if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
118  return NULL;
119  }
120  }
121  memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
122  buf->len += bstr_size(line);
123  buf->buffer[buf->len++] = '\r';
124  buf->buffer[buf->len++] = '\n';
125 
126  size_t i = 0;
127  size_t no_of_headers = htp_table_size(headers);
128  for (; i < no_of_headers; i++) {
129  htp_header_t *h = htp_table_get_index(headers, i, NULL);
130  size_t size1 = bstr_size(h->name);
131  size_t size2 = bstr_size(h->value);
132 #if 0
133  if (flags & STREAM_TOSERVER) {
134  if (size1 == 6 &&
135  SCMemcmpLowercase("cookie", bstr_ptr(h->name), 6) == 0) {
136  continue;
137  }
138  } else {
139  if (size1 == 10 &&
140  SCMemcmpLowercase("set-cookie", bstr_ptr(h->name), 10) == 0) {
141  continue;
142  }
143  }
144 #endif
145  size_t size = size1 + size2 + 4;
146  if (i + 1 == no_of_headers)
147  size += 2;
148  if (size + buf->len > buf->size) {
149  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
150  return NULL;
151  }
152  }
153 
154  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
155  buf->len += bstr_size(h->name);
156  buf->buffer[buf->len++] = ':';
157  buf->buffer[buf->len++] = ' ';
158  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
159  buf->len += bstr_size(h->value);
160  buf->buffer[buf->len++] = '\r';
161  buf->buffer[buf->len++] = '\n';
162  if (i + 1 == no_of_headers) {
163  buf->buffer[buf->len++] = '\r';
164  buf->buffer[buf->len++] = '\n';
165  }
166  }
167 
168  *buffer_len = buf->len;
169  return buf->buffer;
170 }
171 
172 /** \brief HTTP Start Mpm prefilter callback
173  *
174  * \param det_ctx detection engine thread ctx
175  * \param p packet to inspect
176  * \param f flow to inspect
177  * \param txv tx to inspect
178  * \param pectx inspection context
179  */
180 static void PrefilterTxHttpRequestStart(DetectEngineThreadCtx *det_ctx,
181  const void *pectx,
182  Packet *p, Flow *f, void *txv,
183  const uint64_t idx, const uint8_t flags)
184 {
185  SCEnter();
186 
187  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
188  htp_tx_t *tx = (htp_tx_t *)txv;
189 
190  HtpState *htp_state = f->alstate;
191  uint32_t buffer_len = 0;
192  const uint8_t *buffer = GetBufferForTX(tx, idx,
193  NULL, det_ctx, f, htp_state,
194  flags, &buffer_len);
195 
196  if (buffer_len >= mpm_ctx->minlen) {
197  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
198  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
199  }
200 }
201 
202 static int PrefilterTxHttpRequestStartRegister(DetectEngineCtx *de_ctx,
203  SigGroupHead *sgh, MpmCtx *mpm_ctx)
204 {
205  SCEnter();
206 
207  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpRequestStart,
208  ALPROTO_HTTP, HTP_REQUEST_HEADERS,
209  mpm_ctx, NULL, KEYWORD_NAME " (request)");
210  return r;
211 }
212 
213 /** \brief HTTP Start Mpm prefilter callback
214  *
215  * \param det_ctx detection engine thread ctx
216  * \param p packet to inspect
217  * \param f flow to inspect
218  * \param txv tx to inspect
219  * \param pectx inspection context
220  */
221 static void PrefilterTxHttpResponseStart(DetectEngineThreadCtx *det_ctx,
222  const void *pectx,
223  Packet *p, Flow *f, void *txv,
224  const uint64_t idx, const uint8_t flags)
225 {
226  SCEnter();
227 
228  const MpmCtx *mpm_ctx = (MpmCtx *)pectx;
229  htp_tx_t *tx = (htp_tx_t *)txv;
230 
231  HtpState *htp_state = f->alstate;
232  uint32_t buffer_len = 0;
233  const uint8_t *buffer = GetBufferForTX(tx, idx, NULL, det_ctx,
234  f, htp_state, flags, &buffer_len);
235 
236  if (buffer_len >= mpm_ctx->minlen) {
237  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
238  &det_ctx->mtcu, &det_ctx->pmq, buffer, buffer_len);
239  }
240 }
241 
242 static int PrefilterTxHttpResponseStartRegister(DetectEngineCtx *de_ctx,
243  SigGroupHead *sgh, MpmCtx *mpm_ctx)
244 {
245  SCEnter();
246 
247  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxHttpResponseStart,
248  ALPROTO_HTTP, HTP_RESPONSE_HEADERS,
249  mpm_ctx, NULL, KEYWORD_NAME " (response)");
250  return r;
251 }
252 
253 static int InspectEngineHttpStart(ThreadVars *tv,
254  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
255  const Signature *s, const SigMatchData *smd,
256  Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
257 {
258  HtpState *htp_state = (HtpState *)alstate;
259  uint32_t buffer_len = 0;
260  uint8_t *buffer = GetBufferForTX(tx, tx_id,
261  de_ctx, det_ctx, f, htp_state,
262  flags, &buffer_len);
263  if (buffer_len == 0)
264  goto end;
265 
266  det_ctx->buffer_offset = 0;
267  det_ctx->discontinue_matching = 0;
268  det_ctx->inspection_recursion_counter = 0;
269  int r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd,
270  f,
271  buffer, buffer_len,
274  if (r == 1)
276 
277  end:
278  if (flags & STREAM_TOSERVER) {
279  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) > HTP_REQUEST_HEADERS)
281  } else {
282  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) > HTP_RESPONSE_HEADERS)
284  }
286 }
287 
288 static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
289 {
290  s->init_data->list = g_buffer_id;
291  return 0;
292 }
293 
294 /**
295  * \brief Registers the keyword handlers for the "http_header" keyword.
296  */
298 {
302  sigmatch_table[DETECT_AL_HTTP_START].Setup = DetectHttpStartSetup;
303 
305 
307  PrefilterTxHttpRequestStartRegister);
309  PrefilterTxHttpResponseStartRegister);
310 
312  ALPROTO_HTTP, SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
313  InspectEngineHttpStart);
315  ALPROTO_HTTP, SIG_FLAG_TOCLIENT, HTP_RESPONSE_HEADERS,
316  InspectEngineHttpStart);
317 
319  BUFFER_DESC);
320 
321  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
322 
323  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
325 
326  SCLogDebug("keyword %s registered. Thread id %d. "
327  "Buffer %s registered. Buffer id %d",
328  KEYWORD_NAME, g_keyword_thread_id,
329  BUFFER_NAME, g_buffer_id);
330 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1406
SignatureInitData * init_data
Definition: detect.h:563
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1149
#define SCLogDebug(...)
Definition: util-debug.h:335
uint16_t minlen
Definition: util-mpm.h:99
uint16_t discontinue_matching
Definition: detect.h:1029
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
void HttpHeaderThreadDataFree(void *data)
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
#define unlikely(expr)
Definition: util-optimize.h:35
#define KEYWORD_NAME
Data needed for Match()
Definition: detect.h:331
Container for matching data for a signature group.
Definition: detect.h:1298
uint32_t buffer_offset
Definition: detect.h:994
const char * name
Definition: detect.h:1163
Signature container.
Definition: detect.h:495
main detection engine ctx
Definition: detect.h:723
void * alstate
Definition: flow.h:433
int DetectBufferTypeGetByName(const char *name)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
#define BUFFER_SIZE_STEP
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Data structures and function prototypes for keeping state for the detection engine.
uint8_t mpm_type
Definition: util-mpm.h:90
HttpHeaderBuffer * HttpHeaderGetBufferSpaceForTXID(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint64_t tx_id, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
#define SIG_FLAG_TOSERVER
Definition: detect.h:241
#define SCEnter(...)
Definition: util-debug.h:337
PrefilterRuleStore pmq
Definition: detect.h:1064
#define BUFFER_NAME
void * HttpHeaderThreadDataInit(void *data)
#define KEYWORD_DOC
const char * desc
Definition: detect.h:1165
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:169
uint16_t tx_id
MpmThreadCtx mtcu
Definition: detect.h:1062
#define BUFFER_TX_STEP
#define SIGMATCH_NOOPT
Definition: detect.h:1331
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:1166
#define STREAM_TOSERVER
Definition: stream.h:31
int inspection_recursion_counter
Definition: detect.h:1041
#define DETECT_CI_FLAGS_SINGLE
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
#define BUFFER_DESC
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_header" keyword.
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 DOC_VERSION
Definition: suricata.h:91
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
uint16_t flags
Definition: detect.h:1157
Flow data structure.
Definition: flow.h:324
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time