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 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
80  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
81  const int list_id);
82 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
83  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
84  const int list_id);
85 /**
86  * \brief Registration function for keyword: http_cookie
87  */
89 {
90  /* http_cookie content modifier */
91  sigmatch_table[DETECT_AL_HTTP_COOKIE].name = "http_cookie";
92  sigmatch_table[DETECT_AL_HTTP_COOKIE].desc = "content modifier to match only on the HTTP cookie-buffer";
93  sigmatch_table[DETECT_AL_HTTP_COOKIE].url = "/rules/http-keywords.html#http-cookie";
94  sigmatch_table[DETECT_AL_HTTP_COOKIE].Setup = DetectHttpCookieSetup;
95 #ifdef UNITTESTS
97 #endif
101 
102  /* http.cookie sticky buffer */
103  sigmatch_table[DETECT_HTTP_COOKIE].name = "http.cookie";
104  sigmatch_table[DETECT_HTTP_COOKIE].desc = "sticky buffer to match on the HTTP Cookie/Set-Cookie buffers";
105  sigmatch_table[DETECT_HTTP_COOKIE].url = "/rules/http-keywords.html#http-cookie";
106  sigmatch_table[DETECT_HTTP_COOKIE].Setup = DetectHttpCookieSetupSticky;
109 
111  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetRequestData);
113  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetResponseData);
114 
116  GetRequestData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
118  GetResponseData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
119 
121  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetRequestData2);
123  HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetResponseData2);
124 
126  GetRequestData2, ALPROTO_HTTP2, HTTP2StateDataClient);
128  GetResponseData2, ALPROTO_HTTP2, HTTP2StateDataServer);
129 
131  "http cookie header");
132 
133  g_http_cookie_buffer_id = DetectBufferTypeGetByName("http_cookie");
134 }
135 
136 /**
137  * \brief this function setups the http_cookie modifier keyword used in the rule
138  *
139  * \param de_ctx Pointer to the Detection Engine Context
140  * \param s Pointer to the Signature to which the current keyword belongs
141  * \param str Should hold an empty string always
142  *
143  * \retval 0 On success
144  * \retval -1 On failure
145  */
146 
147 static int DetectHttpCookieSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
148 {
150  de_ctx, s, str, DETECT_AL_HTTP_COOKIE, g_http_cookie_buffer_id, ALPROTO_HTTP1);
151 }
152 
153 /**
154  * \brief this function setup the http.user_agent keyword used in the rule
155  *
156  * \param de_ctx Pointer to the Detection Engine Context
157  * \param s Pointer to the Signature to which the current keyword belongs
158  * \param str Should hold an empty string always
159  *
160  * \retval 0 On success
161  */
162 static int DetectHttpCookieSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
163 {
164  if (DetectBufferSetActiveList(s, g_http_cookie_buffer_id) < 0)
165  return -1;
166 
168  return -1;
169 
170  return 0;
171 }
172 
173 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
174  const DetectEngineTransforms *transforms, Flow *_f,
175  const uint8_t _flow_flags, void *txv, const int list_id)
176 {
177  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
178  if (buffer->inspect == NULL) {
179  htp_tx_t *tx = (htp_tx_t *)txv;
180 
181  if (tx->request_headers == NULL)
182  return NULL;
183 
184  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
185  "Cookie");
186  if (h == NULL || h->value == NULL) {
187  SCLogDebug("HTTP cookie header not present in this request");
188  return NULL;
189  }
190 
191  const uint32_t data_len = bstr_len(h->value);
192  const uint8_t *data = bstr_ptr(h->value);
193 
194  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
195  InspectionBufferApplyTransforms(buffer, transforms);
196  }
197 
198  return buffer;
199 }
200 
201 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
202  const DetectEngineTransforms *transforms, Flow *_f,
203  const uint8_t _flow_flags, void *txv, const int list_id)
204 {
205  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
206  if (buffer->inspect == NULL) {
207  htp_tx_t *tx = (htp_tx_t *)txv;
208 
209  if (tx->response_headers == NULL)
210  return NULL;
211 
212  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->response_headers,
213  "Set-Cookie");
214  if (h == NULL || h->value == NULL) {
215  SCLogDebug("HTTP cookie header not present in this request");
216  return NULL;
217  }
218 
219  const uint32_t data_len = bstr_len(h->value);
220  const uint8_t *data = bstr_ptr(h->value);
221 
222  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
223  InspectionBufferApplyTransforms(buffer, transforms);
224  }
225 
226  return buffer;
227 }
228 
229 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
230  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
231  const int list_id)
232 {
233  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
234  if (buffer->inspect == NULL) {
235  uint32_t b_len = 0;
236  const uint8_t *b = NULL;
237 
238  if (rs_http2_tx_get_cookie(txv, STREAM_TOSERVER, &b, &b_len) != 1)
239  return NULL;
240  if (b == NULL || b_len == 0)
241  return NULL;
242 
243  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
244  InspectionBufferApplyTransforms(buffer, transforms);
245  }
246 
247  return buffer;
248 }
249 
250 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
251  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
252  const int list_id)
253 {
254  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
255  if (buffer->inspect == NULL) {
256  uint32_t b_len = 0;
257  const uint8_t *b = NULL;
258 
259  if (rs_http2_tx_get_cookie(txv, STREAM_TOCLIENT, &b, &b_len) != 1)
260  return NULL;
261  if (b == NULL || b_len == 0)
262  return NULL;
263 
264  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
265  InspectionBufferApplyTransforms(buffer, transforms);
266  }
267 
268  return buffer;
269 }
270 
271 /******************************** UNITESTS **********************************/
272 
273 #ifdef UNITTESTS
275 #endif /* UNITTESTS */
276 
277 /**
278  * @}
279  */
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1395
SigTableElmt_::name
const char * name
Definition: detect.h:1201
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:378
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:344
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:676
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
app-layer-htp.h
decode.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
util-print.h
detect-engine-mpm.h
detect.h
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1199
app-layer-parser.h
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:1617
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:174
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
DETECT_AL_HTTP_COOKIE
@ DETECT_AL_HTTP_COOKIE
Definition: detect-engine-register.h:118
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
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
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
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:272
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:1091
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:63
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DETECT_HTTP_COOKIE
@ DETECT_HTTP_COOKIE
Definition: detect-engine-register.h:119
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:857
flow.h
flow-var.h
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
app-layer.h