Skip to content

Instantly share code, notes, and snippets.

@smartm13
Last active February 26, 2019 22:06
Show Gist options
  • Save smartm13/f03df020ea0824037885e3a996e99365 to your computer and use it in GitHub Desktop.
Save smartm13/f03df020ea0824037885e3a996e99365 to your computer and use it in GitHub Desktop.
A python function to convert raw http request (mostly copied from burp or verbose curl) into a requests function.. Creates a dynamic function that can accept variables.
def burp2req(rawDump):
"""paste the raw dump from burp suite and replace all variables b/w << >>.
It will return an equivalent py function that accepts variables from dump and returns response obj"""
head=list(map(str.strip,rawDump.split("\n\n",1)))
if len(head)==2:head,body=head
else:head,body=head[0]," "
l1,head=head.split('\n',1)
l1,head=l1.split(),dict([map(str.strip,h.split(":",1)) for h in head.split('\n') if h.strip()])
host=head.get('Host',head.get('host',head.get("HOST",None)))
pr="https://" if l1[-1]=="HTTP/1.1" else "http://"
path=l1[1]
url=pr+host+path
varsinUrl=__import__("re").findall(r'<<(.*?)>>',url)
url=url.replace("{","{{").replace("}","}}")
url=url.replace("<<","{").replace(">>","}")
varsinBody=__import__("re").findall(r'<<(.*?)>>',body)
body=body.replace("{","{{").replace("}","}}")
body=body.replace("<<","{").replace(">>","}")
#varsinBody=__import__("re").findall(r'{(.*?)}',__import__("re").findall(r'{{(.*?)}}',body)[0])
jhead=__import__("json").dumps(head)
varsinHead=__import__("re").findall(r'<<(.*?)>>',jhead)
jhead=jhead.replace("{","{{").replace("}","}}")
jhead=jhead.replace("<<","{").replace(">>","}")
#varsinHead=__import__("re").findall(r'{{(.*?)}}',jhead)[0]
#varsinHead=__import__("re").findall(r'{(.*?)}',varsinHead)
def req(**kwargs):
doc_str="""accepts:{} return:HTTPresponseObj""".format(varsinHead+varsinBody)
if "get_docstr" in kwargs:return doc_str
import requests,json
f=requests.get if l1[0].lower()=="get" else requests.post
try:
thisurl=url.format_map(kwargs)
thisbody=body.format_map(kwargs)
thisjhead=jhead.format_map(kwargs)
except KeyError as e:
e2=TypeError()
e2.args=("Missing keyword argument=({}) for calling burp2req generated function".format(e),)
raise e2
if body:
r=f(thisurl,data=thisbody,headers=json.loads(thisjhead))
else:
r=f(url)
return r
return req
#sample usage:
# rawReq="""POST /post?arg1=<<val1>> HTTP/1.1
# Host: httpbin.org
# Connection: keep-alive
# Cache-Control: max-age=0
# Upgrade-Insecure-Requests: 1
# User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36
# DNT: 1
# Content-Type: application/json
# Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
# Accept-Encoding: gzip, deflate
# Accept-Language: en-US,en;q=0.9
# Cookie: _gauges_unique=1
# {1:@,l:<<o>>}"""
#httpbinGet=burp2req(rawReq)
#print(httpbinGet(val1=1,o=123).text)
#OUTPUT:
# {
# "args": {
# "arg1": "1"
# },
# "data": "{1:@,l:123}",
# "files": {},
# "form": {},
# "headers": {
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
# "Accept-Encoding": "gzip, deflate",
# "Accept-Language": "en-US,en;q=0.9",
# "Cache-Control": "max-age=0",
# "Content-Length": "11",
# "Content-Type": "application/json",
# "Cookie": "_gauges_unique=1",
# "Dnt": "1",
# "Host": "httpbin.org",
# "Upgrade-Insecure-Requests": "1",
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
# },
# "json": null,
# "origin": "13.234.20.225, 13.234.20.225",
# "url": "https://httpbin.org/post?arg1=1"
# }
@smartm13
Copy link
Author

To add arguments just make variables inside brackets like <>
Now, resulting function will expect a var1 keyworded argument while making the requests call

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