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-buffer.h"
41 #include "detect-engine-mpm.h"
43 #include "detect-content.h"
44 #include "detect-pcre.h"
45 
46 #include "flow.h"
47 #include "flow-var.h"
48 #include "flow-util.h"
49 
50 #include "util-debug.h"
51 #include "util-unittest.h"
52 #include "util-unittest-helper.h"
53 #include "util-spm.h"
54 
55 #include "app-layer.h"
56 #include "app-layer-parser.h"
57 
58 #include "app-layer-htp.h"
59 #include "stream-tcp.h"
60 #include "detect-http-host.h"
61 
62 static int DetectHttpHHSetup(DetectEngineCtx *, Signature *, const char *);
63 #ifdef UNITTESTS
64 static void DetectHttpHHRegisterTests(void);
65 #endif
66 static bool DetectHttpHostValidateCallback(
67  const Signature *s, const char **sigerror, const DetectBufferType *dbt);
68 static int DetectHttpHostSetup(DetectEngineCtx *, Signature *, const char *);
69 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
70  const DetectEngineTransforms *transforms,
71  Flow *_f, const uint8_t _flow_flags,
72  void *txv, const int list_id);
73 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
74  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
75  const int list_id);
76 static int DetectHttpHRHSetup(DetectEngineCtx *, Signature *, const char *);
77 static int g_http_raw_host_buffer_id = 0;
78 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
79 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
80  const DetectEngineTransforms *transforms, Flow *_f,
81  const uint8_t _flow_flags, void *txv, const int list_id);
82 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
83  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
84  const int list_id);
85 static int g_http_host_buffer_id = 0;
86 static int g_http2_thread_id = 0;
87 static int g_http2_raw_thread_id = 0;
88 
89 /**
90  * \brief Registers the keyword handlers for the "http_host" keyword.
91  */
93 {
94  /* http_host content modifier */
96  sigmatch_table[DETECT_HTTP_HOST_CM].desc = "content modifier to match on the HTTP hostname";
98  "/rules/http-keywords.html#http-host-and-http-raw-host";
99  sigmatch_table[DETECT_HTTP_HOST_CM].Setup = DetectHttpHHSetup;
100 #ifdef UNITTESTS
102 #endif
105 
106  /* http.host sticky buffer */
107  sigmatch_table[DETECT_HTTP_HOST].name = "http.host";
108  sigmatch_table[DETECT_HTTP_HOST].desc = "sticky buffer to match on the HTTP Host buffer";
109  sigmatch_table[DETECT_HTTP_HOST].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
110  sigmatch_table[DETECT_HTTP_HOST].Setup = DetectHttpHostSetup;
112 
114  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetData);
115 
117  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
118 
120  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
121 
123  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
124 
126  DetectHttpHostValidateCallback);
127 
129  "http host");
130 
131  g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs(
132  "http_host", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree);
133 
134  g_http_host_buffer_id = DetectBufferTypeGetByName("http_host");
135 
136  /* http_raw_host content modifier */
137  sigmatch_table[DETECT_HTTP_RAW_HOST].name = "http_raw_host";
138  sigmatch_table[DETECT_HTTP_RAW_HOST].desc = "content modifier to match on the HTTP host header "
139  "or the raw hostname from the HTTP uri";
141  "/rules/http-keywords.html#http-host-and-http-raw-host";
142  sigmatch_table[DETECT_HTTP_RAW_HOST].Setup = DetectHttpHRHSetup;
145 
146  /* http.host sticky buffer */
147  sigmatch_table[DETECT_HTTP_HOST_RAW].name = "http.host.raw";
148  sigmatch_table[DETECT_HTTP_HOST_RAW].desc = "sticky buffer to match on the HTTP host header or the raw hostname from the HTTP uri";
149  sigmatch_table[DETECT_HTTP_HOST_RAW].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
150  sigmatch_table[DETECT_HTTP_HOST_RAW].Setup = DetectHttpHostRawSetupSticky;
152 
154  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetRawData);
155 
157  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
158 
160  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRawData2);
161 
163  GetRawData2, ALPROTO_HTTP2, HTTP2StateDataClient);
164 
165  DetectBufferTypeSetDescriptionByName("http_raw_host",
166  "http raw host header");
167 
168  g_http2_raw_thread_id = DetectRegisterThreadCtxGlobalFuncs(
169  "http_raw_host", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree);
170 
171  g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host");
172 }
173 
174 /**
175  * \brief The setup function for the http_host keyword for a signature.
176  *
177  * \param de_ctx Pointer to the detection engine context.
178  * \param s Pointer to the signature for the current Signature being
179  * parsed from the rules.
180  * \param m Pointer to the head of the SigMatch for the current rule
181  * being parsed.
182  * \param arg Pointer to the string holding the keyword value.
183  *
184  * \retval 0 On success
185  * \retval -1 On failure
186  */
187 static int DetectHttpHHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
188 {
190  de_ctx, s, arg, DETECT_HTTP_HOST_CM, g_http_host_buffer_id, ALPROTO_HTTP1);
191 }
192 
193 static bool DetectHttpHostValidateCallback(
194  const Signature *s, const char **sigerror, const DetectBufferType *dbt)
195 {
196  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
197  if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
198  continue;
199  const SigMatch *sm = s->init_data->buffers[x].head;
200  for (; sm != NULL; sm = sm->next) {
201  if (sm->type == DETECT_CONTENT) {
203  if (cd->flags & DETECT_CONTENT_NOCASE) {
204  *sigerror = "http.host keyword "
205  "specified along with \"nocase\". "
206  "The hostname buffer is normalized "
207  "to lowercase, specifying "
208  "nocase is redundant.";
209  SCLogWarning("rule %u: %s", s->id, *sigerror);
210  return false;
211  } else {
212  uint32_t u;
213  for (u = 0; u < cd->content_len; u++) {
214  if (isupper(cd->content[u]))
215  break;
216  }
217  if (u != cd->content_len) {
218  *sigerror = "A pattern with "
219  "uppercase characters detected for http.host. "
220  "The hostname buffer is normalized to lowercase, "
221  "please specify a lowercase pattern.";
222  SCLogWarning("rule %u: %s", s->id, *sigerror);
223  return false;
224  }
225  }
226  }
227  }
228  }
229 
230  return true;
231 }
232 
233 /**
234  * \brief this function setup the http.host keyword used in the rule
235  *
236  * \param de_ctx Pointer to the Detection Engine Context
237  * \param s Pointer to the Signature to which the current keyword belongs
238  * \param str Should hold an empty string always
239  *
240  * \retval 0 On success
241  */
242 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
243 {
244  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_host_buffer_id) < 0)
245  return -1;
247  return -1;
248  return 0;
249 }
250 
251 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
252  const DetectEngineTransforms *transforms, Flow *_f,
253  const uint8_t _flow_flags, void *txv, const int list_id)
254 {
255  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
256  if (buffer->inspect == NULL) {
257  htp_tx_t *tx = (htp_tx_t *)txv;
258 
259  if (htp_tx_request_hostname(tx) == NULL)
260  return NULL;
261 
262  const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_hostname(tx));
263  const uint8_t *data = bstr_ptr(htp_tx_request_hostname(tx));
264 
266  det_ctx, list_id, buffer, data, data_len, transforms);
267  }
268 
269  return buffer;
270 }
271 
272 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
273  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
274  const int list_id)
275 {
276  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
277  if (buffer->inspect == NULL) {
278  uint32_t b_len = 0;
279  const uint8_t *b = NULL;
280  void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
281  if (thread_buf == NULL)
282  return NULL;
283  if (SCHttp2TxGetHostNorm(txv, &b, &b_len, thread_buf) != 1)
284  return NULL;
285  if (b == NULL || b_len == 0)
286  return NULL;
287 
288  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
289  }
290 
291  return buffer;
292 }
293 
294 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
295  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
296  const int list_id)
297 {
298  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
299  if (buffer->inspect == NULL) {
300  uint32_t b_len = 0;
301  const uint8_t *b = NULL;
302  void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_raw_thread_id);
303  if (thread_buf == NULL)
304  return NULL;
305 
306  if (SCHttp2TxGetHost(txv, &b, &b_len, thread_buf) != 1)
307  return NULL;
308  if (b == NULL || b_len == 0)
309  return NULL;
310 
311  InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
312  }
313 
314  return buffer;
315 }
316 
317 /**
318  * \brief The setup function for the http_raw_host keyword for a signature.
319  *
320  * \param de_ctx Pointer to the detection engine context.
321  * \param s Pointer to the signature for the current Signature being
322  * parsed from the rules.
323  * \param m Pointer to the head of the SigMatch for the current rule
324  * being parsed.
325  * \param arg Pointer to the string holding the keyword value.
326  *
327  * \retval 0 On success
328  * \retval -1 On failure
329  */
330 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
331 {
333  de_ctx, s, arg, DETECT_HTTP_RAW_HOST, g_http_raw_host_buffer_id, ALPROTO_HTTP1);
334 }
335 
336 /**
337  * \brief this function setup the http.host keyword used in the rule
338  *
339  * \param de_ctx Pointer to the Detection Engine Context
340  * \param s Pointer to the Signature to which the current keyword belongs
341  * \param str Should hold an empty string always
342  *
343  * \retval 0 On success
344  */
345 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
346 {
347  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_raw_host_buffer_id) < 0)
348  return -1;
350  return -1;
351  return 0;
352 }
353 
354 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
355  const DetectEngineTransforms *transforms, Flow *_f,
356  const uint8_t _flow_flags, void *txv, const int list_id)
357 {
358  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
359  if (buffer->inspect == NULL) {
360  htp_tx_t *tx = (htp_tx_t *)txv;
361 
362  const uint8_t *data = NULL;
363  uint32_t data_len = 0;
364 
365  if (htp_uri_hostname(htp_tx_parsed_uri(tx)) == NULL) {
366  if (htp_tx_request_headers(tx) == NULL)
367  return NULL;
368 
369  const htp_header_t *h = htp_tx_request_header(tx, "Host");
370  if (h == NULL || htp_header_value(h) == NULL)
371  return NULL;
372 
373  data = htp_header_value_ptr(h);
374  data_len = (uint32_t)htp_header_value_len(h);
375  } else {
376  data = (const uint8_t *)bstr_ptr(htp_uri_hostname(htp_tx_parsed_uri(tx)));
377  data_len = (uint32_t)bstr_len(htp_uri_hostname(htp_tx_parsed_uri(tx)));
378  }
379 
381  det_ctx, list_id, buffer, data, data_len, transforms);
382  }
383 
384  return buffer;
385 }
386 
387 /************************************Unittests*********************************/
388 
389 #ifdef UNITTESTS
390 #include "tests/detect-http-host.c"
391 #endif /* UNITTESTS */
392 
393 /**
394  * @}
395  */
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
DetectHttpHHRegister
void DetectHttpHHRegister(void)
Registers the keyword handlers for the "http_host" keyword.
Definition: detect-http-host.c:92
SigTableElmt_::url
const char * url
Definition: detect.h:1464
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:534
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1678
SigTableElmt_::desc
const char * desc
Definition: detect.h:1463
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
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:2049
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1676
DETECT_HTTP_HOST
@ DETECT_HTTP_HOST
Definition: detect-engine-register.h:189
SigTableElmt_::name
const char * name
Definition: detect.h:1461
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:390
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:71
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1452
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1481
DETECT_HTTP_RAW_HOST
@ DETECT_HTTP_RAW_HOST
Definition: detect-engine-register.h:190
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:448
DetectContentData_
Definition: detect-content.h:93
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2236
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1443
detect-pcre.h
detect-http-host.c
Handle HTTP host header. HHHD - Http Host Header Data.
detect-engine-prefilter.h
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
DetectHttpHHRegisterTests
void DetectHttpHHRegisterTests(void)
Definition: detect-http-host.c:2576
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:359
DETECT_HTTP_HOST_RAW
@ DETECT_HTTP_HOST_RAW
Definition: detect-engine-register.h:191
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1577
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1459
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:152
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:358
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
detect-http-host.h
DetectBufferType_::id
int id
Definition: detect.h:451
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:356
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
detect-engine-buffer.h
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:647
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:308
DETECT_HTTP_HOST_CM
@ DETECT_HTTP_HOST_CM
Definition: detect-engine-register.h:188
DetectThreadCtxGetGlobalKeywordThreadCtx
void * DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3849
Signature_::id
uint32_t id
Definition: detect.h:713
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:525
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:355
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
InspectionBufferSetupAndApplyTransforms
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
Definition: detect-engine-inspect-buffer.c:197
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1653
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:273
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:648
flow-var.h
DetectRegisterThreadCtxGlobalFuncs
int DetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:3805
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1450
app-layer.h