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 #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, Flow *f, uint8_t flags, uint32_t *buffer_len)
78 {
79  *buffer_len = 0;
80 
81  HttpHeaderThreadData *hdr_td = NULL;
82  HttpHeaderBuffer *buf =
83  HttpHeaderGetBufferSpace(det_ctx, f, flags, g_keyword_thread_id, &hdr_td);
84  if (unlikely(buf == NULL)) {
85  return NULL;
86  }
87 
88  bstr *line = NULL;
89  htp_table_t *headers;
90  if (flags & STREAM_TOSERVER) {
91  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
92  HTP_REQUEST_HEADERS)
93  return NULL;
94  line = tx->request_line;
95  headers = tx->request_headers;
96  } else {
97  if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
98  HTP_RESPONSE_HEADERS)
99  return NULL;
100  headers = tx->response_headers;
101  line = tx->response_line;
102  }
103  if (line == NULL || headers == NULL)
104  return NULL;
105 
106  size_t line_size = bstr_len(line) + 2;
107  if (line_size + buf->len > buf->size) {
108  if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
109  return NULL;
110  }
111  }
112  memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
113  buf->len += bstr_size(line);
114  buf->buffer[buf->len++] = '\r';
115  buf->buffer[buf->len++] = '\n';
116 
117  size_t i = 0;
118  size_t no_of_headers = htp_table_size(headers);
119  for (; i < no_of_headers; i++) {
120  htp_header_t *h = htp_table_get_index(headers, i, NULL);
121  size_t size1 = bstr_size(h->name);
122  size_t size2 = bstr_size(h->value);
123  size_t size = size1 + size2 + 4;
124  if (i + 1 == no_of_headers)
125  size += 2;
126  if (size + buf->len > buf->size) {
127  if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
128  return NULL;
129  }
130  }
131 
132  memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
133  buf->len += bstr_size(h->name);
134  buf->buffer[buf->len++] = ':';
135  buf->buffer[buf->len++] = ' ';
136  memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
137  buf->len += bstr_size(h->value);
138  buf->buffer[buf->len++] = '\r';
139  buf->buffer[buf->len++] = '\n';
140  if (i + 1 == no_of_headers) {
141  buf->buffer[buf->len++] = '\r';
142  buf->buffer[buf->len++] = '\n';
143  }
144  }
145 
146  *buffer_len = buf->len;
147  return buf->buffer;
148 }
149 
150 static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
151  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
152  const int list_id)
153 {
154  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
155  if (buffer->inspect == NULL) {
156  uint32_t rawdata_len = 0;
157  uint8_t *rawdata = GetBufferForTX(txv, det_ctx, f, flow_flags, &rawdata_len);
158  if (rawdata_len == 0)
159  return NULL;
160 
161  InspectionBufferSetup(det_ctx, list_id, buffer, rawdata, rawdata_len);
162  InspectionBufferApplyTransforms(buffer, transforms);
163  }
164 
165  return buffer;
166 }
167 
168 static int DetectHttpStartSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
169 {
170  if (DetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
171  return -1;
172 
174  return -1;
175 
176  return 0;
177 }
178 
179 /**
180  * \brief Registers the keyword handlers for the "http_start" keyword.
181  */
183 {
188  sigmatch_table[DETECT_AL_HTTP_START].Setup = DetectHttpStartSetup;
190 
192  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
194  GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_HEADERS);
195 
197  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
199  HTP_RESPONSE_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
200 
202  BUFFER_DESC);
203 
204  g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
205 
206  g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
208 
209  SCLogDebug("keyword %s registered. Thread id %d. "
210  "Buffer %s registered. Buffer id %d",
211  KEYWORD_NAME, g_keyword_thread_id,
212  BUFFER_NAME, g_buffer_id);
213 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1500
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
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:2122
KEYWORD_NAME_LEGACY
#define KEYWORD_NAME_LEGACY
Definition: detect-http-start.c:66
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1296
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:409
DetectHttpStartRegister
void DetectHttpStartRegister(void)
Registers the keyword handlers for the "http_start" keyword.
Definition: detect-http-start.c:182
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
BUFFER_DESC
#define BUFFER_DESC
Definition: detect-http-start.c:69
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
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:1086
InspectionBuffer
Definition: detect.h:374
threads.h
Flow_
Flow data structure.
Definition: flow.h:355
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
detect-http-header.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:267
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1479
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
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:745
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
app-layer-parser.h
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:1297
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
BUFFER_SIZE_STEP
#define BUFFER_SIZE_STEP
Definition: detect-http-start.c:73
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:136
KEYWORD_DOC
#define KEYWORD_DOC
Definition: detect-http-start.c:67
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
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:100
HttpHeaderExpandBuffer
int HttpHeaderExpandBuffer(HttpHeaderThreadData *td, HttpHeaderBuffer *buf, uint32_t size)
Definition: detect-http-header-common.c:81
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1169
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:74
DetectRegisterThreadCtxGlobalFuncs
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:3601
app-layer.h