forked from 4ZM/mfterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_cmd.c
539 lines (425 loc) · 12.7 KB
/
term_cmd.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
/**
* Copyright (C) 2011 Anders Sundman <[email protected]>
*
* This file is part of mfterm.
*
* mfterm 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.
* mfterm 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 mfterm. If not, see <http://www.gnu.org/licenses/>.
*
* Parts of code used in this file are from the GNU readline library file
* fileman.c (GPLv3). Copyright (C) 1987-2009 Free Software Foundation, Inc
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "mfterm.h"
#include "tag.h"
#include "term_cmd.h"
#include "mifare_ctrl.h"
#include "dictionary.h"
command_t commands[] = {
{ "help", com_help, 0, 0, "Display this text" },
{ "?", com_help, 0, 0, "Synonym for 'help'" },
{ "quit", com_quit, 0, 1, "Exit the program" },
{ "exit", com_quit, 0, 0, "Synonym for 'quit'" },
{ "load", com_load_tag, 1, 1, "Load tag data from a file" },
{ "save", com_save_tag, 1, 1, "Save tag data to a file" },
{ "clear", com_clear_tag, 0, 1, "Clear the current tag data" },
{ "read", com_read_tag, 0, 1, "A|B : Read tag data from a physical tag" },
{ "write", com_write_tag, 0, 1, "A|B : Write tag data to a physical tag" },
{ "print", com_print, 0, 1, "1k|4k : Print tag data" },
{ "print keys", com_print_keys, 0, 1, "1k|4k : Print tag's keys" },
{ "set", com_set, 0, 1, "#block #offset = xx xx xx : Set tag data" },
{ "keys load", com_keys_load, 1, 1, "Load keys from a file" },
{ "keys save", com_keys_save, 1, 1, "Save keys to a file" },
{ "keys clear", com_keys_clear, 0, 1, "Clear the keys" },
{ "keys set", com_keys_set, 0, 1, "A|B #S key : Set a key value" },
{ "keys import", com_keys_import, 0, 1, "Import keys from the current tag" },
{ "keys test", com_keys_test, 0, 1, "Try to authenticate with the keys" },
{ "keys", com_keys_print, 0, 1, "1k|4k : Print the keys" },
{ "dict load", com_dict_load, 1, 1, "Load a dictionary key file" },
{ "dict clear", com_dict_clear, 0, 1, "Clear the key dictionary" },
{ "dict attack", com_dict_attack, 0, 1, "Find keys of a physical tag"},
{ "dict", com_dict_print, 0, 1, "Print the key dictionary" },
{ (char *)NULL, (cmd_func_t)NULL, 0, 0, (char *)NULL }
};
// Parse a Mifare size type argument (1k|4k)
mf_size_t parse_size(const char* str);
// Parse a Mifare size type argument (1k|4k). Return the default
// argument value if the string is NULL.
mf_size_t parse_size_default(const char* str, mf_size_t default_size);
// Parse a Mifare key type argument (A|B)
mf_key_type_t parse_key_type(const char* str);
// Parse a Mifare key type argument (A|B). Return the default
// argument value if the string is NULL.
mf_key_type_t parse_key_type_default(const char* str,
mf_key_type_t default_type);
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
command_t* find_command(const char *name) {
command_t* cmd = NULL;
size_t cmd_len = 0;
for (int i = 0; commands[i].name; i++) {
size_t l = strlen(commands[i].name);
if (l > cmd_len && strncmp(name, commands[i].name, l) == 0) {
cmd = &commands[i];
cmd_len = l;
}
}
return cmd;
}
/**
* Helper function to print the specified command alligned with the longest
* command name.
*/
void print_help_(size_t cmd) {
// Find longest command (and cache the result)
static int cmd_len_max = 0;
if (cmd_len_max == 0) {
for (int i = 0; commands[i].name; i++) {
size_t cmd_len = strlen(commands[i].name);
cmd_len_max = cmd_len > cmd_len_max ? cmd_len : cmd_len_max;
}
}
// Format: 4x' ' | cmd | ' '-pad-to-longest-cmd | 4x' ' | doc
printf (" %s", commands[cmd].name);
for (int j = cmd_len_max - strlen(commands[cmd].name); j >= 0; --j)
printf(" ");
printf (" %s.\n", commands[cmd].doc);
}
int com_help(char* arg) {
// Help request for specific command?
if (arg) {
for (int i = 0; commands[i].name; ++i) {
if (strcmp(arg, commands[i].name) == 0) {
print_help_(i);
return 0;
}
}
printf ("No commands match '%s'\n", arg);
}
// Help for all commands (with doc flag)
for (int i = 0; commands[i].name; i++) {
if (commands[i].document)
print_help_(i);
}
return 0;
}
int com_quit(char *arg) {
stop_input_loop();
return 0;
}
int com_load_tag(char *arg) {
int res = load_tag(arg);
if (res == 0)
printf("Successfully loaded tag from: %s\n", arg);
return 0;
}
int com_save_tag(char* arg) {
int res = save_tag(arg);
if (res == 0)
printf("Successfully wrote tag to: %s\n", arg);
return 0;
}
int com_clear_tag(char* arg) {
clear_tag(¤t_tag);
return 0;
}
int com_read_tag(char* arg) {
// Add option to choose key
char* ab = strtok(arg, " ");
if (ab && strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
if (!ab)
printf("No key argument (A|B) given. Defaulting to A\n");
// Parse key selection
mf_key_type_t key_type = parse_key_type_default(ab, MF_KEY_A);
if (key_type == MF_INVALID_KEY_TYPE) {
printf("Invalid argument (A|B): %s\n", ab);
return -1;
}
// Issue the read request
mf_read_tag(¤t_tag, key_type);
return 0;
}
int com_write_tag(char* arg) {
// Add option to choose key
char* ab = strtok(arg, " ");
if (!ab) {
printf("Too few arguments: (A|B)\n");
return -1;
}
if (strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
// Parse key selection
mf_key_type_t key_type = parse_key_type(ab);
if (key_type == MF_INVALID_KEY_TYPE) {
printf("Invalid argument (A|B): %s\n", ab);
return -1;
}
// Issue the read request
mf_write_tag(¤t_tag, key_type);
return 0;
}
int com_print(char* arg) {
char* a = strtok(arg, " ");
if (a && strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
mf_size_t size = parse_size_default(a, MF_1K);
if (size == MF_INVALID_SIZE) {
printf("Unknown argument: %s\n", a);
return -1;
}
print_tag(size);
return 0;
}
int com_set(char* arg) {
char* block_str = strtok(arg, " ");
char* offset_str = strtok(NULL, " ");
char* byte_str = strtok(NULL, " ");
if (!block_str || !offset_str || !byte_str) {
printf("Too few arguments: #block #offset xx xx xx .. xx\n");
return -1;
}
int block = strtol(block_str, &block_str, 16);
if (*block_str != '\0') {
printf("Invalid block character (non hex): %s\n", block_str);
return -1;
}
if (block < 0 || block > 0xff) {
printf("Invalid block [0,ff]: %x\n", block);
return -1;
}
int offset = strtol(offset_str, &offset_str, 16);
if (*offset_str != '\0') {
printf("Invalid offset character (non hex): %s\n", offset_str);
return -1;
}
if (offset < 0 || offset > 0x0f) {
printf("Invalid offset [0,f]: %x\n", offset);
return -1;
}
// Consume the byte tokens
do {
int byte = strtol(byte_str, &byte_str, 16);
if (*byte_str != '\0') {
printf("Invalid byte character (non hex)q: %s\n", byte_str);
return -1;
}
if (byte < 0 || byte > 0xff) {
printf("Invalid byte value [0,ff]: %x\n", byte);
return -1;
}
if (offset > 0x0f) {
printf("Too many bytes specified.\n");
return -1;
}
// Write the data
current_tag.amb[block].mbd.abtData[offset++] = byte;
} while((byte_str = strtok(NULL, " ")) != (char*)NULL);
return 0;
}
int com_print_keys(char* arg) {
char* a = strtok(arg, " ");
if (a && strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
mf_size_t size = parse_size_default(a, MF_1K);
if (size == MF_INVALID_SIZE) {
printf("Unknown argument: %s\n", a);
return -1;
}
print_keys(¤t_tag, size);
return 0;
}
int com_keys_load(char* arg) {
int res = load_auth(arg);
if (res == 0)
printf("Successfully loaded keys from: %s\n", arg);
return 0;
}
int com_keys_save(char* arg) {
int res = save_auth(arg);
if (res == 0)
printf("Successfully wrote keys to: %s\n", arg);
return 0;
}
int com_keys_clear(char* arg) {
clear_tag(¤t_auth);
return 0;
}
int com_keys_set(char* arg) {
// Arg format: A|B #S key
char* ab = strtok(arg, " ");
char* sector_str = strtok(NULL, " ");
char* key_str = strtok(NULL, " ");
if (strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
if (!ab || !sector_str || !key_str) {
printf("Too few arguments: (A|B) #sector key\n");
return -1;
}
// Read sector
int sector = strtol(sector_str, §or_str, 16);
// Sanity check sector range
if (*sector_str != '\0') {
printf("Invalid sector character (non hex): %s\n", sector_str);
return -1;
}
if (sector < 0 || sector > 0x1b) {
printf("Invalid sector [0,1b]: %x\n", sector);
return -1;
}
// Sanity check key length
if (strncmp(key_str, "0x", 2) == 0)
key_str += 2;
if (strlen(key_str) != 12) {
printf("Invalid key (6 byte hex): %s\n", key_str);
return -1;
}
// Compute the block that houses the key for the desired sector
size_t block = sector_to_trailer(sector);
// Parse key selection and point to appropriate key
byte_t* key;
mf_key_type_t key_type = parse_key_type(ab);
if (key_type == MF_KEY_A)
key = current_auth.amb[block].mbt.abtKeyA;
else if (key_type == MF_KEY_B)
key = current_auth.amb[block].mbt.abtKeyB;
else {
printf("Invalid argument (A|B): %s\n", ab);
return -1;
}
// Parse the key
if (read_key(key, key_str) == NULL) {
printf("Invalid key character (non hex)\n");
return -1;
}
return 0;
}
int com_keys_import(char* arg) {
import_auth();
return 0;
}
int com_keys_test(char* arg) {
// Arg format: 1k|4k A|B
char* s = strtok(arg, " ");
char* ab = strtok(NULL, " ");
if (s && ab && strtok(NULL, " ") != NULL) {
printf("Too many arguments\n");
return -1;
}
if (!s || !ab) {
printf("Too few arguments: (1k|4k) (A|B)\n");
return -1;
}
// Parse arguments
mf_size_t size = parse_size(s);
if (size == MF_INVALID_SIZE) {
printf("Unknown size argument (1k|4k): %s\n", s);
return -1;
}
mf_key_type_t key_type = parse_key_type(ab);
if (key_type == MF_INVALID_KEY_TYPE) {
printf("Unknown key type argument (A|B): %s\n", ab);
return -1;
}
// Run the auth test
mf_test_auth(¤t_auth, size, key_type);
return 0;
}
int com_keys_print(char* arg) {
char* a = strtok(arg, " ");
if (a && strtok(NULL, " ") != (char*)NULL) {
printf("Too many arguments\n");
return -1;
}
mf_size_t size = parse_size_default(a, MF_1K);
if (size == MF_INVALID_SIZE) {
printf("Unknown argument: %s\n", a);
return -1;
}
print_keys(¤t_auth, size);
return 0;
}
int com_dict_load(char* arg) {
FILE* dict_file = fopen(arg, "r");
if (dict_file == NULL) {
printf("Could not open file: %s\n", arg);
return 1;
}
dictionary_import(dict_file);
fclose(dict_file);
return 0;
}
int com_dict_clear(char* arg) {
dictionary_clear();
return 0;
}
int com_dict_attack(char* arg) {
// Not much point if we don't have any keys
if (!dictionary_get()) {
printf("Dictionary is empty!");
return -1;
}
mf_dictionary_attack(¤t_auth);
return 0;
}
int com_dict_print(char* arg) {
key_list_t* kl = dictionary_get();
int count = 0;
while(kl) {
printf("%s\n", sprint_key(kl->key));
kl = kl->next;
++count;
}
printf("Dictionary contains: %d keys\n", count);
return 0;
}
mf_size_t parse_size(const char* str) {
if (str == NULL)
return MF_INVALID_SIZE;
if (strcasecmp(str, "1k") == 0)
return MF_1K;
if (strcasecmp(str, "4k") == 0)
return MF_4K;
return MF_INVALID_SIZE;
}
mf_size_t parse_size_default(const char* str, mf_size_t default_size) {
if (str == NULL)
return default_size;
return parse_size(str);
}
mf_key_type_t parse_key_type(const char* str) {
if (str == NULL)
return MF_INVALID_KEY_TYPE;
if (strcasecmp(str, "a") == 0)
return MF_KEY_A;
if (strcasecmp(str, "b") == 0)
return MF_KEY_B;
return MF_INVALID_KEY_TYPE;
}
mf_key_type_t parse_key_type_default(const char* str,
mf_key_type_t default_type) {
if (str == NULL)
return default_type;
return parse_key_type(str);
}