-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpyscraper_example.py
65 lines (49 loc) · 1.64 KB
/
pyscraper_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
"""
Example showing how to selectively translate parts of a USB packet capture into higher level functionality
When something isn't recognized it prints using default usbrply decoding
"""
import json
import subprocess
from usbrply.pyprinter import LibusbPyPrinter
from usbrply.printer import indented
class Scraper:
def __init__(self, verbose=False):
self.pyprint = LibusbPyPrinter(argsj={"wrapper": True},
verbose=verbose)
def parse_data(self, d):
# Translate to higher level function
if d["type"] == "controlRead" and d["bRequest"] == 0x02:
indented("vendor_request1()")
# Ignore bulk reads
elif d["type"] == "bulkRead":
pass
# Default: print as normal python replay
else:
self.pyprint.parse_data(d)
def run(self, j):
self.pyprint.header()
for d in j["data"]:
self.parse_data(d)
self.pyprint.footer()
def load_json(fin, usbrply=""):
if fin.find('.cap') >= 0 or fin.find('.pcapng') >= 0:
json_fn = '/tmp/scrape.json'
cmd = 'usbrply %s --json %s >%s' % (usbrply, fin, json_fn)
subprocess.check_call(cmd, shell=True)
else:
json_fn = fin
j = json.load(open(json_fn))
return j, json_fn
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--usbrply', default='')
parser.add_argument('fin')
args = parser.parse_args()
j, json_fn = load_json(
args.fin,
args.usbrply,
)
scraper = Scraper()
scraper.run(j)