Skip to content

Instantly share code, notes, and snippets.

@homedirectory
Last active September 8, 2021 15:37
Show Gist options
  • Save homedirectory/cd7c27ecb66eac364d3b1bbbcfe05cfb to your computer and use it in GitHub Desktop.
Save homedirectory/cd7c27ecb66eac364d3b1bbbcfe05cfb to your computer and use it in GitHub Desktop.
snip long values from HTTP requests - URL parameters, Cookie values, Authorization tokens
MAX_LENGTH = 12
def snip(request):
"""
request - str
"""
snipped_req = ""
lines = list(filter(lambda l: len(l) > 0, request.split('\n')))
# 1st line
# METHOD PATH HTTP_VERSION
method, path, http_v = lines[0].split()
# TODO snip parameters
first_q = path.index('?')
snipped_line = method + ' ' + path[:first_q] + '?'
params = path[first_q + 1:].split('&')
for p in params:
p_name, p_value = p.split('=')
if len(p_value) > MAX_LENGTH:
p_value = "[SNIPPED]"
snipped_line += f"{p_name}={p_value}&"
snipped_line = snipped_line.strip('&')
snipped_line += ' ' + http_v
snipped_req += snipped_line + '\n'
for line in lines[1:]:
snipped_line = line
if line.startswith("Cookie:"):
snipped_line = "Cookie: "
cookies = [c.strip() for c in line[7:].strip().split(';')]
for c in cookies:
# name=value
c_name, c_value = c.split('=')
if len(c_value) > MAX_LENGTH:
c_value = "[SNIPPED]"
snipped_line += f"{c_name}={c_value}; "
snipped_line = snipped_line.strip('; ')
if line.startswith("Authorization:"):
snipped_line = "Authorization: "
auth_type, creds = [i for i in line[14:].split()]
if len(creds) > MAX_LENGTH:
creds = "[SNIPPED]"
snipped_line += f"{auth_type} {creds}"
snipped_req += snipped_line + '\n'
return snipped_req
if __name__ == "__main__":
import sys
try:
req_file = sys.argv[1]
except:
print(f"usage: python3 snip.py FILE")
print("FILE - plaintext request (with newlines pls)")
sys.exit(1)
with open(req_file, 'r') as f:
req = f.read()
print(snip(req))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment