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_AL_HTTP_HOST].desc = "content modifier to match on the HTTP hostname";
93  sigmatch_table[DETECT_AL_HTTP_HOST].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
94  sigmatch_table[DETECT_AL_HTTP_HOST].Setup = DetectHttpHHSetup;
95 #ifdef UNITTESTS
97 #endif
100 
101  /* http.host sticky buffer */
102  sigmatch_table[DETECT_HTTP_HOST].name = "http.host";
103  sigmatch_table[DETECT_HTTP_HOST].desc = "sticky buffer to match on the HTTP Host buffer";
104  sigmatch_table[DETECT_HTTP_HOST].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
105  sigmatch_table[DETECT_HTTP_HOST].Setup = DetectHttpHostSetup;
107 
109  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetData);
110 
112  GetData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
113 
115  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
116 
118  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
119 
121  DetectHttpHostValidateCallback);
122 
124  "http host");
125 
126  g_http_host_buffer_id = DetectBufferTypeGetByName("http_host");
127 
128  /* http_raw_host content modifier */
129  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].name = "http_raw_host";
130  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].desc = "content modifier to match on the HTTP host header or the raw hostname from the HTTP uri";
131  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
132  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].Setup = DetectHttpHRHSetup;
135 
136  /* http.host sticky buffer */
137  sigmatch_table[DETECT_HTTP_HOST_RAW].name = "http.host.raw";
138  sigmatch_table[DETECT_HTTP_HOST_RAW].desc = "sticky buffer to match on the HTTP host header or the raw hostname from the HTTP uri";
139  sigmatch_table[DETECT_HTTP_HOST_RAW].url = "/rules/http-keywords.html#http-host-and-http-raw-host";
140  sigmatch_table[DETECT_HTTP_HOST_RAW].Setup = DetectHttpHostRawSetupSticky;
142 
144  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetRawData);
145 
147  GetRawData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
148 
150  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRawData2);
151 
153  GetRawData2, ALPROTO_HTTP2, HTTP2StateDataClient);
154 
155  DetectBufferTypeSetDescriptionByName("http_raw_host",
156  "http raw host header");
157 
158  g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host");
159 }
160 
161 /**
162  * \brief The setup function for the http_host keyword for a signature.
163  *
164  * \param de_ctx Pointer to the detection engine context.
165  * \param s Pointer to the signature for the current Signature being
166  * parsed from the rules.
167  * \param m Pointer to the head of the SigMatch for the current rule
168  * being parsed.
169  * \param arg Pointer to the string holding the keyword value.
170  *
171  * \retval 0 On success
172  * \retval -1 On failure
173  */
174 static int DetectHttpHHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
175 {
177  de_ctx, s, arg, DETECT_AL_HTTP_HOST, g_http_host_buffer_id, ALPROTO_HTTP1);
178 }
179 
180 static bool DetectHttpHostValidateCallback(const Signature *s, const char **sigerror)
181 {
182  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
183  if (s->init_data->buffers[x].id != (uint32_t)g_http_host_buffer_id)
184  continue;
185  const SigMatch *sm = s->init_data->buffers[x].head;
186  for (; sm != NULL; sm = sm->next) {
187  if (sm->type == DETECT_CONTENT) {
189  if (cd->flags & DETECT_CONTENT_NOCASE) {
190  *sigerror = "http.host keyword "
191  "specified along with \"nocase\". "
192  "The hostname buffer is normalized "
193  "to lowercase, specifying "
194  "nocase is redundant.";
195  SCLogWarning("rule %u: %s", s->id, *sigerror);
196  return false;
197  } else {
198  uint32_t u;
199  for (u = 0; u < cd->content_len; u++) {
200  if (isupper(cd->content[u]))
201  break;
202  }
203  if (u != cd->content_len) {
204  *sigerror = "A pattern with "
205  "uppercase characters detected for http.host. "
206  "The hostname buffer is normalized to lowercase, "
207  "please specify a lowercase pattern.";
208  SCLogWarning("rule %u: %s", s->id, *sigerror);
209  return false;
210  }
211  }
212  }
213  }
214  }
215 
216  return true;
217 }
218 
219 /**
220  * \brief this function setup the http.host keyword used in the rule
221  *
222  * \param de_ctx Pointer to the Detection Engine Context
223  * \param s Pointer to the Signature to which the current keyword belongs
224  * \param str Should hold an empty string always
225  *
226  * \retval 0 On success
227  */
228 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
229 {
230  if (DetectBufferSetActiveList(de_ctx, s, g_http_host_buffer_id) < 0)
231  return -1;
233  return -1;
234  return 0;
235 }
236 
237 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
238  const DetectEngineTransforms *transforms, Flow *_f,
239  const uint8_t _flow_flags, void *txv, const int list_id)
240 {
241  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
242  if (buffer->inspect == NULL) {
243  htp_tx_t *tx = (htp_tx_t *)txv;
244 
245  if (tx->request_hostname == NULL)
246  return NULL;
247 
248  const uint32_t data_len = bstr_len(tx->request_hostname);
249  const uint8_t *data = bstr_ptr(tx->request_hostname);
250 
251  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
252  InspectionBufferApplyTransforms(buffer, transforms);
253  }
254 
255  return buffer;
256 }
257 
258 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
259  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
260  const int list_id)
261 {
262  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
263  if (buffer->inspect == NULL) {
264  uint32_t b_len = 0;
265  const uint8_t *b = NULL;
266 
267  if (rs_http2_tx_get_host_norm(txv, &b, &b_len) != 1)
268  return NULL;
269  if (b == NULL || b_len == 0)
270  return NULL;
271 
272  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
273  InspectionBufferApplyTransforms(buffer, transforms);
274  }
275 
276  return buffer;
277 }
278 
279 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
280  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
281  const int list_id)
282 {
283  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
284  if (buffer->inspect == NULL) {
285  uint32_t b_len = 0;
286  const uint8_t *b = NULL;
287 
288  if (rs_http2_tx_get_host(txv, &b, &b_len) != 1)
289  return NULL;
290  if (b == NULL || b_len == 0)
291  return NULL;
292 
293  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
294  InspectionBufferApplyTransforms(buffer, transforms);
295  }
296 
297  return buffer;
298 }
299 
300 /**
301  * \brief The setup function for the http_raw_host keyword for a signature.
302  *
303  * \param de_ctx Pointer to the detection engine context.
304  * \param s Pointer to the signature for the current Signature being
305  * parsed from the rules.
306  * \param m Pointer to the head of the SigMatch for the current rule
307  * being parsed.
308  * \param arg Pointer to the string holding the keyword value.
309  *
310  * \retval 0 On success
311  * \retval -1 On failure
312  */
313 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
314 {
316  de_ctx, s, arg, DETECT_AL_HTTP_RAW_HOST, g_http_raw_host_buffer_id, ALPROTO_HTTP1);
317 }
318 
319 /**
320  * \brief this function setup the http.host keyword used in the rule
321  *
322  * \param de_ctx Pointer to the Detection Engine Context
323  * \param s Pointer to the Signature to which the current keyword belongs
324  * \param str Should hold an empty string always
325  *
326  * \retval 0 On success
327  */
328 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
329 {
330  if (DetectBufferSetActiveList(de_ctx, s, g_http_raw_host_buffer_id) < 0)
331  return -1;
333  return -1;
334  return 0;
335 }
336 
337 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
338  const DetectEngineTransforms *transforms, Flow *_f,
339  const uint8_t _flow_flags, void *txv, const int list_id)
340 {
341  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
342  if (buffer->inspect == NULL) {
343  htp_tx_t *tx = (htp_tx_t *)txv;
344 
345  const uint8_t *data = NULL;
346  uint32_t data_len = 0;
347 
348  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
349  if (tx->request_headers == NULL)
350  return NULL;
351 
352  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
353  "Host");
354  if (h == NULL || h->value == NULL)
355  return NULL;
356 
357  data = (const uint8_t *)bstr_ptr(h->value);
358  data_len = bstr_len(h->value);
359  } else {
360  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
361  data_len = bstr_len(tx->parsed_uri->hostname);
362  }
363 
364  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
365  InspectionBufferApplyTransforms(buffer, transforms);
366  }
367 
368  return buffer;
369 }
370 
371 /************************************Unittests*********************************/
372 
373 #ifdef UNITTESTS
374 #include "tests/detect-http-host.c"
375 #endif /* UNITTESTS */
376 
377 /**
378  * @}
379  */
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:88
DETECT_AL_HTTP_HOST
@ DETECT_AL_HTTP_HOST
Definition: detect-engine-register.h:166
SigTableElmt_::url
const char * url
Definition: detect.h:1296
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:527
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1497
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
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
flow-util.h
DETECT_AL_HTTP_RAW_HOST
@ DETECT_AL_HTTP_RAW_HOST
Definition: detect-engine-register.h:168
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1495
DETECT_HTTP_HOST
@ DETECT_HTTP_HOST
Definition: detect-engine-register.h:167
SigTableElmt_::name
const char * name
Definition: detect.h:1293
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:406
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
InspectionBuffer
Definition: detect.h:371
threads.h
Flow_
Flow data structure.
Definition: flow.h:351
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1287
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
MpmListIdDataArgs::txv
void * txv
Definition: detect-engine-mpm.h:130
DetectContentData_
Definition: detect-content.h:93
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
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: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:263
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:1284
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1092
DetectHttpHHRegisterTests
void DetectHttpHHRegisterTests(void)
Definition: detect-http-host.c:7026
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:351
DETECT_HTTP_HOST_RAW
@ DETECT_HTTP_HOST_RAW
Definition: detect-engine-register.h:169
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1291
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
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:350
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:205
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:662
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
detect-http-host.h
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:348
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
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
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:583
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:372
str
#define str(s)
Definition: suricata-common.h:291
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
Signature_::id
uint32_t id
Definition: detect.h:628
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:520
Signature_
Signature container.
Definition: detect.h:593
SigMatch_
a single match condition for a signature
Definition: detect.h:347
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1473
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
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:584
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285
app-layer.h