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"
39 #include "detect-engine-buffer.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-helper.h"
55 #include "util-spm.h"
56 #include "util-print.h"
57 
58 #include "app-layer.h"
59 #include "app-layer-parser.h"
60 
61 #include "app-layer-htp.h"
62 #include "detect-http-header.h"
63 #include "stream-tcp.h"
64 
65 #define KEYWORD_NAME "http.start"
66 #define KEYWORD_NAME_LEGACY "http_start"
67 #define KEYWORD_DOC "http-keywords.html#http-start"
68 #define BUFFER_NAME "http_start"
69 #define BUFFER_DESC "http start: request/response line + headers"
70 static int g_buffer_id = 0;
71 static int g_keyword_thread_id = 0;
72 
73 #define BUFFER_SIZE_STEP 2048
75 
76 static uint8_t *GetBufferForTX(
77  htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, uint8_t flags, uint32_t *buffer_len)
78 {
79  *buffer_len = 0;
80 
81  HttpHeaderThreadData *hdr_td = NULL;
82  HttpHeaderBuffer *buf = HttpHeaderGetBufferSpace(det_ctx, flags, g_keyword_thread_id, &hdr_td);
83  if (unlikely(buf == NULL)) {
84  return NULL;
85  }
86 
87  const bstr *line = NULL;
88  const htp_headers_t *headers;
89  if (flags & STREAM_TOSERVER) {
90  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
91  HTP_REQUEST_PROGRESS_HEADERS)
92  return NULL;
93  line = htp_tx_request_line(tx);
94  headers = htp_tx_request_headers(tx);
95  } else {
96  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
97  HTP_RESPONSE_PROGRESS_HEADERS)
98  return NULL;
99  headers = htp_tx_response_headers(tx);
100  line = htp_tx_response_line(tx);
101  }
102  if (line == NULL || headers == NULL)
103  return NULL;
104 
105  size_t line_size = bstr_len(line) + 2;
106  if (line_size + buf->len > buf->size) {
107  if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
108  return NULL;
109  }
110  }
111  memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
112  buf->len += bstr_size(line);
113  buf->buffer[buf->len++] = '\r';
114  buf->buffer[buf->len++] = '\n';
115 
116  size_t i = 0;
117  size_t no_of_headers = htp_headers_size(headers);
118  for (; i < no_of_headers; i++) {
119  const htp_header_t *h = htp_headers_get_index(headers, i);
120  size_t size1 = htp_header_name_len(h);
121  size_t size2 = htp_header_value_len(h);
122  size_t size = size1 + size2 + 4;
123  if (i + 1 == no_of_headers)
124  size += 2;
125  if (size + buf->len > buf->size) {
126  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
127  return NULL;
128  }
129  }
130 
131  memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
132  buf->len += htp_header_name_len(h);
133  buf->buffer[buf->len++] = ':';
134  buf->buffer[buf->len++] = ' ';
135  memcpy(buf->buffer + buf->len, htp_header_value_ptr(h), htp_header_value_len(h));
136  buf->len += htp_header_value_len(h);
137  buf->buffer[buf->len++] = '\r';
138  buf->buffer[buf->len++] = '\n';
139  if (i + 1 == no_of_headers) {
140  buf->buffer[buf->len++] = '\r';
141  buf->buffer[buf->len++] = '\n';
142  }
143  }
144 
145  *buffer_len = buf->len;
146  return buf->buffer;
147 }
148 
149 static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
150  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
151  const int list_id)
152 {
153  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
154  if (buffer->inspect == NULL) {
155  uint32_t rawdata_len = 0;
156  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, flow_flags, &rawdata_len);
157  if (rawdata_len == 0)
158  return NULL;
159 
161  det_ctx, list_id, buffer, rawdata, rawdata_len, transforms);
162  }
163 
164  return buffer;
165 }
166 
167 static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
168 {
169  if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
170  return -1;
171 
173  return -1;
174 
175  return 0;
176 }
177 
178 /**
179  * \brief Registers the keyword handlers for the "http_start" keyword.
180  */
182 {
185  sigmatch_table[DETECT_HTTP_START].desc = BUFFER_NAME " sticky buffer";
187  sigmatch_table[DETECT_HTTP_START].Setup = DetectHttpStartSetup;
189 
191  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
193  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
194 
196  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
198  HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
199 
201  BUFFER_DESC);
202 
203  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
204 
205  g_keyword_thread_id = SCDetectRegisterThreadCtxGlobalFuncs(
207 
208  SCLogDebug("keyword %s registered. Thread id %d. "
209  "Buffer %s registered. Buffer id %d",
210  KEYWORD_NAME, g_keyword_thread_id,
211  BUFFER_NAME, g_buffer_id);
212 }
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:308
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
DetectEngineInspectBufferGeneric
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.
Definition: detect-engine.c:2267
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-start.c:66
flow-util.h
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1542
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:395
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
DetectHttpStartRegister
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_start" keyword.
Definition: detect-http-start.c:181
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-start.c:69
HttpHeaderBuffer_::len
uint32_t len
Definition: detect-http-header-common.h:30
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:359
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(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
Definition: detect-engine-inspect-buffer.c:197
HttpHeaderGetBufferSpace
HttpHeaderBuffer * HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, uint8_t flags, const int keyword_id, HttpHeaderThreadData **ret_hdr_td)
Definition: detect-http-header-common.c:99
detect-http-header.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2613
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:275
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, size_t size)
Definition: detect-http-header-common.c:81
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
detect-pcre.h
detect-engine-prefilter.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:275
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1452
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:274
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
util-print.h
detect-engine-mpm.h
detect.h
detect-http-start.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1707
app-layer-parser.h
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *tx, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1199
DETECT_HTTP_START
@ DETECT_HTTP_START
Definition: detect-engine-register.h:166
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
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-http-start.c:68
detect-engine-content-inspection.h
HttpHeaderThreadDataInit
void * HttpHeaderThreadDataInit(void *data)
Definition: detect-http-header-common.c:57
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-http-start.c:65
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1543
suricata-common.h
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-start.c:73
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
HttpHeaderBuffer_::size
uint32_t size
Definition: detect-http-header-common.h:29
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-start.c:67
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:330
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1549
HttpHeaderBuffer_
Definition: detect-http-header-common.h:27
flow.h
SCDetectRegisterThreadCtxGlobalFuncs
int SCDetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:4053
flow-var.h
HttpHeaderThreadData_
Definition: detect-http-header-common.h:37
HttpHeaderThreadDataFree
void HttpHeaderThreadDataFree(void *data)
Definition: detect-http-header-common.c:74
app-layer.h
f
Flow f
Definition: fuzz_dataset.c:32