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"
41#include "detect-engine-mpm.h"
42#include "detect-engine-state.h"
45#include "detect-content.h"
46#include "detect-pcre.h"
49
50#include "flow.h"
51#include "flow-var.h"
52#include "flow-util.h"
53
54#include "util-debug.h"
55#include "util-unittest.h"
57#include "util-spm.h"
58#include "util-print.h"
59
60#include "app-layer.h"
61#include "app-layer-parser.h"
62
63#include "app-layer-htp.h"
64#include "detect-http-header.h"
65#include "stream-tcp.h"
66
67#define KEYWORD_NAME "http.header_names"
68#define KEYWORD_NAME_LEGACY "http_header_names"
69#define KEYWORD_DOC "http-keywords.html#http-header-names"
70#define BUFFER_NAME "http_header_names"
71#define BUFFER_DESC "http header names"
72static int g_buffer_id = 0;
73static int g_keyword_thread_id = 0;
74
75#define BUFFER_SIZE_STEP 256
76static HttpHeaderThreadDataConfig g_td_config = { BUFFER_SIZE_STEP };
77
78static uint8_t *GetBufferForTX(
79 htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint32_t *buffer_len)
80{
81 *buffer_len = 0;
82
83 HttpHeaderThreadData *hdr_td = NULL;
84 HttpHeaderBuffer *buf =
85 HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
86 if (unlikely(buf == NULL)) {
87 return NULL;
88 }
89
90 const htp_headers_t *headers;
91 if (flags & STREAM_TOSERVER) {
92 if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
93 HTP_REQUEST_PROGRESS_HEADERS)
94 return NULL;
95 headers = htp_tx_request_headers(tx);
96 } else {
97 if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
98 HTP_RESPONSE_PROGRESS_HEADERS)
99 return NULL;
100 headers = htp_tx_response_headers(tx);
101 }
102 if (headers == NULL)
103 return NULL;
104
105 /* fill the buffer. \r\nName1\r\nName2\r\n\r\n */
106 size_t i = 0;
107 size_t no_of_headers = htp_headers_size(headers);
108 for (; i < no_of_headers; i++) {
109 const htp_header_t *h = htp_headers_get_index(headers, i);
110 size_t size = htp_header_name_len(h) + 2; // for \r\n
111 if (i == 0)
112 size += 2;
113 if (i + 1 == no_of_headers)
114 size += 2;
115
116 SCLogDebug("size %"PRIuMAX" + buf->len %u vs buf->size %u",
117 (uintmax_t)size, buf->len, buf->size);
118 if (size + buf->len > buf->size) {
119 if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
120 return NULL;
121 }
122 }
123
124 /* start with a \r\n */
125 if (i == 0) {
126 buf->buffer[buf->len++] = '\r';
127 buf->buffer[buf->len++] = '\n';
128 }
129
130 memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
131 buf->len += htp_header_name_len(h);
132 buf->buffer[buf->len++] = '\r';
133 buf->buffer[buf->len++] = '\n';
134
135 /* end with an extra \r\n */
136 if (i + 1 == no_of_headers) {
137 buf->buffer[buf->len++] = '\r';
138 buf->buffer[buf->len++] = '\n';
139 }
140 }
141
142 *buffer_len = buf->len;
143 return buf->buffer;
144}
145
146static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
147 const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
148 const int list_id)
149{
150 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
151 if (buffer->inspect == NULL) {
152 uint32_t rawdata_len = 0;
153 uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flow_flags, &rawdata_len);
154 if (rawdata_len == 0)
155 return NULL;
156
158 det_ctx, list_id, buffer, rawdata, rawdata_len, transforms);
159 }
160
161 return buffer;
162}
163
164static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
165 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
166 const int list_id)
167{
168 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
169 if (buffer->inspect == NULL) {
170 uint32_t b_len = 0;
171 const uint8_t *b = NULL;
172
173 if (SCHttp2TxGetHeaderNames(txv, flow_flags, &b, &b_len) != 1)
174 return NULL;
175 if (b == NULL || b_len == 0)
176 return NULL;
177
178 InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
179 }
180
181 return buffer;
182}
183
184/**
185 * \brief The setup function for the http.header_names keyword for a signature.
186 *
187 * \param de_ctx Pointer to the detection engine context.
188 * \param s Pointer to signature for the current Signature being parsed
189 * from the rules.
190 * \param m Pointer to the head of the SigMatchs for the current rule
191 * being parsed.
192 * \param arg Pointer to the string holding the keyword value.
193 *
194 * \retval 0 On success.
195 * \retval -1 On failure.
196 */
197static int DetectHttpHeaderNamesSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
198{
199 if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
200 return -1;
201
203 return -1;
204
205 return 0;
206}
207
208/**
209 * \brief Registers the keyword handlers for the "http.header_names" keyword.
210 */
212{
217 sigmatch_table[DETECT_HTTP_HEADER_NAMES].Setup = DetectHttpHeaderNamesSetup;
218
220
221 /* http1 */
223 GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
225 GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
226
228 HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
230 HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
231
232 /* http2 */
234 GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
236 GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
237
239 HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
241 HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
242
245
247
250
251 SCLogDebug("keyword %s registered. Thread id %d. "
252 "Buffer %s registered. Buffer id %d",
253 KEYWORD_NAME, g_keyword_thread_id,
254 BUFFER_NAME, g_buffer_id);
255}
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
@ ALPROTO_HTTP2
@ ALPROTO_HTTP
@ ALPROTO_HTTP1
uint8_t flags
Definition decode-gre.h:0
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_HTTP_HEADER_NAMES
Data structures and function prototypes for keeping state for the detection engine.
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint8_t 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 DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
#define KEYWORD_NAME_LEGACY
#define KEYWORD_DOC
#define BUFFER_DESC
#define BUFFER_NAME
#define BUFFER_SIZE_STEP
#define KEYWORD_NAME
void DetectHttpHeaderNamesRegister(void)
Registers the keyword handlers for the "http.header_names" keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, size_t size)
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
void * HttpHeaderThreadDataInit(void *data)
void HttpHeaderThreadDataFree(void *data)
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define SCLogDebug(...)
Definition util-debug.h:275
#define unlikely(expr)