| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import pyshark |
| 4 |
import sys |
| 5 |
import binascii |
| 6 |
from abc import ABCMeta |
| 7 |
|
| 8 |
dev_addr = 1 |
| 9 |
|
| 10 |
if len(sys.argv) != 2: |
| 11 |
print("Usage: extract.py PCAPNG_FILE") |
| 12 |
|
| 13 |
|
| 14 |
def hexDump(s: bytes, prefix: str = "", addr: int = 0) -> str: |
| 15 |
""" Utility function to hex-dump binary data. """ |
| 16 |
N = len(s) |
| 17 |
Nb = N//16 |
| 18 |
if (N%16): Nb += 1 |
| 19 |
res = "" |
| 20 |
for j in range(Nb): |
| 21 |
a,b = j*16, min((j+1)*16,N) |
| 22 |
|
| 23 |
h = " ".join(map("{:02x}".format, s[a:b])) |
| 24 |
h += " "*(16-(b-a)) |
| 25 |
t = "" |
| 26 |
for i in range(a,b): |
| 27 |
c = s[i] |
| 28 |
if c>=0x20 and c<0x7f: |
| 29 |
t += chr(c) |
| 30 |
else: |
| 31 |
t += "." |
| 32 |
|
| 33 |
res += (prefix + "{:08X} ".format(addr+16*j) + h + " | " + t + "\n") |
| 34 |
return res[:-1] |
| 35 |
|
| 36 |
def isFromHost(p): |
| 37 |
return ("host" == p.usb.src) and ((None==dev_addr) or (dev_addr == int(p.usb.device_address))) |
| 38 |
|
| 39 |
def isToHost(p): |
| 40 |
return (("host" == p.usb.dest) and ((None==dev_addr) or (dev_addr == int(p.usb.device_address)))) |
| 41 |
|
| 42 |
def isDataPacket(p): |
| 43 |
return ("USB.CAPDATA" in p) |
| 44 |
|
| 45 |
def getData(p): |
| 46 |
if not isDataPacket(p): |
| 47 |
return None |
| 48 |
return binascii.a2b_hex(p["USB.CAPDATA_RAW"].value) |
| 49 |
|
| 50 |
|
| 51 |
cap = pyshark.FileCapture(sys.argv[1], include_raw=True, use_json=True) |
| 52 |
nextaddr = 0 |
| 53 |
for p in cap: |
| 54 |
if isDataPacket(p): |
| 55 |
if isFromHost(p): |
| 56 |
print(hexDump(getData(p), "< ")) |
| 57 |
else: |
| 58 |
print(hexDump(getData(p), "> ")) |
| 59 |
#print(p) |
| 60 |
|