diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index e74a7ae8e..8d0af47ee 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1830,67 +1830,6 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type, return res; } -static int handler_desfire_clear_record_file(uint8_t file_no) { - if (file_no > 0x1F) - return PM3_EINVARG; - - sAPDU apdu = {0x90, MFDES_CLEAR_RECORD_FILE, 0x00, 0x00, 1, &file_no}; // 0xEB - uint16_t sw = 0; - uint32_t recvlen = 0; - int res = send_desfire_cmd(&apdu, false, NULL, &recvlen, &sw, 0, true); - if (res != PM3_SUCCESS) { - PrintAndLogEx(WARNING, _RED_(" Can't clear record file -> %s"), DesfireGetErrorString(res, &sw)); - DropFieldDesfire(); - return res; - } else { - res = handler_desfire_commit_transaction(); - if (res != PM3_SUCCESS) { - PrintAndLogEx(WARNING, _RED_(" Can't commit transaction -> %s"), DesfireGetErrorString(res, &sw)); - DropFieldDesfire(); - return res; - } - } - return res; -} - -static int handler_desfire_create_linearrecordfile(mfdes_linear_t *file) { - if (file->fileno > 0x1F) - return PM3_EINVARG; - - if (memcmp(file->recordsize, "\x00\x00\x00", 3) == 0) return PM3_EINVARG; - sAPDU apdu = {0x90, MFDES_CREATE_LINEAR_RECORD_FILE, 0x00, 0x00, sizeof(mfdes_linear_t), (uint8_t *)file}; // 0xC1 - - uint16_t sw = 0; - uint32_t recvlen = 0; - int res = send_desfire_cmd(&apdu, false, NULL, &recvlen, &sw, 0, true); - if (res != PM3_SUCCESS) { - PrintAndLogEx(WARNING, _RED_(" Can't create linear record file -> %s"), DesfireGetErrorString(res, &sw)); - DropFieldDesfire(); - return res; - } - return res; -} - -static int handler_desfire_create_cyclicrecordfile(mfdes_linear_t *file) { - if (memcmp(file->recordsize, "\x00\x00\x00", 3) == 0) - return PM3_EINVARG; - - if (file->fileno > 0x1F) - return PM3_EINVARG; - - sAPDU apdu = {0x90, MFDES_CREATE_CYCLIC_RECORD_FILE, 0x00, 0x00, sizeof(mfdes_linear_t), (uint8_t *)file}; // 0xC0 - - uint16_t sw = 0; - uint32_t recvlen = 0; - int res = send_desfire_cmd(&apdu, false, NULL, &recvlen, &sw, 0, true); - if (res != PM3_SUCCESS) { - PrintAndLogEx(WARNING, _RED_(" Can't create cyclic record file -> %s"), DesfireGetErrorString(res, &sw)); - DropFieldDesfire(); - return res; - } - return res; -} - static int getKeySettings(uint8_t *aid) { if (aid == NULL) return PM3_EINVARG; @@ -1962,13 +1901,6 @@ static void swap24(uint8_t *data) { data[2] = tmp; }; -static void swap16(uint8_t *data) { - if (data == NULL) return; - uint8_t tmp = data[0]; - data[0] = data[1]; - data[1] = tmp; -}; - static int desfire_authenticate(int cmdAuthMode, int cmdAuthAlgo, uint8_t *aid, uint8_t *key, int cmdKeyNo, uint8_t cmdKdfAlgo, uint8_t kdfInputLen, uint8_t *kdfInput, mfdes_auth_res_t *rpayload) { switch (cmdAuthMode) { case MFDES_AUTH_DES: @@ -2141,58 +2073,6 @@ static int selectfile(uint8_t *aid, uint8_t fileno, uint8_t *cs) { return res; } -static int CmdHF14ADesClearRecordFile(const char *Cmd) { - CLIParserContext *ctx; - CLIParserInit(&ctx, "hf mfdes clearfile", - "Clear record file\nMake sure to select aid or authenticate aid before running this command.", - "hf mfdes clearfile -n 01" - ); - - void *argtable[] = { - arg_param_begin, - arg_int0("n", "fileno", "", "File Number (0 - 31)"), - arg_strx0("a", "aid", "", "App ID to select as hex bytes (3 bytes, big endian)"), - arg_param_end - }; - CLIExecWithReturn(ctx, Cmd, argtable, false); - int fno = arg_get_int_def(ctx, 1, 0); - int aidlength = 3; - uint8_t aid[3] = {0}; - CLIGetHexWithReturn(ctx, 2, aid, &aidlength); - swap24(aid); - CLIParserFree(ctx); - - if (fno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (exp 0 - 31), got %d", fno); - return PM3_EINVARG; - } - - if (aidlength != 3 && aidlength != 0) { - PrintAndLogEx(ERR, _RED_(" The given aid must have 3 bytes (big endian).")); - return PM3_ESOFT; - } else if (aidlength == 0) { - if (memcmp(&tag->selected_application, aid, 3) == 0) { - PrintAndLogEx(ERR, _RED_(" You need to select an aid first.")); - return PM3_ESOFT; - } - memcpy(aid, (uint8_t *)&tag->selected_application, 3); - } - uint8_t cs = 0; - if (selectfile(aid, fno, &cs) != PM3_SUCCESS) { - PrintAndLogEx(ERR, _RED_(" Error on selecting file.")); - return PM3_ESOFT; - } - - int res = handler_desfire_clear_record_file(fno); - if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully cleared record file."); - } else { - PrintAndLogEx(ERR, "Error on deleting file : %d", res); - } - DropFieldDesfire(); - return res; -} - static int CmdHF14ADesReadData(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes read", @@ -2406,136 +2286,6 @@ static int CmdHF14ADesWriteData(const char *Cmd) { return res; } -static int CmdHF14ADesCreateRecordFile(const char *Cmd) { - CLIParserContext *ctx; - CLIParserInit(&ctx, "hf mfdes createrecordfile", - "Create Linear / Cyclic Record File\n" - "Make sure to select aid or authenticate aid before running this command.", - "hf mfdes createrecordfile -f 1122 -n 02 -c 0 -r EEEE -s 000010 -m 000005 -a 123456" - ); - - void *argtable[] = { - arg_param_begin, - arg_int0("n", "fileno", "", "File Number (0 - 31)"), - arg_strx0("f", "fileid", "", "ISO FID (2 hex bytes, big endian)"), - arg_int0("c", "com", "", "Communication setting (0 = Plain, 1 = Plain + MAC, 3 = Enciphered)"), -// arg_strx0("s", "recordsize", "", "Record size (3 hex bytes, big endian, 000001 to FFFFFF)"), - arg_strx0("r", "rights", "", "Access rights (2 hex bytes -> RW/Chg/R/W, 0x0 - 0xD Key, 0xE Free, 0xF Denied)"), - arg_strx0("s", "size", "", "Record size (3 hex bytes, big endian, 000001 to FFFFFF)"), - arg_strx0("m", "maxrecord", "", "Max. Number of Records (3 hex bytes, big endian)"), - arg_lit0("b", "cyclic", "Create cyclic record file instead of linear record file"), - arg_strx0("a", "aid", "", "App ID to select as hex bytes (3 bytes, big endian)"), - arg_param_end - }; - - CLIExecWithReturn(ctx, Cmd, argtable, false); - int fno = arg_get_int_def(ctx, 1, 0); - - int fidlength = 0; - uint8_t fid[2] = {0}; - int res_flen = CLIParamHexToBuf(arg_get_str(ctx, 2), fid, 2, &fidlength); - - uint8_t comset = arg_get_int(ctx, 3); - - int arlength = 0; - uint8_t ar[2] = {0}; - CLIGetHexWithReturn(ctx, 4, ar, &arlength); - - int rsizelen = 0; - uint8_t recordsize[3] = {0}; - CLIGetHexWithReturn(ctx, 5, recordsize, &rsizelen); - - int msizelen = 0; - uint8_t maxnumrecords[3] = {0}; - CLIGetHexWithReturn(ctx, 6, maxnumrecords, &msizelen); - - bool cyclic = arg_get_lit(ctx, 7); - - int aidlength = 3; - uint8_t aid[3] = {0}; - CLIGetHexWithReturn(ctx, 8, aid, &aidlength); - swap24(aid); - CLIParserFree(ctx); - - swap16(fid); - swap24(recordsize); - swap24(maxnumrecords); - - if (msizelen != 3) { - PrintAndLogEx(ERR, "Maximum number of records must have 3 hex bytes length."); - return PM3_EINVARG; - } - - if (memcmp("\x00\x00\x00", maxnumrecords, 3) == 0x0) { - PrintAndLogEx(ERR, "Maximum number of records is invalid (0x000001-0xFFFFFF)."); - return PM3_EINVARG; - } - - if (fno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (exp 0 - 31), got %d", fno); - return PM3_EINVARG; - } - - if (comset != 0 && comset != 1 && comset != 3) { - PrintAndLogEx(ERR, "Communication setting must be either 0=Plain, 1=Plain+MAC or 3=Encrypt."); - return PM3_EINVARG; - } - - if (arlength != 2) { - PrintAndLogEx(ERR, "Access rights must have 2 hex bytes length."); - return PM3_EINVARG; - } - - if (rsizelen != 3) { - PrintAndLogEx(ERR, "Recordsize must have 3 hex bytes length."); - return PM3_EINVARG; - } - - if (res_flen || fidlength != 2) { - PrintAndLogEx(ERR, "ISO File id must have 2 hex bytes length."); - return PM3_EINVARG; - } - - mfdes_linear_t ft = { - .fileno = fno, - .comset = comset - }; - memcpy(ft.fid, fid, 2); - memcpy(ft.access_rights, ar, 2); - memcpy(ft.recordsize, recordsize, 3); - memcpy(ft.maxnumrecords, maxnumrecords, 3); - - if (aidlength != 3 && aidlength != 0) { - PrintAndLogEx(ERR, _RED_(" The given aid must have 3 bytes (big endian).")); - return PM3_ESOFT; - } else if (aidlength == 0) { - if (memcmp(&tag->selected_application, aid, 3) == 0) { - PrintAndLogEx(ERR, _RED_(" You need to select an aid first.")); - return PM3_ESOFT; - } - memcpy(aid, (uint8_t *)&tag->selected_application, 3); - } - if (handler_desfire_select_application(aid) != PM3_SUCCESS) { - PrintAndLogEx(ERR, _RED_(" Error on selecting aid.")); - return PM3_ESOFT; - } - - int res = PM3_SUCCESS; - if (cyclic) { - res = handler_desfire_create_cyclicrecordfile(&ft); - } else { - res = handler_desfire_create_linearrecordfile(&ft); - } - - if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully created linear/cyclic record file."); - } else { - PrintAndLogEx(ERR, "Couldn't create linear/cyclic record file. Error %d", res); - } - DropFieldDesfire(); - return res; -} - static int CmdHF14ADesInfo(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes info", @@ -4383,8 +4133,10 @@ static int CmdHF14ADesChangeKey(const char *Cmd) { uint8_t newkeynum = arg_get_int_def(ctx, 14, 0); int newkeytype = oldkeytype; - if (CLIGetOptionList(arg_get_str(ctx, 15), DesfireAlgoOpts, &newkeytype)) + if (CLIGetOptionList(arg_get_str(ctx, 15), DesfireAlgoOpts, &newkeytype)) { + CLIParserFree(ctx); return PM3_ESOFT; + } uint8_t newkey[DESFIRE_MAX_KEY_SIZE] = {0}; memset(keydata, 0x00, sizeof(keydata)); @@ -5628,8 +5380,10 @@ static int CmdHF14ADesChFileSettings(const char *Cmd) { settingslen = sdatalen; } else { int cmode = DCMNone; - if (CLIGetOptionList(arg_get_str(ctx, 14), DesfireCommunicationModeOpts, &cmode)) + if (CLIGetOptionList(arg_get_str(ctx, 14), DesfireCommunicationModeOpts, &cmode)) { + CLIParserFree(ctx); return PM3_ESOFT; + } if (cmode == DCMPlain) settings[0] = 0x00; @@ -5639,17 +5393,25 @@ static int CmdHF14ADesChFileSettings(const char *Cmd) { settings[0] = 0x03; int r_mode = 0x0e; - if (CLIGetOptionList(arg_get_str(ctx, 15), DesfireFileAccessModeOpts, &r_mode)) + if (CLIGetOptionList(arg_get_str(ctx, 15), DesfireFileAccessModeOpts, &r_mode)) { + CLIParserFree(ctx); return PM3_ESOFT; + } int w_mode = 0x0e; - if (CLIGetOptionList(arg_get_str(ctx, 16), DesfireFileAccessModeOpts, &w_mode)) + if (CLIGetOptionList(arg_get_str(ctx, 16), DesfireFileAccessModeOpts, &w_mode)) { + CLIParserFree(ctx); return PM3_ESOFT; + } int rw_mode = 0x0e; - if (CLIGetOptionList(arg_get_str(ctx, 17), DesfireFileAccessModeOpts, &rw_mode)) + if (CLIGetOptionList(arg_get_str(ctx, 17), DesfireFileAccessModeOpts, &rw_mode)) { + CLIParserFree(ctx); return PM3_ESOFT; + } int ch_mode = 0x0e; - if (CLIGetOptionList(arg_get_str(ctx, 18), DesfireFileAccessModeOpts, &ch_mode)) + if (CLIGetOptionList(arg_get_str(ctx, 18), DesfireFileAccessModeOpts, &ch_mode)) { + CLIParserFree(ctx); return PM3_ESOFT; + } DesfireEncodeFileAcessMode(&settings[1], r_mode, w_mode, rw_mode, ch_mode) ; } @@ -5971,7 +5733,7 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { return PM3_ESOFT; } - PrintAndLogEx(SUCCESS, "File %02x in the app %06x created " _GREEN_("successfully"), data[0], appid); + PrintAndLogEx(SUCCESS, "%s file %02x in the app %06x created " _GREEN_("successfully"), GetDesfireFileType(filetype), data[0], appid); DropField(); return PM3_SUCCESS; @@ -6118,6 +5880,134 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesCreateRecordFile(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes createrecordfile", + "Create Linear/Cyclic Record file in the application. Application master key needs to be provided or flag --no-auth set (depend on application settings).", + "--rawrights have priority over the separate rights settings.\n" + "Key/mode/etc of the authentication depends on application settings\n" + "hf mfdes createrecordfile --aid 123456 --fid 01 --size 000010 --maxrecord 000010 --cyclic -> create cyclic record file with parameters. Rights from default. Authentication with defaults from `default` command\n" + "hf mfdes createrecordfile --aid 123456 --fid 01 --amode plain --rrights free --wrights free --rwrights free --chrights key0 --size 000010 --maxrecord 000010 -> create linear record file app=123456, file=01 and mentioned rights with defaults from `default` command\n" + "hf mfdes createrecordfile -n 0 -t des -k 0000000000000000 -f none --aid 123456 --fid 01 --size 000010 --maxrecord 000010 -> execute with default factory setup"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), + arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_str0(NULL, "aid", "", "Application ID (3 hex bytes, big endian)"), + arg_str0(NULL, "fid", "", "File ID (1 hex byte)"), + arg_str0(NULL, "isofid", "", "ISO File ID (2 hex bytes)"), + arg_str0(NULL, "amode", "", "File access mode: plain/mac/encrypt"), + arg_str0(NULL, "rawrights", "", "Access rights for file (HEX 2 byte) R/W/RW/Chg, 0x0 - 0xD Key, 0xE Free, 0xF Denied"), + arg_str0(NULL, "rrights", "", "Read file access mode: the specified key, free, deny"), + arg_str0(NULL, "wrights", "", "Write file access mode: the specified key, free, deny"), + arg_str0(NULL, "rwrights", "", "Read/Write file access mode: the specified key, free, deny"), + arg_str0(NULL, "chrights", "", "Change file settings access mode: the specified key, free, deny"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_str0(NULL, "size", "", "Record size (3 hex bytes, big endian, 000001 to FFFFFF)"), + arg_str0(NULL, "maxrecord", "", "Max. Number of Records (3 hex bytes, big endian)"), + arg_lit0(NULL, "cyclic", "Create cyclic record file instead of linear record file"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + bool noauth = arg_get_lit(ctx, 20); + + bool cyclic = arg_get_lit(ctx, 23); + uint8_t filetype = (cyclic) ? 0x04 : 0x03; // linear(03) / cyclic(04) record file + + DesfireContext dctx; + int securechann = defaultSecureChannel; + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMPlain, &appid); + if (res) { + CLIParserFree(ctx); + return res; + } + + if (appid == 0x000000) { + PrintAndLogEx(ERR, "Can't create files at card level."); + CLIParserFree(ctx); + return PM3_EINVARG; + } + + uint8_t data[250] = {0}; + size_t datalen = 0; + + res = DesfireCreateFileParameters(ctx, 12, 13, 14, 15, 16, 17, 18, 19, data, &datalen); + if (res) { + CLIParserFree(ctx); + return res; + } + + uint32_t size = 0; + res = arg_get_u32_hexstr_def_nlen(ctx, 21, 0, &size, 3, true); + if (res == 2) { + PrintAndLogEx(ERR, "Record size must have 3 bytes length"); + CLIParserFree(ctx); + return PM3_EINVARG; + } + + uint32_t maxrecord = 0; + res = arg_get_u32_hexstr_def_nlen(ctx, 22, 0, &maxrecord, 3, true); + if (res == 2) { + PrintAndLogEx(ERR, "Max number of records must have 3 bytes length"); + CLIParserFree(ctx); + return PM3_EINVARG; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + + Uint3byteToMemLe(&data[datalen], size); + datalen += 3; + Uint3byteToMemLe(&data[datalen], maxrecord); + datalen += 3; + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, appid, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + if (verbose) + PrintAndLogEx(INFO, "App: %06x. File num: 0x%02x type: 0x%02x data[%zu]: %s", appid, data[0], filetype, datalen, sprint_hex(data, datalen)); + DesfirePrintCreateFileSettings(filetype, data, datalen); + + + res = DesfireCreateFile(&dctx, filetype, data, datalen, true); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire CreateFile command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + PrintAndLogEx(SUCCESS, "%s file %02x in the app %06x created " _GREEN_("successfully"), GetDesfireFileType(filetype), data[0], appid); + + DropField(); + return PM3_SUCCESS; +} + static int CmdHF14ADesDeleteFile(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes deletefile", @@ -6296,7 +6186,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { if (op == MFDES_GET_VALUE) { PrintAndLogEx(SUCCESS, "Value: " _GREEN_("%d (0x%08x)"), value, value); } else { - DesfireCommitTrqansaction(&dctx, false, 0); + res = DesfireCommitTransaction(&dctx, false, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire CommitTrqansaction command " _RED_("error") ". Result: %d", res); DropField(); @@ -6308,7 +6198,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { } else { res = DesfireValueFileOperations(&dctx, fileid, MFDES_GET_VALUE, &value); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire GetValue command " _RED_("error") ". Result: %d", op, res); + PrintAndLogEx(ERR, "Desfire GetValue command " _RED_("error") ". Result: %d", res); DropField(); return PM3_ESOFT; } @@ -6326,7 +6216,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { } if (verbose) - PrintAndLogEx(INFO, "file settings[%d]: %s", buflen, sprint_hex(buf, buflen)); + PrintAndLogEx(INFO, "file settings[%zu]: %s", buflen, sprint_hex(buf, buflen)); if (buflen < 8 || buf[0] != 0x02) { PrintAndLogEx(ERR, "Desfire GetFileSettings command returns " _RED_("wrong") " data"); @@ -6352,7 +6242,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { if (verbose) PrintAndLogEx(INFO, "Value debited"); - DesfireCommitTrqansaction(&dctx, false, 0); + res = DesfireCommitTransaction(&dctx, false, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire CommitTrqansaction command " _RED_("error") ". Result: %d", res); DropField(); @@ -6373,6 +6263,88 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesClearRecordFile(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes clearrecfile", + "Clear record file. Master key needs to be provided or flag --no-auth set (depend on cards settings).", + "hf mfdes clearrecfile --aid 123456 --fid 01 -> clear record file for: app=123456, file=01 with defaults from `default` command"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), + arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_str0(NULL, "aid", "", "Application ID (3 hex bytes, big endian)"), + arg_str0(NULL, "fid", "", "File ID for clearing (1 hex byte)"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + bool noauth = arg_get_lit(ctx, 13); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMPlain, &appid); + if (res) { + CLIParserFree(ctx); + return res; + } + + uint32_t fnum = 1; + res = arg_get_u32_hexstr_def_nlen(ctx, 12, 1, &fnum, 1, true); + if (res == 2) { + PrintAndLogEx(ERR, "File ID must have 1 byte length"); + CLIParserFree(ctx); + return PM3_EINVARG; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + if (fnum > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (exp 0 - 31), got %d", fnum); + return PM3_EINVARG; + } + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, appid, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + res = DesfireClearRecordFile(&dctx, fnum); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire ClearRecordFile command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + PrintAndLogEx(SUCCESS, "File %02x in the app %06x cleared " _GREEN_("successfully"), fnum, appid); + + DropField(); + return PM3_SUCCESS; +} + static int CmdHF14ADesTest(const char *Cmd) { DesfireTest(true); return PM3_SUCCESS; @@ -6408,17 +6380,17 @@ static command_t CommandTable[] = { {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("Files") " -----------------------"}, {"getfileids", CmdHF14ADesGetFileIDs, IfPm3Iso14443a, "[new]Get File IDs list"}, {"getfileisoids", CmdHF14ADesGetFileISOIDs, IfPm3Iso14443a, "[new]Get File ISO IDs list"}, - {"getfilesettings", CmdHF14ADesGetFileSettings, IfPm3Iso14443a, "[new]Get file settings"}, - {"chfilesettings", CmdHF14ADesChFileSettings, IfPm3Iso14443a, "[new]Change file settings"}, - {"clearfile", CmdHF14ADesClearRecordFile, IfPm3Iso14443a, "Clear record File"}, {"createfile", CmdHF14ADesCreateFile, IfPm3Iso14443a, "[new]Create Standard/Backup File"}, {"createvaluefile", CmdHF14ADesCreateValueFile, IfPm3Iso14443a, "[new]Create Value File"}, - {"createrecordfile", CmdHF14ADesCreateRecordFile, IfPm3Iso14443a, "Create Linear/Cyclic Record File"}, + {"createrecordfile", CmdHF14ADesCreateRecordFile, IfPm3Iso14443a, "[new]Create Linear/Cyclic Record File"}, {"deletefile", CmdHF14ADesDeleteFile, IfPm3Iso14443a, "[new]Delete File"}, + {"getfilesettings", CmdHF14ADesGetFileSettings, IfPm3Iso14443a, "[new]Get file settings"}, + {"chfilesettings", CmdHF14ADesChFileSettings, IfPm3Iso14443a, "[new]Change file settings"}, {"dump", CmdHF14ADesDump, IfPm3Iso14443a, "Dump all files"}, - {"value", CmdHF14ADesValueOperations, IfPm3Iso14443a, "[new]Operations with value file (get/credit/limited credit/debit/clear)"}, {"read", CmdHF14ADesReadData, IfPm3Iso14443a, "Read data from standard/backup/record file"}, {"write", CmdHF14ADesWriteData, IfPm3Iso14443a, "Write data to standard/backup/record file"}, + {"value", CmdHF14ADesValueOperations, IfPm3Iso14443a, "[new]Operations with value file (get/credit/limited credit/debit/clear)"}, + {"clearrecfile", CmdHF14ADesClearRecordFile, IfPm3Iso14443a, "[new]Clear record File"}, {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("System") " -----------------------"}, {"test", CmdHF14ADesTest, AlwaysAvailable, "Test crypto"}, {NULL, NULL, NULL, NULL} diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 70c872849..8003b3096 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1062,7 +1062,7 @@ int DesfireCreateFile(DesfireContext *dctx, uint8_t ftype, uint8_t *fdata, size_ const DesfireCreateFileCommandsS *rcmd = GetDesfireFileCmdRec(ftype); if (rcmd == NULL) return -100; - if (checklen && fdatalen != (rcmd->len + 1) && fdatalen != (rcmd->len + 1 + (rcmd->mayHaveISOfid ? 2 : 0))) + if (checklen && fdatalen != (rcmd->createlen + 1) && fdatalen != (rcmd->createlen + 1 + (rcmd->mayHaveISOfid ? 2 : 0))) return -110; return DesfireCommandTxData(dctx, rcmd->cmd, fdata, fdatalen); @@ -1072,14 +1072,18 @@ int DesfireDeleteFile(DesfireContext *dctx, uint8_t fnum) { return DesfireCommandTxData(dctx, MFDES_DELETE_FILE, &fnum, 1); } -int DesfireCommitTrqansaction(DesfireContext *dctx, bool enable_options, uint8_t options) { +int DesfireClearRecordFile(DesfireContext *dctx, uint8_t fnum) { + return DesfireCommandTxData(dctx, MFDES_CLEAR_RECORD_FILE, &fnum, 1); +} + +int DesfireCommitTransaction(DesfireContext *dctx, bool enable_options, uint8_t options) { if (enable_options) return DesfireCommandTxData(dctx, MFDES_COMMIT_TRANSACTION, &options, 1); else return DesfireCommandNoData(dctx, MFDES_COMMIT_TRANSACTION); } -int DesfireAbortTrqansaction(DesfireContext *dctx) { +int DesfireAbortTransaction(DesfireContext *dctx) { return DesfireCommandNoData(dctx, MFDES_ABORT_TRANSACTION); } @@ -1209,7 +1213,7 @@ const DesfireCreateFileCommandsS *GetDesfireFileCmdRec(uint8_t type) { return NULL; } -static const char *GetDesfireFileType(uint8_t type) { +const char *GetDesfireFileType(uint8_t type) { const DesfireCreateFileCommandsS *res = GetDesfireFileCmdRec(type); if (res != NULL) return res->text; @@ -1334,6 +1338,7 @@ static void DesfirePrintFileSettDynPart(uint8_t filetype, uint8_t *data, size_t PrintAndLogEx(INFO, "Record size : %d (0x%X) bytes", recordsize, recordsize); PrintAndLogEx(INFO, "Max num records : %d (0x%X)", maxrecords, maxrecords); + PrintAndLogEx(INFO, "Total size : %d (0x%X) bytes", recordsize * maxrecords, recordsize * maxrecords); if (!create) PrintAndLogEx(INFO, "Curr num records : %d (0x%X)", currentrecord, currentrecord); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 3b9d2507c..14453993b 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -87,11 +87,13 @@ void DesfirePrintFileSettings(uint8_t *data, size_t len); void DesfirePrintSetFileSettings(uint8_t *data, size_t len); void DesfirePrintCreateFileSettings(uint8_t filetype, uint8_t *data, size_t len); +const char *GetDesfireFileType(uint8_t type); int DesfireCreateFile(DesfireContext *dctx, uint8_t ftype, uint8_t *fdata, size_t fdatalen, bool checklen); -int DesfireDeleteFile(DesfireContext *dctx, uint8_t fid); -int DesfireCommitTrqansaction(DesfireContext *dctx, bool enable_options, uint8_t options); -int DesfireAbortTrqansaction(DesfireContext *dctx); +int DesfireDeleteFile(DesfireContext *dctx, uint8_t fnum); +int DesfireCommitTransaction(DesfireContext *dctx, bool enable_options, uint8_t options); +int DesfireAbortTransaction(DesfireContext *dctx); int DesfireValueFileOperations(DesfireContext *dctx, uint8_t fid, uint8_t operation, uint32_t *value); +int DesfireClearRecordFile(DesfireContext *dctx, uint8_t fnum); #endif // __DESFIRECORE_H diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c index 6c9a9253e..1452c43e9 100644 --- a/client/src/mifare/desfiresecurechan.c +++ b/client/src/mifare/desfiresecurechan.c @@ -42,6 +42,7 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_LIMITED_CREDIT, DACd40, DCCNative, DCMPlain}, {MFDES_DEBIT, DACd40, DCCNative, DCMPlain}, {MFDES_COMMIT_TRANSACTION, DACd40, DCCNative, DCMPlain}, + {MFDES_CLEAR_RECORD_FILE, DACd40, DCCNative, DCMPlain}, {MFDES_READ_DATA, DACd40, DCCNative, DCMMACed}, {MFDES_WRITE_DATA, DACd40, DCCNative, DCMMACed}, @@ -93,6 +94,7 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_LIMITED_CREDIT, DACEV1, DCCNative, DCMMACed}, {MFDES_DEBIT, DACEV1, DCCNative, DCMMACed}, {MFDES_COMMIT_TRANSACTION, DACEV1, DCCNative, DCMMACed}, + {MFDES_CLEAR_RECORD_FILE, DACEV1, DCCNative, DCMMACed}, {MFDES_GET_UID, DACEV1, DCCNative, DCMEncrypted}, {MFDES_CHANGE_KEY_SETTINGS, DACEV1, DCCNative, DCMEncrypted},