-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRe2Pcap-cmd
executable file
·67 lines (54 loc) · 2.71 KB
/
Re2Pcap-cmd
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
#!/usr/bin/env python3
import requests, logging, sys, os, json
# Define logger and logLevel
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
def usage():
print("\nUsage: Re2Pcap-cmd [ --req <Request file> ] [ --res <Response file> ] [--pcap <PCAP file name> ]"
"\nExample: Re2Pcap-cmd --req request_file --res response_files --pcap result\n")
def main():
print("""
╦═╗╔═╗┌─┐╔═╗╔═╗╔═╗╔═╗
╠╦╝║╣ ┌─┘╠═╝║ ╠═╣╠═╝
╩╚═╚═╝└─┘╩ ╚═╝╩ ╩╩ """)
inputRequest = ""
inputResponse = ""
pcapFileName = "Re2Pcap-Result"
if len(sys.argv) < 3:
usage()
sys.exit(1)
if '--req' in sys.argv:
inputRequest = open(sys.argv[sys.argv.index('--req') + 1]).read()
if '--res' in sys.argv:
inputResponse = open(sys.argv[sys.argv.index('--res') + 1]).read()
if '--pcap' in sys.argv:
pcapFileName = sys.argv[sys.argv.index('--pcap') + 1]
# Validate the given Request/ Response before sending it to /createPcap route
validate_url = 'http://localhost:5000/validate'
data = {"inputRequest": inputRequest, "inputResponse": inputResponse}
try:
validation_response = requests.post(validate_url, data=data)
if "success" in validation_response.text:
logger.info(" Woot! Input seems to be valid. Creating PCAP; please wait!")
# Make request to Re2Pcap container running at localhost:5000. Please change this if container is running on different IP/port
url = "http://localhost:5000/createPcap"
data = {"inputRequest": inputRequest, "inputResponse": inputResponse, "pcapFileName": pcapFileName}
try:
response = requests.post(url, data=data, stream=True)
if response.status_code == 200:
with open(os.path.join(os.getcwd(),"{}.pcap".format(pcapFileName)), "wb") as wfile:
for chunk in response.iter_content(chunk_size=8192):
wfile.write(chunk)
logger.info(" PCAP file is stored as {}.pcap.\n\nThank you for using Re2Pcap :)".format(pcapFileName))
else:
logger.error(" Something went wrong while creating PCAP. Please try again....")
except requests.exceptions.RequestException as e:
logger.error(" Failed to send request to Re2Pcap container :( Please try again....\n --> {}".format(e))
else:
logger.error(" The input is not parsable. Please check following error and correct the input\n --> {} ".format(json.loads(validation_response.text)['error']))
except requests.exceptions.RequestException as e:
logger.error(" Failed to send request to Re2Pcap container :( Please try again....\n{}".format(e))
if __name__ == "__main__":
main()