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].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host-and-http-raw-host";
88  sigmatch_table[DETECT_AL_HTTP_HOST].Setup = DetectHttpHHSetup;
89 #ifdef UNITTESTS
91 #endif
94 
95  /* http.host sticky buffer */
96  sigmatch_table[DETECT_HTTP_HOST].name = "http.host";
97  sigmatch_table[DETECT_HTTP_HOST].desc = "sticky buffer to match on the HTTP Host buffer";
98  sigmatch_table[DETECT_HTTP_HOST].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host-and-http-raw-host";
99  sigmatch_table[DETECT_HTTP_HOST].Setup = DetectHttpHostSetup;
101 
103  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
105 
108  HTP_REQUEST_HEADERS);
109 
111  DetectHttpHostValidateCallback);
112 
114  "http host");
115 
116  g_http_host_buffer_id = DetectBufferTypeGetByName("http_host");
117 
118  /* http_raw_host content modifier */
119  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].name = "http_raw_host";
120  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";
121  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host-and-http-raw-host";
122  sigmatch_table[DETECT_AL_HTTP_RAW_HOST].Setup = DetectHttpHRHSetup;
125 
126  /* http.host sticky buffer */
127  sigmatch_table[DETECT_HTTP_HOST_RAW].name = "http.host.raw";
128  sigmatch_table[DETECT_HTTP_HOST_RAW].desc = "sticky buffer to match on the HTTP host header or the raw hostname from the HTTP uri";
129  sigmatch_table[DETECT_HTTP_HOST_RAW].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-host-and-http-raw-host";
130  sigmatch_table[DETECT_HTTP_HOST_RAW].Setup = DetectHttpHostRawSetupSticky;
132 
134  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
136 
137  DetectAppLayerMpmRegister2("http_raw_host", SIG_FLAG_TOSERVER, 2,
139  HTP_REQUEST_HEADERS);
140 
141  DetectBufferTypeSetDescriptionByName("http_raw_host",
142  "http raw host header");
143 
144  g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host");
145 }
146 
147 /**
148  * \brief The setup function for the http_host keyword for a signature.
149  *
150  * \param de_ctx Pointer to the detection engine context.
151  * \param s Pointer to the signature for the current Signature being
152  * parsed from the rules.
153  * \param m Pointer to the head of the SigMatch for the current rule
154  * being parsed.
155  * \param arg Pointer to the string holding the keyword value.
156  *
157  * \retval 0 On success
158  * \retval -1 On failure
159  */
160 static int DetectHttpHHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
161 {
162  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
164  g_http_host_buffer_id,
165  ALPROTO_HTTP);
166 }
167 
168 static _Bool DetectHttpHostValidateCallback(const Signature *s, const char **sigerror)
169 {
170  const SigMatch *sm = s->init_data->smlists[g_http_host_buffer_id];
171  for ( ; sm != NULL; sm = sm->next) {
172  if (sm->type == DETECT_CONTENT) {
174  if (cd->flags & DETECT_CONTENT_NOCASE) {
175  *sigerror = "http_host keyword "
176  "specified along with \"nocase\". "
177  "Since the hostname buffer we match against "
178  "is actually lowercase. So having a "
179  "nocase is redundant.";
180  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
181  return FALSE;
182  } else {
183  uint32_t u;
184  for (u = 0; u < cd->content_len; u++) {
185  if (isupper(cd->content[u]))
186  break;
187  }
188  if (u != cd->content_len) {
189  *sigerror = "A pattern with "
190  "uppercase chars detected for http_host. "
191  "Since the hostname buffer we match against "
192  "is lowercase only, please specify a "
193  "lowercase pattern.";
194  SCLogWarning(SC_WARN_POOR_RULE, "rule %u: %s", s->id, *sigerror);
195  return FALSE;
196  }
197  }
198  }
199  }
200 
201  return TRUE;
202 }
203 
204 /**
205  * \brief this function setup the http.host keyword used in the rule
206  *
207  * \param de_ctx Pointer to the Detection Engine Context
208  * \param s Pointer to the Signature to which the current keyword belongs
209  * \param str Should hold an empty string always
210  *
211  * \retval 0 On success
212  */
213 static int DetectHttpHostSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
214 {
215  if (DetectBufferSetActiveList(s, g_http_host_buffer_id) < 0)
216  return -1;
218  return -1;
219  return 0;
220 }
221 
222 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
223  const DetectEngineTransforms *transforms, Flow *_f,
224  const uint8_t _flow_flags, void *txv, const int list_id)
225 {
226  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
227  if (buffer->inspect == NULL) {
228  htp_tx_t *tx = (htp_tx_t *)txv;
229 
230  if (tx->request_hostname == NULL)
231  return NULL;
232 
233  const uint32_t data_len = bstr_len(tx->request_hostname);
234  const uint8_t *data = bstr_ptr(tx->request_hostname);
235 
236  InspectionBufferSetup(buffer, data, data_len);
237  InspectionBufferApplyTransforms(buffer, transforms);
238  }
239 
240  return buffer;
241 }
242 
243 /**
244  * \brief The setup function for the http_raw_host keyword for a signature.
245  *
246  * \param de_ctx Pointer to the detection engine context.
247  * \param s Pointer to the signature for the current Signature being
248  * parsed from the rules.
249  * \param m Pointer to the head of the SigMatch for the current rule
250  * being parsed.
251  * \param arg Pointer to the string holding the keyword value.
252  *
253  * \retval 0 On success
254  * \retval -1 On failure
255  */
256 int DetectHttpHRHSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
257 {
258  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
260  g_http_raw_host_buffer_id,
261  ALPROTO_HTTP);
262 }
263 
264 /**
265  * \brief this function setup the http.host keyword used in the rule
266  *
267  * \param de_ctx Pointer to the Detection Engine Context
268  * \param s Pointer to the Signature to which the current keyword belongs
269  * \param str Should hold an empty string always
270  *
271  * \retval 0 On success
272  */
273 static int DetectHttpHostRawSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
274 {
275  if (DetectBufferSetActiveList(s, g_http_raw_host_buffer_id) < 0)
276  return -1;
278  return -1;
279  return 0;
280 }
281 
282 static InspectionBuffer *GetRawData(DetectEngineThreadCtx *det_ctx,
283  const DetectEngineTransforms *transforms, Flow *_f,
284  const uint8_t _flow_flags, void *txv, const int list_id)
285 {
286  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
287  if (buffer->inspect == NULL) {
288  htp_tx_t *tx = (htp_tx_t *)txv;
289 
290  const uint8_t *data = NULL;
291  uint32_t data_len = 0;
292 
293  if (tx->parsed_uri == NULL || tx->parsed_uri->hostname == NULL) {
294  if (tx->request_headers == NULL)
295  return NULL;
296 
297  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
298  "Host");
299  if (h == NULL || h->value == NULL)
300  return NULL;
301 
302  data = (const uint8_t *)bstr_ptr(h->value);
303  data_len = bstr_len(h->value);
304  } else {
305  data = (const uint8_t *)bstr_ptr(tx->parsed_uri->hostname);
306  data_len = bstr_len(tx->parsed_uri->hostname);
307  }
308 
309  InspectionBufferSetup(buffer, data, data_len);
310  InspectionBufferApplyTransforms(buffer, transforms);
311  }
312 
313  return buffer;
314 }
315 
316 /************************************Unittests*********************************/
317 
318 #ifdef UNITTESTS
319 #include "tests/detect-http-host.c"
320 #endif /* UNITTESTS */
321 
322 /**
323  * @}
324  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
SignatureInitData * init_data
Definition: detect.h:591
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
uint32_t id
Definition: detect.h:555
#define FALSE
void DetectHttpHHRegister(void)
Registers the keyword handlers for the "http_host" keyword.
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1391
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1200
Handle HTTP host header. HHHD - Http Host Header Data.
Signature container.
Definition: detect.h:522
#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:322
main detection engine ctx
Definition: detect.h:761
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1198
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:319
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:1202
struct SigMatch_ ** smlists
Definition: detect.h:516
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
SigMatchCtx * ctx
Definition: detect.h:321
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:1369
const char * url
Definition: detect.h:1203
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:343
#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:1194
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1192
a single match condition for a signature
Definition: detect.h:318