-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.py
executable file
·75 lines (62 loc) · 2.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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)
parser.add_option('-d', '--drop-bodies', action='store_true',
dest='drop_bodies', default=False)
parser.add_option('-r', '--resource-usage', action='store_true',
dest='resource_usage', default=False)
parser.add_option('--pad_missing_tcp_data', action='store_true',
dest='pad_missing_tcp_data', default=False)
options, args = parser.parse_args()
# copy options to settings module
settings.process_pages = options.pages
settings.drop_bodies = options.drop_bodies
settings.pad_missing_tcp_data = options.pad_missing_tcp_data
# 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]
elif len(args) == 1:
inputfile = args[0]
outputfile = inputfile+'.har'
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)))
def print_rusage():
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if sys.platform == 'darwin':
rss /= 1024 # Mac OSX returns rss in bytes, not KiB
print 'max_rss:', rss, 'KiB'
#write the HAR file
with open(outputfile, 'w') as f:
json.dump(session, f, cls=har.JsonReprEncoder, indent=2, encoding='utf8', sort_keys=True)
if options.resource_usage:
print_rusage()