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 
73 /**
74  * \brief Registers the keyword handlers for the "http_user_agent" keyword.
75  */
77 {
78  /* http_user_agent content modifier */
79  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].name = "http_user_agent";
80  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].desc = "content modifier to match only on the HTTP User-Agent header";
81  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-user-agent";
82  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].Setup = DetectHttpUASetup;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_AL_HTTP_USER_AGENT].RegisterTests = DetectHttpUARegisterTests;
85 #endif
89 
90  /* http.user_agent sticky buffer */
91  sigmatch_table[DETECT_HTTP_UA].name = "http.user_agent";
92  sigmatch_table[DETECT_HTTP_UA].desc = "sticky buffer to match specifically and only on the HTTP User Agent buffer";
93  sigmatch_table[DETECT_HTTP_UA].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-user-agent";
94  sigmatch_table[DETECT_HTTP_UA].Setup = DetectHttpUserAgentSetup;
97 
99  SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
101 
102  DetectAppLayerMpmRegister2("http_user_agent", SIG_FLAG_TOSERVER, 2,
104  HTP_REQUEST_HEADERS);
105 
106  DetectBufferTypeSetDescriptionByName("http_user_agent",
107  "http user agent");
108 
109  g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent");
110 }
111 
112 /**
113  * \brief The setup function for the http_user_agent keyword for a signature.
114  *
115  * \param de_ctx Pointer to the detection engine context.
116  * \param s Pointer to the signature for the current Signature being
117  * parsed from the rules.
118  * \param m Pointer to the head of the SigMatch for the current rule
119  * being parsed.
120  * \param arg Pointer to the string holding the keyword value.
121  *
122  * \retval 0 On success
123  * \retval -1 On failure
124  */
125 int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
126 {
127  return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
129  g_http_ua_buffer_id,
130  ALPROTO_HTTP);
131 }
132 
133 /**
134  * \brief this function setup the http.user_agent keyword used in the rule
135  *
136  * \param de_ctx Pointer to the Detection Engine Context
137  * \param s Pointer to the Signature to which the current keyword belongs
138  * \param str Should hold an empty string always
139  *
140  * \retval 0 On success
141  */
142 static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
143 {
144  if (DetectBufferSetActiveList(s, g_http_ua_buffer_id) < 0)
145  return -1;
147  return -1;
148  return 0;
149 }
150 
151 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
152  const DetectEngineTransforms *transforms, Flow *_f,
153  const uint8_t _flow_flags, void *txv, const int list_id)
154 {
155  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
156  if (buffer->inspect == NULL) {
157  htp_tx_t *tx = (htp_tx_t *)txv;
158 
159  if (tx->request_headers == NULL)
160  return NULL;
161 
162  htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
163  "User-Agent");
164  if (h == NULL || h->value == NULL) {
165  SCLogDebug("HTTP UA header not present in this request");
166  return NULL;
167  }
168 
169  const uint32_t data_len = bstr_len(h->value);
170  const uint8_t *data = bstr_ptr(h->value);
171 
172  InspectionBufferSetup(buffer, data, data_len);
173  InspectionBufferApplyTransforms(buffer, transforms);
174  }
175 
176  return buffer;
177 }
178 
179 #ifdef UNITTESTS
181 #endif /* UNITTESTS */
182 
183 /**
184  * @}
185  */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
#define SCLogDebug(...)
Definition: util-debug.h:335
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id)
#define SIGMATCH_INFO_CONTENT_MODIFIER
Definition: detect.h:1350
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
Handle HTTP user agent match.
main detection engine ctx
Definition: detect.h:720
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1352
Data structures and function prototypes for keeping state for the detection engine.
#define SIG_FLAG_TOSERVER
Definition: detect.h:243
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:1158
void DetectHttpUARegister(void)
Registers the keyword handlers for the "http_user_agent" keyword.
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:1162
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:1328
const char * url
Definition: detect.h:1163
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:349
#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:1154
Flow data structure.
Definition: flow.h:327
void(* RegisterTests)(void)
Definition: detect.h:1152
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine