suricata
detect-http-start.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 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 * \file
26 *
27 * \author Victor Julien <victor@inliniac.net>
28 *
29 * Implements http_start
30 */
31
32#include "suricata-common.h"
33#include "threads.h"
34#include "decode.h"
35
36#include "detect.h"
37#include "detect-parse.h"
38#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"
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#define KEYWORD_NAME "http.start"
67#define KEYWORD_NAME_LEGACY "http_start"
68#define KEYWORD_DOC "http-keywords.html#http-start"
69#define BUFFER_NAME "http_start"
70#define BUFFER_DESC "http start: request/response line + headers"
71static int g_buffer_id = 0;
72static int g_keyword_thread_id = 0;
73
74#define BUFFER_SIZE_STEP 2048
75static HttpHeaderThreadDataConfig g_td_config = { BUFFER_SIZE_STEP };
76
77static uint8_t *GetBufferForTX(
78 htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, uint32_t *buffer_len)
79{
80 *buffer_len = 0;
81
82 HttpHeaderThreadData *hdr_td = NULL;
83 HttpHeaderBuffer *buf =
84 HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
85 if (unlikely(buf == NULL)) {
86 return NULL;
87 }
88
89 const bstr *line = NULL;
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 line = htp_tx_request_line(tx);
96 headers = htp_tx_request_headers(tx);
97 } else {
98 if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
99 HTP_RESPONSE_PROGRESS_HEADERS)
100 return NULL;
101 headers = htp_tx_response_headers(tx);
102 line = htp_tx_response_line(tx);
103 }
104 if (line == NULL || headers == NULL)
105 return NULL;
106
107 size_t line_size = bstr_len(line) + 2;
108 if (line_size + buf->len > buf->size) {
109 if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
110 return NULL;
111 }
112 }
113 memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
114 buf->len += bstr_size(line);
115 buf->buffer[buf->len++] = '\r';
116 buf->buffer[buf->len++] = '\n';
117
118 size_t i = 0;
119 size_t no_of_headers = htp_headers_size(headers);
120 for (; i < no_of_headers; i++) {
121 const htp_header_t *h = htp_headers_get_index(headers, i);
122 size_t size1 = htp_header_name_len(h);
123 size_t size2 = htp_header_value_len(h);
124 size_t size = size1 + size2 + 4;
125 if (i + 1 == no_of_headers)
126 size += 2;
127 if (size + buf->len > buf->size) {
128 if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
129 return NULL;
130 }
131 }
132
133 memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
134 buf->len += htp_header_name_len(h);
135 buf->buffer[buf->len++] = ':';
136 buf->buffer[buf->len++] = ' ';
137 memcpy(buf->buffer + buf->len, htp_header_value_ptr(h), htp_header_value_len(h));
138 buf->len += htp_header_value_len(h);
139 buf->buffer[buf->len++] = '\r';
140 buf->buffer[buf->len++] = '\n';
141 if (i + 1 == no_of_headers) {
142 buf->buffer[buf->len++] = '\r';
143 buf->buffer[buf->len++] = '\n';
144 }
145 }
146
147 *buffer_len = buf->len;
148 return buf->buffer;
149}
150
151static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
152 const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
153 const int list_id)
154{
155 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
156 if (buffer->inspect == NULL) {
157 uint32_t rawdata_len = 0;
158 uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flow_flags, &rawdata_len);
159 if (rawdata_len == 0)
160 return NULL;
161
163 det_ctx, list_id, buffer, rawdata, rawdata_len, transforms);
164 }
165
166 return buffer;
167}
168
169static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
170{
171 if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
172 return -1;
173
175 return -1;
176
177 return 0;
178}
179
180/**
181 * \brief Registers the keyword handlers for the "http_start" keyword.
182 */
184{
189 sigmatch_table[DETECT_HTTP_START].Setup = DetectHttpStartSetup;
191
193 GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
195 GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
196
198 HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
200 HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
201
204
206
209
210 SCLogDebug("keyword %s registered. Thread id %d. "
211 "Buffer %s registered. Buffer id %d",
212 KEYWORD_NAME, g_keyword_thread_id,
213 BUFFER_NAME, g_buffer_id);
214}
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
@ 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_START
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
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_start" keyword.
#define BUFFER_NAME
#define BUFFER_SIZE_STEP
#define KEYWORD_NAME
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)