-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebcrab.py
71 lines (63 loc) · 1.83 KB
/
webcrab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""
quick script to collect responses from a web server
to be copied to the DShield honeypot
"""
import requests
import sys
import json
import time
def jsonfy(resp,comment):
"""
create a json version of the response compatible with the
dshield honeypot
"""
jresp={}
jresp['id']=0
jresp['headers']=dict(resp.headers)
jresp['comment']=comment
jresp['status_code']=resp.status_code
jresp['body']=resp.text
return(json.dumps(dict(jresp), indent=2))
def sendrequest(r):
"""
send the request to the webserver
"""
required=['host','host','url','method','comment','scheme']
for x in required:
if x not in r:
print(f"Missing required parameter {x} in request. \n\n{r}")
sys.exit()
if 'headers' not in 'r':
r['headers']={}
if 'body' not in 'r':
r['body']=''
if 'port' in 'r':
url = r['scheme']+'://'+r['host']+':'+r['port']+r['url']
else:
url = r['scheme']+'://'+r['host']+r['url']
print(f"Requesting {url}")
response = requests.request(r['method'],url, headers=r['headers'], data=r['body'])
return(response)
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: webcrab.py [request file] [response file]")
sys.exit()
requestfile = sys.argv[1]
resultfile = sys.argv[2] + "." + str(int(time.time()))
try:
f = open(requestfile)
except FileNotFoundError:
print(f"Can not find {requestfile}")
sys.exit()
try:
data = json.load(f)
except json.decoder.JSONDecodeError:
print(f"Error parsing '{requestfile}'")
sys.exit()
f.close()
f = open(resultfile,'a')
for r in data:
response = sendrequest(r)
f.write(jsonfy(response,r['comment']))
f.close()