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 int DetectHttpHRHSetup(DetectEngineCtx *, Signature *, const char *);
72 static int g_http_raw_host_buffer_id = 0;
73 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
74 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
75  const DetectEngineTransforms *transforms, Flow *_f,
76  const uint8_t _flow_flags, void *txv, const int list_id);
77 static int g_http_host_buffer_id = 0;
78 
79 /**
80  * \brief Registers the keyword handlers for the "http_host" keyword.
81  */
83 {
84  /* http_host content modifier */
86  sigmatch_table[DETECT_AL_HTTP_HOST].desc = "content modifier to match on the HTTP hostname";
87  sigmatch_table[DETECT_AL_HTTP_HOST].Setup = DetectHttpHHSetup;
88 #ifdef UNITTESTS
90 #endif
93 
94  /* http.host sticky buffer */
95  sigmatch_table[DETECT_HTTP_HOST].name = "http.host";
96  sigmatch_table[DETECT_HTTP_HOST].desc = "sticky buffer to match on the HTTP Host buffer";
97  sigmatch_table[DETECT_HTTP_HOST].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host";
98  sigmatch_table[DETECT_HTTP_HOST].Setup = DetectHttpHostSetup;
100 
102  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
104 
107  HTP_REQUEST_HEADERS);
108 
110  DetectHttpHostValidateCallback);
111 
113  "http host");
114 
115  g_http_host_buffer_id = DetectBufferTypeGetByName("http_host");
116 
117  /* http_raw_host content modifier */
118  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].name = "http_raw_host";
119  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";
120  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].Setup = DetectHttpHRHSetup;
123 
124  /* http.host sticky buffer */
125  sigmatch_table[DETECT_HTTP_HOST_RAW].name = "http.host.raw";
126  sigmatch_table[DETECT_HTTP_HOST_RAW].desc = "sticky buffer to match on the HTTP host header or the raw hostname from the HTTP uri";
127  sigmatch_table[DETECT_HTTP_HOST_RAW].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host";
128  sigmatch_table[DETECT_HTTP_HOST_RAW].Setup = DetectHttpHostRawSetupSticky;
130 
132  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
134 
135  DetectAppLayerMpmRegister2("http_raw_host", SIG_FLAG_TOSERVER, 2,
137  HTP_REQUEST_HEADERS);
138 
139  DetectBufferTypeSetDescriptionByName("http_raw_host",
140  "http raw host header");
141 
142  g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host");
143 }
144 
145 /**
146  * \brief The setup function for the http_host keyword for a signature.
147  *
148  * \param de_ctx Pointer to the detection engine context.
149  * \param s Pointer to the signature for the current Signature being
150  * parsed from the rules.
151  * \param m Pointer to the head of the SigMatch for the current rule
152  * being parsed.
153  * \param arg Pointer to the string holding the keyword value.
154  *
155  * \retval 0 On success
156  * \retval -1 On failure
157  */
158 static int DetectHttpHHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
159 {
160  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
162  g_http_host_buffer_id,
163  ALPROTO_HTTP);
164 }
165 
166 static _Bool DetectHttpHostValidateCallback(const Signature *s, const char **sigerror)
167 {
168  const SigMatch *sm = s->init_data->smlists[g_http_host_buffer_id];
169  for ( ; sm != NULL; sm = sm->next) {
170  if (sm->type == DETECT_CONTENT) {
172  if (cd->flags & DETECT_CONTENT_NOCASE) {
173  *sigerror = "http_host keyword "
174  "specified along with \"nocase\". "
175  "Since the hostname buffer we match against "
176  "is actually lowercase. So having a "
177  "nocase is redundant.";
178  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
179  return FALSE;
180  } else {
181  uint32_t u;
182  for (u = 0; u < cd->content_len; u++) {
183  if (isupper(cd->content[u]))
184  break;
185  }
186  if (u != cd->content_len) {
187  *sigerror = "A pattern with "
188  "uppercase chars detected for http_host. "
189  "Since the hostname buffer we match against "
190  "is lowercase only, please specify a "
191  "lowercase pattern.";
192  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
193  return FALSE;
194  }
195  }
196  }
197  }
198 
199  return TRUE;
200 }
201 
202 /**
203  * \brief this function setup the http.host keyword used in the rule
204  *
205  * \param de_ctx Pointer to the Detection Engine Context
206  * \param s Pointer to the Signature to which the current keyword belongs
207  * \param str Should hold an empty string always
208  *
209  * \retval 0 On success
210  */
211 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
212 {
213  if (DetectBufferSetActiveList(s, g_http_host_buffer_id) < 0)
214  return -1;
216  return -1;
217  return 0;
218 }
219 
220 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
221  const DetectEngineTransforms *transforms, Flow *_f,
222  const uint8_t _flow_flags, void *txv, const int list_id)
223 {
224  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
225  if (buffer->inspect == NULL) {
226  htp_tx_t *tx = (htp_tx_t *)txv;
227 
228  if (tx->request_hostname == NULL)
229  return NULL;
230 
231  const uint32_t data_len = bstr_len(tx->request_hostname);
232  const uint8_t *data = bstr_ptr(tx->request_hostname);
233 
234  InspectionBufferSetup(buffer, data, data_len);
235  InspectionBufferApplyTransforms(buffer, transforms);
236  }
237 
238  return buffer;
239 }
240 
241 /**
242  * \brief The setup function for the http_raw_host keyword for a signature.
243  *
244  * \param de_ctx Pointer to the detection engine context.
245  * \param s Pointer to the signature for the current Signature being
246  * parsed from the rules.
247  * \param m Pointer to the head of the SigMatch for the current rule
248  * being parsed.
249  * \param arg Pointer to the string holding the keyword value.
250  *
251  * \retval 0 On success
252  * \retval -1 On failure
253  */
254 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
255 {
256  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
258  g_http_raw_host_buffer_id,
259  ALPROTO_HTTP);
260 }
261 
262 /**
263  * \brief this function setup the http.host keyword used in the rule
264  *
265  * \param de_ctx Pointer to the Detection Engine Context
266  * \param s Pointer to the Signature to which the current keyword belongs
267  * \param str Should hold an empty string always
268  *
269  * \retval 0 On success
270  */
271 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
272 {
273  if (DetectBufferSetActiveList(s, g_http_raw_host_buffer_id) < 0)
274  return -1;
276  return -1;
277  return 0;
278 }
279 
280 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
281  const DetectEngineTransforms *transforms, Flow *_f,
282  const uint8_t _flow_flags, void *txv, const int list_id)
283 {
284  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
285  if (buffer->inspect == NULL) {
286  htp_tx_t *tx = (htp_tx_t *)txv;
287 
288  const uint8_t *data = NULL;
289  uint32_t data_len = 0;
290 
291  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
292  if (tx->request_headers == NULL)
293  return NULL;
294 
295  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
296  "Host");
297  if (h == NULL || h->value == NULL)
298  return NULL;
299 
300  data = (const uint8_t *)bstr_ptr(h->value);
301  data_len = bstr_len(h->value);
302  } else {
303  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
304  data_len = bstr_len(tx->parsed_uri->hostname);
305  }
306 
307  InspectionBufferSetup(buffer, data, data_len);
308  InspectionBufferApplyTransforms(buffer, transforms);
309  }
310 
311  return buffer;
312 }
313 
314 /************************************Unittests*********************************/
315 
316 #ifdef UNITTESTS
317 #include "tests/detect-http-host.c"
318 #endif /* UNITTESTS */
319 
320 /**
321  * @}
322  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
SignatureInitData * init_data
Definition: detect.h:586
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint32_t id
Definition: detect.h:550
#define FALSE
void DetectHttpHHRegister(void)
Registers the keyword handlers for the "http_host" keyword.
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1384
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1193
Handle HTTP host header. HHHD - Http Host Header Data.
Signature container.
Definition: detect.h:517
#define TRUE
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
struct SigMatch_ * next
Definition: detect.h:317
main detection engine ctx
Definition: detect.h:756
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1386
#define SIG_FLAG_TOSERVER
Definition: detect.h:232
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
uint16_t alternative
Definition: detect.h:1191
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
uint8_t type
Definition: detect.h:314
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.
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
const char * desc
Definition: detect.h:1195
struct SigMatch_ ** smlists
Definition: detect.h:511
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
SigMatchCtx * ctx
Definition: detect.h:316
void DetectHttpHHRegisterTests(void)
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1362
const char * url
Definition: detect.h:1196
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:338
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
#define DETECT_CONTENT_NOCASE
void DetectBufferTypeRegisterValidateCallback(const char *name, _Bool(*ValidateCallback)(const Signature *, const char **sigerror))
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1187
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1185
a single match condition for a signature
Definition: detect.h:313