suricata
app-layer-http2.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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  * \file
20  *
21  * \author Philippe Antoine <p.antoine@catenacyber.fr>
22  *
23  * Parser for HTTP2, RFC 7540
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "util-unittest.h"
29 
30 #include "app-layer-detect-proto.h"
31 
32 #include "app-layer-htp.h"
33 #include "app-layer-http2.h"
34 #include "app-layer-protos.h"
35 #include "htp/htp_rs.h"
36 #include "rust-bindings.h"
37 #include "rust.h"
38 #include "util-streaming-buffer.h"
39 
40 static int HTTP2RegisterPatternsForProtocolDetection(void)
41 {
42  /* Using the 24 bytes pattern makes AppLayerTest09 fail/leak
43  * The complete pattern is "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
44  */
46  IPPROTO_TCP, ALPROTO_HTTP2, "PRI * HTTP/2.0\r\n", 16, 0, STREAM_TOSERVER) < 0) {
47  return -1;
48  }
49  return 0;
50 }
51 
53 static SuricataFileContext sfc = { &sbcfg };
54 
56 {
57  const char *proto_name = "http2";
58 
59  if (SCAppLayerProtoDetectConfProtoDetectionEnabledDefault("tcp", proto_name, true)) {
61  if (HTTP2RegisterPatternsForProtocolDetection() < 0)
62  return;
63 
64  SCHttp2Init(&sfc);
65  SCRegisterHttp2Parser();
66  }
67 
68 #ifdef UNITTESTS
69  //TODOask HTTP2ParserRegisterTests();
70 #endif
71 }
72 
73 void SCHTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
74 {
75  htp_tx_t *h1tx = HtpGetTxForH2(alstate_orig);
76  if (h2s == NULL || h1tx == NULL) {
77  return;
78  }
79  if (htp_tx_request_method(h1tx) == NULL) {
80  // may happen if we only got the reply, not the HTTP1 request
81  return;
82  }
83  // else
84  SCHttp2TxSetMethod(h2s, bstr_ptr(htp_tx_request_method(h1tx)),
85  (uint32_t)bstr_len(htp_tx_request_method(h1tx)));
86  if (htp_tx_request_uri(h1tx) != NULL) {
87  // A request line without spaces gets interpreted as a request_method
88  // and has request_uri=NULL
89  SCHttp2TxSetUri(h2s, bstr_ptr(htp_tx_request_uri(h1tx)),
90  (uint32_t)bstr_len(htp_tx_request_uri(h1tx)));
91  }
92  size_t nbheaders = htp_tx_request_headers_size(h1tx);
93  for (size_t i = 0; i < nbheaders; i++) {
94  const htp_header_t *h = htp_tx_request_header_index(h1tx, i);
95  SCHttp2TxAddHeader(h2s, htp_header_name_ptr(h), (uint32_t)htp_header_name_len(h),
96  htp_header_value_ptr(h), (uint32_t)htp_header_value_len(h));
97  }
98 }
SCAppLayerProtoDetectPMRegisterPatternCI
int SCAppLayerProtoDetectPMRegisterPatternCI(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
Registers a case-insensitive pattern for protocol detection.
Definition: app-layer-detect-proto.c:1660
STREAMING_BUFFER_CONFIG_INITIALIZER
#define STREAMING_BUFFER_CONFIG_INITIALIZER
Definition: util-streaming-buffer.h:74
HtpGetTxForH2
void * HtpGetTxForH2(void *alstate)
Definition: app-layer-htp.c:2521
rust.h
app-layer-http2.h
SCHTTP2MimicHttp1Request
void SCHTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
Definition: app-layer-http2.c:73
util-unittest.h
app-layer-detect-proto.h
app-layer-htp.h
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1769
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
suricata-common.h
util-streaming-buffer.h
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
app-layer-protos.h
SCAppLayerProtoDetectConfProtoDetectionEnabledDefault
int SCAppLayerProtoDetectConfProtoDetectionEnabledDefault(const char *ipproto, const char *alproto, bool default_enabled)
Given a protocol name, checks if proto detection is enabled in the conf file.
Definition: app-layer-detect-proto.c:1906
RegisterHTTP2Parsers
void RegisterHTTP2Parsers(void)
Definition: app-layer-http2.c:55