suricata
detect-http-ua.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 Anoop Saldanha <anoopsaldanha@gmail.com>
29  *
30  * Implements support for the http_user_agent keyword.
31  */
32 
33 #include "suricata-common.h"
34 #include "threads.h"
35 #include "decode.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.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-unittest.h"
52 #include "util-unittest-helper.h"
53 #include "util-spm.h"
54 
55 #include "app-layer.h"
56 #include "app-layer-parser.h"
57 
58 #include "app-layer-htp.h"
59 #include "stream-tcp.h"
60 #include "detect-http-ua.h"
61 
62 static int DetectHttpUASetup(DetectEngineCtx *, Signature *, const char *);
63 #ifdef UNITTESTS
64 static void DetectHttpUARegisterTests(void);
65 #endif
66 static int g_http_ua_buffer_id = 0;
67 static int DetectHttpUserAgentSetup(DetectEngineCtx *, Signature *, const char *);
68 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
69  const DetectEngineTransforms *transforms,
70  Flow *_f, const uint8_t _flow_flags,
71  void *txv, const int list_id);
72 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
73  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
74  const int list_id);
75 
76 /**
77  * \brief Registers the keyword handlers for the "http_user_agent" keyword.
78  */
80 {
81  /* http_user_agent content modifier */
82  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].name = "http_user_agent";
83  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].desc = "content modifier to match only on the HTTP User-Agent header";
84  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].url = "/rules/http-keywords.html#http-user-agent";
85  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].Setup = DetectHttpUASetup;
86 #ifdef UNITTESTS
87  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].RegisterTests = DetectHttpUARegisterTests;
88 #endif
92 
93  /* http.user_agent sticky buffer */
94  sigmatch_table[DETECT_HTTP_UA].name = "http.user_agent";
95  sigmatch_table[DETECT_HTTP_UA].desc = "sticky buffer to match specifically and only on the HTTP User Agent buffer";
96  sigmatch_table[DETECT_HTTP_UA].url = "/rules/http-keywords.html#http-user-agent";
97  sigmatch_table[DETECT_HTTP_UA].Setup = DetectHttpUserAgentSetup;
100 
102  HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetData);
103 
105  GetData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
106 
108  HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
109 
111  GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
112 
113  DetectBufferTypeSetDescriptionByName("http_user_agent",
114  "http user agent");
115 
116  g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent");
117 }
118 
119 /**
120  * \brief The setup function for the http_user_agent keyword for a signature.
121  *
122  * \param de_ctx Pointer to the detection engine context.
123  * \param s Pointer to the signature for the current Signature being
124  * parsed from the rules.
125  * \param m Pointer to the head of the SigMatch for the current rule
126  * being parsed.
127  * \param arg Pointer to the string holding the keyword value.
128  *
129  * \retval 0 On success
130  * \retval -1 On failure
131  */
132 int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
133 {
135  de_ctx, s, arg, DETECT_AL_HTTP_USER_AGENT, g_http_ua_buffer_id, ALPROTO_HTTP1);
136 }
137 
138 /**
139  * \brief this function setup the http.user_agent keyword used in the rule
140  *
141  * \param de_ctx Pointer to the Detection Engine Context
142  * \param s Pointer to the Signature to which the current keyword belongs
143  * \param str Should hold an empty string always
144  *
145  * \retval 0 On success
146  */
147 static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
148 {
149  if (DetectBufferSetActiveList(de_ctx, s, g_http_ua_buffer_id) < 0)
150  return -1;
152  return -1;
153  return 0;
154 }
155 
156 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
157  const DetectEngineTransforms *transforms, Flow *_f,
158  const uint8_t _flow_flags, void *txv, const int list_id)
159 {
160  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
161  if (buffer->inspect == NULL) {
162  htp_tx_t *tx = (htp_tx_t *)txv;
163 
164  if (tx->request_headers == NULL)
165  return NULL;
166 
167  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
168  "User-Agent");
169  if (h == NULL || h->value == NULL) {
170  SCLogDebug("HTTP UA header not present in this request");
171  return NULL;
172  }
173 
174  const uint32_t data_len = bstr_len(h->value);
175  const uint8_t *data = bstr_ptr(h->value);
176 
177  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
178  InspectionBufferApplyTransforms(buffer, transforms);
179  }
180 
181  return buffer;
182 }
183 
184 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
185  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
186  const int list_id)
187 {
188  SCEnter();
189 
190  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
191  if (buffer->inspect == NULL) {
192  uint32_t b_len = 0;
193  const uint8_t *b = NULL;
194 
195  if (rs_http2_tx_get_useragent(txv, &b, &b_len) != 1)
196  return NULL;
197  if (b == NULL || b_len == 0)
198  return NULL;
199 
200  InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
201  InspectionBufferApplyTransforms(buffer, transforms);
202  }
203 
204  return buffer;
205 }
206 
207 #ifdef UNITTESTS
209 #endif /* UNITTESTS */
210 
211 /**
212  * @}
213  */
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
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:2154
flow-util.h
SIGMATCH_INFO_CONTENT_MODIFIER
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1510
SigTableElmt_::name
const char * name
Definition: detect.h:1304
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:408
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DETECT_AL_HTTP_USER_AGENT
@ DETECT_AL_HTTP_USER_AGENT
Definition: detect-engine-register.h:178
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1500
util-unittest-helper.h
detect-http-ua.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
app-layer-htp.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
DetectHttpUARegister
void DetectHttpUARegister(void)
Registers the keyword handlers for the "http_user_agent" keyword.
Definition: detect-http-ua.c:79
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:1302
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:194
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:64
DETECT_HTTP_UA
@ DETECT_HTTP_UA
Definition: detect-engine-register.h:179
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1711
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
util-spm.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
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:1595
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:70
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
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:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
flow.h
detect-http-user-agent.c
Handle HTTP user agent match.
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1296
app-layer.h