| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import pyshark |
| 4 |
import struct |
| 5 |
import sys |
| 6 |
import binascii |
| 7 |
|
| 8 |
def hexDump(s): |
| 9 |
h = " ".join(map("{:02x}".format, s)) |
| 10 |
t = "" |
| 11 |
for i in range(len(s)): |
| 12 |
c = s[i] |
| 13 |
if c>=0x20 and c<0x7f: |
| 14 |
t += chr(c) |
| 15 |
else: |
| 16 |
t += "." |
| 17 |
return( h + " | " + t) |
| 18 |
|
| 19 |
def isWriteCommand(p): |
| 20 |
if not isDataPacket(p): |
| 21 |
return False |
| 22 |
data = getData(p) |
| 23 |
return 'W' == chr(data[0]) |
| 24 |
|
| 25 |
def isDataPacket(p): |
| 26 |
return ("host" == p.usb.src) and ("USB.CAPDATA" in p) |
| 27 |
|
| 28 |
def getData(p): |
| 29 |
if not isDataPacket(p): |
| 30 |
return None |
| 31 |
return binascii.a2b_hex(p["USB.CAPDATA_RAW"].value) |
| 32 |
|
| 33 |
|
| 34 |
def dumpWriteCommand(p, nextaddr): |
| 35 |
if not isWriteCommand(p): |
| 36 |
return |
| 37 |
data = getData(p) |
| 38 |
res = struct.unpack(">cIB16sBB", data) |
| 39 |
addr = res[1] |
| 40 |
if nextaddr != addr: |
| 41 |
print((8+3+16*3+16+2)*"-") |
| 42 |
print("{:08X} : {}".format(addr, hexDump(res[3]))) |
| 43 |
return addr+16 |
| 44 |
|
| 45 |
cap = pyshark.FileCapture(sys.argv[1], include_raw=True, use_json=True) |
| 46 |
nextaddr = 0 |
| 47 |
for p in cap: |
| 48 |
if isWriteCommand(p): |
| 49 |
nextaddr = dumpWriteCommand(p, nextaddr) |