Skip to content

Instantly share code, notes, and snippets.

@NicolaiSoeborg
Created August 5, 2023 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicolaiSoeborg/e29441e6fda41d81c489cdc1bc00f107 to your computer and use it in GitHub Desktop.
Save NicolaiSoeborg/e29441e6fda41d81c489cdc1bc00f107 to your computer and use it in GitHub Desktop.
SSH + HTTP Polyglot
import trio # python3 -m pip install --upgrade trio
HTML = "<html>Hello World!</html>"
HTTP_BANNER = f"HTTP/1.1 200 OK\nContent-Length: {len(HTML)+1}\n\n{HTML}\n".encode()
async def forward_from_a_to_b(a, b):
async for chunk in a:
print(f"=> {chunk}", flush=True)
await b.send_all(chunk)
async def proxy(listen_stream):
await listen_stream.send_all(HTTP_BANNER)
target_stream = await trio.open_tcp_stream('localhost', 22)
async with trio.open_nursery() as nursery:
nursery.start_soon(forward_from_a_to_b, listen_stream, target_stream)
nursery.start_soon(forward_from_a_to_b, target_stream, listen_stream)
async def main():
while True:
try:
await trio.serve_tcp(proxy, 422)
except KeyboardInterrupt:
break
except trio.BrokenResourceError:
print("Conn died")
if __name__ == '__main__':
trio.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment