๐ Introduction
HTTP/2 Protocol and H2C Switching
HTTP2 ํ๋กํ ์ฝ์ ๊ตฌ๊ธ์์ ๊ฐ๋ฐ ๋ฐ ์๋ฒ ์ฌ์ฉ์ค์ด๋ SPDY ํ๋กํ ์ฝ์ ์ฌ์์ ๊ธฐ๋ฐ์ผ๋ก HTTP-WG์์ ์งํํ ํ๋กํ ์ฝ์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ 2015๋ HTTP/2 ํ์ค์ผ๋ก ์ฑํ๋จ๊ณผ ๋์์ SPDY๋ ์ง์ ์ค๋จ์ ํ๊ฒ ๋์์ง์. ์๋ฌดํผ HTTP/2๋ ์ฐจ์ธ๋ ์น ํ๋กํ ์ฝ๋ก ๋ณด์๋ฉด ๋ ๊ฒ ๊ฐ๊ณ , 1.x ๋ฒ์ ๋๋ณด๋ค ์ฑ๋ฅ์ ์ผ๋ก ํฐ ๊ฐ์ ๋ค์ด ์์ด์ ์์ํ ๋ง์ ์๋ฒ๋ค์ด HTTP2 ์ง์์ ํ๊ณ ์๋ ์ํ์ ๋๋ค. HTTP2์ ๋ํ ๋ด์ฉ์ google developers ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์.
H2C๋ HTTP/1.x ๋ฒ์ ์์ HTTP/2 ๋ฒ์ ์ผ๋ก ํ๋กํ ์ฝ์ ์ค์์นญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฒ/ํด๋ผ์ด์ธํธ ๋ชจ๋ HTTP/2 ์ง์์ด ํ์ํฉ๋๋ค. (RFC-7540)
Request
GET / HTTP/1.1
Host: test.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
Response - HTTP/2 ์ง์
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
[ HTTP/2 connection ...
Response - HTTP/2 ๋ฏธ์ง์
์๋ฒ๊ฐ ๋ฌด์
RFC-7540์ ์ฝ์ด๋ณด๋ฉด HTTP/2 ํต์ ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์น ์์ผ ํต์ ๊ณผ ๋ง์ด ์ ์ฌํฉ๋๋ค. ํด๋ผ์ด์ธํธ(์น๋ธ๋ผ์ฐ์ )๋ ์น ์๋ฒ์์ HTTP/2 ํต์ ์ ํ๊ธฐ ์ํด ์๋ฒ์์ HTTP/2 ์ง์ ์ฌ๋ถ๋ฅผ ๋ฌผ์ด๋ณด๊ณ , ๊ทธ์ ๋ฐ๋ผ์ HTTP/2๋ฅผ ํต์ ์ ์ฌ์ฉํ ์ง, HTTP/1.x ํต์ ์ ์ฌ์ฉํ ์ง ๊ฒฐ์ ํฉ๋๋ค.
HTTP/2 ํต์ ์ 7 Layer(Application) ์์ ์ํ๋๋ ํ๋กํ ์ฝ์ด๊ณ , TCP Connection์ ์ฌ์ฉํฉ๋๋ค. ๊ทธ๋์ ๊ธฐ์กด HTTP ํต์ ๊ณผ๋ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์น ์์ผ๊ณผ ๊ฐ์ด ์ง์์ฌ๋ถ ์ฒดํฌ ํ ํ๋กํ ์ฝ ๋ณํ์ธ 101 Switching protocol์ ์ฌ์ฉํฉ๋๋ค. (uri๋ http์ ๋์ผํ๊ฒ http->80, https->443์ ์ฌ์ฉํฉ๋๋ค.)
An HTTP/2 connection is an application-layer protocol running on top of a TCP connection (TCP). The client is the TCP connection initiator. HTTP/2 uses the same โhttpโ and โhttpsโ URI schemes used by HTTP/1.1. HTTP/2 shares the same default port numbers: 80 for โhttpโ URIs and 443 for โhttpsโ URIs. As a result, implementations processing requests for target resource URIs like โhttp://example.org/fooโ or โhttps://example.com/barโ are required to first discover whether the upstream server (the immediate peer to which the client wishes to establish a connection) supports HTTP/2. The means by which support for HTTP/2 is determined is different for โhttpโ and โhttpsโ URIs. Discovery for โhttpโ URIs is described in Section 3.2. Discovery for โhttpsโ URIs is described in Section 3.3.
http/1.x -> http/2๋ก ์
๊ทธ๋ ์ด๋๋ฅผ ์ํด์ http request์ Upgrade ํค๋์ indicator์ HTTP2-Settings ํค๋๋ฅผ ์ ๋ฌํฉ๋๋ค. indicator ์ข
๋ฅ๋ HTTP๋ h2c HTTPS๋ h2 ๋ฅผ ์๋ฏธํฉ๋๋ค. ์ฆ Upgrade: h2c
๋ HTTP/2๋ฅผ ํ๋ฌธ์ผ๋ก ํต์ ํ๊ฒ ๋ค๋ ์๋ฏธ์
๋๋ค.
GET / HTTP/1.1
Host: test.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>
์ดํ HTTP/2๋ฅผ ์ง์ํ๊ฒ ๋๋ฉด ํด๋ผ์ด์ธํธ์๊ฒ 101 Switching protocol์ ์ ๋ฌํ๊ณ TLS Connection(HTTP/2)์ ์ฌ์ฉํ์ฌ ํด๋ผ์ด์ธํธ์ ํต์ ํฉ๋๋ค. ์ด ๋ ํ๋กํ ์ฝ์ ๋ํ ํฉ์๋ TLS-ALPN(Application-Layer-Protocol Negotiation)์ ์ฌ์ฉํฉ๋๋ค. ์ด ๊ณผ์ ์์ APLN extension์ผ๋ก ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์๊ฒ ๋ฒ์ ์ ๋ฆฌ์คํธ๋ฅผ ์ ๊ณตํ๊ณ , ์๋ฒ๋ ํ๋๋ฅผ ์ ํํฉ๋๋ค. https๋ฅผ ์ฌ์ฉํ๋ HTTP/2์ ๊ฐ์ h2์ ๋๋ค.
๋ฌผ๋ก HTTP/2๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ๋ ์ผ์ด์ค์ ๊ฒฝ์ฐ TLS-ALPN์ ํตํด ๋ฐ๋ก ํ๋กํ ์ฝ ํ์ ํ TLS ์ปค๋ฅ์ ์ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค.
H2C Smuggling
๋ง์ ์น ์๋น์ค๋ค์ Reverse Proxy๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ด๋ฌํ ๊ณผ์ ์์ 101 Switching ์ ์ฌ์ฉํด์ผํ๋ ์ํฉ์ด ์ค๋ฉด, ํ๋ก์ ์๋ฒ๋ ๋ณ๋์ ์ฒ๋ฆฌ์์ด ์ค์ฌ์ ์ญํ ์ ์ํํ๊ฒ ๋ฉ๋๋ค. ์ฌ๊ธฐ์ bishopfox๋ HTTP/2 ํ๋กํ ์ฝ์ ๋ํ ์ฐ๊ตฌ๋ฅผ ์งํํ๊ณ , ๊ฒฐ๊ตญ ์ฌ๋ฏธ์๋ ๊ฒฐํจ์ ๋ฐ๊ฒฌํ๊ฒ ๋ฉ๋๋ค.
RFC-7540์ 3.2.1 ๋ถ๋ถ์ ๋ช ์๋ ๋ด์ฉ๊ณผ TLS ๋ด HTTP/2 ์ค์ ์์ h2c Upgrade๋ cleartext connection์์๋ง ํ์ฉ๋๊ณ ์ด ๋ HTTP2-Settings ํค๋๋ ์ ๋ฌํ์ง ๋ง์์ผํ๋ค๊ณ ๋ช ์๋์ด ์์ต๋๋ค.
HTTP2-Settings = token68
A server MUST NOT upgrade the connection to HTTP/2 if this header
field is not present or if more than one is present. A server MUST
NOT send this header field.
https://tools.ietf.org/html/rfc7540#section-3.2.1
HTTP2 ์คํ์๋ ์ด๋ ๊ฒ ๋ช ์๋์ด ์์ต๋๋ค.
3.3 Starting HTTP/2 for "https" URIs
A client that makes a request to an "https" URI uses TLS [TLS12] with the application-layer protocol negotiation (ALPN) extension [TLS-ALPN].
HTTP/2 over TLS uses the "h2" protocol identifier. The "h2c" protocol identifier MUST NOT be sent by a client or selected by a server; the "h2c" protocol identifier describes a protocol that does not use TLS.
Once TLS negotiation is complete, both the client and the server MUST send a connection preface (Section 3.5).
TLS๋ฅผ ํตํ HTTP/2๋ฅผ ์ฌ์ฉํ ๋ h2c๊ฐ ์๋ h2 ํ๋กํ ์ฝ ์๋ณ์๋ฅผ ์ฌ์ฉํ๋ผ๊ณ ๋์ด์์ต๋๋ค. ๋งจ ์์์ ์ด์ผ๊ธฐ๋๋ ธ๋ฏ์ด h2c๋ http, h2๋ https๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๊ณ ์๋ indicator์ด๊ธฐ ๋๋ฌธ์ด์ฃ .๋ง์ฝ proxy๊ฐ ๊ปด์๋ ๊ตฌ์กฐ์์ cleartext๊ฐ ์๋ TLS ์์์ proxy๊ฐ h2c ๋ฅผ ๋ฐฑ์๋์ ์ ๋ฌํ์ฌ upgrade๊ฐ ๋ฐ์ํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
Proxy๊ฐ ์๋ hops ํ๊ฒฝ์์ ๋ฐฑ์๋ ์๋ฒ๋ ํด๋ผ์ด์ธํธ๊ฐ Cleartext์ธ์ง TLS์ธ์ง ์ ์ ์๋ ๋ฐฉ๋ฒ์ด h2c,h2 ๋ฑ์ indicator ๋ฟ์ด๋ผ ์น์ด ์๋ TLS Connection์ HTTP๋ก ํ๋จํ์ฌ TLS Connection ์์ TCP Tunnel ์ ๋ง๋ญ๋๋ค. ์ด ๋ ํด๋ผ์ด์ธํธ๋ HTTP๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ๊ธฐ์กด ์ปค๋ฅ์ ์ ๊ทธ๋๋ก ์ฌ์ฉํฉ๋๋ค. (Over TLS)
์ฆ ์ด๋ฏธ ์ฐ๊ฒฐ๋์ด ์๋ ์ปค๋ฅ์ ์ด๊ณ HTTP ํต์ ์ด ์๋๊ธฐ ๋๋ฌธ์ Proxy์ ACL ์ ์ฑ ์ ์ํฅ์ ๋ฐ์ง ์์ง๋ง, TCP Tunnel์์ ๋ฐ์ํ ์์ฒญ์ด HTTP๋ก ๋์ํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฐจ๋จ๋ ๋ฆฌ์์ค์ ์ ๊ทผํ ์ ์๋ ํฌ์ธํธ๊ฐ ์๊น๋๋ค.
Flows
- ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ(ํ๋ก์)๋ก HTTP/1.1 Upgrade ์์ฒญ ์ ์ก (์๋ชป๋ ํค๋๋ฅผ ์ ์ก)
- ํ๋ก์๊ฐ ํด๋น ์์ฒญ์ ๋ฐฑ์๋์ ์ ๋ฌํ๊ณ 101 Swiching protocol๋ก response๋ฅผ ๋ด๋ ค์ค
- ๋ฐฑ์๋ ์๋ฒ์์ 101์ ๋ฐ์ผ๋ฉด TCP Tunnel์ ์์ฑํจ
- ํด๋ผ์ด์ธํธ๊ฐ ํ๋ก์๋ก๋ถํฐ 101์ ๋ฐ์ผ๋ฉด ๊ธฐ์กด ์ปค๋ฅ์ ์ ์ฌ์ฌ์ฉํ๊ณ , HTTP/2 ์ด๊ธฐํ๋ฅผ ์งํ
- ํด๋ผ์ด์ธํธ๊ฐ HTTP/2 multiplexing์ ์ฌ์ฉํ์ฌ priavteํ ๊ฒฝ๋ก์ ๋ํด ์ถ๊ฐ ์์ฒญ์ ์ ์กํจ
- ํ๋ก์๋ TCP ํต์ ์ ๊ฐ์ํ์ง ์๊ธฐ ๋๋ฌธ์(์ ์ฑ ์ HTTP) ์ ํ๋ ํ์ด์ง์ ์ถ๊ฐ ์์ฒญ์ ์ ์กํ๊ณ , ์๋ฒ๋ ์๋ต์ TLS Tunnel๋ก ์ ๋ฌํจ
๐ก Offensive techniques
Detect
ํด๋น ์ทจ์ฝ์ ์ ๋ฐ๊ฒฌํ ByshopFox๊ฐ ๋๊ตฌ์ ํ๊ฒฝ์ ๋ง๋ค์ด๋์ด์ ์ฝ๊ฒ ํ ์คํธํ ์ ์์ต๋๋ค. https://github.com/BishopFox/h2csmuggler
- Set-up
git clone https://github.com/BishopFox/h2csmuggler cd h2csmuggler pip3 install h2
- Scanning
python3 h2csmuggler.py --scan-list urls.txt --threads 5
- Get internal endpoint
python3 h2csmuggler.py -x https://edgeserver \ -X POST -d '{"user":128457 "role": "admin"}' \ -H "Content-Type: application/json" \ -H "X-SYSTEM-USER: true" \ http://backend/api/internal/user/permissions
- Bruteforce endpoint
python3 h2csmuggler.py -x https://edgeserver -i dirs.txt http://localhost/
- Get AWS Metadata API
python3 h2csmuggler.py -x https://edgeserver \ -X PUT \ -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \ http://169.254.169.254/latest/api/token`
๐ก Defensive techniques
HTTP Request Smuggling / WebSocket Connection Smuggling๊ณผ ๊ฐ์ด ์ฌ๋ฌ๊ฐ์ง ๋์๋ฐฉ์์ด ์๊ฒ ์ง๋ง, ์๋ฆฌ์ ์ธ ์ธก๋ฉด์์ ๋ณด๋ฉด RFC ๋ฌธ์์์ ์ด์ผ๊ธฐ๋๋๋ก tls connection์์ h2c upgrade๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ์ ํํฉ๋๋ค.
๐น Tools
๐ Articles
๐ References
- https://labs.bishopfox.com/tech-blog/h2c-smuggling-request-smuggling-via-http/2-cleartext-h2c
- https://www.hahwul.com/2020/09/16/http2-h2c-smuggling/
- https://developers.google.com/web/fundamentals/performance/http2?hl=ko
- https://en.wikipedia.org/wiki/HTTP/2
- https://tools.ietf.org/html/rfc7540
- https://tools.ietf.org/html/rfc7301
- https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids