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 "decode.h"
36 #include "detect.h"
37 
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-error.h"
51 #include "util-unittest.h"
52 #include "util-unittest-helper.h"
53 #include "util-spm.h"
54 #include "util-print.h"
55 
56 #include "app-layer.h"
57 #include "app-layer-parser.h"
58 
59 #include "app-layer-htp.h"
60 #include "detect-http-cookie.h"
61 #include "stream-tcp.h"
62 
63 static int DetectHttpCookieSetup (DetectEngineCtx *, Signature *, const char *);
64 static int DetectHttpCookieSetupSticky (DetectEngineCtx *, Signature *, const char *);
65 #ifdef UNITTESTS
66 static void DetectHttpCookieRegisterTests(void);
67 #endif
68 static int g_http_cookie_buffer_id = 0;
69 
70 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
71  const DetectEngineTransforms *transforms,
72  Flow *_f, const uint8_t _flow_flags,
73  void *txv, const int list_id);
74 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
75  const DetectEngineTransforms *transforms,
76  Flow *_f, const uint8_t _flow_flags,
77  void *txv, const int list_id);
78 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
79  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
80  const int list_id);
81 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
82  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
83  const int list_id);
84 /**
85  * \brief Registration function for keyword: http_cookie
86  */
88 {
89  /* http_cookie content modifier */
90  sigmatch_table[DETECT_HTTP_COOKIE_CM].name = "http_cookie";
92  "content modifier to match only on the HTTP cookie-buffer";
93  sigmatch_table[DETECT_HTTP_COOKIE_CM].url = "/rules/http-keywords.html#http-cookie";
94  sigmatch_table[DETECT_HTTP_COOKIE_CM].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 
114 
118  GetResponseData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_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_HTTP_COOKIE_CM, g_http_cookie_buffer_id, ALPROTO_HTTP1);
151 }
152 
153 /**
154  * \brief this function setup the http.cookie 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(de_ctx, 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 (htp_tx_request_headers(tx) == NULL)
182  return NULL;
183 
184  const htp_header_t *h = htp_tx_request_header(tx, "Cookie");
185  if (h == NULL || htp_header_value(h) == NULL) {
186  SCLogDebug("HTTP cookie header not present in this request");
187  return NULL;
188  }
189 
190  const uint32_t data_len = htp_header_value_len(h);
191  const uint8_t *data = htp_header_value_ptr(h);
192 
193  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
194  InspectionBufferApplyTransforms(buffer, transforms);
195  }
196 
197  return buffer;
198 }
199 
200 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
201  const DetectEngineTransforms *transforms, Flow *_f,
202  const uint8_t _flow_flags, void *txv, const int list_id)
203 {
204  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
205  if (buffer->inspect == NULL) {
206  htp_tx_t *tx = (htp_tx_t *)txv;
207 
208  if (htp_tx_response_headers(tx) == NULL)
209  return NULL;
210 
211  const htp_header_t *h = htp_tx_response_header(tx, "Set-Cookie");
212  if (h == NULL || htp_header_value(h) == NULL) {
213  SCLogDebug("HTTP cookie header not present in this request");
214  return NULL;
215  }
216 
217  const uint32_t data_len = htp_header_value_len(h);
218  const uint8_t *data = htp_header_value_ptr(h);
219 
220  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
221  InspectionBufferApplyTransforms(buffer, transforms);
222  }
223 
224  return buffer;
225 }
226 
227 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
228  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
229  const int list_id)
230 {
231  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
232  if (buffer->inspect == NULL) {
233  uint32_t b_len = 0;
234  const uint8_t *b = NULL;
235 
236  if (rs_http2_tx_get_cookie(txv, STREAM_TOSERVER, &b, &b_len) != 1)
237  return NULL;
238  if (b == NULL || b_len == 0)
239  return NULL;
240 
241  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
242  InspectionBufferApplyTransforms(buffer, transforms);
243  }
244 
245  return buffer;
246 }
247 
248 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
249  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
250  const int list_id)
251 {
252  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
253  if (buffer->inspect == NULL) {
254  uint32_t b_len = 0;
255  const uint8_t *b = NULL;
256 
257  if (rs_http2_tx_get_cookie(txv, STREAM_TOCLIENT, &b, &b_len) != 1)
258  return NULL;
259  if (b == NULL || b_len == 0)
260  return NULL;
261 
262  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
263  InspectionBufferApplyTransforms(buffer, transforms);
264  }
265 
266  return buffer;
267 }
268 
269 /******************************** UNITESTS **********************************/
270 
271 #ifdef UNITTESTS
273 #endif /* UNITTESTS */
274 
275 /**
276  * @}
277  */
htp_header_value_len
#define htp_header_value_len(h)
Definition: app-layer-htp-libhtp.h:191
SigTableElmt_::url
const char * url
Definition: detect.h:1322
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1770
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1527
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
DetectEngineInspectBufferGeneric
uint8_t 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:2157
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1525
SigTableElmt_::name
const char * name
Definition: detect.h:1319
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:410
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1359
InspectionBuffer
Definition: detect.h:375
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
htp_tx_response_headers
#define htp_tx_response_headers(tx)
Definition: app-layer-htp-libhtp.h:174
htp_tx_response_header
#define htp_tx_response_header(tx, header)
Definition: app-layer-htp-libhtp.h:185
DETECT_HTTP_COOKIE_CM
@ DETECT_HTTP_COOKIE_CM
Definition: detect-engine-register.h:151
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1503
util-unittest-helper.h
htp_header_value
#define htp_header_value(h)
Definition: app-layer-htp-libhtp.h:193
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
htp_tx_request_headers
#define htp_tx_request_headers(tx)
Definition: app-layer-htp-libhtp.h:173
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
app-layer-htp.h
decode.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1109
util-print.h
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1317
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
app-layer-parser.h
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:220
htp_header_value_ptr
#define htp_header_value_ptr(h)
Definition: app-layer-htp-libhtp.h:192
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1714
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
HTP_REQUEST_PROGRESS_HEADERS
#define HTP_REQUEST_PROGRESS_HEADERS
Definition: app-layer-htp-libhtp.h:89
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
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:1598
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1503
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:242
DETECT_HTTP_COOKIE
@ DETECT_HTTP_COOKIE
Definition: detect-engine-register.h:152
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1193
flow.h
flow-var.h
htp_tx_request_header
#define htp_tx_request_header(tx, header)
Definition: app-layer-htp-libhtp.h:184
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1311
app-layer.h