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