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  "Since the hostname buffer we match against "
190  "is actually lowercase. So having a "
191  "nocase is redundant.";
192  SCLogWarning(SC_WARN_POOR_RULE, "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 chars detected for http_host. "
203  "Since the hostname buffer we match against "
204  "is lowercase only, please specify a "
205  "lowercase pattern.";
206  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
207  return false;
208  }
209  }
210  }
211  }
212 
213  return true;
214 }
215 
216 /**
217  * \brief this function setup the http.host keyword used in the rule
218  *
219  * \param de_ctx Pointer to the Detection Engine Context
220  * \param s Pointer to the Signature to which the current keyword belongs
221  * \param str Should hold an empty string always
222  *
223  * \retval 0 On success
224  */
225 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
226 {
227  if (DetectBufferSetActiveList(s, g_http_host_buffer_id) < 0)
228  return -1;
230  return -1;
231  return 0;
232 }
233 
234 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
235  const DetectEngineTransforms *transforms, Flow *_f,
236  const uint8_t _flow_flags, void *txv, const int list_id)
237 {
238  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
239  if (buffer->inspect == NULL) {
240  htp_tx_t *tx = (htp_tx_t *)txv;
241 
242  if (tx->request_hostname == NULL)
243  return NULL;
244 
245  const uint32_t data_len = bstr_len(tx->request_hostname);
246  const uint8_t *data = bstr_ptr(tx->request_hostname);
247 
248  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
249  InspectionBufferApplyTransforms(buffer, transforms);
250  }
251 
252  return buffer;
253 }
254 
255 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
256  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
257  const int list_id)
258 {
259  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
260  if (buffer->inspect == NULL) {
261  uint32_t b_len = 0;
262  const uint8_t *b = NULL;
263 
264  if (rs_http2_tx_get_host_norm(txv, &b, &b_len) != 1)
265  return NULL;
266  if (b == NULL || b_len == 0)
267  return NULL;
268 
269  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
270  InspectionBufferApplyTransforms(buffer, transforms);
271  }
272 
273  return buffer;
274 }
275 
276 static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx,
277  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
278  const int list_id)
279 {
280  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
281  if (buffer->inspect == NULL) {
282  uint32_t b_len = 0;
283  const uint8_t *b = NULL;
284 
285  if (rs_http2_tx_get_host(txv, &b, &b_len) != 1)
286  return NULL;
287  if (b == NULL || b_len == 0)
288  return NULL;
289 
290  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
291  InspectionBufferApplyTransforms(buffer, transforms);
292  }
293 
294  return buffer;
295 }
296 
297 /**
298  * \brief The setup function for the http_raw_host keyword for a signature.
299  *
300  * \param de_ctx Pointer to the detection engine context.
301  * \param s Pointer to the signature for the current Signature being
302  * parsed from the rules.
303  * \param m Pointer to the head of the SigMatch for the current rule
304  * being parsed.
305  * \param arg Pointer to the string holding the keyword value.
306  *
307  * \retval 0 On success
308  * \retval -1 On failure
309  */
310 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
311 {
313  de_ctx, s, arg, DETECT_AL_HTTP_RAW_HOST, g_http_raw_host_buffer_id, ALPROTO_HTTP1);
314 }
315 
316 /**
317  * \brief this function setup the http.host keyword used in the rule
318  *
319  * \param de_ctx Pointer to the Detection Engine Context
320  * \param s Pointer to the Signature to which the current keyword belongs
321  * \param str Should hold an empty string always
322  *
323  * \retval 0 On success
324  */
325 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
326 {
327  if (DetectBufferSetActiveList(s, g_http_raw_host_buffer_id) < 0)
328  return -1;
330  return -1;
331  return 0;
332 }
333 
334 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
335  const DetectEngineTransforms *transforms, Flow *_f,
336  const uint8_t _flow_flags, void *txv, const int list_id)
337 {
338  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
339  if (buffer->inspect == NULL) {
340  htp_tx_t *tx = (htp_tx_t *)txv;
341 
342  const uint8_t *data = NULL;
343  uint32_t data_len = 0;
344 
345  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
346  if (tx->request_headers == NULL)
347  return NULL;
348 
349  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
350  "Host");
351  if (h == NULL || h->value == NULL)
352  return NULL;
353 
354  data = (const uint8_t *)bstr_ptr(h->value);
355  data_len = bstr_len(h->value);
356  } else {
357  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
358  data_len = bstr_len(tx->parsed_uri->hostname);
359  }
360 
361  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
362  InspectionBufferApplyTransforms(buffer, transforms);
363  }
364 
365  return buffer;
366 }
367 
368 /************************************Unittests*********************************/
369 
370 #ifdef UNITTESTS
371 #include "tests/detect-http-host.c"
372 #endif /* UNITTESTS */
373 
374 /**
375  * @}
376  */
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:155
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
flow-util.h
DETECT_AL_HTTP_RAW_HOST
@ DETECT_AL_HTTP_RAW_HOST
Definition: detect-engine-register.h:157
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1475
DETECT_HTTP_HOST
@ DETECT_HTTP_HOST
Definition: detect-engine-register.h:156
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:379
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:60
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:753
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
DetectContentData_
Definition: detect-content.h:86
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
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:1360
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
SC_WARN_POOR_RULE
@ SC_WARN_POOR_RULE
Definition: util-error.h:309
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1271
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
DetectHttpHHRegisterTests
void DetectHttpHHRegisterTests(void)
Definition: detect-http-host.c:8754
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:325
DETECT_HTTP_HOST_RAW
@ DETECT_HTTP_HOST_RAW
Definition: detect-engine-register.h:158
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1265
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
DetectEngineInspectBufferGeneric
int 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:1983
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
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:225
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:97
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:619
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:542
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:62
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:322
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1543
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:87
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
str
#define str(s)
Definition: suricata-common.h:272
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
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:1441
Signature_::id
uint32_t id
Definition: detect.h:582
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
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:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
flow.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
app-layer.h