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"
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"
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
63static int DetectHttpUASetup(DetectEngineCtx *, Signature *, const char *);
64#ifdef UNITTESTS
65static void DetectHttpUARegisterTests(void);
66#endif
67static int g_http_ua_buffer_id = 0;
68static int DetectHttpUserAgentSetup(DetectEngineCtx *, Signature *, const char *);
69static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
70 const DetectEngineTransforms *transforms,
71 Flow *_f, const uint8_t _flow_flags,
72 void *txv, const int list_id);
73static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
74 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
75 const int list_id);
76
77/**
78 * \brief Registers the keyword handlers for the "http_user_agent" keyword.
79 */
81{
82 /* http_user_agent content modifier */
83 sigmatch_table[DETECT_HTTP_USER_AGENT].name = "http_user_agent";
85 "content modifier to match only on the HTTP User-Agent header";
86 sigmatch_table[DETECT_HTTP_USER_AGENT].url = "/rules/http-keywords.html#http-user-agent";
87 sigmatch_table[DETECT_HTTP_USER_AGENT].Setup = DetectHttpUASetup;
88#ifdef UNITTESTS
89 sigmatch_table[DETECT_HTTP_USER_AGENT].RegisterTests = DetectHttpUARegisterTests;
90#endif
94
95 /* http.user_agent sticky buffer */
96 sigmatch_table[DETECT_HTTP_UA].name = "http.user_agent";
97 sigmatch_table[DETECT_HTTP_UA].desc = "sticky buffer to match specifically and only on the HTTP User Agent buffer";
98 sigmatch_table[DETECT_HTTP_UA].url = "/rules/http-keywords.html#http-user-agent";
99 sigmatch_table[DETECT_HTTP_UA].Setup = DetectHttpUserAgentSetup;
102
104 HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetData);
105
107 GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
108
110 HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
111
113 GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
114
115 DetectBufferTypeSetDescriptionByName("http_user_agent",
116 "http user agent");
117
118 g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent");
119}
120
121/**
122 * \brief The setup function for the http_user_agent keyword for a signature.
123 *
124 * \param de_ctx Pointer to the detection engine context.
125 * \param s Pointer to the signature for the current Signature being
126 * parsed from the rules.
127 * \param m Pointer to the head of the SigMatch for the current rule
128 * being parsed.
129 * \param arg Pointer to the string holding the keyword value.
130 *
131 * \retval 0 On success
132 * \retval -1 On failure
133 */
134int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
135{
137 de_ctx, s, arg, DETECT_HTTP_USER_AGENT, g_http_ua_buffer_id, ALPROTO_HTTP1);
138}
139
140/**
141 * \brief this function setup the http.user_agent keyword used in the rule
142 *
143 * \param de_ctx Pointer to the Detection Engine Context
144 * \param s Pointer to the Signature to which the current keyword belongs
145 * \param str Should hold an empty string always
146 *
147 * \retval 0 On success
148 */
149static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
150{
151 if (SCDetectBufferSetActiveList(de_ctx, s, g_http_ua_buffer_id) < 0)
152 return -1;
154 return -1;
155 return 0;
156}
157
158static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
159 const DetectEngineTransforms *transforms, Flow *_f,
160 const uint8_t _flow_flags, void *txv, const int list_id)
161{
162 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
163 if (buffer->inspect == NULL) {
164 htp_tx_t *tx = (htp_tx_t *)txv;
165
166 if (htp_tx_request_headers(tx) == NULL)
167 return NULL;
168
169 const htp_header_t *h = htp_tx_request_header(tx, "User-Agent");
170 if (h == NULL || htp_header_value(h) == NULL) {
171 SCLogDebug("HTTP UA header not present in this request");
172 return NULL;
173 }
174
175 const uint32_t data_len = (uint32_t)htp_header_value_len(h);
176 const uint8_t *data = htp_header_value_ptr(h);
177
179 det_ctx, list_id, buffer, data, data_len, transforms);
180 }
181
182 return buffer;
183}
184
185static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
186 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
187 const int list_id)
188{
189 SCEnter();
190
191 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
192 if (buffer->inspect == NULL) {
193 uint32_t b_len = 0;
194 const uint8_t *b = NULL;
195
196 if (SCHttp2TxGetUserAgent(txv, &b, &b_len) != 1)
197 return NULL;
198 if (b == NULL || b_len == 0)
199 return NULL;
200
201 InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
202 }
203
204 return buffer;
205}
206
207#ifdef UNITTESTS
209#endif /* UNITTESTS */
210
211/**
212 * @}
213 */
@ ALPROTO_HTTP2
@ ALPROTO_HTTP
@ ALPROTO_HTTP1
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(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
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
@ DETECT_HTTP_USER_AGENT
Data structures and function prototypes for keeping state for the detection engine.
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
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.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
void DetectHttpUARegister(void)
Registers the keyword handlers for the "http_user_agent" keyword.
Handle HTTP user agent match.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
int DetectEngineContentModifierBufferSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg, int sm_type, int sm_list, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition detect.h:1674
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t alternative
Definition detect.h:1457
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275