-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscapy_dump.py
executable file
·174 lines (142 loc) · 3.84 KB
/
scapy_dump.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""
decoder.py - An analysis of Medtronic Carelink Diabetes protocol using
scapy.
"""
# stdlib
import user
import sys
import argparse
import logging
import binascii
from os.path import getsize
logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger('decoder')
logger.setLevel(logging.INFO)
# Requires scapy to be in your PYATHONPATH
#import scapy
from scapy.all import *
from scapy import utils
from scapy import automaton
_usb_response = { 0x00: 'OUT', 0x55: "Success", 0x66: "Fail" }
_usb_commands = {
0x00: 'nil',
0x01: 'MCPY',
0x02: 'XFER',
0x03: 'POLL',
0x04: 'INFO',
0x05: 'stat',
0x06: 'SIGNAL',
0x0C: 'RFLEN',
}
class ProdInfo(Packet):
name = "ProductInfo"
fields_desc = [
XByteField('version.major', 0),
XByteField('version.minor', 0),
]
class USBReq(Packet):
name = "USBRequest"
fields_desc = [
ByteEnumField("code", 0x00, _usb_commands),
ByteEnumField("resp", 0x00, _usb_response),
XByteField("error", 0),
]
class CLMMComm(Packet):
name ="CLMM Hexline"
fields_desc = [
StrStopField('dir', 'error', ','),
PacketField('stick', None, USBReq),
]
class CLMMPair(Packet):
name = "CLMM command/response"
fields_desc = [
PacketField('send', None, CLMMComm),
PacketField('recv', None, CLMMComm),
]
class Handler(object):
def __init__(self, path, opts):
self.path = path
self.opts = opts
def __call__(self):
self.open( )
self.decode( )
self.close( )
def open(self):
self.handle = None
if self.path == '-':
self.handle = sys.stdin
else:
self.handle = open(self.path, 'rU')
def close(self):
if self.path != '-':
self.handle.close( )
class Decoder(Handler):
def clean(self, line):
line = line.strip( )
method, data = line.split(',')
data = binascii.unhexlify(data)
return ','.join([ method, data ])
def decode(self):
lines = self.handle.readlines( )
self.lines = [ ]
self.decoded = [ ]
L = len(lines)
for x in range(L):
line = self.clean(lines[x])
p = CLMMComm(line)
logger.debug("Line: %s" % x)
logger.debug(utils.hexstr(str(p.stick)))
if len(self.lines) <= 1:
self.lines.append(p)
else:
if len(self.lines) == 2 and self.lines[0].dir == p.dir:
one = self.lines[0]
two = self.lines[1]
pair = CLMMPair( send = one, recv = two)
#pair.
#pair.
pair.show( )
self.decoded.append(pair)
self.lines = [p]
else:
self.lines.append(p)
for p in self.lines:
print "###", "XXX unusual line!", p.dir
p.show( )
class Console:
_log_map = { 0: logging.ERROR, 1: logging.WARN,
2: logging.INFO, 3: logging.DEBUG }
def __init__(self, args):
self.raw_args = args
self.parser = self.get_argparser( )
args = list(args)
cmd, args = args[0], args[1:]
self.opts = self.parser.parse_args((args))
logger.setLevel(self._log_map.get(self.opts.verbose, 3))
cmdverbose = ''
if self.opts.verbose > 0:
cmdverbose = '-' + ('v' * self.opts.verbose)
#logger.info('opts: %s' % (pformat(args)))
cmdline = [ cmd, cmdverbose ] + self.opts.input
print ' '.join(cmdline)
def main(self):
logger.info('opening %s' % (self.opts.input))
for item in self.opts.input:
self.do_input(item)
def do_input(self, item):
decode = Decoder(item, self.opts)
decode( )
def get_argparser(self):
"""Prepare an argument parser."""
parser = argparse.ArgumentParser(description=__doc__ )
parser.add_argument('-v', '--verbose', action='count', default=0,
help="Verbosity.")
parser.add_argument('input', nargs='+', help="Input files")
return parser
if __name__ == '__main__':
app = Console(sys.argv)
app.main( )
#####
# EOF