Skip to content

Commit b944315

Browse files
authored
Merge pull request #2987 from merlokk/desfire_clear_modes
fixed clear command logic. will works with some default settings.
2 parents 8f3bc87 + 28da83c commit b944315

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
33
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
44

55
## [unreleased][unreleased]
6+
- Fix `hf mfdes value --op clear` commands for clearing more than 0x80000000 values and getfilesettings mac mode (@merlokk)
67
- Added ATR fingerprinting to `hf 14a/14b info` (@doegox)
78

89
## [Phrack.4.20728][2025-09-11]

client/src/cmdhfmfdes.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4541,7 +4541,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
45414541
arg_str0(NULL, "aid", "<hex>", "Application ID (3 hex bytes, big endian)"),
45424542
arg_str0(NULL, "isoid", "<hex>", "Application ISO ID (ISO DF ID) (2 hex bytes, big endian)"),
45434543
arg_str0(NULL, "fid", "<hex>", "File ID (1 hex byte)"),
4544-
arg_str0("o", "op", "<get/credit/limcredit/debit/clear>", "Operation: get(default)/credit/limcredit(limited credit)/debit/clear. Operation clear: get-getopt-debit to min value"),
4544+
arg_str0("o", "op", "<get/credit/limcredit/debit/clear>", "Operation: get(default)/credit/limcredit(limited credit)/debit/clear. Additional operation clear: get-getopt-debit to min value"),
45454545
arg_str0("d", "data", "<hex>", "Value for operation (HEX 4 bytes)"),
45464546
arg_lit0(NULL, "no-auth", "Execute without authentication"),
45474547
arg_param_end
@@ -4623,18 +4623,21 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
46234623
PrintAndLogEx(SUCCESS, "Value changed " _GREEN_("successfully"));
46244624
}
46254625
} else {
4626+
DesfireCommunicationMode fileCommMode = dctx.commMode;
4627+
46264628
res = DesfireValueFileOperations(&dctx, fileid, MFDES_GET_VALUE, &value);
46274629
if (res != PM3_SUCCESS) {
46284630
PrintAndLogEx(ERR, "Desfire GetValue command ( " _RED_("error") ") Result: %d", res);
46294631
DropField();
46304632
return PM3_ESOFT;
46314633
}
46324634
if (verbose)
4633-
PrintAndLogEx(INFO, "current value: 0x%08x", value);
4635+
PrintAndLogEx(INFO, _YELLOW_("GetValue") " command is " _GREEN_("ok") ". Current value: 0x%08x", value);
46344636

46354637
uint8_t buf[250] = {0};
46364638
size_t buflen = 0;
46374639

4640+
DesfireSetCommMode(&dctx, DCMMACed);
46384641
res = DesfireGetFileSettings(&dctx, fileid, buf, &buflen);
46394642
if (res != PM3_SUCCESS) {
46404643
PrintAndLogEx(ERR, "Desfire GetFileSettings command ( " _RED_("error") " ) Result: %d", res);
@@ -4643,22 +4646,41 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
46434646
}
46444647

46454648
if (verbose)
4646-
PrintAndLogEx(INFO, "file settings[%zu]: %s", buflen, sprint_hex(buf, buflen));
4649+
PrintAndLogEx(INFO, _YELLOW_("GetFileSettings") " is " _GREEN_("ok") " . File settings[%zu]: %s", buflen, sprint_hex(buf, buflen));
46474650

46484651
if (buflen < 8 || buf[0] != 0x02) {
46494652
PrintAndLogEx(ERR, "Desfire GetFileSettings command returns " _RED_("wrong") " data");
46504653
DropField();
46514654
return PM3_ESOFT;
46524655
}
46534656

4654-
uint32_t minvalue = MemLeToUint4byte(&buf[4]);
4655-
uint32_t delta = (value > minvalue) ? value - minvalue : 0;
4657+
int32_t minvalue = (int)MemLeToUint4byte(&buf[4]);
4658+
uint32_t delta = ((int64_t)value > minvalue) ? value - minvalue : 0;
46564659
if (verbose) {
4657-
PrintAndLogEx(INFO, "minimum value: 0x%08x", minvalue);
4658-
PrintAndLogEx(INFO, "delta value : 0x%08x", delta);
4660+
PrintAndLogEx(INFO, "value: 0x%08x (%d)", value, value);
4661+
PrintAndLogEx(INFO, "minimum value: 0x%08x (%d)", minvalue, minvalue);
4662+
PrintAndLogEx(INFO, "delta value : 0x%08x (%d)", delta, delta);
46594663
}
46604664

46614665
if (delta > 0) {
4666+
DesfireSetCommMode(&dctx, fileCommMode);
4667+
4668+
uint32_t maxdelta = 0x7fffffff;
4669+
if (delta > maxdelta) {
4670+
res = DesfireValueFileOperations(&dctx, fileid, MFDES_DEBIT, &maxdelta);
4671+
if (res != PM3_SUCCESS) {
4672+
PrintAndLogEx(ERR, "Desfire Debit maxdelta operation ( " _RED_("error") " ) Result: %d", res);
4673+
DropField();
4674+
return PM3_ESOFT;
4675+
}
4676+
4677+
delta -= maxdelta;
4678+
if (verbose) {
4679+
PrintAndLogEx(INFO, "Value maxdelta debited " _GREEN_("ok"));
4680+
PrintAndLogEx(INFO, "delta value : 0x%08x (%d)", delta, delta);
4681+
}
4682+
}
4683+
46624684
res = DesfireValueFileOperations(&dctx, fileid, MFDES_DEBIT, &delta);
46634685
if (res != PM3_SUCCESS) {
46644686
PrintAndLogEx(ERR, "Desfire Debit operation ( " _RED_("error") " ) Result: %d", res);
@@ -4667,7 +4689,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
46674689
}
46684690

46694691
if (verbose)
4670-
PrintAndLogEx(INFO, "Value debited");
4692+
PrintAndLogEx(INFO, "Value debited " _GREEN_("ok"));
46714693

46724694
DesfireSetCommMode(&dctx, DCMMACed);
46734695
res = DesfireCommitTransaction(&dctx, false, 0);
@@ -4678,7 +4700,7 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
46784700
}
46794701

46804702
if (verbose)
4681-
PrintAndLogEx(INFO, "Transaction committed");
4703+
PrintAndLogEx(INFO, "Transaction :" _GREEN_("committed"));
46824704
} else {
46834705
if (verbose)
46844706
PrintAndLogEx(INFO, "Nothing to clear. Value already in the minimum level.");

0 commit comments

Comments
 (0)