hf iclass managekeys - now use cliparser

This commit is contained in:
tcprst 2020-11-27 18:27:02 -05:00
commit 4e1c1f96f8
No known key found for this signature in database
GPG key ID: 9145EAF5121AED25
2 changed files with 77 additions and 94 deletions

View file

@ -92,29 +92,6 @@ static int usage_hf_iclass_calc_newkey(void) {
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
return PM3_SUCCESS; return PM3_SUCCESS;
} }
static int usage_hf_iclass_managekeys(void) {
PrintAndLogEx(NORMAL, "Manage iCLASS Keys in client memory:\n");
PrintAndLogEx(NORMAL, "Usage: hf iclass managekeys n [keynbr] k [key] f [filename] s l p\n");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h : Show this help");
PrintAndLogEx(NORMAL, " n <keynbr> : specify the keyNbr to set in memory");
PrintAndLogEx(NORMAL, " k <key> : set a key in memory");
PrintAndLogEx(NORMAL, " f <filename> : specify a filename to use with load or save operations");
PrintAndLogEx(NORMAL, " s : save keys in memory to file specified by filename");
PrintAndLogEx(NORMAL, " l : load keys to memory from file specified by filename");
PrintAndLogEx(NORMAL, " p : print keys loaded into memory\n");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " -- set key");
PrintAndLogEx(NORMAL, _YELLOW_("\thf iclass managekeys n 0 k 1122334455667788"));
PrintAndLogEx(NORMAL, " -- save key file");
PrintAndLogEx(NORMAL, _YELLOW_("\thf iclass managekeys f mykeys.bin s"));
PrintAndLogEx(NORMAL, " -- load key file");
PrintAndLogEx(NORMAL, _YELLOW_("\thf iclass managekeys f mykeys.bin l"));
PrintAndLogEx(NORMAL, " -- print keys");
PrintAndLogEx(NORMAL, _YELLOW_("\thf iclass managekeys p"));
PrintAndLogEx(NORMAL, "");
return PM3_SUCCESS;
}
static int usage_hf_iclass_lookup(void) { static int usage_hf_iclass_lookup(void) {
PrintAndLogEx(NORMAL, "Lookup keys takes some sniffed trace data and tries to verify what key was used against a dictionary file\n"); PrintAndLogEx(NORMAL, "Lookup keys takes some sniffed trace data and tries to verify what key was used against a dictionary file\n");
PrintAndLogEx(NORMAL, "Usage: hf iclass lookup [h|e|r] [f (*.dic)] [u <csn>] [p <epurse>] [m <macs>]\n"); PrintAndLogEx(NORMAL, "Usage: hf iclass lookup [h|e|r] [f (*.dic)] [u <csn>] [p <epurse>] [m <macs>]\n");
@ -2505,83 +2482,88 @@ static int printKeys(void) {
} }
static int CmdHFiClassManageKeys(const char *Cmd) { static int CmdHFiClassManageKeys(const char *Cmd) {
uint8_t keyNbr = 0; CLIParserContext *ctx;
uint8_t dataLen = 0; CLIParserInit(&ctx, "hf iclass managekeys",
uint8_t KEY[8] = {0}; "Manage iCLASS Keys in client memory",
char filename[FILE_PATH_SIZE]; "hf iclass managekeys --ki 0 -k 1122334455667788 -> set key\n"
uint8_t fileNameLen = 0; "hf iclass managekeys -f mykeys.bin --save -> save key file\n"
bool errors = false; "hf iclass managekeys -f mykeys.bin --load -> load key file\n"
uint8_t operation = 0; "hf iclass managekeys -p -> print keys");
char tempStr[20];
uint8_t cmdp = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { void *argtable[] = {
switch (tolower(param_getchar(Cmd, cmdp))) { arg_param_begin,
case 'h': arg_str0("f", "file", "<filename>", "Specify a filename to use with load or save operations"),
return usage_hf_iclass_managekeys(); arg_str0("k", "key", "<hex>", "Access key as 16 hex symbols"),
case 'f': arg_int0(NULL, "ki", "<dec>", "Specify key index to set key in memory"),
fileNameLen = param_getstr(Cmd, cmdp + 1, filename, sizeof(filename)); arg_lit0(NULL, "save", "Save keys in memory to file specified by filename"),
if (fileNameLen < 1) { arg_lit0(NULL, "load", "Load keys to memory from file specified by filename"),
PrintAndLogEx(ERR, "No filename found"); arg_lit0("p", "print", "Print keys loaded into memory"),
errors = true; arg_param_end
} };
cmdp += 2; CLIExecWithReturn(ctx, Cmd, argtable, false);
break;
case 'n': int fnlen = 0;
keyNbr = param_get8(Cmd, cmdp + 1); char filename[FILE_PATH_SIZE] = {0};
if (keyNbr >= ICLASS_KEYS_MAX) { CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
PrintAndLogEx(ERR, "Invalid block number, MAX is " _YELLOW_("%d"), ICLASS_KEYS_MAX);
errors = true; int key_len = 0;
} uint8_t key[8] = {0};
cmdp += 2; CLIGetHexWithReturn(ctx, 2, key, &key_len);
break; uint8_t operation = 0;
case 'k':
operation += 3; //set key if (key_len > 0) {
dataLen = param_getstr(Cmd, cmdp + 1, tempStr, sizeof(tempStr)); operation += 3;
if (dataLen == 16) { //ul-c or ev1/ntag key length if (key_len != 8) {
errors = param_gethex(tempStr, 0, KEY, dataLen); PrintAndLogEx(ERR, "Key is incorrect length");
} else { CLIParserFree(ctx);
PrintAndLogEx(WARNING, "\nERROR: Key is incorrect length\n"); return PM3_EINVARG;
errors = true;
}
cmdp += 2;
break;
case 'p':
operation += 4; //print keys in memory
cmdp++;
break;
case 'l':
operation += 5; //load keys from file
cmdp++;
break;
case 's':
operation += 6; //save keys to file
cmdp++;
break;
default:
PrintAndLogEx(WARNING, "unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
errors = true;
break;
} }
} }
if (errors) return usage_hf_iclass_managekeys();
int key_nr = arg_get_int_def(ctx, 3, -1);
if (key_nr >= 0) {
if (key_nr < ICLASS_KEYS_MAX) {
PrintAndLogEx(SUCCESS, "Setting key[%d] " _GREEN_("%s"), key_nr);
} else {
PrintAndLogEx(ERR, "Key number is invalid");
CLIParserFree(ctx);
return PM3_EINVARG;
}
}
if (arg_get_lit(ctx, 4)) { //save
operation += 6;
}
if (arg_get_lit(ctx, 5)) { //load
operation += 5;
}
if (arg_get_lit(ctx, 6)) { //print
operation += 4;
}
CLIParserFree(ctx);
if (operation == 0) { if (operation == 0) {
PrintAndLogEx(WARNING, "no operation specified (load, save, or print)\n"); PrintAndLogEx(ERR, "No operation specified (load, save, or print)\n");
return usage_hf_iclass_managekeys(); return PM3_EINVARG;
} }
if (operation > 6) { if (operation > 6) {
PrintAndLogEx(WARNING, "Too many operations specified\n"); PrintAndLogEx(ERR, "Too many operations specified\n");
return usage_hf_iclass_managekeys(); return PM3_EINVARG;
} }
if (operation > 4 && fileNameLen == 0) { if (operation > 4 && fnlen == 0) {
PrintAndLogEx(WARNING, "You must enter a filename when loading or saving\n"); PrintAndLogEx(ERR, "You must enter a filename when loading or saving\n");
return usage_hf_iclass_managekeys(); return PM3_EINVARG;
}
if (key_len > 0 && key_nr == -1) {
PrintAndLogEx(ERR, "Please specify key index when specifying key");
return PM3_EINVARG;
} }
switch (operation) { switch (operation) {
case 3: case 3:
memcpy(iClass_Key_Table[keyNbr], KEY, 8); memcpy(iClass_Key_Table[key_nr], key, 8);
return PM3_SUCCESS; return PM3_SUCCESS;
case 4: case 4:
return printKeys(); return printKeys();
@ -2613,7 +2595,7 @@ static void add_key(uint8_t *key) {
if (i == ICLASS_KEYS_MAX) { if (i == ICLASS_KEYS_MAX) {
PrintAndLogEx(INFO, "Couldn't find an empty keyslot"); PrintAndLogEx(INFO, "Couldn't find an empty keyslot");
} else { } else {
PrintAndLogEx(HINT, "Try " _YELLOW_("`hf iclass managekeys p`") " to view keys"); PrintAndLogEx(HINT, "Try " _YELLOW_("`hf iclass managekeys -p`") " to view keys");
} }
} }

View file

@ -109,19 +109,20 @@ Print keystore
``` ```
Options Options
--- ---
p : print keys loaded into memory -p, --print Print keys loaded into memory
pm3 --> hf iclass managekeys p
pm3 --> hf iclass managekeys -p
``` ```
Add key to keystore [0-7] Add key to keystore [0-7]
``` ```
Options Options
--- ---
n <keynbr> : specify the keyNbr to set in memory -f, --file <filename> Specify a filename to use with load or save operations
k <key> : set a key in memory --ki <dec> Specify key index to set key in memory
pm3 --> hf iclass managekeys n 3 k AFA785A7DAB33378 pm3 --> hf iclass managekeys --ki 3 -k AFA785A7DAB33378
``` ```
Encrypt iCLASS Block Encrypt iCLASS Block