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  const SigMatch *sm = s->init_data->smlists[g_http_host_buffer_id];
183  for ( ; sm != NULL; sm = sm->next) {
184  if (sm->type == DETECT_CONTENT) {
186  if (cd->flags & DETECT_CONTENT_NOCASE) {
187  *sigerror = "http.host keyword "
188  "specified along with \"nocase\". "
189  "The hostname buffer is normalized "
190  "to lowercase, specifying "
191  "nocase is redundant.";
192  SCLogWarning("rule %u: %s", s->id, *sigerror);
193  return false;
194  } else {
195  uint32_t u;
196  for (u = 0; u < cd->content_len; u++) {
197  if (isupper(cd->content[u]))
198  break;
199  }
200  if (u != cd->content_len) {
201  *sigerror = "A pattern with "
202  "uppercase characters detected for http.host. "
203  "The hostname buffer is normalized to lowercase, "
204  "please specify a lowercase pattern.";
205  SCLogWarning("rule %u: %s", s->id, *sigerror);
206  return false;
207  }
208  }
209  }
210  }
211 
212  return true;
213 }
214 
215 /**
216  * \brief this function setup the http.host keyword used in the rule
217  *
218  * \param de_ctx Pointer to the Detection Engine Context
219  * \param s Pointer to the Signature to which the current keyword belongs
220  * \param str Should hold an empty string always
221  *
222  * \retval 0 On success
223  */
224 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
225 {
226  if (DetectBufferSetActiveList(s, g_http_host_buffer_id) < 0)
227  return -1;
229  return -1;
230  return 0;
231 }
232 
233 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
234  const DetectEngineTransforms *transforms, Flow *_f,
235  const uint8_t _flow_flags, void *txv, const int list_id)
236 {
237  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
238  if (buffer->inspect == NULL) {
239  htp_tx_t *tx = (htp_tx_t *)txv;
240 
241  if (tx->request_hostname == NULL)
242  return NULL;
243 
244  const uint32_t data_len = bstr_len(tx->request_hostname);
245  const uint8_t *data = bstr_ptr(tx->request_hostname);
246 
247  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
248  InspectionBufferApplyTransforms(buffer, transforms);
249  }
250 
251  return buffer;
252 }
253 
254 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
255  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
256  const int list_id)
257 {
258  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
259  if (buffer->inspect == NULL) {
260  uint32_t b_len = 0;
261  const uint8_t *b = NULL;
262 
263  if (rs_http2_tx_get_host_norm(txv, &b, &b_len) != 1)
264  return NULL;
265  if (b == NULL || b_len == 0)
266  return NULL;
267 
268  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
269  InspectionBufferApplyTransforms(buffer, transforms);
270  }
271 
272  return buffer;
273 }
274 
275 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
276  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
277  const int list_id)
278 {
279  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
280  if (buffer->inspect == NULL) {
281  uint32_t b_len = 0;
282  const uint8_t *b = NULL;
283 
284  if (rs_http2_tx_get_host(txv, &b, &b_len) != 1)
285  return NULL;
286  if (b == NULL || b_len == 0)
287  return NULL;
288 
289  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
290  InspectionBufferApplyTransforms(buffer, transforms);
291  }
292 
293  return buffer;
294 }
295 
296 /**
297  * \brief The setup function for the http_raw_host keyword for a signature.
298  *
299  * \param de_ctx Pointer to the detection engine context.
300  * \param s Pointer to the signature for the current Signature being
301  * parsed from the rules.
302  * \param m Pointer to the head of the SigMatch for the current rule
303  * being parsed.
304  * \param arg Pointer to the string holding the keyword value.
305  *
306  * \retval 0 On success
307  * \retval -1 On failure
308  */
309 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
310 {
312  de_ctx, s, arg, DETECT_AL_HTTP_RAW_HOST, g_http_raw_host_buffer_id, ALPROTO_HTTP1);
313 }
314 
315 /**
316  * \brief this function setup the http.host keyword used in the rule
317  *
318  * \param de_ctx Pointer to the Detection Engine Context
319  * \param s Pointer to the Signature to which the current keyword belongs
320  * \param str Should hold an empty string always
321  *
322  * \retval 0 On success
323  */
324 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
325 {
326  if (DetectBufferSetActiveList(s, g_http_raw_host_buffer_id) < 0)
327  return -1;
329  return -1;
330  return 0;
331 }
332 
333 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
334  const DetectEngineTransforms *transforms, Flow *_f,
335  const uint8_t _flow_flags, void *txv, const int list_id)
336 {
337  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
338  if (buffer->inspect == NULL) {
339  htp_tx_t *tx = (htp_tx_t *)txv;
340 
341  const uint8_t *data = NULL;
342  uint32_t data_len = 0;
343 
344  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
345  if (tx->request_headers == NULL)
346  return NULL;
347 
348  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
349  "Host");
350  if (h == NULL || h->value == NULL)
351  return NULL;
352 
353  data = (const uint8_t *)bstr_ptr(h->value);
354  data_len = bstr_len(h->value);
355  } else {
356  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
357  data_len = bstr_len(tx->parsed_uri->hostname);
358  }
359 
360  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
361  InspectionBufferApplyTransforms(buffer, transforms);
362  }
363 
364  return buffer;
365 }
366 
367 /************************************Unittests*********************************/
368 
369 #ifdef UNITTESTS
370 #include "tests/detect-http-host.c"
371 #endif /* UNITTESTS */
372 
373 /**
374  * @}
375  */
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:160
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
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:1991
flow-util.h
DETECT_AL_HTTP_RAW_HOST
@ DETECT_AL_HTTP_RAW_HOST
Definition: detect-engine-register.h:162
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1445
DETECT_HTTP_HOST
@ DETECT_HTTP_HOST
Definition: detect-engine-register.h:161
SigTableElmt_::name
const char * name
Definition: detect.h:1238
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:372
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
InspectionBuffer
Definition: detect.h:337
threads.h
Flow_
Flow data structure.
Definition: flow.h:357
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:749
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DetectContentData_
Definition: detect-content.h:86
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
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:1364
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
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:1273
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
DetectHttpHHRegisterTests
void DetectHttpHHRegisterTests(void)
Definition: detect-http-host.c:8492
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:317
DETECT_HTTP_HOST_RAW
@ DETECT_HTTP_HOST_RAW
Definition: detect-engine-register.h:163
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1236
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:149
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:224
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:97
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:611
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:534
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
detect-http-host.h
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
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1550
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:87
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
str
#define str(s)
Definition: suricata-common.h:280
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:1446
Signature_::id
uint32_t id
Definition: detect.h:574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:313
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:66
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:88
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1176
flow.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1230
app-layer.h