Skip to content

Instantly share code, notes, and snippets.

@allanlw
Last active December 30, 2023 22:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save allanlw/5b5ad650e90d29cd65c7f9d9c8d46af0 to your computer and use it in GitHub Desktop.
Save allanlw/5b5ad650e90d29cd65c7f9d9c8d46af0 to your computer and use it in GitHub Desktop.
Generate an HTTP2 Request for piping to netcat
#!/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="")
@q2dg
Copy link

q2dg commented Dec 30, 2023

When I want to run this code I get this error:

Traceback (most recent call last):
  File "./pipeHttp2.py", line 32, in <module>
    print(HTTP2_HDR + frame(0x4, 0, 0, '') + request("localhost", "/example.php?foo=bar", extra_headers={"User-Agent": "baz"}), end="")
                      ^^^^^^^^^^^^^^^^^^^^
  File "./pipeHttp2.py", line 10, in frame
    return struct.pack(">L", len(payload))[1:4] + struct.pack(">BBL", ty, flags, streamid) + payload
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
TypeError: can't concat str to bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment