Coverage Report

Created: 2026-08-14 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-http-ua.c
Line
Count
Source
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 g_http2_thread_id = 0;
68
static int DetectHttpUserAgentSetup(DetectEngineCtx *, Signature *, const char *);
69
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
70
        const DetectEngineTransforms *transforms,
71
        Flow *_f, const uint8_t _flow_flags,
72
        void *txv, const int list_id);
73
static 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
 */
80
void DetectHttpUARegister(void)
81
74
{
82
    /* http_user_agent content modifier */
83
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].name = "http_user_agent";
84
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].desc = "content modifier to match only on the HTTP User-Agent header";
85
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].url = "/rules/http-keywords.html#http-user-agent";
86
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].Setup = DetectHttpUASetup;
87
#ifdef UNITTESTS
88
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].RegisterTests = DetectHttpUARegisterTests;
89
#endif
90
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].flags |= SIGMATCH_NOOPT;
91
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].flags |= SIGMATCH_INFO_CONTENT_MODIFIER;
92
74
    sigmatch_table[DETECT_AL_HTTP_USER_AGENT].alternative = DETECT_HTTP_UA;
93
94
    /* http.user_agent sticky buffer */
95
74
    sigmatch_table[DETECT_HTTP_UA].name = "http.user_agent";
96
74
    sigmatch_table[DETECT_HTTP_UA].desc = "sticky buffer to match specifically and only on the HTTP User Agent buffer";
97
74
    sigmatch_table[DETECT_HTTP_UA].url = "/rules/http-keywords.html#http-user-agent";
98
74
    sigmatch_table[DETECT_HTTP_UA].Setup = DetectHttpUserAgentSetup;
99
74
    sigmatch_table[DETECT_HTTP_UA].flags |= SIGMATCH_NOOPT;
100
74
    sigmatch_table[DETECT_HTTP_UA].flags |= SIGMATCH_INFO_STICKY_BUFFER;
101
102
74
    DetectAppLayerInspectEngineRegister2("http_user_agent", ALPROTO_HTTP1, SIG_FLAG_TOSERVER,
103
74
            HTP_REQUEST_HEADERS, DetectEngineInspectBufferGeneric, GetData);
104
105
74
    DetectAppLayerMpmRegister2("http_user_agent", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
106
74
            GetData, ALPROTO_HTTP1, HTP_REQUEST_HEADERS);
107
108
74
    DetectAppLayerInspectEngineRegister2("http_user_agent", ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
109
74
            HTTP2StateOpen, DetectEngineInspectBufferGeneric, GetData2);
110
111
74
    DetectAppLayerMpmRegister2("http_user_agent", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
112
74
            GetData2, ALPROTO_HTTP2, HTTP2StateOpen);
113
114
74
    DetectBufferTypeSetDescriptionByName("http_user_agent",
115
74
            "http user agent");
116
117
74
    g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs(
118
74
            "http_user_agent", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree);
119
120
74
    g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent");
121
74
}
122
123
/**
124
 * \brief The setup function for the http_user_agent keyword for a signature.
125
 *
126
 * \param de_ctx Pointer to the detection engine context.
127
 * \param s      Pointer to the signature for the current Signature being
128
 *               parsed from the rules.
129
 * \param m      Pointer to the head of the SigMatch for the current rule
130
 *               being parsed.
131
 * \param arg    Pointer to the string holding the keyword value.
132
 *
133
 * \retval  0 On success
134
 * \retval -1 On failure
135
 */
136
int DetectHttpUASetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
137
12.5k
{
138
12.5k
    return DetectEngineContentModifierBufferSetup(
139
12.5k
            de_ctx, s, arg, DETECT_AL_HTTP_USER_AGENT, g_http_ua_buffer_id, ALPROTO_HTTP1);
140
12.5k
}
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
 */
151
static int DetectHttpUserAgentSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
152
38.8k
{
153
38.8k
    if (DetectBufferSetActiveList(de_ctx, s, g_http_ua_buffer_id) < 0)
154
33
        return -1;
155
38.8k
    if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
156
334
        return -1;
157
38.5k
    return 0;
158
38.8k
}
159
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
1.31k
{
164
1.31k
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
165
1.31k
    if (buffer->inspect == NULL) {
166
1.19k
        htp_tx_t *tx = (htp_tx_t *)txv;
167
168
1.19k
        if (tx->request_headers == NULL)
169
0
            return NULL;
170
171
1.19k
        htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
172
1.19k
                "User-Agent");
173
1.19k
        if (h == NULL || h->value == NULL) {
174
808
            SCLogDebug("HTTP UA header not present in this request");
175
808
            return NULL;
176
808
        }
177
178
385
        const uint32_t data_len = bstr_len(h->value);
179
385
        const uint8_t *data = bstr_ptr(h->value);
180
181
385
        InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
182
385
        InspectionBufferApplyTransforms(buffer, transforms);
183
385
    }
184
185
503
    return buffer;
186
1.31k
}
187
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
886
{
192
886
    SCEnter();
193
194
886
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
195
886
    if (buffer->inspect == NULL) {
196
886
        uint32_t b_len = 0;
197
886
        const uint8_t *b = NULL;
198
886
        void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
199
886
        if (thread_buf == NULL)
200
0
            return NULL;
201
886
        if (SCHttp2TxGetUserAgent(txv, &b, &b_len, thread_buf) != 1)
202
687
            return NULL;
203
199
        if (b == NULL || b_len == 0)
204
1
            return NULL;
205
206
198
        InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
207
198
        InspectionBufferApplyTransforms(buffer, transforms);
208
198
    }
209
210
198
    return buffer;
211
886
}
212
213
#ifdef UNITTESTS
214
#include "tests/detect-http-user-agent.c"
215
#endif /* UNITTESTS */
216
217
/**
218
 * @}
219
 */