/src/suricata8/src/detect-http-response-line.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2022 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 Victor Julien <victor@inliniac.net> |
29 | | * |
30 | | * Implements support for the http_response_line 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-buffer.h" |
41 | | #include "detect-engine-mpm.h" |
42 | | #include "detect-engine-state.h" |
43 | | #include "detect-engine-prefilter.h" |
44 | | #include "detect-engine-content-inspection.h" |
45 | | #include "detect-content.h" |
46 | | #include "detect-pcre.h" |
47 | | |
48 | | #include "flow.h" |
49 | | #include "flow-var.h" |
50 | | #include "flow-util.h" |
51 | | |
52 | | #include "util-debug.h" |
53 | | #include "util-unittest.h" |
54 | | #include "util-unittest-helper.h" |
55 | | #include "util-spm.h" |
56 | | |
57 | | #include "app-layer.h" |
58 | | #include "app-layer-parser.h" |
59 | | |
60 | | #include "app-layer-htp.h" |
61 | | #include "stream-tcp.h" |
62 | | #include "detect-http-response-line.h" |
63 | | |
64 | | static int DetectHttpResponseLineSetup(DetectEngineCtx *, Signature *, const char *); |
65 | | #ifdef UNITTESTS |
66 | | static void DetectHttpResponseLineRegisterTests(void); |
67 | | #endif |
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 int g_http_response_line_id = 0; |
73 | | |
74 | | static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx, |
75 | | const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv, |
76 | | const int list_id) |
77 | | { |
78 | | SCEnter(); |
79 | | |
80 | | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
81 | | if (buffer->inspect == NULL) { |
82 | | uint32_t b_len = 0; |
83 | | const uint8_t *b = NULL; |
84 | | |
85 | | if (SCHttp2TxGetResponseLine(txv, &b, &b_len) != 1) |
86 | | return NULL; |
87 | | if (b == NULL || b_len == 0) |
88 | | return NULL; |
89 | | |
90 | | InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms); |
91 | | } |
92 | | |
93 | | return buffer; |
94 | | } |
95 | | |
96 | | /** |
97 | | * \brief Registers the keyword handlers for the "http_response_line" keyword. |
98 | | */ |
99 | | void DetectHttpResponseLineRegister(void) |
100 | 12 | { |
101 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].name = "http.response_line"; |
102 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].alias = "http_response_line"; |
103 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].desc = |
104 | 12 | "content modifier to match only on the HTTP response line"; |
105 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].url = "/rules/http-keywords.html#http-response-line"; |
106 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].Setup = DetectHttpResponseLineSetup; |
107 | | #ifdef UNITTESTS |
108 | | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].RegisterTests = DetectHttpResponseLineRegisterTests; |
109 | | #endif |
110 | 12 | sigmatch_table[DETECT_HTTP_RESPONSE_LINE].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
111 | | |
112 | 12 | DetectAppLayerInspectEngineRegister("http_response_line", ALPROTO_HTTP1, SIG_FLAG_TOCLIENT, |
113 | 12 | HTP_RESPONSE_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData); |
114 | | |
115 | 12 | DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2, |
116 | 12 | PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_LINE); |
117 | | |
118 | 12 | DetectAppLayerInspectEngineRegister("http_response_line", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT, |
119 | 12 | HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetData2); |
120 | 12 | DetectAppLayerMpmRegister("http_response_line", SIG_FLAG_TOCLIENT, 2, |
121 | 12 | PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2StateDataServer); |
122 | | |
123 | 12 | DetectBufferTypeSetDescriptionByName("http_response_line", |
124 | 12 | "http response line"); |
125 | | |
126 | 12 | g_http_response_line_id = DetectBufferTypeGetByName("http_response_line"); |
127 | 12 | } |
128 | | |
129 | | /** |
130 | | * \brief The setup function for the http_response_line keyword for a signature. |
131 | | * |
132 | | * \param de_ctx Pointer to the detection engine context. |
133 | | * \param s Pointer to the signature for the current Signature being |
134 | | * parsed from the rules. |
135 | | * \param m Pointer to the head of the SigMatch for the current rule |
136 | | * being parsed. |
137 | | * \param arg Pointer to the string holding the keyword value. |
138 | | * |
139 | | * \retval 0 On success |
140 | | * \retval -1 On failure |
141 | | */ |
142 | | static int DetectHttpResponseLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
143 | 348 | { |
144 | 348 | if (SCDetectBufferSetActiveList(de_ctx, s, g_http_response_line_id) < 0) |
145 | 3 | return -1; |
146 | | |
147 | 345 | if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0) |
148 | 10 | return -1; |
149 | | |
150 | 335 | return 0; |
151 | 345 | } |
152 | | |
153 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
154 | | const DetectEngineTransforms *transforms, |
155 | | Flow *_f, const uint8_t _flow_flags, |
156 | | void *txv, const int list_id) |
157 | 179 | { |
158 | 179 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
159 | 179 | if (buffer->inspect == NULL) { |
160 | 139 | htp_tx_t *tx = (htp_tx_t *)txv; |
161 | 139 | if (unlikely(htp_tx_response_line(tx) == NULL)) { |
162 | 48 | return NULL; |
163 | 48 | } |
164 | 91 | const uint32_t data_len = (uint32_t)bstr_len(htp_tx_response_line(tx)); |
165 | 91 | const uint8_t *data = bstr_ptr(htp_tx_response_line(tx)); |
166 | | |
167 | 91 | InspectionBufferSetupAndApplyTransforms( |
168 | 91 | det_ctx, list_id, buffer, data, data_len, transforms); |
169 | 91 | } |
170 | 131 | return buffer; |
171 | 179 | } |
172 | | |
173 | | /************************************Unittests*********************************/ |
174 | | |
175 | | #ifdef UNITTESTS |
176 | | |
177 | | #include "stream-tcp-reassemble.h" |
178 | | |
179 | | /** |
180 | | * \test Test that a signature containing a http_response_line is correctly parsed |
181 | | * and the keyword is registered. |
182 | | */ |
183 | | static int DetectHttpResponseLineTest01(void) |
184 | | { |
185 | | DetectEngineCtx *de_ctx = DetectEngineCtxInit(); |
186 | | FAIL_IF_NULL(de_ctx); |
187 | | |
188 | | de_ctx->flags |= DE_QUIET; |
189 | | de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any " |
190 | | "(http_response_line; content:\"200 OK\"; sid:1;)"); |
191 | | FAIL_IF_NULL(de_ctx->sig_list); |
192 | | |
193 | | DetectEngineCtxFree(de_ctx); |
194 | | PASS; |
195 | | } |
196 | | |
197 | | void DetectHttpResponseLineRegisterTests(void) |
198 | | { |
199 | | UtRegisterTest("DetectHttpResponseLineTest01", DetectHttpResponseLineTest01); |
200 | | } |
201 | | #endif /* UNITTESTS */ |
202 | | /** |
203 | | * @} |
204 | | */ |