Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/detect-http-stat-msg.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 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-engine-buffer.h"
42
#include "detect-content.h"
43
#include "detect-pcre.h"
44
#include "detect-engine-mpm.h"
45
#include "detect-engine-prefilter.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-error.h"
53
#include "util-unittest.h"
54
#include "util-unittest-helper.h"
55
#include "util-spm.h"
56
#include "util-print.h"
57
58
#include "app-layer.h"
59
#include "app-layer-parser.h"
60
61
#include "app-layer-htp.h"
62
#include "detect-http-stat-msg.h"
63
#include "stream-tcp-private.h"
64
#include "stream-tcp.h"
65
66
static int DetectHttpStatMsgSetup(DetectEngineCtx *, Signature *, const char *);
67
#ifdef UNITTESTS
68
static void DetectHttpStatMsgRegisterTests(void);
69
#endif
70
static int g_http_stat_msg_buffer_id = 0;
71
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
72
        const DetectEngineTransforms *transforms, Flow *_f,
73
        const uint8_t _flow_flags, void *txv, const int list_id);
74
static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
75
76
static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
77
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
78
        const int list_id)
79
180
{
80
180
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
81
180
    if (buffer->inspect == NULL) {
82
79
        InspectionBufferSetupAndApplyTransforms(
83
79
                det_ctx, list_id, buffer, (const uint8_t *)"", 0, transforms);
84
79
    }
85
86
180
    return buffer;
87
180
}
88
89
/**
90
 * \brief Registration function for keyword: http_stat_msg
91
 */
92
void DetectHttpStatMsgRegister (void)
93
79
{
94
    /* http_stat_msg content modifier */
95
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].name = "http_stat_msg";
96
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].desc =
97
79
            "content modifier to match on HTTP stat-msg-buffer";
98
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].url = "/rules/http-keywords.html#http-stat-msg";
99
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].Setup = DetectHttpStatMsgSetup;
100
#ifdef UNITTESTS
101
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].RegisterTests = DetectHttpStatMsgRegisterTests;
102
#endif
103
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].flags |=
104
79
            SIGMATCH_NOOPT | SIGMATCH_INFO_CONTENT_MODIFIER;
105
79
    sigmatch_table[DETECT_HTTP_STAT_MSG_CM].alternative = DETECT_HTTP_STAT_MSG;
106
107
    /* http.stat_msg sticky buffer */
108
79
    sigmatch_table[DETECT_HTTP_STAT_MSG].name = "http.stat_msg";
109
79
    sigmatch_table[DETECT_HTTP_STAT_MSG].desc = "sticky buffer to match on the HTTP response status message";
110
79
    sigmatch_table[DETECT_HTTP_STAT_MSG].url = "/rules/http-keywords.html#http-stat-msg";
111
79
    sigmatch_table[DETECT_HTTP_STAT_MSG].Setup = DetectHttpStatMsgSetupSticky;
112
79
    sigmatch_table[DETECT_HTTP_STAT_MSG].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER;
113
114
79
    DetectAppLayerInspectEngineRegister("http_stat_msg", ALPROTO_HTTP1, SIG_FLAG_TOCLIENT,
115
79
            HTP_RESPONSE_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
116
117
79
    DetectAppLayerMpmRegister("http_stat_msg", SIG_FLAG_TOCLIENT, 3, PrefilterGenericMpmRegister,
118
79
            GetData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_LINE);
119
120
79
    DetectAppLayerInspectEngineRegister("http_stat_msg", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
121
79
            HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2);
122
79
    DetectAppLayerMpmRegister("http_stat_msg", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
123
79
            GetData2, ALPROTO_HTTP2, HTTP2StateDataServer);
124
125
79
    DetectBufferTypeSetDescriptionByName("http_stat_msg",
126
79
            "http response status message");
127
128
79
    g_http_stat_msg_buffer_id = DetectBufferTypeGetByName("http_stat_msg");
129
79
}
130
131
/**
132
 * \brief this function setups the http_stat_msg modifier keyword used in the rule
133
 *
134
 * \param de_ctx   Pointer to the Detection Engine Context
135
 * \param s        Pointer to the Signature to which the current keyword belongs
136
 * \param str      Should hold an empty string always
137
 *
138
 * \retval  0 On success
139
 * \retval -1 On failure
140
 */
141
142
static int DetectHttpStatMsgSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
143
36
{
144
36
    return DetectEngineContentModifierBufferSetup(
145
36
            de_ctx, s, arg, DETECT_HTTP_STAT_MSG_CM, g_http_stat_msg_buffer_id, ALPROTO_HTTP1);
146
36
}
147
148
/**
149
 * \brief this function setup the http.stat_msg keyword used in the rule
150
 *
151
 * \param de_ctx   Pointer to the Detection Engine Context
152
 * \param s        Pointer to the Signature to which the current keyword belongs
153
 * \param str      Should hold an empty string always
154
 *
155
 * \retval 0       On success
156
 */
157
static int DetectHttpStatMsgSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
158
822
{
159
822
    if (SCDetectBufferSetActiveList(de_ctx, s, g_http_stat_msg_buffer_id) < 0)
160
2
        return -1;
161
820
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
162
19
        return -1;
163
801
    return 0;
164
820
}
165
166
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
167
        const DetectEngineTransforms *transforms, Flow *_f,
168
        const uint8_t _flow_flags, void *txv, const int list_id)
169
1.62k
{
170
1.62k
    SCEnter();
171
172
1.62k
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
173
1.62k
    if (buffer->inspect == NULL) {
174
1.62k
        htp_tx_t *tx = (htp_tx_t *)txv;
175
176
1.62k
        if (htp_tx_response_message(tx) == NULL)
177
804
            return NULL;
178
179
820
        const uint32_t data_len = (uint32_t)bstr_len(htp_tx_response_message(tx));
180
820
        const uint8_t *data = bstr_ptr(htp_tx_response_message(tx));
181
182
820
        InspectionBufferSetupAndApplyTransforms(
183
820
                det_ctx, list_id, buffer, data, data_len, transforms);
184
820
    }
185
186
823
    return buffer;
187
1.62k
}
188
189
#ifdef UNITTESTS
190
#include "tests/detect-http-stat-msg.c"
191
#endif /* UNITTESTS */
192
193
/**
194
 * @}
195
 */