diff --git a/tools/simmodule/sim011.bin b/client/resources/sim011.bin similarity index 100% rename from tools/simmodule/sim011.bin rename to client/resources/sim011.bin diff --git a/tools/simmodule/sim011.sha512.txt b/client/resources/sim011.sha512.txt similarity index 100% rename from tools/simmodule/sim011.sha512.txt rename to client/resources/sim011.sha512.txt diff --git a/client/src/cmdsmartcard.c b/client/src/cmdsmartcard.c index ddeece950..02d34c47c 100644 --- a/client/src/cmdsmartcard.c +++ b/client/src/cmdsmartcard.c @@ -22,6 +22,7 @@ #include "util.h" #include "fileutils.h" #include "crc16.h" // crc +#include "cliparser.h" // cliparsing static int CmdHelp(const char *Cmd); @@ -42,43 +43,7 @@ static int usage_sm_raw(void) { PrintAndLogEx(NORMAL, " smart raw 0 t d 00a4040007a0000000031010 - Visa"); return PM3_SUCCESS; } -static int usage_sm_reader(void) { - PrintAndLogEx(NORMAL, "Usage: smart reader [h|s]"); - PrintAndLogEx(NORMAL, " h : this help"); - PrintAndLogEx(NORMAL, " s : silent (no messages)"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " smart reader"); - return PM3_SUCCESS; -} -static int usage_sm_info(void) { - PrintAndLogEx(NORMAL, "Usage: smart info [h|s]"); - PrintAndLogEx(NORMAL, " h : this help"); - PrintAndLogEx(NORMAL, " s : silent (no messages)"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " smart info"); - return PM3_SUCCESS; -} -static int usage_sm_upgrade(void) { - PrintAndLogEx(NORMAL, "Upgrade RDV4.0 Sim module firmware"); - PrintAndLogEx(NORMAL, "Usage: smart upgrade f "); - PrintAndLogEx(NORMAL, " h : this help"); - PrintAndLogEx(NORMAL, " f : firmware file name"); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " smart upgrade f ../tools/simmodule/sim011.bin"); - return PM3_SUCCESS; -} -static int usage_sm_setclock(void) { - PrintAndLogEx(NORMAL, "Usage: smart setclock [h] c "); - PrintAndLogEx(NORMAL, " h : this help"); - PrintAndLogEx(NORMAL, " c <> : clockspeed (0 = 16MHz, 1=8MHz, 2=4MHz) "); - PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " smart setclock c 2"); - return PM3_SUCCESS; -} + static int usage_sm_brute(void) { PrintAndLogEx(NORMAL, "Tries to bruteforce SFI, using a known list of AID's "); PrintAndLogEx(NORMAL, "Usage: smart brute [h]"); @@ -524,36 +489,29 @@ static int CmdSmartRaw(const char *Cmd) { static int CmdSmartUpgrade(const char *Cmd) { - PrintAndLogEx(WARNING, "WARNING - Sim module firmware upgrade."); - PrintAndLogEx(WARNING, "A dangerous command, do wrong and you could brick the sim module"); + + PrintAndLogEx(INFO, "-------------------------------------------------------------------"); + PrintAndLogEx(WARNING, _RED_("WARNING") " - sim module firmware upgrade"); + PrintAndLogEx(WARNING, _RED_("A dangerous command, do wrong and you could brick the sim module")); + PrintAndLogEx(INFO, "-------------------------------------------------------------------"); PrintAndLogEx(NORMAL, ""); + CLIParserContext *ctx; + CLIParserInit(&ctx, "smart upgrade", + "Upgrade RDV4.0 sim module firmware", + "smart upgrade -f sim011.bin" + ); + + void *argtable[] = { + arg_param_begin, + arg_str1("f", "file", "", "firmware file name"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; - uint8_t cmdp = 0; - bool errors = false; - - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'f': - if (param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE) { - PrintAndLogEx(FAILED, "Filename too long"); - errors = true; - break; - } - cmdp += 2; - break; - case 'h': - return usage_sm_upgrade(); - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - } - - //Validations - if (errors || cmdp == 0) return usage_sm_upgrade(); - + CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, sizeof(filename), &fnlen); + CLIParserFree(ctx); char *bin_extension = filename; char *dot_position = NULL; @@ -623,7 +581,8 @@ static int CmdSmartUpgrade(const char *Cmd) { } free(hashstring); - PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3"); + PrintAndLogEx(INFO, _GREEN_("Don\'t turn off your PM3!")); + PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3..."); PacketResponseNG resp; @@ -669,7 +628,7 @@ static int CmdSmartUpgrade(const char *Cmd) { PrintAndLogEx(INPLACE, "%d bytes sent", bytes_sent); } PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(SUCCESS, "Sim module firmware updating, don\'t turn off your PM3!"); + PrintAndLogEx(SUCCESS, "Sim module firmware updating..."); // trigger the firmware upgrade clearCommandBuffer(); @@ -700,37 +659,35 @@ static int CmdSmartUpgrade(const char *Cmd) { } static int CmdSmartInfo(const char *Cmd) { - uint8_t cmdp = 0; - bool errors = false, silent = false; + CLIParserContext *ctx; + CLIParserInit(&ctx, "smart info", + "Extract more detailed information from smart card.", + "smart info -v" + ); - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_sm_info(); - case 's': - silent = true; - break; - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - cmdp++; - } - - //Validations - if (errors) return usage_sm_info(); + void *argtable[] = { + arg_param_begin, + arg_lit0("v", "verbose", "verbose"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + bool verbose = arg_get_lit(ctx, 1); + CLIParserFree(ctx); clearCommandBuffer(); SendCommandNG(CMD_SMART_ATR, NULL, 0); PacketResponseNG resp; if (!WaitForResponseTimeout(CMD_SMART_ATR, &resp, 2500)) { - if (!silent) PrintAndLogEx(WARNING, "smart card select failed"); + if (verbose) { + PrintAndLogEx(WARNING, "smart card select failed"); + } return PM3_ETIMEOUT; } if (resp.status != PM3_SUCCESS) { - if (!silent) PrintAndLogEx(WARNING, "smart card select failed"); + if (verbose) { + PrintAndLogEx(WARNING, "smart card select failed"); + } return PM3_ESOFT; } @@ -772,37 +729,36 @@ static int CmdSmartInfo(const char *Cmd) { } static int CmdSmartReader(const char *Cmd) { - uint8_t cmdp = 0; - bool errors = false, silent = false; - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_sm_reader(); - case 's': - silent = true; - break; - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - cmdp++; - } + CLIParserContext *ctx; + CLIParserInit(&ctx, "smart reader", + "Act as a smart card reader.", + "smart reader" + ); - //Validations - if (errors) return usage_sm_reader(); + void *argtable[] = { + arg_param_begin, + arg_lit0("v", "verbose", "verbose"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + bool verbose = arg_get_lit(ctx, 1); + CLIParserFree(ctx); clearCommandBuffer(); SendCommandNG(CMD_SMART_ATR, NULL, 0); PacketResponseNG resp; if (!WaitForResponseTimeout(CMD_SMART_ATR, &resp, 2500)) { - if (!silent) PrintAndLogEx(WARNING, "smart card select failed"); + if (verbose) { + PrintAndLogEx(WARNING, "smart card select failed"); + } return PM3_ETIMEOUT; } if (resp.status != PM3_SUCCESS) { - if (!silent) PrintAndLogEx(WARNING, "smart card select failed"); + if (verbose) { + PrintAndLogEx(WARNING, "smart card select failed"); + } return PM3_ESOFT; } smart_card_atr_t card; @@ -813,34 +769,42 @@ static int CmdSmartReader(const char *Cmd) { } static int CmdSmartSetClock(const char *Cmd) { - uint8_t cmdp = 0; - bool errors = false; - uint8_t new_clk = 0; - while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { - switch (tolower(param_getchar(Cmd, cmdp))) { - case 'h': - return usage_sm_setclock(); - case 'c': - new_clk = param_get8ex(Cmd, cmdp + 1, 2, 10); - if (new_clk > 2) - errors = true; - cmdp += 2; - break; - default: - PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); - errors = true; - break; - } + CLIParserContext *ctx; + CLIParserInit(&ctx, "smart setclock", + "Set clock speed for smart card interface.", + "smart setclock --4mhz\n" + "smart setclock --16mhz" + ); + + void *argtable[] = { + arg_param_begin, + arg_lit0(NULL, "16mhz", "16 MHz clock speed"), + arg_lit0(NULL, "8mhz", "8 MHz clock speed"), + arg_lit0(NULL, "4mhz", "4 MHz clock speed"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + bool c16 = arg_get_lit(ctx, 1); + bool c8 = arg_get_lit(ctx, 2); + bool c4 = arg_get_lit(ctx, 3); + CLIParserFree(ctx); + + if ((c16 + c8 + c4) > 1 ) { + PrintAndLogEx(WARNING, "Only one clock speed can be used at a time"); + return PM3_EINVARG; } - //Validations - if (errors || cmdp == 0) return usage_sm_setclock(); - struct { uint32_t new_clk; } PACKED payload; - payload.new_clk = new_clk; + + if (c16) + payload.new_clk = 0; + else if (c8) + payload.new_clk = 1; + else if (c4) + payload.new_clk = 2; clearCommandBuffer(); SendCommandNG(CMD_SMART_SETCLOCK, (uint8_t *)&payload, sizeof(payload)); @@ -855,15 +819,15 @@ static int CmdSmartSetClock(const char *Cmd) { return PM3_ESOFT; } - switch (new_clk) { + switch (payload.new_clk) { case 0: - PrintAndLogEx(SUCCESS, "Clock changed to 16MHz giving 10800 baudrate"); + PrintAndLogEx(SUCCESS, "Clock changed to " _GREEN_("16") " MHz giving " _GREEN_("10800") " baudrate"); break; case 1: - PrintAndLogEx(SUCCESS, "Clock changed to 8MHz giving 21600 baudrate"); + PrintAndLogEx(SUCCESS, "Clock changed to " _GREEN_("8") " MHz giving " _GREEN_("21600") " baudrate"); break; case 2: - PrintAndLogEx(SUCCESS, "Clock changed to 4MHz giving 86400 baudrate"); + PrintAndLogEx(SUCCESS, "Clock changed to " _GREEN_("4") " MHz giving " _GREEN_("86400") " baudrate"); break; default: break;