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"
42 #include "detect-engine-prefilter.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  */
DetectHttpUARegister(void)79 void DetectHttpUARegister(void)
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
89     sigmatch_table[DETECT_AL_HTTP_USER_AGENT].flags |= SIGMATCH_NOOPT;
90     sigmatch_table[DETECT_AL_HTTP_USER_AGENT].flags |= SIGMATCH_INFO_CONTENT_MODIFIER;
91     sigmatch_table[DETECT_AL_HTTP_USER_AGENT].alternative = DETECT_HTTP_UA;
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;
98     sigmatch_table[DETECT_HTTP_UA].flags |= SIGMATCH_NOOPT;
99     sigmatch_table[DETECT_HTTP_UA].flags |= SIGMATCH_INFO_STICKY_BUFFER;
100 
101     DetectAppLayerInspectEngineRegister2("http_user_agent", ALPROTO_HTTP,
102             SIG_FLAG_TOSERVER, HTP_REQUEST_HEADERS,
103             DetectEngineInspectBufferGeneric, GetData);
104 
105     DetectAppLayerMpmRegister2("http_user_agent", SIG_FLAG_TOSERVER, 2,
106             PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP,
107             HTP_REQUEST_HEADERS);
108 
109     DetectAppLayerInspectEngineRegister2("http_user_agent", ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
110             HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
111 
112     DetectAppLayerMpmRegister2("http_user_agent", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
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  */
DetectHttpUASetup(DetectEngineCtx * de_ctx,Signature * s,const char * arg)134 int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
135 {
136     return DetectEngineContentModifierBufferSetup(de_ctx, s, arg,
137                                                   DETECT_AL_HTTP_USER_AGENT,
138                                                   g_http_ua_buffer_id,
139                                                   ALPROTO_HTTP);
140 }
141 
142 /**
143  * \brief this function setup the http.user_agent keyword used in the rule
144  *
145  * \param de_ctx   Pointer to the Detection Engine Context
146  * \param s        Pointer to the Signature to which the current keyword belongs
147  * \param str      Should hold an empty string always
148  *
149  * \retval 0       On success
150  */
DetectHttpUserAgentSetup(DetectEngineCtx * de_ctx,Signature * s,const char * str)151 static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
152 {
153     if (DetectBufferSetActiveList(s, g_http_ua_buffer_id) < 0)
154         return -1;
155     if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
156         return -1;
157     return 0;
158 }
159 
GetData(DetectEngineThreadCtx * det_ctx,const DetectEngineTransforms * transforms,Flow * _f,const uint8_t _flow_flags,void * txv,const int list_id)160 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
161         const DetectEngineTransforms *transforms, Flow *_f,
162         const uint8_t _flow_flags, void *txv, const int list_id)
163 {
164     InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
165     if (buffer->inspect == NULL) {
166         htp_tx_t *tx = (htp_tx_t *)txv;
167 
168         if (tx->request_headers == NULL)
169             return NULL;
170 
171         htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
172                 "User-Agent");
173         if (h == NULL || h->value == NULL) {
174             SCLogDebug("HTTP UA header not present in this request");
175             return NULL;
176         }
177 
178         const uint32_t data_len = bstr_len(h->value);
179         const uint8_t *data = bstr_ptr(h->value);
180 
181         InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
182         InspectionBufferApplyTransforms(buffer, transforms);
183     }
184 
185     return buffer;
186 }
187 
GetData2(DetectEngineThreadCtx * det_ctx,const DetectEngineTransforms * transforms,Flow * _f,const uint8_t _flow_flags,void * txv,const int list_id)188 static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
189         const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
190         const int list_id)
191 {
192     SCEnter();
193 
194     InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
195     if (buffer->inspect == NULL) {
196         uint32_t b_len = 0;
197         const uint8_t *b = NULL;
198 
199         if (rs_http2_tx_get_useragent(txv, &b, &b_len) != 1)
200             return NULL;
201         if (b == NULL || b_len == 0)
202             return NULL;
203 
204         InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
205         InspectionBufferApplyTransforms(buffer, transforms);
206     }
207 
208     return buffer;
209 }
210 
211 #ifdef UNITTESTS
212 #include "tests/detect-http-user-agent.c"
213 #endif /* UNITTESTS */
214 
215 /**
216  * @}
217  */
218