#!/usr/bin/env python from optparse import OptionParser import sys import shutil import pcapy import impacket.ImpactDecoder as Decoders import impacket.ImpactPacket as Packets import os.path import os import chardet import string import gzip class pcapGzip: def __init__(self, pcapfile, reportpath="./report"): assert pcapfile if not os.path.exists(pcapfile): raise TypeError("Pcap file not found. Please check location.") self.reportpath = reportpath if not os.path.exists(self.reportpath): os.makedirs(self.reportpath) self.pcapfile = pcapfile def uncompressGzip(self, file): """Gunzip a gz file """ try: r_file = gzip.GzipFile(file, 'r') write_file = string.rstrip(file, '.gz') w_file = open(write_file, 'w') w_file.write(r_file.read()) w_file.close() r_file.close() os.unlink(file) print "Successfully uncompressed %s" % (file) except: print "***Error: Failed to uncompress %s" % (file) def tagFiles(self): """ Browses a given dir and tries to uncompress gz files """ listDir = os.listdir("report") for f in listDir: fullpath = os.path.join(self.reportpath, f) # full path without gz extension if open(fullpath, 'r').read(2)=='\037\213': # magic number for application/x-gzip os.rename(fullpath, fullpath+".gz") # first give gz extension to gz files self.uncompressGzip(fullpath+".gz") # then uncompress gz files def decodePayload(self, payload): """Decode a payload from the parser and returns an array of lines """ decoder = Decoders.EthDecoder() eth = decoder.decode(payload) ip = eth.child() tcp = ip.child() try: if tcp.get_RST()!=1: data = tcp.get_data_as_string() # raw data data = data.replace('\r\n', '\r\n###~~~###') arrline = data.split('\r\n') return arrline else: return None except: return None def writeFile(self, f, content): """Dump content in a file """ obFile = open(os.path.join(self.reportpath, f), 'a') obFile.write(content) obFile.close() def decodeMac(self, mac): """Decode mac address """ m = '' for i in mac: t = "%x" % i if len(t)==1: t = '0'+t m=m+":"+t return m[1:] def createFlows(self): """Create necessary flows based on pcap file """ print "running..." self.writeFile("report.html", '' + '
' + '| Num. | ' + 'Flow | ' + 'Request/Response | ' + 'Attachment | ' + '  | ') self.writeFile("report.html", '
|---|---|---|---|
| '+str(countPacket)+' | ') self.writeFile("report.html", ''+flow+' | ')
if line.startswith("Content-Type"):
style = ' style="background:#ffff00"'
ext = '.'+line.split("/")[1].split(";")[0]
if ext == '.gzip':
ext = '.gz'
else:
style = ''
self.writeFile("report.html", ' '+line+' ')
else: # raw data
if sessionFile + "-" + str(packetnum) + ext != lastAttach:
# New file
line = line.replace('###~~~###', '')
lastAttach = sessionFile + "-" + str(packetnum) + ext
self.writeFile("report.html",' | ')
if ext==".jpeg" or ext==".gif":
self.writeFile("report.html",' '
+ ext[1:] + ' ')
self.writeFile("report.html", ' |