/src/suricata7/src/detect-http-request-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_request_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-mpm.h" |
41 | | #include "detect-engine-state.h" |
42 | | #include "detect-engine-prefilter.h" |
43 | | #include "detect-engine-content-inspection.h" |
44 | | #include "detect-content.h" |
45 | | #include "detect-pcre.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-unittest.h" |
53 | | #include "util-unittest-helper.h" |
54 | | #include "util-spm.h" |
55 | | |
56 | | #include "app-layer.h" |
57 | | #include "app-layer-parser.h" |
58 | | |
59 | | #include "app-layer-htp.h" |
60 | | #include "stream-tcp.h" |
61 | | #include "detect-http-request-line.h" |
62 | | |
63 | | static int DetectHttpRequestLineSetup(DetectEngineCtx *, Signature *, const char *); |
64 | | #ifdef UNITTESTS |
65 | | static void DetectHttpRequestLineRegisterTests(void); |
66 | | #endif |
67 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
68 | | const DetectEngineTransforms *transforms, |
69 | | Flow *_f, const uint8_t _flow_flags, |
70 | | void *txv, const int list_id); |
71 | | static int g_http_request_line_buffer_id = 0; |
72 | | |
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 | 470 | { |
77 | 470 | SCEnter(); |
78 | | |
79 | 470 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
80 | 470 | if (buffer->inspect == NULL) { |
81 | 404 | uint32_t b_len = 0; |
82 | 404 | const uint8_t *b = NULL; |
83 | | |
84 | 404 | if (rs_http2_tx_get_request_line(txv, &b, &b_len) != 1) |
85 | 0 | return NULL; |
86 | 404 | if (b == NULL || b_len == 0) |
87 | 0 | return NULL; |
88 | | |
89 | 404 | InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len); |
90 | 404 | InspectionBufferApplyTransforms(buffer, transforms); |
91 | 404 | } |
92 | | |
93 | 470 | return buffer; |
94 | 470 | } |
95 | | |
96 | | /** |
97 | | * \brief Registers the keyword handlers for the "http_request_line" keyword. |
98 | | */ |
99 | | void DetectHttpRequestLineRegister(void) |
100 | 74 | { |
101 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].name = "http.request_line"; |
102 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].alias = "http_request_line"; |
103 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].desc = "sticky buffer to match on the HTTP request line"; |
104 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].url = "/rules/http-keywords.html#http-request-line"; |
105 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].Match = NULL; |
106 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].Setup = DetectHttpRequestLineSetup; |
107 | | #ifdef UNITTESTS |
108 | | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].RegisterTests = DetectHttpRequestLineRegisterTests; |
109 | | #endif |
110 | 74 | sigmatch_table[DETECT_AL_HTTP_REQUEST_LINE].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER; |
111 | | |
112 | 74 | DetectAppLayerInspectEngineRegister2("http_request_line", ALPROTO_HTTP1, SIG_FLAG_TOSERVER, |
113 | 74 | HTP_REQUEST_LINE, DetectEngineInspectBufferGeneric, GetData); |
114 | | |
115 | 74 | DetectAppLayerMpmRegister2("http_request_line", SIG_FLAG_TOSERVER, 2, |
116 | 74 | PrefilterGenericMpmRegister, GetData, ALPROTO_HTTP1, HTP_REQUEST_LINE); |
117 | | |
118 | 74 | DetectAppLayerInspectEngineRegister2("http_request_line", ALPROTO_HTTP2, SIG_FLAG_TOSERVER, |
119 | 74 | HTTP2StateOpen, DetectEngineInspectBufferGeneric, GetData2); |
120 | 74 | DetectAppLayerMpmRegister2("http_request_line", SIG_FLAG_TOSERVER, 2, |
121 | 74 | PrefilterGenericMpmRegister, GetData2, ALPROTO_HTTP2, HTTP2StateOpen); |
122 | | |
123 | 74 | DetectBufferTypeSetDescriptionByName("http_request_line", |
124 | 74 | "http request line"); |
125 | | |
126 | 74 | g_http_request_line_buffer_id = DetectBufferTypeGetByName("http_request_line"); |
127 | 74 | } |
128 | | |
129 | | /** |
130 | | * \brief The setup function for the http_request_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 DetectHttpRequestLineSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg) |
143 | 16.6k | { |
144 | 16.6k | if (DetectBufferSetActiveList(de_ctx, s, g_http_request_line_buffer_id) < 0) |
145 | 102 | return -1; |
146 | | |
147 | 16.5k | if (DetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0) |
148 | 44 | return -1; |
149 | | |
150 | 16.5k | return 0; |
151 | 16.5k | } |
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 | 1.22k | { |
158 | 1.22k | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
159 | 1.22k | if (buffer->inspect == NULL) { |
160 | 1.10k | htp_tx_t *tx = (htp_tx_t *)txv; |
161 | 1.10k | if (unlikely(tx->request_line == NULL)) { |
162 | 322 | return NULL; |
163 | 322 | } |
164 | 778 | const uint32_t data_len = bstr_len(tx->request_line); |
165 | 778 | const uint8_t *data = bstr_ptr(tx->request_line); |
166 | | |
167 | 778 | InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len); |
168 | 778 | InspectionBufferApplyTransforms(buffer, transforms); |
169 | 778 | } |
170 | 906 | return buffer; |
171 | 1.22k | } |
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_request_line is correctly parsed |
181 | | * and the keyword is registered. |
182 | | */ |
183 | | static int DetectHttpRequestLineTest01(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_request_line; content:\"GET /\"; sid:1;)"); |
191 | | FAIL_IF_NULL(de_ctx->sig_list); |
192 | | |
193 | | DetectEngineCtxFree(de_ctx); |
194 | | PASS; |
195 | | } |
196 | | |
197 | | static void DetectHttpRequestLineRegisterTests(void) |
198 | | { |
199 | | UtRegisterTest("DetectHttpRequestLineTest01", DetectHttpRequestLineTest01); |
200 | | } |
201 | | #endif /* UNITTESTS */ |
202 | | /** |
203 | | * @} |
204 | | */ |