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-buffer.h"
41 #include "detect-engine-mpm.h"
42 #include "detect-engine-state.h"
44 #include "detect-content.h"
45 #include "detect-pcre.h"
46 
47 #include "flow.h"
48 #include "flow-var.h"
49 #include "flow-util.h"
50 
51 #include "util-debug.h"
52 #include "util-unittest.h"
53 #include "util-unittest-helper.h"
54 #include "util-spm.h"
55 
56 #include "app-layer.h"
57 #include "app-layer-parser.h"
58 
59 #include "app-layer-htp.h"
60 #include "stream-tcp.h"
61 #include "detect-http-ua.h"
62 
63 static int DetectHttpUASetup(DetectEngineCtx *, Signature *, const char *);
64 #ifdef UNITTESTS
65 static void DetectHttpUARegisterTests(void);
66 #endif
67 static int g_http_ua_buffer_id = 0;
68 static int g_http2_thread_id = 0;
69 static int DetectHttpUserAgentSetup(DetectEngineCtx *, Signature *, const char *);
70 static InspectionBuffer *GetData(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 *GetData2(DetectEngineThreadCtx *det_ctx,
75  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
76  const int list_id);
77 
78 /**
79  * \brief Registers the keyword handlers for the "http_user_agent" keyword.
80  */
82 {
83  /* http_user_agent content modifier */
84  sigmatch_table[DETECT_HTTP_USER_AGENT].name = "http_user_agent";
86  "content modifier to match only on the HTTP User-Agent header";
87  sigmatch_table[DETECT_HTTP_USER_AGENT].url = "/rules/http-keywords.html#http-user-agent";
88  sigmatch_table[DETECT_HTTP_USER_AGENT].Setup = DetectHttpUASetup;
89 #ifdef UNITTESTS
90  sigmatch_table[DETECT_HTTP_USER_AGENT].RegisterTests = DetectHttpUARegisterTests;
91 #endif
95 
96  /* http.user_agent sticky buffer */
97  sigmatch_table[DETECT_HTTP_UA].name = "http.user_agent";
98  sigmatch_table[DETECT_HTTP_UA].desc = "sticky buffer to match specifically and only on the HTTP User Agent buffer";
99  sigmatch_table[DETECT_HTTP_UA].url = "/rules/http-keywords.html#http-user-agent";
100  sigmatch_table[DETECT_HTTP_UA].Setup = DetectHttpUserAgentSetup;
103 
105  HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetData);
106 
108  GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
109 
111  HTTP2TxTypeStream, HTTP2ProgHeaders, DetectEngineInspectBufferGeneric, GetData2);
112 
114  PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2TxTypeStream,
115  HTTP2ProgHeaders);
116 
117  DetectBufferTypeSetDescriptionByName("http_user_agent",
118  "http user agent");
119 
120  g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs(
121  "http_user_agent", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree);
122 
123  g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent");
124 }
125 
126 /**
127  * \brief The setup function for the http_user_agent keyword for a signature.
128  *
129  * \param de_ctx Pointer to the detection engine context.
130  * \param s Pointer to the signature for the current Signature being
131  * parsed from the rules.
132  * \param m Pointer to the head of the SigMatch for the current rule
133  * being parsed.
134  * \param arg Pointer to the string holding the keyword value.
135  *
136  * \retval 0 On success
137  * \retval -1 On failure
138  */
139 int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
140 {
142  de_ctx, s, arg, DETECT_HTTP_USER_AGENT, g_http_ua_buffer_id, ALPROTO_HTTP1);
143 }
144 
145 /**
146  * \brief this function setup the http.user_agent keyword used in the rule
147  *
148  * \param de_ctx Pointer to the Detection Engine Context
149  * \param s Pointer to the Signature to which the current keyword belongs
150  * \param str Should hold an empty string always
151  *
152  * \retval 0 On success
153  */
154 static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
155 {
156  if (SCDetectBufferSetActiveList(de_ctx, s, g_http_ua_buffer_id) < 0)
157  return -1;
159  return -1;
160  return 0;
161 }
162 
163 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
164  const DetectEngineTransforms *transforms, Flow *_f,
165  const uint8_t _flow_flags, void *txv, const int list_id)
166 {
167  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
168  if (buffer->inspect == NULL) {
169  htp_tx_t *tx = (htp_tx_t *)txv;
170 
171  if (htp_tx_request_headers(tx) == NULL)
172  return NULL;
173 
174  const htp_header_t *h = htp_tx_request_header(tx, "User-Agent");
175  if (h == NULL || htp_header_value(h) == NULL) {
176  SCLogDebug("HTTP UA header not present in this request");
177  return NULL;
178  }
179 
180  const uint32_t data_len = (uint32_t)htp_header_value_len(h);
181  const uint8_t *data = htp_header_value_ptr(h);
182 
184  det_ctx, list_id, buffer, data, data_len, transforms);
185  }
186 
187  return buffer;
188 }
189 
190 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
191  const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
192  const int list_id)
193 {
194  SCEnter();
195 
196  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
197  if (buffer->inspect == NULL) {
198  uint32_t b_len = 0;
199  const uint8_t *b = NULL;
200  void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
201  if (thread_buf == NULL)
202  return NULL;
203  if (SCHttp2TxGetUserAgent(txv, &b, &b_len, thread_buf) != 1)
204  return NULL;
205  if (b == NULL || b_len == 0)
206  return NULL;
207 
208  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
209  }
210 
211  return buffer;
212 }
213 
214 #ifdef UNITTESTS
216 #endif /* UNITTESTS */
217 
218 /**
219  * @}
220  */
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
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
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
detect-http-ua.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
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1300
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
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:81
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
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:69
DETECT_HTTP_UA
@ DETECT_HTTP_UA
Definition: detect-engine-register.h:192
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_HTTP_USER_AGENT
@ DETECT_HTTP_USER_AGENT
Definition: detect-engine-register.h:191
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
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
detect-http-user-agent.c
Handle HTTP user agent match.
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1507
app-layer.h