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  if (DetectBufferSetActiveList(s, g_http_cookie_buffer_id) < 0)
156  return -1;
157 
159  return -1;
160 
161  return 0;
162 }
163 
164 static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
165  const DetectEngineTransforms *transforms, Flow *_f,
166  const uint8_t _flow_flags, void *txv, const int list_id)
167 {
168  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
169  if (buffer->inspect == NULL) {
170  htp_tx_t *tx = (htp_tx_t *)txv;
171 
172  if (tx->request_headers == NULL)
173  return NULL;
174 
175  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
176  "Cookie");
177  if (h == NULL || h->value == NULL) {
178  SCLogDebug("HTTP cookie header not present in this request");
179  return NULL;
180  }
181 
182  const uint32_t data_len = bstr_len(h->value);
183  const uint8_t *data = bstr_ptr(h->value);
184 
185  InspectionBufferSetup(buffer, data, data_len);
186  InspectionBufferApplyTransforms(buffer, transforms);
187  }
188 
189  return buffer;
190 }
191 
192 static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
193  const DetectEngineTransforms *transforms, Flow *_f,
194  const uint8_t _flow_flags, void *txv, const int list_id)
195 {
196  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
197  if (buffer->inspect == NULL) {
198  htp_tx_t *tx = (htp_tx_t *)txv;
199 
200  if (tx->response_headers == NULL)
201  return NULL;
202 
203  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->response_headers,
204  "Set-Cookie");
205  if (h == NULL || h->value == NULL) {
206  SCLogDebug("HTTP cookie header not present in this request");
207  return NULL;
208  }
209 
210  const uint32_t data_len = bstr_len(h->value);
211  const uint8_t *data = bstr_ptr(h->value);
212 
213  InspectionBufferSetup(buffer, data, data_len);
214  InspectionBufferApplyTransforms(buffer, transforms);
215  }
216 
217  return buffer;
218 }
219 
220 /******************************** UNITESTS **********************************/
221 
222 #ifdef UNITTESTS
224 #endif /* UNITTESTS */
225 
226 /**
227  * @}
228  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1391
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
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
main detection engine ctx
Definition: detect.h:761
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1198
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:1202
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
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:1369
const char * url
Definition: detect.h:1203
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:343
#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:1194
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1192