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-mpm.h"
40 #include "detect-engine-state.h"
43 #include "detect-content.h"
44 #include "detect-pcre.h"
46 #include "detect-http-start.h"
47 
48 #include "flow.h"
49 #include "flow-var.h"
50 #include "flow-util.h"
51 
52 #include "util-debug.h"
53 #include "util-unittest.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 #include "util-print.h"
66 
67 #define KEYWORD_NAME "http.start"
68 #define KEYWORD_NAME_LEGACY "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_SIZE_STEP 2048
77 
78 static 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  bstr *line = NULL;
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  line = tx->request_line;
97  headers = tx->request_headers;
98  } else {
99  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
100  HTP_RESPONSE_HEADERS)
101  return NULL;
102  headers = tx->response_headers;
103  line = tx->response_line;
104  }
105  if (line == NULL || headers == NULL)
106  return NULL;
107 
108  size_t line_size = bstr_len(line) + 2;
109  if (line_size + buf->len > buf->size) {
110  if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
111  return NULL;
112  }
113  }
114  memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
115  buf->len += bstr_size(line);
116  buf->buffer[buf->len++] = '\r';
117  buf->buffer[buf->len++] = '\n';
118 
119  size_t i = 0;
120  size_t no_of_headers = htp_table_size(headers);
121  for (; i < no_of_headers; i++) {
122  htp_header_t *h = htp_table_get_index(headers, i, NULL);
123  size_t size1 = bstr_size(h->name);
124  size_t size2 = bstr_size(h->value);
125  size_t size = size1 + size2 + 4;
126  if (i + 1 == no_of_headers)
127  size += 2;
128  if (size + buf->len > buf->size) {
129  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
130  return NULL;
131  }
132  }
133 
134  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
135  buf->len += bstr_size(h->name);
136  buf->buffer[buf->len++] = ':';
137  buf->buffer[buf->len++] = ' ';
138  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
139  buf->len += bstr_size(h->value);
140  buf->buffer[buf->len++] = '\r';
141  buf->buffer[buf->len++] = '\n';
142  if (i + 1 == no_of_headers) {
143  buf->buffer[buf->len++] = '\r';
144  buf->buffer[buf->len++] = '\n';
145  }
146  }
147 
148  *buffer_len = buf->len;
149  return buf->buffer;
150 }
151 
152 static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
153  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
154  const int list_id)
155 {
156  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
157  if (buffer->inspect == NULL) {
158  uint32_t rawdata_len = 0;
159  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flow_flags, &rawdata_len);
160  if (rawdata_len == 0)
161  return NULL;
162 
163  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
164  InspectionBufferApplyTransforms(buffer, transforms);
165  }
166 
167  return buffer;
168 }
169 
170 static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
171 {
172  if (DetectBufferSetActiveList(s, g_buffer_id) < 0)
173  return -1;
174 
176  return -1;
177 
178  return 0;
179 }
180 
181 /**
182  * \brief Registers the keyword handlers for the "http_start" keyword.
183  */
185 {
190  sigmatch_table[DETECT_AL_HTTP_START].Setup = DetectHttpStartSetup;
192 
194  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
196  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_HEADERS);
197 
199  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
201  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
202 
204  BUFFER_DESC);
205 
206  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
207 
208  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
210 
211  SCLogDebug("keyword %s registered. Thread id %d. "
212  "Buffer %s registered. Buffer id %d",
213  KEYWORD_NAME, g_keyword_thread_id,
214  BUFFER_NAME, g_buffer_id);
215 }
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-start.c:68
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1201
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:378
DetectHttpStartRegister
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_start" keyword.
Definition: detect-http-start.c:184
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-start.c:71
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:1041
InspectionBuffer
Definition: detect.h:344
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:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
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:1186
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
util-print.h
detect-engine-mpm.h
detect.h
detect-http-start.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:1617
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:174
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:70
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
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-http-start.c:67
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1202
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-start.c:75
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
DETECT_AL_HTTP_START
@ DETECT_AL_HTTP_START
Definition: detect-engine-register.h:123
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-start.c:69
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
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:1091
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
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
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:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
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:3119
app-layer.h