Last active
March 20, 2025 15:51
-
-
Save hhue13/929583d49674b823c4bc61042844f2e9 to your computer and use it in GitHub Desktop.
A simple HTTPSServer to dump POST payload to the console
This file contains hidden or 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 python3 | |
# -*- coding: utf-8 -*- | |
# httpsServer.py | |
# | |
# Inspired by: https://anvileight.com/blog/posts/simple-python-http-server/ | |
#################################################################################### | |
''' | |
Start the server: | |
httpsServer.py \ | |
--host 192.168.122.1 \ | |
--port 6725 \ | |
--keyFile /home/hhuebler/data/oscp/hetzner/certs/hhuelinux2.key \ | |
--certFile /home/hhuebler/data/oscp/hetzner/certs/hhuelinux2.crt | |
To test the server with curl run: | |
curl -k -X POST -H "Content-Type: application/json" -d '{"name":"hhue", "age":"-1", "xx": "yy"}' https://192.168.122.1:6725 | |
which should print a well formatted json object to the console. | |
''' | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
import argparse | |
import ssl | |
import json | |
from io import BytesIO | |
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
def do_POST(self): | |
content_length = int(self.headers['Content-Length']) | |
content_type = self.headers['Content-Type'] | |
body = self.rfile.read(content_length) | |
self.send_response(200) | |
self.end_headers() | |
response = BytesIO() | |
self.wfile.write(response.getvalue()) | |
if (content_type == "application/json"): | |
jsonBody = json.loads(body.decode('utf-8')) | |
print(json.dumps(jsonBody, indent=2)) | |
else: | |
print(body.decode('utf-8')) | |
def log_message(*args): | |
pass | |
def parseArgs(): | |
""" | |
Function to parse the command line arguments and return them as an args namespace object | |
""" | |
parser = argparse.ArgumentParser(prog='httpsServer.py', | |
description='Acts as an HTTP server to dump POST data sent to it.', | |
epilog='Good luck!') | |
parser.add_argument('--host', type=str, help='Hostname or ip to listen for requests.', default='127.0.0.1') | |
parser.add_argument('--port', type=int, help='Port number to listen for requests.', default=6725) | |
parser.add_argument('--keyFile', type=argparse.FileType('r', encoding='utf-8'), help='Path to the key file for the server.', required=True) | |
parser.add_argument('--certFile', type=argparse.FileType('r', encoding='utf-8'), help='Path to the certificate file for the server.', required=True) | |
args = parser.parse_args() | |
return args | |
## | |
## Parse the arguments | |
## | |
args = parseArgs() | |
httpd = HTTPServer((args.host, args.port), SimpleHTTPRequestHandler) | |
httpd.socket = ssl.wrap_socket (httpd.socket, | |
keyfile=args.keyFile.name, | |
certfile=args.certFile.name, server_side=True) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment