Skip to content

Commit d8d0906

Browse files
committed
'hf mfu incr' to increment counters of UL-EV1 family; Also fix reading of NTAG counters in 'hf mfu info'
1 parent ca15bbd commit d8d0906

File tree

1 file changed

+123
-9
lines changed

1 file changed

+123
-9
lines changed

client/src/cmdhfmfu.c

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,21 +1540,13 @@ static int ulev1_print_version(uint8_t *data) {
15401540
}
15411541

15421542
static int ntag_print_counter(void) {
1543-
// NTAG has one counter/tearing. At address 0x02.
1543+
// NTAG has one counter. At address 0x02. With no tearing.
15441544
PrintAndLogEx(NORMAL, "");
15451545
PrintAndLogEx(INFO, "--- " _CYAN_("Tag Counter"));
1546-
uint8_t tear[1] = {0};
15471546
uint8_t counter[3] = {0, 0, 0};
15481547
uint16_t len;
1549-
len = ulev1_readTearing(0x02, tear, sizeof(tear));
1550-
(void)len;
15511548
len = ulev1_readCounter(0x02, counter, sizeof(counter));
1552-
(void)len;
15531549
PrintAndLogEx(INFO, " [02]: %s", sprint_hex(counter, 3));
1554-
PrintAndLogEx(SUCCESS, " - %02X tearing ( %s )"
1555-
, tear[0]
1556-
, (tear[0] == 0xBD) ? _GREEN_("ok") : _RED_("fail")
1557-
);
15581550
return len;
15591551
}
15601552

@@ -5833,6 +5825,127 @@ static int CmdHF14AMfuWipe(const char *Cmd) {
58335825
return PM3_SUCCESS;
58345826
}
58355827

