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-buffer.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 static int g_http2_thread_id = 0;
71 
72 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
73  const DetectEngineTransforms *transforms,
74  Flow *_f, const uint8_t _flow_flags,
75  void *txv, const int list_id);
76 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
77  const DetectEngineTransforms *transforms,
78  Flow *_f, const uint8_t _flow_flags,
79  void *txv, const int list_id);
80 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
81  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
82  const int list_id);
83 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
84  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
85  const int list_id);
86 /**
87  * \brief Registration function for keyword: http_cookie
88  */
90 {
91  /* http_cookie content modifier */
92  sigmatch_table[DETECT_HTTP_COOKIE_CM].name = "http_cookie";
94  "content modifier to match only on the HTTP cookie-buffer";
95  sigmatch_table[DETECT_HTTP_COOKIE_CM].url = "/rules/http-keywords.html#http-cookie";
96  sigmatch_table[DETECT_HTTP_COOKIE_CM].Setup = DetectHttpCookieSetup;
97 #ifdef UNITTESTS
99 #endif
103 
104  /* http.cookie sticky buffer */
105  sigmatch_table[DETECT_HTTP_COOKIE].name = "http.cookie";
106  sigmatch_table[DETECT_HTTP_COOKIE].desc = "sticky buffer to match on the HTTP Cookie/Set-Cookie buffers";
107  sigmatch_table[DETECT_HTTP_COOKIE].url = "/rules/http-keywords.html#http-cookie";
108  sigmatch_table[DETECT_HTTP_COOKIE].Setup = DetectHttpCookieSetupSticky;
111 
113  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetRequestData);
115  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetResponseData);
116 
118  GetRequestData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
120  GetResponseData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
121 
123  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric, GetRequestData2);
125  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric,
126  GetResponseData2);
127 
129  PrefilterGenericMpmRegister, GetRequestData2, ALPROTO_HTTP2, HTTP2TxTypeStream,
130  HTTP2ProgHeaders);
132  PrefilterGenericMpmRegister, GetResponseData2, ALPROTO_HTTP2, HTTP2TxTypeStream,
133  HTTP2ProgHeaders);
134 
136  "http cookie header");
137 
138  g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs(
139  "http_cookie", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree);
140 
141  g_http_cookie_buffer_id = DetectBufferTypeGetByName("http_cookie");
142 }
143 
144 /**
145  * \brief this function setups the http_cookie modifier 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  * \retval -1 On failure
153  */
154 
155 static int DetectHttpCookieSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
156 {
158  de_ctx, s, str, DETECT_HTTP_COOKIE_CM, g_http_cookie_buffer_id, ALPROTO_HTTP1);
159 }
160 
161 /**
162  * \brief this function setup the http.cookie keyword used in the rule
163  *
164  * \param de_ctx Pointer to the Detection Engine Context
165  * \param s Pointer to the Signature to which the current keyword belongs
166  * \param str Should hold an empty string always
167  *
168  * \retval 0 On success
169  */
170 static int DetectHttpCookieSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
171 {
172  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_cookie_buffer_id) < 0)
173  return -1;
174 
176  return -1;
177 
178  return 0;
179 }
180 
181 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
182  const DetectEngineTransforms *transforms, Flow *_f,
183  const uint8_t _flow_flags, void *txv, const int list_id)
184 {
185  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
186  if (buffer->inspect == NULL) {
187  htp_tx_t *tx = (htp_tx_t *)txv;
188 
189  if (htp_tx_request_headers(tx) == NULL)
190  return NULL;
191 
192  const htp_header_t *h = htp_tx_request_header(tx, "Cookie");
193  if (h == NULL || htp_header_value(h) == NULL) {
194  SCLogDebug("HTTP cookie header not present in this request");
195  return NULL;
196  }
197 
198  const uint32_t data_len = (uint32_t)htp_header_value_len(h);
199  const uint8_t *data = htp_header_value_ptr(h);
200 
202  det_ctx, list_id, buffer, data, data_len, transforms);
203  }
204 
205  return buffer;
206 }
207 
208 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
209  const DetectEngineTransforms *transforms, Flow *_f,
210  const uint8_t _flow_flags, void *txv, const int list_id)
211 {
212  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
213  if (buffer->inspect == NULL) {
214  htp_tx_t *tx = (htp_tx_t *)txv;
215 
216  if (htp_tx_response_headers(tx) == NULL)
217  return NULL;
218 
219  const htp_header_t *h = htp_tx_response_header(tx, "Set-Cookie");
220  if (h == NULL || htp_header_value(h) == NULL) {
221  SCLogDebug("HTTP cookie header not present in this request");
222  return NULL;
223  }
224 
225  const uint32_t data_len = (uint32_t)htp_header_value_len(h);
226  const uint8_t *data = htp_header_value_ptr(h);
227 
229  det_ctx, list_id, buffer, data, data_len, transforms);
230  }
231 
232  return buffer;
233 }
234 
235 static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx,
236  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
237  const int list_id)
238 {
239  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
240  if (buffer->inspect == NULL) {
241  uint32_t b_len = 0;
242  const uint8_t *b = NULL;
243 
244  void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
245  if (thread_buf == NULL)
246  return NULL;
247  if (SCHttp2TxGetCookie(txv, STREAM_TOSERVER, &b, &b_len, thread_buf) != 1)
248  return NULL;
249  if (b == NULL || b_len == 0)
250  return NULL;
251 
252  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
253  }
254 
255  return buffer;
256 }
257 
258 static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx,
259  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
260  const int list_id)
261 {
262  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
263  if (buffer->inspect == NULL) {
264  uint32_t b_len = 0;
265  const uint8_t *b = NULL;
266 
267  void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
268  if (thread_buf == NULL)
269  return NULL;
270  if (SCHttp2TxGetCookie(txv, STREAM_TOCLIENT, &b, &b_len, thread_buf) != 1)
271  return NULL;
272  if (b == NULL || b_len == 0)
273  return NULL;
274 
275  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
276  }
277 
278  return buffer;
279 }
280 
281 /******************************** UNITESTS **********************************/
282 
283 #ifdef UNITTESTS
285 #endif /* UNITTESTS */
286 
287 /**
288  * @}
289  */
SigTableElmt_::url
const char * url
Definition: detect.h:1521
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:306
SigTableElmt_::desc
const char * desc
Definition: detect.h:1520
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
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:2226
flow-util.h
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1518
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:391
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1509
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SCDetectThreadCtxGetGlobalKeywordThreadCtx
void * SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:4057
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:981
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:197
DETECT_HTTP_COOKIE_CM
@ DETECT_HTTP_COOKIE_CM
Definition: detect-engine-register.h:159
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2461
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1500
detect-pcre.h
DetectAppLayerMpmRegisterSubState
void DetectAppLayerMpmRegisterSubState(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t sub_state, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:167
detect-engine-prefilter.h
util-unittest.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:276
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1455
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
app-layer-htp.h
decode.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1300
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:1674
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1516
app-layer-parser.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect-engine-register.h:326
DetectEngineContentModifierBufferSetup
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
Definition: detect-parse.c:146
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
util-spm.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
detect-engine-buffer.h
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:299
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:316
detect-parse.h
Signature_
Signature container.
Definition: detect.h:682
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:77
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:328
DETECT_HTTP_COOKIE
@ DETECT_HTTP_COOKIE
Definition: detect-engine-register.h:160
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1552
flow.h
SCDetectRegisterThreadCtxGlobalFuncs
int SCDetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:4013
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1507
app-layer.h