forked from andrewf/pcap2har
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·54 lines (43 loc) · 1.22 KB
/
main.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
#!/usr/bin/python
'''
Main program that converts pcaps to HAR's.
'''
import pcap
import os
import optparse
import logging
import sys
import http
import httpsession
import har
import json
import tcp
import settings
from packetdispatcher import PacketDispatcher
# get cmdline args/options
parser = optparse.OptionParser(
usage='usage: %prog inputfile outputfile'
)
parser.add_option('--no-pages', action="store_false", dest="pages", default=True)
options, args = parser.parse_args()
# copy options to settings module
settings.process_pages = options.pages
# setup logs
logging.basicConfig(filename='pcap2har.log', level=logging.INFO)
# get filenames, or bail out with usage error
if len(args) == 2:
inputfile, outputfile = args[0:2]
else:
parser.print_help()
sys.exit()
logging.info("Processing %s", inputfile)
# parse pcap file
dispatcher = PacketDispatcher()
pcap.ParsePcap(dispatcher, filename=inputfile)
dispatcher.finish()
# parse HAR stuff
session = httpsession.HttpSession(dispatcher)
logging.info("Flows=%d. HTTP pairs=%d" % (len(session.flows),len(session.entries)))
#write the HAR file
with open(outputfile, 'w') as f:
json.dump(session, f, cls=har.JsonReprEncoder, indent=2, encoding='utf8', sort_keys=True)