5828+
static int CmdHF14AMfUIncr(const char *Cmd) {
5829+
CLIParserContext *ctx;
5830+
CLIParserInit(&ctx, "hf mfu incr",
5831+
"Increment a MIFARE Ultralight Ev1 counter\n"
5832+
"Will read but not increment counter if NTAG is detected",
5833+
"hf mfu incr -c 0 -v 1337\n"
5834+
"hf mfu incr -c 2 -v 0 -p FFFFFFFF");
5835+
void *argtable[] = {
5836+
arg_param_begin,
5837+
arg_int1("c", "cnt", "<dec>", "Counter index from 0"),
5838+
arg_int1("v", "val", "<dec>", "Value to increment by (0-16777215)"),
5839+
arg_str0("p", "pwd", "<hex>", "PWD to authenticate with"),
5840+
arg_param_end
5841+
};
5842+
CLIExecWithReturn(ctx, Cmd, argtable, true);
5843+
5844+
uint8_t counter = arg_get_int_def(ctx, 1, 3);
5845+
uint32_t value = arg_get_u32_def(ctx, 2, 16777216);
5846+
5847+
int pwd_len;
5848+
uint8_t pwd[4] = { 0x00 };
5849+
CLIGetHexWithReturn(ctx, 3, pwd, &pwd_len);
5850+
bool has_key = false;
5851+
if (pwd_len) {
5852+
has_key = true;
5853+
if (pwd_len != 4) {
5854+
PrintAndLogEx(WARNING, "incorrect PWD length");
5855+
return PM3_EINVARG;
5856+
}
5857+
}
5858+
5859+
CLIParserFree(ctx);
5860+
5861+
if (counter > 2) {
5862+
PrintAndLogEx(WARNING, "Counter index must be in range 0-2");
5863+
return PM3_EINVARG;
5864+
}
5865+
if (value > 16777215) {
5866+
PrintAndLogEx(WARNING, "Value to increment must be in range 0-16777215");
5867+
return PM3_EINVARG;
5868+
}
5869+
5870+
uint8_t increment_cmd[6] = { MIFARE_ULEV1_INCR_CNT, counter, 0x00, 0x00, 0x00, 0x00 };
5871+
5872+
for (uint8_t i = 0; i < 3; i++) {
5873+
increment_cmd[i + 2] = (value >> (8 * i)) & 0xff;
5874+
}
5875+
5876+
iso14a_card_select_t card;
5877+
if (ul_select(&card) == false) {
5878+
PrintAndLogEx(FAILED, "failed to select card, exiting...");
5879+
return PM3_ESOFT;
5880+
}
5881+
5882+
uint64_t tagtype = GetHF14AMfU_Type();
5883+
uint64_t tags_with_counter_ul = MFU_TT_UL_EV1_48 | MFU_TT_UL_EV1_128 | MFU_TT_UL_EV1;
5884+
uint64_t tags_with_counter_ntag = MFU_TT_NTAG_213 | MFU_TT_NTAG_213_F | MFU_TT_NTAG_213_C | MFU_TT_NTAG_213_TT | MFU_TT_NTAG_215 | MFU_TT_NTAG_216;
5885+
if ((tagtype & (tags_with_counter_ul | tags_with_counter_ntag)) == 0) {
5886+
PrintAndLogEx(WARNING, "tag type does not have counters");
5887+
DropField();
5888+
return PM3_ESOFT;
5889+
}
5890+
5891+
bool is_ntag = (tagtype & tags_with_counter_ntag) != 0;
5892+
if (is_ntag && (counter != 2)) {
5893+
PrintAndLogEx(WARNING, "NTAG only has one counter at index 2");
5894+
DropField();
5895+
return PM3_EINVARG;
5896+
}
5897+
5898+
uint8_t pack[4] = { 0, 0, 0, 0 };
5899+
if (has_key) {
5900+
if (ulev1_requestAuthentication(pwd, pack, sizeof(pack)) == PM3_EWRONGANSWER) {
5901+
PrintAndLogEx(FAILED, "authentication failed UL-EV1/NTAG");
5902+
DropField();
5903+
return PM3_ESOFT;
5904+
}
5905+
}
5906+
5907+
uint8_t current_counter[3] = { 0, 0, 0 };
5908+
int len = ulev1_readCounter(counter, current_counter, sizeof(current_counter));
5909+
if (len != sizeof(current_counter)) {
5910+
PrintAndLogEx(FAILED, "failed to read old counter");
5911+
if (is_ntag) {
5912+
PrintAndLogEx(HINT, "NTAG detected, try reading with PWD");
5913+
}
5914+
DropField();
5915+
return PM3_ESOFT;
5916+
}
5917+
5918+
uint32_t current_counter_num = current_counter[0] | (current_counter[1] << 8) | (current_counter[2] << 16);
5919+
PrintAndLogEx(INFO, "Current counter... " _GREEN_("%8d") " - " _GREEN_("%s"), current_counter_num, sprint_hex(current_counter, 3));
5920+
5921+
if ((tagtype & tags_with_counter_ntag) != 0) {
5922+
PrintAndLogEx(WARNING, "NTAG detected, unable to manually increment counter");
5923+
DropField();
5924+
return PM3_ESOFT;
5925+
}
5926+
5927+
uint8_t resp[1] = { 0x00 };
5928+
if (ul_send_cmd_raw(increment_cmd, sizeof(increment_cmd), resp, sizeof(resp)) < 0) {
5929+
PrintAndLogEx(FAILED, "failed to increment counter");
5930+
DropField();
5931+
return PM3_ESOFT;
5932+
}
5933+
5934+
uint8_t new_counter[3] = { 0, 0, 0 };
5935+
int new_len = ulev1_readCounter(counter, new_counter, sizeof(new_counter));
5936+
if (new_len != sizeof(current_counter)) {
5937+
PrintAndLogEx(FAILED, "failed to read new counter");
5938+
DropField();
5939+
return PM3_ESOFT;
5940+
}
5941+
5942+
uint32_t new_counter_num = new_counter[0] | (new_counter[1] << 8) | (new_counter[2] << 16);
5943+
PrintAndLogEx(INFO, "New counter....... " _GREEN_("%8d") " - " _GREEN_("%s"), new_counter_num, sprint_hex(new_counter, 3));
5944+
5945+
DropField();
5946+
return PM3_SUCCESS;
5947+
}
5948+
58365949
static command_t CommandTable[] = {
58375950
{"help", CmdHelp, AlwaysAvailable, "This help"},
58385951
{"list", CmdHF14AMfuList, AlwaysAvailable, "List MIFARE Ultralight / NTAG history"},
@@ -5845,6 +5958,7 @@ static command_t CommandTable[] = {
58455958
{"cauth", CmdHF14AMfUCAuth, IfPm3Iso14443a, "Ultralight-C - Authentication"},
58465959
{"setpwd", CmdHF14AMfUCSetPwd, IfPm3Iso14443a, "Ultralight-C - Set 3DES key"},
58475960
{"dump", CmdHF14AMfUDump, IfPm3Iso14443a, "Dump MIFARE Ultralight family tag to binary file"},
5961+
{"incr", CmdHF14AMfUIncr, IfPm3Iso14443a, "Increments Ev1/NTAG counter"},
58485962
{"info", CmdHF14AMfUInfo, IfPm3Iso14443a, "Tag information"},
58495963
{"ndefread", CmdHF14MfuNDEFRead, IfPm3Iso14443a, "Prints NDEF records from card"},
58505964
{"rdbl", CmdHF14AMfURdBl, IfPm3Iso14443a, "Read block"},

0 commit comments

Comments
 (0)