Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/detect-http-stat-msg.c
Line
Count
Source (jump to first uncovered line)
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 Gurvinder Singh <gurvindersinghdahiya@gmail.com>
29
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
30
 *
31
 * Implements the http_stat_msg keyword
32
 */
33
34
#include "suricata-common.h"
35
#include "threads.h"
36
#include "decode.h"
37
#include "detect.h"
38
39
#include "detect-parse.h"
40
#include "detect-engine.h"
41
#include "detect-content.h"
42
#include "detect-pcre.h"
43
#include "detect-engine-mpm.h"
44
#include "detect-engine-prefilter.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-error.h"
52
#include "util-unittest.h"
53
#include "util-unittest-helper.h"
54
#include "util-spm.h"
55
#include "util-print.h"
56
57
#include "app-layer.h"
58
#include "app-layer-parser.h"
59
60
#include "app-layer-htp.h"
61
#include "detect-http-stat-msg.h"
62
#include "stream-tcp-private.h"
63
#include "stream-tcp.h"
64
65
static int DetectHttpStatMsgSetup(DetectEngineCtx *, Signature *, const char *);
66
#ifdef UNITTESTS
67
static void DetectHttpStatMsgRegisterTests(void);
68
#endif
69
static int g_http_stat_msg_buffer_id = 0;
70
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
71
        const DetectEngineTransforms *transforms, Flow *_f,
72
        const uint8_t _flow_flags, void *txv, const int list_id);
73
static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
74
75
static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
76
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
77
        const int list_id)
78
371
{
79
371
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
80
371
    if (buffer->inspect == NULL) {
81
182
        InspectionBufferSetup(det_ctx, list_id, buffer, (const uint8_t *)"", 0);
82
182
        InspectionBufferApplyTransforms(buffer, transforms);
83
182
    }
84
85
371
    return buffer;
86
371
}
87
88
/**
89
 * \brief Registration function for keyword: http_stat_msg
90
 */
91
void DetectHttpStatMsgRegister (void)
92
73
{
93
    /* http_stat_msg content modifier */
94
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].name = "http_stat_msg";
95
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].desc = "content modifier to match on HTTP stat-msg-buffer";
96
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].url = "/rules/http-keywords.html#http-stat-msg";
97
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetup;
98
#ifdef UNITTESTS
99
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].RegisterTests = DetectHttpStatMsgRegisterTests;
100
#endif
101
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_CONTENT_MODIFIER;
102
73
    sigmatch_table[DETECT_AL_HTTP_STAT_MSG].alternative = DETECT_HTTP_STAT_MSG;
103
104
    /* http.stat_msg sticky buffer */
105
73
    sigmatch_table[DETECT_HTTP_STAT_MSG].name = "http.stat_msg";
106
73
    sigmatch_table[DETECT_HTTP_STAT_MSG].desc = "sticky buffer to match on the HTTP response status message";
107
73
    sigmatch_table[DETECT_HTTP_STAT_MSG].url = "/rules/http-keywords.html#http-stat-msg";
108
73
    sigmatch_table[DETECT_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetupSticky;
109
73
    sigmatch_table[DETECT_HTTP_STAT_MSG].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER;
110
111
73
    DetectAppLayerInspectEngineRegister2("http_stat_msg", ALPROTO_HTTP1, SIG_FLAG_TOCLIENT,
112
73
            HTP_RESPONSE_LINE, DetectEngineInspectBufferGeneric, GetData);
113
114
73
    DetectAppLayerMpmRegister2("http_stat_msg", SIG_FLAG_TOCLIENT, 3, PrefilterGenericMpmRegister,
115
73
            GetData, ALPROTO_HTTP1, HTP_RESPONSE_LINE);
116
117
73
    DetectAppLayerInspectEngineRegister2("http_stat_msg", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
118
73
            HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
119
73
    DetectAppLayerMpmRegister2("http_stat_msg", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
120
73
            GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
121
122
73
    DetectBufferTypeSetDescriptionByName("http_stat_msg",
123
73
            "http response status message");
124
125
73
    g_http_stat_msg_buffer_id = DetectBufferTypeGetByName("http_stat_msg");
126
73
}
127
128
/**
129
 * \brief this function setups the http_stat_msg modifier keyword used in the rule
130
 *
131
 * \param de_ctx   Pointer to the Detection Engine Context
132
 * \param s        Pointer to the Signature to which the current keyword belongs
133
 * \param str      Should hold an empty string always
134
 *
135
 * \retval  0 On success
136
 * \retval -1 On failure
137
 */
138
139
static int DetectHttpStatMsgSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
140
151
{
141
151
    return DetectEngineContentModifierBufferSetup(
142
151
            de_ctx, s, arg, DETECT_AL_HTTP_STAT_MSG, g_http_stat_msg_buffer_id, ALPROTO_HTTP1);
143
151
}
144
145
/**
146
 * \brief this function setup the http.stat_msg 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 DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
155
996
{
156
996
    if (DetectBufferSetActiveList(de_ctx, s, g_http_stat_msg_buffer_id) < 0)
157
2
        return -1;
158
994
    if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
159
18
        return -1;
160
976
    return 0;
161
994
}
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
456
{
167
456
    SCEnter();
168
169
456
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
170
456
    if (buffer->inspect == NULL) {
171
456
        htp_tx_t *tx = (htp_tx_t *)txv;
172
173
456
        if (tx->response_message == NULL)
174
456
            return NULL;
175
176
0
        const uint32_t data_len = bstr_len(tx->response_message);
177
0
        const uint8_t *data = bstr_ptr(tx->response_message);
178
179
0
        InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
180
0
        InspectionBufferApplyTransforms(buffer, transforms);
181
0
    }
182
183
0
    return buffer;
184
456
}
185
186
#ifdef UNITTESTS
187
#include "tests/detect-http-stat-msg.c"
188
#endif /* UNITTESTS */
189
190
/**
191
 * @}
192
 */