suricata
detect-http-cookie.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 Gurvinder Singh <gurvindersinghdahiya@gmail.com>
29  *
30  * Implements the http_cookie keyword
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "debug.h"
36 #include "decode.h"
37 #include "detect.h"
38 
39 #include "detect-parse.h"
40 #include "detect-engine.h"
41 #include "detect-engine-mpm.h"
43 #include "detect-content.h"
44 #include "detect-pcre.h"
45 
46 #include "flow.h"
47 #include "flow-var.h"
48 #include "flow-util.h"
49 
50 #include "util-debug.h"
51 #include "util-error.h"
52 #include "util-unittest.h"
53 #include "util-unittest-helper.h"
54 #include "util-spm.h"
55 #include "util-print.h"
56 
57 #include "app-layer.h"
58 #include "app-layer-parser.h"
59 
60 #include "app-layer-htp.h"
61 #include "detect-http-cookie.h"
62 #include "stream-tcp.h"
63 
64 static int DetectHttpCookieSetup (DetectEngineCtx *, Signature *, const char *);
65 static int DetectHttpCookieSetupSticky (DetectEngineCtx *, Signature *, const char *);
66 #ifdef UNITTESTS
67 static void DetectHttpCookieRegisterTests(void);
68 #endif
69 static int g_http_cookie_buffer_id = 0;
70 
71 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
72  const DetectEngineTransforms *transforms,
73  Flow *_f, const uint8_t _flow_flags,
74  void *txv, const int list_id);
75 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
76  const DetectEngineTransforms *transforms,
77  Flow *_f, const uint8_t _flow_flags,
78  void *txv, const int list_id);
79 
80 /**
81  * \brief Registration function for keyword: http_cookie
82  */
84 {
85  /* http_cookie content modifier */
86  sigmatch_table[DETECT_AL_HTTP_COOKIE].name = "http_cookie";
87  sigmatch_table[DETECT_AL_HTTP_COOKIE].desc = "content modifier to match only on the HTTP cookie-buffer";
88  sigmatch_table[DETECT_AL_HTTP_COOKIE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-cookie";
89  sigmatch_table[DETECT_AL_HTTP_COOKIE].Setup = DetectHttpCookieSetup;
90 #ifdef UNITTESTS
92 #endif
96 
97  /* http.cookie sticky buffer */
98  sigmatch_table[DETECT_HTTP_COOKIE].name = "http.cookie";
99  sigmatch_table[DETECT_HTTP_COOKIE].desc = "sticky buffer to match on the HTTP Cookie/Set-Cookie buffers";
100  sigmatch_table[DETECT_HTTP_COOKIE].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-cookie";
101  sigmatch_table[DETECT_HTTP_COOKIE].Setup = DetectHttpCookieSetupSticky;
104 
106  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
107  DetectEngineInspectBufferGeneric, GetRequestData);
109  SIG_FLAG_TOCLIENT, HTP_REQUEST_HEADERS,
110  DetectEngineInspectBufferGeneric, GetResponseData);
111 
113  PrefilterGenericMpmRegister, GetRequestData, ALPROTO_HTTP,
114  HTP_REQUEST_HEADERS);
116  PrefilterGenericMpmRegister, GetResponseData, ALPROTO_HTTP,
117  HTP_REQUEST_HEADERS);
118 
120  "http cookie header");
121 
122  g_http_cookie_buffer_id = DetectBufferTypeGetByName("http_cookie");
123 }
124 
125 /**
126  * \brief this function setups the http_cookie modifier keyword used in the rule
127  *
128  * \param de_ctx Pointer to the Detection Engine Context
129  * \param s Pointer to the Signature to which the current keyword belongs
130  * \param str Should hold an empty string always
131  *
132  * \retval 0 On success
133  * \retval -1 On failure
134  */
135 
136 static int DetectHttpCookieSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
137 {
138  return DetectEngineContentModifierBufferSetup(de_ctx, s, str,
140  g_http_cookie_buffer_id,
141  ALPROTO_HTTP);
142 }
143 
144 /**
145  * \brief this function setup the http.user_agent keyword used in the rule
146  *
147  * \param de_ctx Pointer to the Detection Engine Context
148  * \param s Pointer to the Signature to which the current keyword belongs
149  * \param str Should hold an empty string always
150  *
151  * \retval 0 On success
152  */
153 static int DetectHttpCookieSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
154 {
155  DetectBufferSetActiveList(s, g_http_cookie_buffer_id);
156  s->alproto = ALPROTO_HTTP;
157  return 0;
158 }
159 
160 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
161  const DetectEngineTransforms *transforms, Flow *_f,
162  const uint8_t _flow_flags, void *txv, const int list_id)
163 {
164  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
165  if (buffer->inspect == NULL) {
166  htp_tx_t *tx = (htp_tx_t *)txv;
167 
168  if (tx->request_headers == NULL)
169  return NULL;
170 
171  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
172  "Cookie");
173  if (h == NULL || h->value == NULL) {
174  SCLogDebug("HTTP cookie header not present in this request");
175  return NULL;
176  }
177 
178  const uint32_t data_len = bstr_len(h->value);
179  const uint8_t *data = bstr_ptr(h->value);
180 
181  InspectionBufferSetup(buffer, data, data_len);
182  InspectionBufferApplyTransforms(buffer, transforms);
183  }
184 
185  return buffer;
186 }
187 
188 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
189  const DetectEngineTransforms *transforms, Flow *_f,
190  const uint8_t _flow_flags, void *txv, const int list_id)
191 {
192  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
193  if (buffer->inspect == NULL) {
194  htp_tx_t *tx = (htp_tx_t *)txv;
195 
196  if (tx->response_headers == NULL)
197  return NULL;
198 
199  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->response_headers,
200  "Set-Cookie");
201  if (h == NULL || h->value == NULL) {
202  SCLogDebug("HTTP cookie header not present in this request");
203  return NULL;
204  }
205 
206  const uint32_t data_len = bstr_len(h->value);
207  const uint8_t *data = bstr_ptr(h->value);
208 
209  InspectionBufferSetup(buffer, data, data_len);
210  InspectionBufferApplyTransforms(buffer, transforms);
211  }
212 
213  return buffer;
214 }
215 
216 /******************************** UNITESTS **********************************/
217 
218 #ifdef UNITTESTS
220 #endif /* UNITTESTS */
221 
222 /**
223  * @}
224  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1406
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1149
#define SCLogDebug(...)
Definition: util-debug.h:335
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1353
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1163
Signature container.
Definition: detect.h:495
main detection engine ctx
Definition: detect.h:723
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:242
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1355
#define SIG_FLAG_TOSERVER
Definition: detect.h:241
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:1161
AppProto alproto
Definition: detect.h:499
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:143
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.
const char * desc
Definition: detect.h:1165
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:1331
const char * url
Definition: detect.h:1166
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1157
Flow data structure.
Definition: flow.h:324
void(* RegisterTests)(void)
Definition: detect.h:1155
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