From 159a12e3577deb14e36934edd0a639cade69d109 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 27 Feb 2021 18:50:12 +0100 Subject: [PATCH] hf 14b sriwrite - now uses cliparser --- client/src/cmdhf14b.c | 103 ++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/client/src/cmdhf14b.c b/client/src/cmdhf14b.c index ac3b1e982..a9524abb4 100644 --- a/client/src/cmdhf14b.c +++ b/client/src/cmdhf14b.c @@ -37,21 +37,6 @@ bool apdu_in_framing_enable = true; static int CmdHelp(const char *Cmd); -static int usage_hf_14b_write_srx(void) { - PrintAndLogEx(NORMAL, "Usage: hf 14b [h] sriwrite <1|2> "); - PrintAndLogEx(NORMAL, "Options:"); - PrintAndLogEx(NORMAL, " h this help"); - PrintAndLogEx(NORMAL, " <1|2> 1 = SRIX4K , 2 = SRI512"); - PrintAndLogEx(NORMAL, " (hex) block number depends on tag, special block == FF"); - PrintAndLogEx(NORMAL, " hex bytes of data to be written"); - PrintAndLogEx(NORMAL, "Example:"); - PrintAndLogEx(NORMAL, _YELLOW_(" hf 14b sriwrite 1 7F 11223344")); - PrintAndLogEx(NORMAL, _YELLOW_(" hf 14b sriwrite 1 FF 11223344")); - PrintAndLogEx(NORMAL, _YELLOW_(" hf 14b sriwrite 2 15 11223344")); - PrintAndLogEx(NORMAL, _YELLOW_(" hf 14b sriwrite 2 FF 11223344")); - return PM3_SUCCESS; -} - static int switch_off_field_14b(void) { clearCommandBuffer(); SendCommandMIX(CMD_HF_ISO14443B_COMMAND, ISO14B_DISCONNECT, 0, 0, NULL, 0); @@ -1138,56 +1123,78 @@ static int CmdHF14BWriteSri(const char *Cmd) { * Special block FF = otp_lock_reg block. * Data len 4 bytes- */ - char cmdp = tolower(param_getchar(Cmd, 0)); - uint8_t blockno = -1; - uint8_t data[4] = {0x00}; - bool isSrix4k = true; - char str[30]; - memset(str, 0x00, sizeof(str)); - if (strlen(Cmd) < 1 || cmdp == 'h') return usage_hf_14b_write_srx(); + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf 14b sriwrite", + "Write data to a SRI512 | SRIX4K block", + "hf 14b sriwrite --4k -b 100 -d 11223344\n" + "hf 14b sriwrite --4k --sb -d 11223344 --> special block write\n" + "hf 14b sriwrite --512 -b 15 -d 11223344\n" + "hf 14b sriwrite --512 --sb -d 11223344 --> special block write\n" + ); - if (cmdp == '2') - isSrix4k = false; - - //blockno = param_get8(Cmd, 1); - - if (param_gethex(Cmd, 1, &blockno, 2)) { - PrintAndLogEx(WARNING, "block number must include 2 HEX symbols"); - return 0; + void *argtable[] = { + arg_param_begin, + arg_int0("b", "block", "", "block number"), + arg_str1("d", "data", "", "4 hex bytes"), + arg_lit0(NULL, "512", "target SRI 512 tag"), + arg_lit0(NULL, "4k", "target SRIX 4k tag"), + arg_lit0(NULL, "sb", "special block write at end of memory (0xFF)"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + int blockno = arg_get_int_def(ctx, 1, -1); + int dlen = 0; + uint8_t data[4] = {0,0,0,0}; + int res = CLIParamHexToBuf(arg_get_str(ctx, 2), data, sizeof(data), &dlen); + if (res) { + CLIParserFree(ctx); + return PM3_EINVARG; } - if (isSrix4k) { - if (blockno > 0x7f && blockno != 0xff) { - PrintAndLogEx(FAILED, "block number out of range"); - return PM3_ESOFT; - } - } else { - if (blockno > 0x0f && blockno != 0xff) { - PrintAndLogEx(FAILED, "block number out of range"); - return PM3_ESOFT; - } + bool use_sri512 = arg_get_lit(ctx, 3); + bool use_srix4k = arg_get_lit(ctx, 4); + bool special = arg_get_lit(ctx, 5); + CLIParserFree(ctx); + + if (dlen != sizeof(data)) { + PrintAndLogEx(FAILED, "data must be 4 hex bytes, got %d", dlen); + return PM3_EINVARG; } - if (param_gethex(Cmd, 2, data, 8)) { - PrintAndLogEx(WARNING, "data must include 8 HEX symbols"); - return PM3_ESOFT; + if (use_sri512 + use_srix4k > 1) { + PrintAndLogEx(FAILED, "Select only one card type"); + return PM3_EINVARG; } - if (blockno == 0xff) { + if (use_srix4k && blockno > 0x7F) { + PrintAndLogEx(FAILED, "block number out of range, max 127 (0x7F)"); + return PM3_EINVARG; + } + + if (use_sri512 && blockno > 0x0F) { + PrintAndLogEx(FAILED, "block number out of range, max 15 (0x0F)"); + return PM3_EINVARG; + } + + // special block at end of memory + if (special) { + blockno = 0xFF; PrintAndLogEx(SUCCESS, "[%s] Write special block %02X [ " _YELLOW_("%s")" ]", - (isSrix4k) ? "SRIX4K" : "SRI512", + (use_srix4k) ? "SRIX4K" : "SRI512", blockno, - sprint_hex(data, 4) + sprint_hex(data, sizeof(data)) ); } else { PrintAndLogEx(SUCCESS, "[%s] Write block %02X [ " _YELLOW_("%s")" ]", - (isSrix4k) ? "SRIX4K" : "SRI512", + (use_srix4k) ? "SRIX4K" : "SRI512", blockno, - sprint_hex(data, 4) + sprint_hex(data, sizeof(data)) ); } + char str[36]; + memset(str, 0x00, sizeof(str)); sprintf(str, "--sr -c --data %02x%02x%02x%02x%02x%02x", ISO14443B_WRITE_BLK, blockno, data[0], data[1], data[2], data[3]); return CmdHF14BCmdRaw(str); }