-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinjector.py
More file actions
executable file
·341 lines (265 loc) · 8.88 KB
/
injector.py
File metadata and controls
executable file
·341 lines (265 loc) · 8.88 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Injects arbitrary RF4CE packets. Support encryption.
"""
from __future__ import (absolute_import,
print_function, unicode_literals)
from builtins import *
import argparse
import time
from datetime import datetime
import binascii
import readline
from rf4ce import Dot15d4FCS, Dot15d4Data, Raw, makeFCS
from rf4ce import LinkConfig, Rf4ceFrame, Rf4ceConstants
from rf4ce.radio import TxFlow
from rf4ce.packetprocessor import PacketProcessor
import huepy as hue
class AckProcessor(PacketProcessor):
"""ACK processor thread
Thread processing incomming ACK
Full-duplex SDR is needed for this to work
"""
def __init__(self):
PacketProcessor.__init__(self)
self.last_ack = -1
def process(self, data):
"""Parses a 802.15.4 ACK and extract the seqnum"""
# Check if the 802.15.4 packet is valid
if makeFCS(data[:-2]) != data[-2:]:
print(hue.bad("Received invalid packet"))
return
packet = Dot15d4FCS(data)
if packet.fcf_frametype == 2: # ACK
self.last_ack = packet.seqnum
def get_last_ack(self):
"""Returns the seqnum of the last received ACK"""
return self.last_ack
class InjectorCmd(object):
"""Injector command object
Contains the command & arguments of all possible
injector commands
"""
PACKET = 1
PROFILE = 2
COUNTER = 3
DELAY = 4
CIPHERED = 5
HELP = 6
def to_int(self, n):
if type(n) == int:
return n
if n.startswith("0x"):
return int(n, 16)
else:
return int(n)
def to_bool(self, r):
if type(r) == bool:
return r
n = self.to_int(r)
if not n in (0, 1):
raise ValueError()
return n == 1
def __init__(self, cmd, arg):
self.action = cmd
if self.action == self.PROFILE:
self.arg = self.to_int(arg)
elif self.action == self.COUNTER:
self.arg = self.to_int(arg)
elif self.action == self.DELAY:
self.arg = float(arg)
elif self.action == self.CIPHERED:
self.arg = self.to_bool(arg)
else:
self.arg = arg
class Injector(object):
"""Injector util main class"""
def __init__(self, link_config, channel, sdr_device):
self.link_config = link_config
self.sdr_device = sdr_device
# Build a RF4CE data frame based on the link configuration
self.rf4ce_frame = Rf4ceFrame()
self.rf4ce_frame.source = self.link_config.source
self.rf4ce_frame.destination = self.link_config.destination
self.rf4ce_frame.frame_type = Rf4ceConstants.FRAME_TYPE_DATA
if self.link_config.key:
self.rf4ce_frame.frame_ciphered = True
self.rf4ce_frame.key = binascii.unhexlify(self.link_config.key)
else:
self.rf4ce_frame.frame_ciphered = False
self.rf4ce_frame.frame_counter = self.link_config.frame_counter
self.seqnum = 0
# inter-packet delay
self.packet_delay = 0.1
# Pluto-sdr support full duplex
# ACK can be received
if self.sdr_device == "pluto-sdr":
self.ack_processor = AckProcessor()
else:
self.ack_processor = None
self.tb = TxFlow(channel, self.ack_processor, self.sdr_device)
def run(self):
self.log("SRC:({}) -> DST:({})".format(self.link_config.source,
self.link_config.destination), hue.info)
if not self.link_config.key:
self.log("No secured configuration provided. Will only send plaintext packets.", hue.info)
self.log("Loading last frame counter: {}".format(self.link_config.frame_counter), hue.info)
self.help()
self.tb.start()
if self.ack_processor:
self.ack_processor.start()
# Main loop, iterate through user-supplied commands
for cmd in self.prompt():
if cmd.action == InjectorCmd.PACKET:
self.seqnum = (self.seqnum + 1) % 255
self.rf4ce_frame.frame_counter += 1
self.rf4ce_frame.payload = cmd.arg
data = self.gen_ieee_packet(self.rf4ce_frame.pack())
self.log("Transmitting {}".format(binascii.hexlify(data)), hue.info)
if self.sdr_device == "pluto-sdr":
self.ack_transmit(data)
else:
self.tb.transmit(data)
time.sleep(self.packet_delay)
elif cmd.action == InjectorCmd.PROFILE:
self.log("Set profile to 0x{:02x}".format(cmd.arg), hue.info)
self.rf4ce_frame.profile_indentifier = cmd.arg
elif cmd.action == InjectorCmd.COUNTER:
self.log("Set counter to {}".format(cmd.arg), hue.info)
self.rf4ce_frame.frame_counter = cmd.arg
elif cmd.action == InjectorCmd.DELAY:
self.log("Set delay to {}".format(cmd.arg), hue.info)
self.packet_delay = cmd.arg
elif cmd.action == InjectorCmd.CIPHERED:
self.log("Set ciphered to {}".format(cmd.arg), hue.info)
if cmd.arg:
if not link_config.key:
self.log("No key provided. Cannot send ciphered packets.", hue.bad)
else:
self.rf4ce_frame.frame_ciphered = True
else:
self.rf4ce_frame.frame_ciphered = False
elif cmd.action == InjectorCmd.HELP:
self.help()
self.link_config.frame_counter = self.rf4ce_frame.frame_counter
self.log("Saving last frame counter: {}".format(self.link_config.frame_counter), hue.info)
self.link_config.save()
if self.sdr_device == "pluto-sdr":
self.ack_processor.stop()
self.tb.stop()
self.tb.wait()
def help(self):
help_text = """
Available commands:
counter <value> Set the frame counter value
delay <value> Minimum delay between packet (seconds)
ciphered [0, 1] Send AES ciphered payloads of cleartext payloads.
Only possible if a key has been configured
profile <profile> Select a profile number
exit
Other inputs will be considered as data to be sent.
"""
print(help_text)
def prompt(self):
"""Generates the injector util prompt"""
while True:
try:
if self.rf4ce_frame.frame_ciphered:
ciphered_status = "ciphered"
else:
ciphered_status = "plain"
a = hue.lightblue("{}".format(self.rf4ce_frame.frame_counter))
b = hue.lightblue("0x{:02x}".format(self.rf4ce_frame.profile_indentifier))
c = hue.lightblue(ciphered_status)
cmd = raw_input("({} - {} - {})>>> ".format(a, b, c))
except KeyboardInterrupt:
raise StopIteration
if cmd.startswith("profile"):
try:
yield InjectorCmd(InjectorCmd.PROFILE, cmd.split()[1])
except:
self.log("Malformed command", hue.bad)
continue
elif cmd.startswith("counter"):
try:
yield InjectorCmd(InjectorCmd.COUNTER, cmd.split()[1])
except:
self.log("Malformed command", hue.bad)
continue
elif cmd.startswith("delay"):
try:
yield InjectorCmd(InjectorCmd.DELAY, cmd.split()[1])
except:
self.log("Malformed command", hue.bad)
continue
elif cmd.startswith("ciphered"):
try:
yield InjectorCmd(InjectorCmd.CIPHERED, cmd.split()[1])
except:
self.log("Malformed command", hue.bad)
continue
elif cmd.startswith("help"):
yield InjectorCmd(InjectorCmd.HELP, None)
elif cmd.startswith("exit"):
raise StopIteration
else:
for packet in cmd.split():
try:
data = binascii.unhexlify(packet)
except:
self.log("Malformed command", hue.bad)
continue
yield InjectorCmd(InjectorCmd.PACKET, data)
def gen_ieee_packet(self, data):
"""Encapsulates data into a 802.15.4 packet"""
packet = Dot15d4FCS() / Dot15d4Data() / Raw(load=data)
packet.fcf_srcaddrmode = 2
packet.fcf_destaddrmode = 2
packet.fcf_panidcompress = True
packet.fcf_ackreq = True
packet.seqnum = self.seqnum
packet.dest_panid = self.link_config.dest_panid
packet.dest_addr = self.link_config.destination.get_short_address()
packet.src_addr = self.link_config.source.get_short_address()
return packet.build()
def ack_transmit(self, data, max_freq_retry=5, max_tx_retry=10):
"""Transmit data with ACK check
Tries to transmit a packet until a ACK is received
Full-duplex SDR is needed for this to work
"""
transmit_success = False
for freq_retry in range(max_freq_retry):
for tx_retry in range(max_tx_retry):
self.tb.transmit(data)
time.sleep(0.15)
if self.ack_processor.get_last_ack() != self.seqnum:
self.log("Warning: no ACK received, retrying", hue.bad)
else:
self.log("ACK received", hue.good)
transmit_success = True
return True
self.log("Warning: switching frequency", hue.bad)
self.tb.frequency_switch()
return False
def log(self, data, format=None):
if format:
print(format("[{}] {}".format(datetime.now(), data)))
else:
print("[{}] {}".format(datetime.now(), data))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("config_file", help="JSON file containing link information")
parser.add_argument("-c", "--channel", help="RF4CE channel (default: 15)", type=int,
choices=[15, 20, 25], default=15)
parser.add_argument("-s", "--sdr", help="SDR Device to use (default: pluto-sdr)",
choices=["hackrf", "pluto-sdr"], default="pluto-sdr")
args = parser.parse_args()
try:
link_config = LinkConfig(args.config_file)
except:
print(hue.bad("Cannot load configuration file"))
exit(-1)
print(link_config)
injector = Injector(link_config, args.channel, args.sdr)
injector.run()