Skip to content
Newer
Older
100755 78 lines (63 sloc) 2.35 KB
509fb60 flow stitching appears to be working
Andrew Fleenor authored
1 #!/usr/bin/python
2
17af897 Widespread documentation improvements.
Andrew Fleenor authored
3 '''
4 Main program that converts pcaps to HAR's.
5 '''
6
d6b4afb started adding HTTP Parsing
Andrew Fleenor authored
7 import os
8 import optparse
9 import logging
10 import sys
0515fcc now writing proto-HAR
Andrew Fleenor authored
11 import json
b557365 Move most pcap2har .py files into a library directory
Andrew Fleenor authored
12
13 from pcap2har import pcap
14 from pcap2har import http
15 from pcap2har import httpsession
16 from pcap2har import har
17 from pcap2har import tcp
18 from pcap2har import settings
19 from pcap2har.packetdispatcher import PacketDispatcher
4120b4f Economize on memory in http, and related changes.
Andrew Fleenor authored
20 from pcap2har.pcaputil import print_rusage
21
509fb60 flow stitching appears to be working
Andrew Fleenor authored
22
8281362 beginning testing, comparing pcap2har and pyper
Andrew Fleenor authored
23 # get cmdline args/options
bde24af DNS processing in progress
Andrew Fleenor authored
24 parser = optparse.OptionParser(
abe2462 more doc formatting improvements, dead code removed
Andrew Fleenor authored
25 usage='usage: %prog inputfile outputfile'
bde24af DNS processing in progress
Andrew Fleenor authored
26 )
c9c0fa8 Merge changes from Google
Andrew Fleenor authored
27 parser.add_option('--no-pages', action='store_false',
28 dest='pages', default=True)
29 parser.add_option('-d', '--drop-bodies', action='store_true',
30 dest='drop_bodies', default=False)
938787e Correctly handle requests with no response.
Andrew Fleenor authored
31 parser.add_option('-k', '--keep-unfulfilled-requests', action='store_true',
32 dest='keep_unfulfilled', default=False)
c9c0fa8 Merge changes from Google
Andrew Fleenor authored
33 parser.add_option('-r', '--resource-usage', action='store_true',
34 dest='resource_usage', default=False)
8ee63ca Options added to pad missing TCP data (making HTTP parsing more
Matt Welsh authored
35 parser.add_option('--pad_missing_tcp_data', action='store_true',
36 dest='pad_missing_tcp_data', default=False)
5558e74 Merge in fixes and tweaks to Matt's changes from Google.
Andrew Fleenor authored
37 parser.add_option('--strict-http-parsing', action='store_true',
38 dest='strict_http_parsing', default=False)
624f478 Add log file option to main.py
Andrew Fleenor authored
39 parser.add_option('-l', '--log', dest='logfile', default='pcap2har.log')
2496357 made main.py batchable by using cmdline args instead of hardcoded var…
Andrew Fleenor authored
40 options, args = parser.parse_args()
41
99ae432 optionalized page parsing, with cmd-line option
Andrew Fleenor authored
42 # copy options to settings module
43 settings.process_pages = options.pages
c9c0fa8 Merge changes from Google
Andrew Fleenor authored
44 settings.drop_bodies = options.drop_bodies
938787e Correctly handle requests with no response.
Andrew Fleenor authored
45 settings.keep_unfulfilled_requests = options.keep_unfulfilled
8ee63ca Options added to pad missing TCP data (making HTTP parsing more
Matt Welsh authored
46 settings.pad_missing_tcp_data = options.pad_missing_tcp_data
5558e74 Merge in fixes and tweaks to Matt's changes from Google.
Andrew Fleenor authored
47 settings.strict_http_parse_body = options.strict_http_parsing
99ae432 optionalized page parsing, with cmd-line option
Andrew Fleenor authored
48
1b174b4 more, maybe better logging and error handling
Andrew Fleenor authored
49 # setup logs
624f478 Add log file option to main.py
Andrew Fleenor authored
50 logging.basicConfig(filename=options.logfile, level=logging.INFO)
1b174b4 more, maybe better logging and error handling
Andrew Fleenor authored
51
d6b4afb started adding HTTP Parsing
Andrew Fleenor authored
52 # get filenames, or bail out with usage error
53 if len(args) == 2:
54 inputfile, outputfile = args[0:2]
71d8f99 fixed optional arguments, catch dpkt errors from ModifiedReader
Andrew Fleenor authored
55 elif len(args) == 1:
e717730 Add default output filename for cmdline app
Andrew Fleenor authored
56 inputfile = args[0]
57 outputfile = inputfile+'.har'
d6b4afb started adding HTTP Parsing
Andrew Fleenor authored
58 else:
59 parser.print_help()
60 sys.exit()
c3db28a I don't know everything that's in here.
Andrew Fleenor authored
61
0c142ac Wide-ranging cleanup
Andrew Fleenor authored
62 logging.info('Processing %s', inputfile)
8939321 changed architecture for processing pcap files
Andrew Fleenor authored
63
64 # parse pcap file
3623265 add EasyParsePcap function to handle boilerplate.
Andrew Fleenor authored
65 dispatcher = pcap.EasyParsePcap(filename=inputfile)
92ace86 Getting timestamps from TCP flows appears to be working with HTTP par…
Andrew Fleenor authored
66
ccca495 created HTTPSession class, associated bug fixes.
Andrew Fleenor authored
67 # parse HAR stuff
0a9ab09 nitpicky cleanup
Andrew Fleenor authored
68 session = httpsession.HttpSession(dispatcher)
47dd1ed tentative DNS integration
Andrew Fleenor authored
69
0c142ac Wide-ranging cleanup
Andrew Fleenor authored
70 logging.info('Flows=%d. HTTP pairs=%d' % (len(session.flows), len(session.entries)))
0515fcc now writing proto-HAR
Andrew Fleenor authored
71
0a9ab09 nitpicky cleanup
Andrew Fleenor authored
72 #write the HAR file
0515fcc now writing proto-HAR
Andrew Fleenor authored
73 with open(outputfile, 'w') as f:
55a607e sort JSON keys, for testing purposes
Andrew Fleenor authored
74 json.dump(session, f, cls=har.JsonReprEncoder, indent=2, encoding='utf8', sort_keys=True)
c9c0fa8 Merge changes from Google
Andrew Fleenor authored
75
76 if options.resource_usage:
77 print_rusage()
Something went wrong with that request. Please try again.