write file command

This commit is contained in:
merlokk committed 2021-07-28 18:13:07 +03:00
1 parent 76e93227cf
commit f5d5f088b8
1 file changed
+259 -11
+259 -11
View File
@@ -1518,7 +1518,7 @@ static int handler_desfire_getkeysettings(uint8_t *key_settings, uint8_t *num_ke
return res; return res;
} }
static int handler_desfire_commit_transaction(void) { /*static int handler_desfire_commit_transaction(void) {
sAPDU apdu = {0x90, MFDES_COMMIT_TRANSACTION, 0x00, 0x00, 0x00, NULL}; //0xC7 sAPDU apdu = {0x90, MFDES_COMMIT_TRANSACTION, 0x00, 0x00, 0x00, NULL}; //0xC7
uint32_t recv_len = 0; uint32_t recv_len = 0;
uint16_t sw = 0; uint16_t sw = 0;
@@ -1531,7 +1531,7 @@ static int handler_desfire_commit_transaction(void) {
return PM3_ESOFT; return PM3_ESOFT;
return res; return res;
} }*/
// --- GET APPIDS // --- GET APPIDS
static int handler_desfire_appids(uint8_t *dest, uint32_t *app_ids_len) { static int handler_desfire_appids(uint8_t *dest, uint32_t *app_ids_len) {
@@ -1747,12 +1747,12 @@ static int handler_desfire_getvalue(mfdes_value_t *value, uint32_t *resplen, uin
return res; return res;
} }
static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type, uint8_t cs) { //static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type, uint8_t cs) {
/* LC FN OF OF OF LN LN LN DD DD DD /* LC FN OF OF OF LN LN LN DD DD DD
90 3d 00 00 16 01 00 00 00 0f 00 00 00 0f 20 00 3b 00 34 04 06 e1 04 0f fe 00 00 00 90 3d 00 00 16 01 00 00 00 0f 00 00 00 0f 20 00 3b 00 34 04 06 e1 04 0f fe 00 00 00
90 3d 00 00 09 02 00 00 00 02 00 00 00 00 00 90 3d 00 00 09 02 00 00 00 02 00 00 00 00 00
*/ */
/*
if (data->fileno > 0x1F) { if (data->fileno > 0x1F) {
return PM3_EINVARG; return PM3_EINVARG;
} }
@@ -1802,14 +1802,14 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type,
apdu.Lc = plen - 1; apdu.Lc = plen - 1;
} }
/* *
// we dont want to change the value of datasize, so delt with above without change // we dont want to change the value of datasize, so delt with above without change
// Doing so can create wrong offsets and endless loop. // Doing so can create wrong offsets and endless loop.
if (plen != -1) datasize = (uint8_t)plen; if (plen != -1) datasize = (uint8_t)plen;
memcpy(&tmp[7], p, datasize); memcpy(&tmp[7], p, datasize);
apdu.Lc = datasize + 1 + 3 + 3; apdu.Lc = datasize + 1 + 3 + 3;
*/ *
res = send_desfire_cmd(&apdu, false, NULL, &recvlen, &sw, 0, true); res = send_desfire_cmd(&apdu, false, NULL, &recvlen, &sw, 0, true);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
@@ -1828,7 +1828,7 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type,
} }
} }
return res; return res;
} }*/
static int getKeySettings(uint8_t *aid) { static int getKeySettings(uint8_t *aid) {
if (aid == NULL) return PM3_EINVARG; if (aid == NULL) return PM3_EINVARG;
@@ -2041,7 +2041,7 @@ static int CmdHF14ADesSelectApp(const char *Cmd) {
return res; return res;
} }
static int selectfile(uint8_t *aid, uint8_t fileno, uint8_t *cs) { /*static int selectfile(uint8_t *aid, uint8_t fileno, uint8_t *cs) {
if (handler_desfire_select_application(aid) != PM3_SUCCESS) { if (handler_desfire_select_application(aid) != PM3_SUCCESS) {
PrintAndLogEx(ERR, _RED_(" Couldn't select aid.")); PrintAndLogEx(ERR, _RED_(" Couldn't select aid."));
return PM3_ESOFT; return PM3_ESOFT;
@@ -2071,7 +2071,7 @@ static int selectfile(uint8_t *aid, uint8_t fileno, uint8_t *cs) {
} }
*cs = filesettings[1]; *cs = filesettings[1];
return res; return res;
} }*/
static int CmdHF14ADesInfo(const char *Cmd) { static int CmdHF14ADesInfo(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
@@ -6252,7 +6252,7 @@ static int CmdHF14ADesReadData(const char *Cmd) {
arg_str0("c", "ccset", "<native/niso/iso>", "Communicaton command set: native/niso/iso"), arg_str0("c", "ccset", "<native/niso/iso>", "Communicaton command set: native/niso/iso"),
arg_str0("s", "schann", "<d40/ev1/ev2>", "Secure channel: d40/ev1/ev2"), arg_str0("s", "schann", "<d40/ev1/ev2>", "Secure channel: d40/ev1/ev2"),
arg_str0(NULL, "aid", "<app id hex>", "Application ID (3 hex bytes, big endian)"), arg_str0(NULL, "aid", "<app id hex>", "Application ID (3 hex bytes, big endian)"),
arg_str0(NULL, "fid", "<file id hex>", "File ID for clearing (1 hex byte)"), arg_str0(NULL, "fid", "<file id hex>", "File ID (1 hex byte)"),
arg_lit0(NULL, "no-auth", "execute without authentication"), arg_lit0(NULL, "no-auth", "execute without authentication"),
arg_str0(NULL, "type", "<auto/data/value/record/mac>", "File Type auto/data(Standard/Backup)/value/record(linear/cyclic)/mac). Auto - check file settings and then read. Default: auto"), arg_str0(NULL, "type", "<auto/data/value/record/mac>", "File Type auto/data(Standard/Backup)/value/record(linear/cyclic)/mac). Auto - check file settings and then read. Default: auto"),
arg_str0("o", "offset", "<hex>", "File Offset (3 hex bytes, big endian). For records - record number (0 - lastest record). Default 0"), arg_str0("o", "offset", "<hex>", "File Offset (3 hex bytes, big endian). For records - record number (0 - lastest record). Default 0"),
@@ -6479,8 +6479,255 @@ static int CmdHF14ADesReadData(const char *Cmd) {
} }
static int CmdHF14ADesWriteData(const char *Cmd) { static int CmdHF14ADesWriteData(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes write",
"Write data from file. Key needs to be provided or flag --no-auth set (depend on file settings).",
"hf mfdes write --aid 123456 --fid 01 -d 01020304 -> write file: app=123456, file=01, offset=0, get file type from card. use default channel settings from `default` command\n"
"hf mfdes write --aid 123456 --fid 01 --type record --offset 000000 -d 11223344 -> write record to record file. use default channel settings 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", "<keyno>", "Key number"),
arg_str0("t", "algo", "<DES/2TDEA/3TDEA/AES>", "Crypt algo: DES, 2TDEA, 3TDEA, AES"),
arg_str0("k", "key", "<Key>", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"),
arg_str0("f", "kdf", "<none/AN10922/gallagher>", "Key Derivation Function (KDF): None, AN10922, Gallagher"),
arg_str0("i", "kdfi", "<kdfi>", "KDF input (HEX 1-31 bytes)"),
arg_str0("m", "cmode", "<plain/mac/encrypt>", "Communicaton mode: plain/mac/encrypt"),
arg_str0("c", "ccset", "<native/niso/iso>", "Communicaton command set: native/niso/iso"),
arg_str0("s", "schann", "<d40/ev1/ev2>", "Secure channel: d40/ev1/ev2"),
arg_str0(NULL, "aid", "<app id hex>", "Application ID (3 hex bytes, big endian)"),
arg_str0(NULL, "fid", "<file id hex>", "File ID (1 hex byte)"),
arg_lit0(NULL, "no-auth", "execute without authentication"),
arg_str0(NULL, "type", "<auto/data/value/record/mac>", "File Type auto/data(Standard/Backup)/value/record(linear/cyclic)/mac). Auto - check file settings and then write. Default: auto"),
arg_str0("o", "offset", "<hex>", "File Offset (3 hex bytes, big endian). For records - record number (0 - lastest record). Default 0"),
arg_str0("d", "data", "<hex>", "data for write (data/record file), credit/debit(value file)"),
arg_lit0(NULL, "debit", "use for value file debit operation instead of credit"),
arg_lit0(NULL, "commit", "commit needs for backup, value and record data file. In `auto` type it set automatically."),
arg_int0(NULL, "updaterec", "<record number dec>", "Record number for update record command. Updates record instead of write. Lastest record - 0"),
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;
}
int op = RFTAuto;
if (CLIGetOptionList(arg_get_str(ctx, 14), DesfireReadFileTypeOpts, &op)) {
CLIParserFree(ctx);
return PM3_ESOFT;
}
uint32_t offset = 0;
res = arg_get_u32_hexstr_def_nlen(ctx, 15, 0, &offset, 3, true);
if (res == 2) {
PrintAndLogEx(ERR, "Offset must have 3 byte length");
CLIParserFree(ctx);
return PM3_EINVARG;
}
uint8_t data[1024] = {0};
int datalen = sizeof(data);
CLIGetHexWithReturn(ctx, 16, data, &datalen);
if (datalen == 0) {
PrintAndLogEx(ERR, "Data for write must be present.");
CLIParserFree(ctx);
return PM3_EINVARG;
}
bool debit = arg_get_lit(ctx, 17);
bool commit = arg_get_lit(ctx, 18);
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;
}
}
// get file settings
if (op == RFTAuto) {
FileSettingsS fsettings;
DesfireCommunicationMode commMode = dctx.commMode;
DesfireSetCommMode(&dctx, DCMPlain);
res = DesfireGetFileSettingsStruct(&dctx, fnum, &fsettings);
DesfireSetCommMode(&dctx, commMode);
if (res == PM3_SUCCESS) {
switch(fsettings.fileType) {
case 0x00:
case 0x01: {
op = RFTData;
commit = (fsettings.fileType == 0x01);
break;
}
case 0x02: {
op = RFTValue;
commit = true;
break;
}
case 0x03:
case 0x04: {
op = RFTRecord;
commit = true;
if (datalen > fsettings.recordSize)
PrintAndLogEx(WARNING, "Record size (%d) " _RED_("is less") " than data length (%d)", fsettings.recordSize, datalen);
break;
}
case 0x05: {
op = RFTMAC;
commit = false;
break;
}
default: {
break;
}
}
DesfireSetCommMode(&dctx, fsettings.commMode);
if (fsettings.fileCommMode != 0 && noauth)
PrintAndLogEx(WARNING, "File needs communication mode `%s` but there is no authentication", CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode));
if (verbose)
PrintAndLogEx(INFO, "Got file type: %s. Option: %s. comm mode: %s",
GetDesfireFileType(fsettings.fileType),
CLIGetOptionListStr(DesfireReadFileTypeOpts, op),
CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode));
} else {
PrintAndLogEx(WARNING, "GetFileSettings error. Can't get file type.");
}
}
if (op == RFTData) {
res = DesfireWriteFile(&dctx, fnum, offset, datalen, data);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire WriteFile command " _RED_("error") ". Result: %d", res);
DropField();
return PM3_ESOFT;
}
if (commit) {
res = DesfireCommitTransaction(&dctx, false, 0);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire CommitTransaction command " _RED_("error") ". Result: %d", res);
DropField();
return PM3_ESOFT;
}
}
PrintAndLogEx(SUCCESS, "Write data file %02x " _GREEN_("success"), fnum);
}
if (op == RFTValue) {
if (datalen != 4)
PrintAndLogEx(WARNING, "Value " _RED_("should be") " 4 byte length instead of %d", datalen);
uint32_t value = MemBeToUint4byte(data);
uint8_t vop = (debit) ? MFDES_DEBIT : MFDES_CREDIT;
res = DesfireValueFileOperations(&dctx, fnum, vop, &value);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire %s operation " _RED_("error") ". Result: %d", CLIGetOptionListStr(DesfireValueFileOperOpts, vop), res);
DropField();
return PM3_ESOFT;
}
if (commit) {
res = DesfireCommitTransaction(&dctx, false, 0);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire CommitTransaction command " _RED_("error") ". Result: %d", res);
DropField();
return PM3_ESOFT;
}
}
PrintAndLogEx(SUCCESS, "Value file %02x (%s) " _GREEN_("success"), fnum, CLIGetOptionListStr(DesfireValueFileOperOpts, vop));
}
if (op == RFTRecord) {
/* resplen = 0;
if (reclen == 0) {
res = DesfireReadRecords(&dctx, fnum, offset, 1, resp, &resplen);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadRecords (len=1) command " _RED_("error") ". Result: %d", res);
DropField();
return PM3_ESOFT;
}
reclen = resplen;
}
if (verbose)
PrintAndLogEx(INFO, "Record length %zu", reclen);
// if we got one record via the DesfireReadRecords before -- we not need to get it 2nd time
if (length != 1 || resplen == 0) {
res = DesfireReadRecords(&dctx, fnum, offset, length, resp, &resplen);
if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadRecords command " _RED_("error") ". Result: %d", res);
DropField();
return PM3_ESOFT;
}
}
if (resplen > 0) {
size_t reccount = resplen / reclen;
PrintAndLogEx(SUCCESS, "Read %u bytes from file 0x%02x from record %d record count %zu record length %zu", resplen, fnum, offset, reccount, reclen);
if (reccount > 1)
PrintAndLogEx(SUCCESS, "Lastest record at the bottom.");
for (int i = 0; i < reccount; i++) {
if (i != 0)
PrintAndLogEx(SUCCESS, "Record %d", i + offset);
print_buffer_with_offset(&resp[i * reclen], reclen, offset, (i == 0));
}
} else {
PrintAndLogEx(SUCCESS, "Read operation returned no data from file %d", fnum);
}*/
}
if (op == RFTMAC) {
PrintAndLogEx(ERR, "Can't " _RED_("write") " to transaction MAC file");
}
DropField();
return PM3_SUCCESS;
/* CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes write", CLIParserInit(&ctx, "hf mfdes write",
"Write data to file\n" "Write data to file\n"
"Make sure to select aid or authenticate aid before running this command.", "Make sure to select aid or authenticate aid before running this command.",
@@ -6574,6 +6821,7 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
} }
DropFieldDesfire(); DropFieldDesfire();
return res; return res;
*/
} }
static int CmdHF14ADesTest(const char *Cmd) { static int CmdHF14ADesTest(const char *Cmd) {