Last active
December 30, 2023 22:08
-
-
Save allanlw/5b5ad650e90d29cd65c7f9d9c8d46af0 to your computer and use it in GitHub Desktop.
Generate an HTTP2 Request for piping to netcat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import struct | |
HTTP2_HDR="PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" | |
# Does the thing for a frame | |
def frame(ty, flags, streamid, payload): | |
return struct.pack(">L", len(payload))[1:4] + struct.pack(">BBL", ty, flags, streamid) + payload | |
last_stream_id = 0 | |
# Returns a single naively hpacked value | |
def encode_header(key, value): | |
key = key.lower() | |
return struct.pack("<BB", 0, len(key)) + key + struct.pack("<B", len(value)) + value | |
# Does the thing for the HEADERS frame type | |
def headers(headers): | |
global last_stream_id | |
last_stream_id += 1 | |
# flags= (END_STREAM (0x1) | END_HEADERS (0x4)) | |
return frame(0x1, 0x5, last_stream_id, "".join(encode_header(x,y) for (x,y) in headers.items())) | |
def request(host, path, method="GET", scheme="http", extra_headers=None): | |
h = {":authority": host, ":path": path, ":method": method, ":scheme": scheme} | |
if extra_headers is not None: | |
h.update(extra_headers) | |
return headers(h) | |
print(HTTP2_HDR + frame(0x4, 0, 0, '') + request("localhost", "/example.php?foo=bar", extra_headers={"User-Agent": "baz"}), end="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I want to run this code I get this error: