2009-09-24 19:02:49 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Tool to index protocols that appears in the given capture files
|
|
|
|
#
|
|
|
|
# Copyright 2009, Kovarththanan Rajaratnam <kovarththanan.rajaratnam@gmail.com>
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# Wireshark - Network traffic analyzer
|
|
|
|
# By Gerald Combs <gerald@wireshark.org>
|
|
|
|
# Copyright 1998 Gerald Combs
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
from optparse import OptionParser
|
2009-09-25 19:41:54 +00:00
|
|
|
import multiprocessing
|
2009-09-24 19:02:49 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import re
|
2009-09-25 15:02:57 +00:00
|
|
|
import pickle
|
|
|
|
|
2009-09-25 19:02:32 +00:00
|
|
|
def process_capture_file(tshark, file):
|
2009-09-25 15:02:57 +00:00
|
|
|
cmd = [tshark, "-Tfields", "-e", "frame.protocols", "-r", file]
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
if p.returncode != 0:
|
2009-09-25 20:50:51 +00:00
|
|
|
return (file, {})
|
2009-09-25 15:02:57 +00:00
|
|
|
|
|
|
|
proto_hash = {}
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
if not re.match(r'^[\w:-]+$', line):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for proto in line.split(':'):
|
2009-09-25 17:28:53 +00:00
|
|
|
proto_hash[proto] = 1 + proto_hash.setdefault(proto, 0)
|
2009-09-25 15:02:57 +00:00
|
|
|
|
|
|
|
return (file, proto_hash)
|
2009-09-24 19:02:49 +00:00
|
|
|
|
2009-09-25 17:28:53 +00:00
|
|
|
def list_proto(cap_hash):
|
|
|
|
proto_hash = {}
|
|
|
|
for files_hash in cap_hash.itervalues():
|
|
|
|
for proto,count in files_hash.iteritems():
|
|
|
|
proto_hash[proto] = count + proto_hash.setdefault(proto, 0)
|
|
|
|
|
|
|
|
print proto_hash
|
|
|
|
|
2009-09-25 17:43:01 +00:00
|
|
|
def list_files(cap_hash):
|
|
|
|
files = cap_hash.keys()
|
|
|
|
files.sort()
|
|
|
|
|
|
|
|
print files
|
|
|
|
|
|
|
|
def index_file_action(options):
|
|
|
|
return options.list_proto or options.list_files
|
|
|
|
|
2009-09-25 17:47:25 +00:00
|
|
|
def find_capture_files(paths, cap_hash):
|
|
|
|
cap_files = []
|
|
|
|
for path in paths:
|
|
|
|
if os.path.isdir(path):
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
cap_files += [os.path.join(root, name) for name in files if os.path.join(root, name) not in cap_hash]
|
|
|
|
elif path not in cap_hash:
|
|
|
|
cap_files.append(path)
|
|
|
|
return cap_files
|
|
|
|
|
2009-09-24 19:02:49 +00:00
|
|
|
def main():
|
2009-09-25 17:28:53 +00:00
|
|
|
parser = OptionParser(usage="usage: %prog [options] index_file [file_1|dir_1 [.. file_n|dir_n]]")
|
2009-09-24 19:02:49 +00:00
|
|
|
parser.add_option("-n", "--no-append", dest="append", default=True, action="store_false", help="Do not append to existing cache file")
|
|
|
|
parser.add_option("-m", "--max-files", dest="max_files", default=sys.maxint, type="int", help="Max number of files to process")
|
|
|
|
parser.add_option("-b", "--binary-dir", dest="bin_dir", default=os.getcwd(), help="Directory containing tshark executable")
|
2009-09-25 15:02:57 +00:00
|
|
|
parser.add_option("-j", dest="num_procs", default=1, type=int, help="Max number of processes to spawn")
|
2009-09-25 17:28:53 +00:00
|
|
|
parser.add_option("-l", "--list-proto", dest="list_proto", default=False, action="store_true", help="List all protocols in index file")
|
2009-09-25 17:43:01 +00:00
|
|
|
parser.add_option("-f", "--list-files", dest="list_files", default=False, action="store_true", help="List all files in index file")
|
2009-09-24 19:02:49 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) == 0:
|
2009-09-25 15:41:17 +00:00
|
|
|
parser.error("index_file is a required argument")
|
2009-09-24 19:02:49 +00:00
|
|
|
|
2009-09-25 17:43:01 +00:00
|
|
|
if len(args) == 1 and not index_file_action(options):
|
2009-09-24 19:02:49 +00:00
|
|
|
parser.error("one capture file/directory must be specified")
|
|
|
|
|
2009-09-25 15:41:17 +00:00
|
|
|
index_file_name = args.pop(0)
|
|
|
|
try:
|
2009-09-25 17:03:44 +00:00
|
|
|
index_file = open(index_file_name, "r")
|
|
|
|
print "index file:", index_file.name, "[OPENED]",
|
2009-09-25 15:41:17 +00:00
|
|
|
cap_hash = pickle.load(index_file)
|
2009-09-25 17:03:44 +00:00
|
|
|
index_file.close()
|
|
|
|
print len(cap_hash), "files"
|
2009-09-25 15:41:17 +00:00
|
|
|
except IOError:
|
2009-09-25 17:03:44 +00:00
|
|
|
print "index file:", index_file_name, "[NEW]"
|
2009-09-25 15:41:17 +00:00
|
|
|
cap_hash = {}
|
|
|
|
|
2009-09-25 17:28:53 +00:00
|
|
|
if options.list_proto:
|
|
|
|
list_proto(cap_hash)
|
|
|
|
exit(0)
|
|
|
|
|
2009-09-25 17:43:01 +00:00
|
|
|
if options.list_files:
|
|
|
|
list_files(cap_hash)
|
|
|
|
exit(0)
|
|
|
|
|
2009-09-25 17:28:53 +00:00
|
|
|
tshark = os.path.join(options.bin_dir, "tshark.exe")
|
|
|
|
if os.access(tshark, os.X_OK):
|
|
|
|
print "tshark:", tshark, "[FOUND]"
|
|
|
|
else:
|
|
|
|
print "tshark:", tshark, "[MISSING]"
|
|
|
|
exit(1)
|
|
|
|
|
2009-09-24 19:02:49 +00:00
|
|
|
paths = args
|
2009-09-25 17:47:25 +00:00
|
|
|
cap_files = find_capture_files(paths, cap_hash)
|
2009-09-24 19:02:49 +00:00
|
|
|
cap_files.sort()
|
2009-09-25 19:41:54 +00:00
|
|
|
options.max_files = min(options.max_files, len(cap_files))
|
2009-09-25 20:50:51 +00:00
|
|
|
print "%u total files, %u indexable files\n" % (len(cap_files), options.max_files)
|
2009-09-24 19:02:49 +00:00
|
|
|
cap_files = cap_files[:options.max_files]
|
|
|
|
|
2009-09-25 19:41:54 +00:00
|
|
|
pool = multiprocessing.Pool(options.num_procs)
|
2009-09-25 19:02:32 +00:00
|
|
|
results = [pool.apply_async(process_capture_file, [tshark, file]) for file in cap_files]
|
2009-09-25 20:50:51 +00:00
|
|
|
for (cur_item_idx,result) in enumerate(results):
|
|
|
|
file_result = result.get()
|
|
|
|
action = "SKIPPED" if file_result[1] is {} else "PROCESSED"
|
|
|
|
print "%s [%u/%u] %s %u bytes" % (action, cur_item_idx+1, options.max_files, file_result[0], os.path.getsize(file_result[0]))
|
2009-09-25 19:02:32 +00:00
|
|
|
cap_hash.update(dict([file_result]))
|
2009-09-24 19:02:49 +00:00
|
|
|
|
2009-09-25 17:03:44 +00:00
|
|
|
index_file = open(index_file_name, "w")
|
2009-09-25 15:41:17 +00:00
|
|
|
pickle.dump(cap_hash, index_file)
|
|
|
|
index_file.close()
|
2009-09-24 19:02:49 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|