suricata
detect-http-host.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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 Anoop Saldanha <anoopsaldanha@gmail.com>
29  *
30  * Implements support for the http_host keyword.
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"
40 #include "detect-engine-mpm.h"
42 #include "detect-content.h"
43 #include "detect-pcre.h"
44 
45 #include "flow.h"
46 #include "flow-var.h"
47 #include "flow-util.h"
48 
49 #include "util-debug.h"
50 #include "util-unittest.h"
51 #include "util-unittest-helper.h"
52 #include "util-spm.h"
53 
54 #include "app-layer.h"
55 #include "app-layer-parser.h"
56 
57 #include "app-layer-htp.h"
58 #include "stream-tcp.h"
59 #include "detect-http-host.h"
60 
61 static int DetectHttpHHSetup(DetectEngineCtx *, Signature *, const char *);
62 #ifdef UNITTESTS
63 static void DetectHttpHHRegisterTests(void);
64 #endif
65 static bool DetectHttpHostValidateCallback(const Signature *s, const char **sigerror);
66 static int DetectHttpHostSetup(DetectEngineCtx *, Signature *, const char *);
67 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms,
69  Flow *_f, const uint8_t _flow_flags,
70  void *txv, const int list_id);
71 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
72  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
73  const int list_id);
74 static int DetectHttpHRHSetup(DetectEngineCtx *, Signature *, const char *);
75 static int g_http_raw_host_buffer_id = 0;
76 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
77 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
78  const DetectEngineTransforms *transforms, Flow *_f,
79  const uint8_t _flow_flags, void *txv, const int list_id);
80 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
81  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
82  const int list_id);
83 static int g_http_host_buffer_id = 0;
84 
85 /**
86  * \brief Registers the keyword handlers for the "http_host" keyword.
87  */
89 {
90  /* http_host content modifier */
92  sigmatch_table[DETECT_HTTP_HOST_CM].desc = "content modifier to match on the HTTP hostname";
94  "/rules/http-keywords.html#http-host-and-http-raw-host";
95  sigmatch_table[DETECT_HTTP_HOST_CM].Setup = DetectHttpHHSetup;
96 #ifdef UNITTESTS
98 #endif
101 
102  /* http.host sticky buffer */
103  sigmatch_table[DETECT_HTTP_HOST].name = "http.host";
104  sigmatch_table[DETECT_HTTP_HOST].desc = "sticky buffer to match on the HTTP Host buffer";
105  sigmatch_table[DETECT_HTTP_HOST].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
106  sigmatch_table[DETECT_HTTP_HOST].Setup = DetectHttpHostSetup;
108 
111 
114 
116  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
117 
119  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
120 
122  DetectHttpHostValidateCallback);
123 
125  "http host");
126 
127  g_http_host_buffer_id = DetectBufferTypeGetByName("http_host");
128 
129  /* http_raw_host content modifier */
130  sigmatch_table[DETECT_HTTP_RAW_HOST].name = "http_raw_host";
131  sigmatch_table[DETECT_HTTP_RAW_HOST].desc = "content modifier to match on the HTTP host header "
132  "or the raw hostname from the HTTP uri";
134  "/rules/http-keywords.html#http-host-and-http-raw-host";
135  sigmatch_table[DETECT_HTTP_RAW_HOST].Setup = DetectHttpHRHSetup;
138 
139  /* http.host sticky buffer */
140  sigmatch_table[DETECT_HTTP_HOST_RAW].name = "http.host.raw";
141  sigmatch_table[DETECT_HTTP_HOST_RAW].desc = "sticky buffer to match on the HTTP host header or the raw hostname from the HTTP uri";
142  sigmatch_table[DETECT_HTTP_HOST_RAW].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
143  sigmatch_table[DETECT_HTTP_HOST_RAW].Setup = DetectHttpHostRawSetupSticky;
145 
148 
151 
153  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRawData2);
154 
156  GetRawData2, ALPROTO_HTTP2, HTTP2StateDataClient);
157 
158  DetectBufferTypeSetDescriptionByName("http_raw_host",
159  "http raw host header");
160 
161  g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host");
162 }
163 
164 /**
165  * \brief The setup function for the http_host keyword for a signature.
166  *
167  * \param de_ctx Pointer to the detection engine context.
168  * \param s Pointer to the signature for the current Signature being
169  * parsed from the rules.
170  * \param m Pointer to the head of the SigMatch for the current rule
171  * being parsed.
172  * \param arg Pointer to the string holding the keyword value.
173  *
174  * \retval 0 On success
175  * \retval -1 On failure
176  */
177 static int DetectHttpHHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
178 {
180  de_ctx, s, arg, DETECT_HTTP_HOST_CM, g_http_host_buffer_id, ALPROTO_HTTP1);
181 }
182 
183 static bool DetectHttpHostValidateCallback(const Signature *s, const char **sigerror)
184 {
185  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
186  if (s->init_data->buffers[x].id != (uint32_t)g_http_host_buffer_id)
187  continue;
188  const SigMatch *sm = s->init_data->buffers[x].head;
189  for (; sm != NULL; sm = sm->next) {
190  if (sm->type == DETECT_CONTENT) {
192  if (cd->flags & DETECT_CONTENT_NOCASE) {
193  *sigerror = "http.host keyword "
194  "specified along with \"nocase\". "
195  "The hostname buffer is normalized "
196  "to lowercase, specifying "
197  "nocase is redundant.";
198  SCLogWarning("rule %u: %s", s->id, *sigerror);
199  return false;
200  } else {
201  uint32_t u;
202  for (u = 0; u < cd->content_len; u++) {
203  if (isupper(cd->content[u]))
204  break;
205  }
206  if (u != cd->content_len) {
207  *sigerror = "A pattern with "
208  "uppercase characters detected for http.host. "
209  "The hostname buffer is normalized to lowercase, "
210  "please specify a lowercase pattern.";
211  SCLogWarning("rule %u: %s", s->id, *sigerror);
212  return false;
213  }
214  }
215  }
216  }
217  }
218 
219  return true;
220 }
221 
222 /**
223  * \brief this function setup the http.host keyword used in the rule
224  *
225  * \param de_ctx Pointer to the Detection Engine Context
226  * \param s Pointer to the Signature to which the current keyword belongs
227  * \param str Should hold an empty string always
228  *
229  * \retval 0 On success
230  */
231 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
232 {
233  if (DetectBufferSetActiveList(de_ctx, s, g_http_host_buffer_id) < 0)
234  return -1;
236  return -1;
237  return 0;
238 }
239 
240 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
241  const DetectEngineTransforms *transforms, Flow *_f,
242  const uint8_t _flow_flags, void *txv, const int list_id)
243 {
244  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
245  if (buffer->inspect == NULL) {
246  htp_tx_t *tx = (htp_tx_t *)txv;
247 
248  if (htp_tx_request_hostname(tx) == NULL)
249  return NULL;
250 
251  const uint32_t data_len = bstr_len(htp_tx_request_hostname(tx));
252  const uint8_t *data = bstr_ptr(htp_tx_request_hostname(tx));
253 
254  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
255  InspectionBufferApplyTransforms(buffer, transforms);
256  }
257 
258  return buffer;
259 }
260 
261 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
262  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
263  const int list_id)
264 {
265  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
266  if (buffer->inspect == NULL) {
267  uint32_t b_len = 0;
268  const uint8_t *b = NULL;
269 
270  if (rs_http2_tx_get_host_norm(txv, &b, &b_len) != 1)
271  return NULL;
272  if (b == NULL || b_len == 0)
273  return NULL;
274 
275  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
276  InspectionBufferApplyTransforms(buffer, transforms);
277  }
278 
279  return buffer;
280 }
281 
282 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
283  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
284  const int list_id)
285 {
286  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
287  if (buffer->inspect == NULL) {
288  uint32_t b_len = 0;
289  const uint8_t *b = NULL;
290 
291  if (rs_http2_tx_get_host(txv, &b, &b_len) != 1)
292  return NULL;
293  if (b == NULL || b_len == 0)
294  return NULL;
295 
296  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
297  InspectionBufferApplyTransforms(buffer, transforms);
298  }
299 
300  return buffer;
301 }
302 
303 /**
304  * \brief The setup function for the http_raw_host keyword for a signature.
305  *
306  * \param de_ctx Pointer to the detection engine context.
307  * \param s Pointer to the signature for the current Signature being
308  * parsed from the rules.
309  * \param m Pointer to the head of the SigMatch for the current rule
310  * being parsed.
311  * \param arg Pointer to the string holding the keyword value.
312  *
313  * \retval 0 On success
314  * \retval -1 On failure
315  */
316 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
317 {
319  de_ctx, s, arg, DETECT_HTTP_RAW_HOST, g_http_raw_host_buffer_id, ALPROTO_HTTP1);
320 }
321 
322 /**
323  * \brief this function setup the http.host keyword used in the rule
324  *
325  * \param de_ctx Pointer to the Detection Engine Context
326  * \param s Pointer to the Signature to which the current keyword belongs
327  * \param str Should hold an empty string always
328  *
329  * \retval 0 On success
330  */
331 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
332 {
333  if (DetectBufferSetActiveList(de_ctx, s, g_http_raw_host_buffer_id) < 0)
334  return -1;
336  return -1;
337  return 0;
338 }
339 
340 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
341  const DetectEngineTransforms *transforms, Flow *_f,
342  const uint8_t _flow_flags, void *txv, const int list_id)
343 {
344  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
345  if (buffer->inspect == NULL) {
346  htp_tx_t *tx = (htp_tx_t *)txv;
347 
348  const uint8_t *data = NULL;
349  uint32_t data_len = 0;
350 
351  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
352  if (htp_tx_request_headers(tx) == NULL)
353  return NULL;
354 
355  const htp_header_t *h = htp_tx_request_header(tx, "Host");
356  if (h == NULL || htp_header_value(h) == NULL)
357  return NULL;
358 
359  data = (const uint8_t *)htp_header_value_ptr(h);
360  data_len = htp_header_value_len(h);
361  } else {
362  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
363  data_len = bstr_len(tx->parsed_uri->hostname);
364  }
365 
366  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
367  InspectionBufferApplyTransforms(buffer, transforms);
368  }
369 
370  return buffer;
371 }
372 
373 /************************************Unittests*********************************/
374 
375 #ifdef UNITTESTS
376 #include "tests/detect-http-host.c"
377 #endif /* UNITTESTS */
378 
379 /**
380  * @}
381  */
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
htp_header_value_len
#define htp_header_value_len(h)
Definition: app-layer-htp-libhtp.h:191
DetectHttpHHRegister
void DetectHttpHHRegister(void)
Registers the keyword handlers for the "http_host" keyword.
Definition: detect-http-host.c:88
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:539
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1527
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2157
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1525
DETECT_HTTP_HOST
@ DETECT_HTTP_HOST
Definition: detect-engine-register.h:186
SigTableElmt_::name
const char * name
Definition: detect.h:1319
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:410
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:72
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1359
InspectionBuffer
Definition: detect.h:375
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
DETECT_HTTP_RAW_HOST
@ DETECT_HTTP_RAW_HOST
Definition: detect-engine-register.h:187
DetectContentData_
Definition: detect-content.h:93
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
detect-pcre.h
detect-http-host.c
Handle HTTP host header. HHHD - Http Host Header Data.
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1503
util-unittest-helper.h
htp_header_value
#define htp_header_value(h)
Definition: app-layer-htp-libhtp.h:193
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
htp_tx_request_headers
#define htp_tx_request_headers(tx)
Definition: app-layer-htp-libhtp.h:173
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
app-layer-htp.h
decode.h
util-debug.h
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror))
Definition: detect-engine.c:1308
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
DetectHttpHHRegisterTests
void DetectHttpHHRegisterTests(void)
Definition: detect-http-host.c:2544
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:355
DETECT_HTTP_HOST_RAW
@ DETECT_HTTP_HOST_RAW
Definition: detect-engine-register.h:188
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1317
DetectAppLayerMpmRegister
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
Definition: detect-engine-mpm.c:151
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:354
htp_tx_request_hostname
#define htp_tx_request_hostname(tx)
Definition: app-layer-htp-libhtp.h:163
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:220
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
htp_header_value_ptr
#define htp_header_value_ptr(h)
Definition: app-layer-htp-libhtp.h:192
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:683
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
detect-http-host.h
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:352
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1714
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
HTP_REQUEST_PROGRESS_HEADERS
#define HTP_REQUEST_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:89
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:596
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
DETECT_HTTP_HOST_CM
@ DETECT_HTTP_HOST_CM
Definition: detect-engine-register.h:185
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:1598
Signature_::id
uint32_t id
Definition: detect.h:649
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:532
Signature_
Signature container.
Definition: detect.h:614
SigMatch_
a single match condition for a signature
Definition: detect.h:351
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:242
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:597
flow-var.h
htp_tx_request_header
#define htp_tx_request_header(tx, header)
Definition: app-layer-htp-libhtp.h:184
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1311
app-layer.h