-
Notifications
You must be signed in to change notification settings - Fork 35
/
pyCCSniffer.py
319 lines (269 loc) · 11.5 KB
/
pyCCSniffer.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
"""
pyCCSniffer - a python module to connect to the CC2531emk USB dongle, decode
the received frames and provide a quick way to get to your
bytes!
Copyright (c) 2014, Andrew Dodd ([email protected])
This is takes the best parts of two existing sniffers:
1. ccsniffer - Copyright (c) 2012, George Oikonomou ([email protected])
2. sensniffer - Copyright (C) 2012 Christian Panton <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
import argparse
import logging
import sys
from binascii import hexlify
from builtins import input
from datetime import datetime
from io import StringIO
from cc253xemk import CC253xEMK
from packet_handler import PacketHandler, SniffedPacket
"""
Functionality
-------------
Read IEEE802.15.4 frames from the default CC2531 EMK sniffer firmware,
decode them and store them in memory (and maybe print(them yeah)!).
In interactive mode, the user can also input commands from stdin.
"""
__version__ = '0.0.1'
defaults = {
'debug_level': 'WARNING',
'log_level': 'INFO',
'log_file': 'pyCCSniffer.log',
'channel': 11,
}
logger = logging.getLogger(__name__)
class DefaultHandler:
def __init__(self, handlers=None, stats=None):
self.stats = {} if stats is None else stats
self.stats['Captured'] = 0
self.stats['Non-Frame'] = 0
self.last_timestamp = -1
self.start_seconds = (datetime.now() -
datetime(1970, 1, 1)).total_seconds()
self.times_wrapped = 0
self.__handlers = handlers or []
self.last_heartbeat_time = None
def received_valid_frame(self, timestamp, mac_pdu):
""" Dispatches any received packets to all registered handlers
Args:
timestamp: The timestamp the packet was received, as reported by
the sniffer device, in microseconds.
macPDU: The 802.15.4 MAC-layer PDU, starting with the Frame Control
Field (FCF).
"""
if len(mac_pdu) > 0:
if timestamp < self.last_timestamp:
self.times_wrapped += 1
logger.warning(f"Timestamp wrapped - {self.times_wrapped}")
self.last_timestamp = timestamp
synced_timestamp = self.start_seconds + (
(self.times_wrapped << 32) | timestamp)
self.stats['Captured'] += 1
packet = SniffedPacket(mac_pdu, synced_timestamp)
for handler in self.__handlers:
handler.handleSniffedPacket(packet)
def received_invalid_frame(self, timestamp, frame_len, frame):
logger.warning(
f"Received a frame with incorrect length, pkgLen:{frame_len}, len(frame):{len(frame)}"
)
self.stats['Non-Frame'] += 1
def received_heartbeat_frame(self, counter):
current_time = datetime.now()
delta = current_time - self.last_heartbeat_time if self.last_heartbeat_time else ""
logger.warning(f"HEARTBEAT - {counter} - {delta}")
self.last_heartbeat_time = current_time
def received_unknown_command(self, cmd, payload_len, payload):
logger.warning(
f"UNKNOWN - CMD[{cmd:02x}] Len[{payload_len}] Bytes[{payload}]")
def received_invalid_command(self, cmd, payload_len, payload):
logger.warning(
f"INVALID - CMD[{cmd:02x}] Len[{payload_len}] Bytes[{payload}]")
def arg_parser():
debug_choices = ('DEBUG', 'INFO', 'WARNING', 'ERROR')
parser = argparse.ArgumentParser(add_help=False,
description='Read IEEE802.15.4 frames \
from a CC2531EMK packet sniffer device, parse them and dispay them in text.'
)
in_group = parser.add_argument_group('Input Options')
in_group.add_argument(
'-c',
'--channel',
type=int,
action='store',
choices=list(range(11, 27)),
default=defaults['channel'],
help=
f"Set the sniffer's CHANNEL. Valid range: 11-26. (Default: {defaults['channel']}",
)
in_group.add_argument(
'-a',
'--annotation',
type=str,
help='Include a free-form annotation on every capture.')
log_group = parser.add_argument_group('Verbosity and Logging')
log_group.add_argument(
'-r',
'--rude',
action='store_true',
default=False,
help=
'Run in non-interactive mode, without accepting user input. (Default Disabled)'
)
log_group.add_argument(
'-D',
'--debug-level',
action='store',
choices=debug_choices,
default=defaults['debug_level'],
help=
f"Print messages of severity DEBUG_LEVEL or higher (Default {defaults['debug_level']}",
)
log_group.add_argument('-L',
'--log-file',
action='store',
nargs='?',
const=defaults['log_file'],
default=False,
help=f"""Log output in LOG_FILE. If -L is specified
but LOG_FILE is omitted, {defaults['log_file']} will be used.
If the argument is omitted altogether,
logging will not take place at all.""")
log_group.add_argument('-l',
'--log-level',
action='store',
choices=debug_choices,
default=defaults['log_level'],
help=f"""Log messages of severity LOG_LEVEL or
higher. Only makes sense if -L is also
specified (Default {defaults['log_level']})"""
)
gen_group = parser.add_argument_group('General Options')
gen_group.add_argument('-v',
'--version',
action='version',
version=f'pyCCSniffer v{__version__}')
gen_group.add_argument('-h',
'--help',
action='help',
help='Shows this message and exits')
return parser.parse_args()
def dump_stats(stats):
s = StringIO()
s.write('Frame Stats:\n')
for name, count in list(stats.items()):
s.write(f'{name:20s}: {count}\n')
print(s.getvalue())
def log_init():
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(getattr(logging, args.debug_level))
cf = logging.Formatter('%(message)s')
ch.setFormatter(cf)
logger.addHandler(ch)
if args.log_file:
fh = logging.handlers.RotatingFileHandler(filename=args.log_file,
maxBytes=5000000)
fh.setLevel(getattr(logging, args.log_level))
ff = logging.Formatter('%(asctime)s - %(levelname)8s - %(message)s')
fh.setFormatter(ff)
logger.addHandler(fh)
if __name__ == '__main__':
args = arg_parser()
log_init()
logger.info('Started logging')
stats = {}
packetHandler = PacketHandler(stats)
packetHandler.enable()
if args.annotation:
packetHandler.setAnnotation(args.annotation)
# Create a list of handlers to dispatch to, NB: handlers must have a "handleSniffedPacket" method
handler = DefaultHandler([packetHandler], stats=stats)
snifferDev = CC253xEMK(handler, args.channel)
def printHelp():
h = StringIO()
deviceStr = str(snifferDev)
h.write(deviceStr + '\n')
h.write('-' * len(deviceStr) + '\n')
h.write('Commands:\n')
h.write('c: Print current RF Channel\n')
h.write('h,?: Print this message\n')
h.write('[11,26]: Change RF channel\n')
h.write('s: Start/stop the packet capture\n')
h.write('d: Toggle frame dissector\n')
h.write('a*: Set an annotation (write "a" to remove it)\n')
h.write('p: Print all capture packets\n')
h.write('q: Quit')
h = h.getvalue()
print(h)
if args.rude == False:
printHelp()
try:
while 1:
if args.rude:
if not snifferDev.isRunning():
snifferDev.start()
else:
try:
# use the Windows friendly "raw_input()", instead of select()
cmd = input('')
if '' != cmd:
logger.debug(f'User input: "{cmd}"')
if cmd in ('h', '?'):
printHelp()
elif cmd == 'c':
# We'll only ever see this if the user asked for it, so we are
# running interactive. Print away
print(
f'Sniffing in channel: {snifferDev.get_channel()}'
)
elif cmd == 'd':
if packetHandler.isEnabled():
packetHandler.disable()
print("Dissector disabled")
else:
packetHandler.enable()
print("Dissector enabled")
elif cmd == 'p':
logger.info('User requested print all')
packetHandler.printAllFrames()
elif cmd == 'q':
logger.info('User requested shutdown')
sys.exit(0)
elif cmd == 's':
if snifferDev.isRunning():
snifferDev.stop()
print("Stopped")
else:
snifferDev.start()
print("Started")
elif 'a' == cmd[0]:
if 1 == len(cmd):
packetHandler.setAnnotation('')
else:
packetHandler.setAnnotation(cmd[1:].strip())
elif int(cmd) in range(11, 27):
snifferDev.set_channel(int(cmd))
print(
f'Sniffing in channel: {snifferDev.get_channel()}'
)
else:
print("Channel must be from 11 to 26 inclusive.")
except ValueError:
print('Unknown Command. Type h or ? for help')
except (KeyboardInterrupt, SystemExit):
logger.warning('Shutting down')
if snifferDev.isRunning():
snifferDev.stop()
dump_stats(packetHandler.stats)
sys.exit(0)