-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfinalkeybackup.c
588 lines (514 loc) · 13.5 KB
/
finalkeybackup.c
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* License: WTFPL ( http://www.wtfpl.net/ )
* Backup tool for creating and restoring backups.
* The output file is identical to the encrypted eeprom, except for one
* additional byte crc8 checksum every 32 bytes.
* Even though the output file is encrypted, it is strongly adviced to store
* this file in a safe place, such of a dedicated USB stick/memory card or
* burn it to another Final Key before deleting the file.
*
* An example output of such a backup file is available for download at:
* http://cyberstalker.dk/finalkey/hacking/ - At the time of committing this,
* that file is not known to have been broken, so at least some security can be
* assumed.
*
* This utility may result in loss of data in the following ways:
* You override an existing backup file.
* You override a Final Key which contained data that was not backed-up.
* You restore a Final Key with an corrupt/invalid key file.
* You restore a backup with another firmware identifier.
* At the time of writing [FinalKey2] is the correct identifier.
* You're generally careless/not knowing what you're doing.
*
*
* This utility may brick your device in the following ways:
* You restore an image to which you do not know the password.
* The restore-operation is interrupted at an unlucky time.
* (After writing byte 13, before writing byte 124,
* and then only if restoring an image with different password).
*
*
* Creating backups (backing up your Final Key):
* You need to know one password, the one which unlocks the key.
* ./fkbck.bin backup mybackup.bin
* That saves your backup to the file "mybackup.bin" I suggest you
* come up with a more fitting name, maybe including the date.
*
* Restoring backups (writing a backup to a Final Key):
* You need to know two passwords, which may be identical:
* The password which unlocks the key that you wish to overwrite.
* The password which unlocks the image that you wish to restore.
* If you do not know the password which unlocks the key, you can't begin
* the restore-process, and if you do not know the password which unlocks
* the image that you write to the key, you won't be able to unlock it.
* ./fkbck.bin restore mybackup.bin
* This writes mybackup.bin onto the key, all existing data on that key
* will be overwritten. So think about it.
*
* How to build:
* To compile, you need a C compiler, and the standard and posix libs.
*
* gcc --std=c99 -o ./fkbck.bin ./finalkeybackup.c
*
* With heavy heart and much shame, follows a late night writing by
* Jimmy Christensen, this could be done better, faster and smarter and safer,
* but I decided to stop when it worked, sorry about that, if you want to,
* improve it and send me a pull-request on github.
*
*/
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <stdint.h>
#include<sys/stat.h>
struct termios tty;
struct termios tty_old;
uint8_t crc8(const uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
while (len--) {
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix) crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}
void fkLogin( char* psw, int USB, char* buf )
{
int lame=0;
printf("Connecting to Final Key.\n");
while( read(USB, buf,1) )
{
if( buf[0]=='#' )
{
break;
}
}
printf("Done.\n\nPress button to login!\n");
while( read(USB, buf,1) )
{
if( buf[0]=='s' && (lame == 0 || lame==1) )
{
lame++;
} else
if( buf[0]==':' && lame == 2 )
{
break;
} else {
lame=0;
}
}
printf("Signing in.\n");
write( USB, psw, strlen(psw) );
memset(psw,0,sizeof(psw));
printf("Waiting for prompt.\n");
while( read(USB, buf,1) )
{
if( buf[0]=='>' )
{
break;
}
}
printf("Got prompt.\n");
}
int getReady(int USB, char* buf, char* msg)
{
int lame=0;
while( read(USB, buf,1) )
{
if( buf[0] == '[' && lame == 0 )
{
lame++;
} else
if( buf[0] == 'R' && lame == 1 )
{
lame++;
} else
if( buf[0] == 'D' && lame == 2 )
{
lame++;
} else
if( buf[0] == 'Y' && lame == 3 )
{
lame++;
} else
if( buf[0] == ']' && lame == 4 )
{
break;
} else
{
lame=0;
}
}
printf(msg);
lame=0;
while( read(USB, buf,1) )
{
if( buf[0] == '[' && lame == 0 )
{
lame++;
} else
if( buf[0] == 'O' && lame == 1 )
{
lame++;
} else
if( buf[0] == 'N' && lame == 1 ) //Catch the 'N' in NO and abort mission.
{
printf("Timed out!\nUnplug and replug and try again...\n");
return(0);
} else
if( buf[0] == 'K' && lame == 2 )
{
lame++;
} else
if( buf[0] == ']' && lame == 3 )
{
lame++;
} else
if( buf[0] == '\r' && lame == 4 )
{
lame++;
} else
if( buf[0] == '\n' && lame == 5 )
{
break;
} else
{
lame=0;
}
}
return(1);
}
void quit(int USB,FILE* fd, int status )
{
if( USB )
{
close(USB);
}
if( fd )
{
fclose(fd);
}
exit(status);
}
int main(int argc, char** argv)
{
//Don't worry, the following buffer sizes has been chosen completely at random and are not checked for overflows.
char buf[100000];
char devFile[1024];
char psw[128];
if( argc != 4 && argc != 3)
{
printf("Usage: finalkeybackup ACTION FILE [TTYDEVICE]\n"
" ACTION - backup or restore\n"
" FILE - The file to export into or import from.\n"
" TTYDEVICE - Device file, defaults to /dev/FinalKey (Optional)\n"
" (The restore option overwrites all data on your final key!)\n"
" (Without the password for the backed-up data, key = bricked!!)\n\n");
return(1);
}
if( argc == 3 )
{
strcpy( devFile, "/dev/FinalKey" );
} else {
strcpy( devFile, argv[3] );
}
struct stat f_st;
//We want to make sure we start with a known state
//So we force people to disconnect and reconnect it.
if( lstat( devFile, &f_st ) == 0 )
{
//Seems like a good place to check if anyone has opened the device already.
//We don't expect lsof or grep to be there, and if it's not, we still run.
char cmdStr[128];
char realPath[8192]; //Almost chosen randomly, 4096 seems to be an acceptable max path length so this should just fine.
if( realpath( devFile, realPath ) == NULL )
{
printf("Error: Could not resolve '%s'\n", devFile);
return(1);
}
sprintf( cmdStr, "lsof -n | grep %s 2> /dev/null", realPath );
FILE* fpCmd = popen( cmdStr, "r" );
if( fpCmd )
{
if(fgets(buf, 32, fpCmd) != NULL)
{
printf("\nError: The Final Key is in use by another application.\n Please close other applications and try again.\n");
pclose(fpCmd);
return(1);
}
pclose(fpCmd);
} else {
printf("It it advised that you install the 'lsof' package before using this program.\n");
}
printf("\nPlease unplug The Final Key now.\n");
while( stat( devFile, &f_st ) == 0 )
{
sleep(1);
}
printf("Thank you, please connect The Final Key again.\n");
} else {
printf("Please connect The Final Key.\n");
}
//Wait for user to insert the key
int timeOut=30;
while( stat( devFile, &f_st ) != 0 )
{
timeOut--;
if(timeOut < 0)
{
printf("Error: Could not connect to %s\n", devFile);
return(1);
}
sleep(1);
}
memset(psw,0,sizeof(psw));
struct termios term, saveTerm;
/* Retrieve current terminal settings, turn echoing off */
if (tcgetattr(STDIN_FILENO, &term) == -1)
{
printf("Could not get termattrs.\n");
return(1);
}
saveTerm = term; /* So we can restore settings later */
term.c_lflag &= ~ECHO; /* ECHO off, other bits unchanged */
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) == -1)
{
printf("Could not set termattrs.\n");
return(1);
}
fflush(stdout);
printf("[Current] FinalKey password:");
fgets(psw, sizeof(psw), stdin);
printf("\n");
strcpy( buf, psw );
sprintf( psw, "%s\n", buf );
memset(buf,0,sizeof(buf));
if (tcsetattr(STDIN_FILENO, TCSANOW, &saveTerm) == -1)
{
printf("Could not restore termattrs.\n");
return(1);
}
int USB = open( devFile, O_RDWR| O_NOCTTY );
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 )
{
printf("Could not connect to %s (Error %i from tcgetattr).\n", devFile, errno );
quit(USB, NULL, EXIT_FAILURE);
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
// tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 0; //was 1 0 makes sense for reading from dev // read doesn't block
tty.c_cc[VTIME] = 0; // was 5 ^^^ // 0.5 seconds read timeout
//tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );
if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
{
printf("Error %i from tcsetattr.\n", errno );
quit(USB,NULL, EXIT_FAILURE);
}
int i=0;
int lame=0;
///Write
uint8_t crc;
if( strcmp( argv[1], "restore" ) == 0 )
{
FILE* in = fopen( argv[2], "rb" );
if(!in)
{
printf("Could not open file %s for reading, aborting.\n", argv[2]);
quit(USB,NULL, EXIT_FAILURE);
} else {
printf("Opened: Writing to device..\n");
}
fkLogin(psw, USB, buf);
printf("Requesting restore.\n");
write( USB, "X",1 );
write( USB, "i", 1 );
printf("Waiting for RDY.\n\nWarning: Point of no return.\n All data on the key will be overwritten!\n\n");
if( !getReady(USB, buf, "Press button now to allow writing data to device.\n") )
{
quit(USB,NULL, EXIT_FAILURE);
}
printf("Writing data.\n");
int run=1;
lame=0;
while( run )
{
while( read(USB, &buf,1) && run )
{
if( buf[0]=='W' || buf[0] == 'O') // 'W'ait or byte-received 'O'kay
{
// nop, just wait for device...
} else if( buf[0]=='F' )
{
printf("\nFailure.\n");
} else if( buf[0]=='R' )
{
lame+=32;
printf("\rWrite: %i/64000 ",lame);
fflush(stdout);
fread( &buf, 1,32, in );
write( USB, &buf, 32 );
crc = fread( &buf, 1,1, in );
write( USB, &buf, 1 ); //Since the CRC was already verified when we recorded it, we simply use the one from the file instead of calculating it again.
} else if( buf[0]=='E' )
{
printf("\nDevice reports done.\n");
run=0;
} else if( buf[0] == 'C' )
{
printf("\nDevice reports CRC error.\nFormat device and try again.\n");
run=0;
} else {
printf("\nError:Got unrecognized byte value: 0x%X\n", buf[0]);
}
}
printf("\nFinished, exiting.\nUnplug and replug before using.\n");
run=0;
}
quit(USB,in, EXIT_SUCCESS);
}
if( strcmp( argv[1], "backup" )==0 )
{
FILE* out = fopen(argv[2], "wb" );
if( !out )
{
printf("Could not open file %s for writing, aborting.\n", argv[2]);
quit(USB,NULL,EXIT_FAILURE);
}
int n = 0;
fkLogin( psw, USB, buf );
write( USB, "X",1 );
write( USB, "e", 1 );
if(!getReady(USB, buf, "\nPress button to allow reading data from device.\n"))
{
close(USB);
return(1);
}
printf("Reading from device.\n");
int b=0;
uint8_t packet[32];
lame=0;
int state=0;
while(read( USB, &buf, 1 ) > 0)
{
if( state == 0 )
{
if( lame== 0 && buf[0] == '[' )
{
lame++;
} else
if( lame== 1 && buf[0] == 'B' )
{
lame++;
} else
if( lame== 2 && buf[0] == 'E' )
{
lame++;
} else
if( lame== 3 && buf[0] == 'G' )
{
lame++;
} else
if( lame== 4 && buf[0] == 'I' )
{
lame++;
} else
if( lame== 5 && buf[0] == 'N' )
{
lame++;
} else
if( lame== 6 && buf[0] == ']' )
{
state++;
lame=0;
i=0;
printf("Got begin sequence.\n");
} else {
printf("\nError:Got unrecognized byte value: 0x%X\n", buf[0]);
quit(USB,out,EXIT_FAILURE);
}
} else
if( state == 1 )
{
i++;
printf("\rReading byte %i/66000 ", i);
fwrite( buf, 1,1, out );
fflush( out );
if(b<32)
{
packet[b] = buf[0];
}
b++;
if(b==33)
{
if( (uint8_t)buf[0] != crc8(packet, 32) )
{
printf("\nError: CRC check failed at byte %i.\nUnplug and replug and try again.\n");
quit(USB,out,EXIT_FAILURE);
}
b=0;
}
if( i == 66000 )
{
state++;
lame=0;
printf("\nFinished, waiting for end sequence.\n");
}
} else
if( state == 2 )
{
if( lame == 0 && buf[0] == '[' )
{
lame++;
} else
if( lame == 1 && buf[0] == 'E' )
{
lame++;
} else
if( lame== 2 && buf[0] == 'N' )
{
lame++;
} else
if( lame== 3 && buf[0] == 'D' )
{
lame++;
} else
if( lame== 4 && buf[0] == ']' )
{
printf("Got end sequence.\n");
break;
} else {
printf("\nError:Got unrecognized byte value: 0x%X\n", buf[0]);
quit(USB,out,EXIT_FAILURE);
}
}
}
printf("\nFile saved.\n");
quit(USB,out,EXIT_SUCCESS);
}
quit(USB,NULL,EXIT_FAILURE);
}