suricata
detect-http-header-names.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 support http_header_names
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"
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.header_names"
69 #define KEYWORD_NAME_LEGACY "http_header_names"
70 #define KEYWORD_DOC "http-keywords.html#http-header-names"
71 #define BUFFER_NAME "http_header_names"
72 #define BUFFER_DESC "http header names"
73 static int g_buffer_id = 0;
74 static int g_keyword_thread_id = 0;
75 
76 #define BUFFER_SIZE_STEP 256
78 
79 static uint8_t *GetBufferForTX(
80  htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint32_t *buffer_len)
81 {
82  *buffer_len = 0;
83 
84  HttpHeaderThreadData *hdr_td = NULL;
85  HttpHeaderBuffer *buf =
86  HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
87  if (unlikely(buf == NULL)) {
88  return NULL;
89  }
90 
91  htp_table_t *headers;
92  if (flags & STREAM_TOSERVER) {
93  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
94  HTP_REQUEST_HEADERS)
95  return NULL;
96  headers = tx->request_headers;
97  } else {
98  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
99  HTP_RESPONSE_HEADERS)
100  return NULL;
101  headers = tx->response_headers;
102  }
103  if (headers == NULL)
104  return NULL;
105 
106  /* fill the buffer. \r\nName1\r\nName2\r\n\r\n */
107  size_t i = 0;
108  size_t no_of_headers = htp_table_size(headers);
109  for (; i < no_of_headers; i++) {
110  htp_header_t *h = htp_table_get_index(headers, i, NULL);
111  size_t size = bstr_size(h->name) + 2; // for \r\n
112  if (i == 0)
113  size += 2;
114  if (i + 1 == no_of_headers)
115  size += 2;
116 
117  SCLogDebug("size %"PRIuMAX" + buf->len %u vs buf->size %u",
118  (uintmax_t)size, buf->len, buf->size);
119  if (size + buf->len > buf->size) {
120  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
121  return NULL;
122  }
123  }
124 
125  /* start with a \r\n */
126  if (i == 0) {
127  buf->buffer[buf->len++] = '\r';
128  buf->buffer[buf->len++] = '\n';
129  }
130 
131  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
132  buf->len += bstr_size(h->name);
133  buf->buffer[buf->len++] = '\r';
134  buf->buffer[buf->len++] = '\n';
135 
136  /* end with an extra \r\n */
137  if (i + 1 == no_of_headers) {
138  buf->buffer[buf->len++] = '\r';
139  buf->buffer[buf->len++] = '\n';
140  }
141  }
142 
143  *buffer_len = buf->len;
144  return buf->buffer;
145 }
146 
147 static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
148  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
149  const int list_id)
150 {
151  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
152  if (buffer->inspect == NULL) {
153  uint32_t rawdata_len = 0;
154  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flow_flags, &rawdata_len);
155  if (rawdata_len == 0)
156  return NULL;
157 
158  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
159  InspectionBufferApplyTransforms(buffer, transforms);
160  }
161 
162  return buffer;
163 }
164 
165 static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
166  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
167  const int list_id)
168 {
169  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
170  if (buffer->inspect == NULL) {
171  uint32_t b_len = 0;
172  const uint8_t *b = NULL;
173 
174  if (rs_http2_tx_get_header_names(txv, flow_flags, &b, &b_len) != 1)
175  return NULL;
176  if (b == NULL || b_len == 0)
177  return NULL;
178 
179  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
180  InspectionBufferApplyTransforms(buffer, transforms);
181  }
182 
183  return buffer;
184 }
185 
186 /**
187  * \brief The setup function for the http.header_names keyword for a signature.
188  *
189  * \param de_ctx Pointer to the detection engine context.
190  * \param s Pointer to signature for the current Signature being parsed
191  * from the rules.
192  * \param m Pointer to the head of the SigMatchs for the current rule
193  * being parsed.
194  * \param arg Pointer to the string holding the keyword value.
195  *
196  * \retval 0 On success.
197  * \retval -1 On failure.
198  */
199 static int DetectHttpHeaderNamesSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
200 {
201  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
202  return -1;
203 
205  return -1;
206 
207  return 0;
208 }
209 
210 /**
211  * \brief Registers the keyword handlers for the "http.header_names" keyword.
212  */
214 {
219  sigmatch_table[DETECT_AL_HTTP_HEADER_NAMES].Setup = DetectHttpHeaderNamesSetup;
220 
222 
223  /* http1 */
225  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
227  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_HEADERS);
228 
230  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
232  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
233 
234  /* http2 */
236  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
238  GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
239 
241  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
243  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
244 
246  BUFFER_DESC);
247 
248  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
249 
250  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
252 
253  SCLogDebug("keyword %s registered. Thread id %d. "
254  "Buffer %s registered. Buffer id %d",
255  KEYWORD_NAME, g_keyword_thread_id,
256  BUFFER_NAME, g_buffer_id);
257 }
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-header-names.c:76
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:379
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-header-names.c:69
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1095
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:753
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
detect-http-header.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
detect-pcre.h
DetectHttpHeaderNamesRegister
void DetectHttpHeaderNamesRegister(void)
Registers the keyword handlers for the "http.header_names" keyword.
Definition: detect-http-header-names.c:213
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1360
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
app-layer-htp.h
decode.h
util-debug.h
detect-http-header-names.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
util-print.h
detect-engine-mpm.h
detect.h
app-layer-parser.h
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:1983
detect-http-header-common.h
HttpHeaderThreadConfig_
Definition: detect-http-header-common.h:33
HttpHeaderBuffer_::buffer
uint8_t * buffer
Definition: detect-http-header-common.h:28
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:225
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-header-names.c:72
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:62
detect-engine-content-inspection.h
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:59
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
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-http-header-names.c:71
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1268
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1543
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
HttpHeaderBuffer_::size
uint32_t size
Definition: detect-http-header-common.h:29
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1441
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-header-names.c:70
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DETECT_AL_HTTP_HEADER_NAMES
@ DETECT_AL_HTTP_HEADER_NAMES
Definition: detect-engine-register.h:133
HttpHeaderGetBufferSpace
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
Definition: detect-http-header-common.c:102
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-http-header-names.c:68
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
Definition: detect-http-header-common.c:83
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
HttpHeaderBuffer_
Definition: detect-http-header-common.h:27
flow.h
flow-var.h
HttpHeaderThreadData_
Definition: detect-http-header-common.h:37
HttpHeaderThreadDataFree
void HttpHeaderThreadDataFree(void *data)
Definition: detect-http-header-common.c:76
DetectRegisterThreadCtxGlobalFuncs
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:3508
app-layer.h