refactoring and lsfiles

This commit is contained in:
merlokk 2021-08-19 16:16:18 +03:00
parent a9dd8aceb5
commit e55c8f969a
3 changed files with 33 additions and 17 deletions

View file

@ -1987,14 +1987,14 @@ static int CmdHF14ADesAuth(const char *Cmd) {
res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
DropField(); DropField();
PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res));
return res; return res;
} }
if (DesfireMFSelected(selectway, id)) if (DesfireMFSelected(selectway, id))
PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully")); PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully"));
else else
PrintAndLogEx(SUCCESS, "Application %s " _CYAN_("%0*x") " selected and authenticated " _GREEN_("succesfully"), DesfireSelectWayToStr(selectway), selectway == ISW6bAID ? 6 : 4, id); PrintAndLogEx(SUCCESS, "Application " _CYAN_("%s") " selected and authenticated " _GREEN_("succesfully"), DesfireWayIDStr(selectway, id));
PrintAndLogEx(SUCCESS, _CYAN_("Context: ")); PrintAndLogEx(SUCCESS, _CYAN_("Context: "));
DesfirePrintContext(&dctx); DesfirePrintContext(&dctx);
@ -3132,7 +3132,7 @@ static int CmdHF14ADesGetFileIDs(const char *Cmd) {
res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
DropField(); DropField();
PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res));
return res; return res;
} }
@ -3205,7 +3205,7 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) {
res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
DropField(); DropField();
PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res));
return res; return res;
} }
@ -3285,7 +3285,7 @@ static int CmdHF14ADesGetFileSettings(const char *Cmd) {
res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
DropField(); DropField();
PrintAndLogEx(FAILED, "Select or authentication %s 0x%06x " _RED_("failed") ". Result [%d] %s", DesfireSelectWayToStr(selectway), id, res, DesfireAuthErrorToStr(res)); PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res));
return res; return res;
} }
@ -3300,7 +3300,7 @@ static int CmdHF14ADesGetFileSettings(const char *Cmd) {
} }
if (verbose) if (verbose)
PrintAndLogEx(INFO, "%s %0*x file %02x settings[%zu]: %s", DesfireSelectWayToStr(selectway), (selectway == ISW6bAID) ? 6 : 4, id, fileid, buflen, sprint_hex(buf, buflen)); PrintAndLogEx(INFO, "%s file %02x settings[%zu]: %s", DesfireWayIDStr(selectway, id), fileid, buflen, sprint_hex(buf, buflen));
DesfirePrintFileSettings(buf, buflen); DesfirePrintFileSettings(buf, buflen);
@ -5121,7 +5121,8 @@ static int CmdHF14ADesLsFiles(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes lsfiles", CLIParserInit(&ctx, "hf mfdes lsfiles",
"Show file list. Master key needs to be provided or flag --no-auth set (depend on cards settings).", "Show file list. Master key needs to be provided or flag --no-auth set (depend on cards settings).",
"hf mfdes lsfiles --aid 123456 -> show file list for: app=123456 with defaults from `default` command"); "hf mfdes lsfiles --aid 123456 -> show file list for: app=123456 with defaults from `default` command"
"hf mfdes lsfiles --appisoid df01 --no-auth -> show files from desfire light");
void *argtable[] = { void *argtable[] = {
arg_param_begin, arg_param_begin,
@ -5144,12 +5145,13 @@ static int CmdHF14ADesLsFiles(const char *Cmd) {
bool APDULogging = arg_get_lit(ctx, 1); bool APDULogging = arg_get_lit(ctx, 1);
bool verbose = arg_get_lit(ctx, 2); bool verbose = arg_get_lit(ctx, 2);
bool noauth = arg_get_lit(ctx, 12); bool noauth = arg_get_lit(ctx, 13);
DesfireContext dctx; DesfireContext dctx;
int securechann = defaultSecureChannel; int securechann = defaultSecureChannel;
uint32_t appid = 0x000000; uint32_t id = 0x000000;
int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, &securechann, DCMMACed, &appid, NULL); DesfireISOSelectWay selectway = ISW6bAID;
int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, &securechann, DCMMACed, &id, &selectway);
if (res) { if (res) {
CLIParserFree(ctx); CLIParserFree(ctx);
return res; return res;
@ -5158,9 +5160,10 @@ static int CmdHF14ADesLsFiles(const char *Cmd) {
SetAPDULogging(APDULogging); SetAPDULogging(APDULogging);
CLIParserFree(ctx); CLIParserFree(ctx);
res = DesfireSelectAndAuthenticateEx(&dctx, securechann, appid, noauth, verbose); res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
DropField(); DropField();
PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res));
return res; return res;
} }
@ -5174,7 +5177,7 @@ static int CmdHF14ADesLsFiles(const char *Cmd) {
} }
if (filescount == 0) { if (filescount == 0) {
PrintAndLogEx(INFO, "There is no files in the application %06x", appid); PrintAndLogEx(INFO, "There is no files in the %s", DesfireWayIDStr(selectway, id));
DropField(); DropField();
return res; return res;
} }

View file

@ -312,6 +312,18 @@ const char *DesfireSelectWayToStr(DesfireISOSelectWay way) {
return ""; return "";
} }
char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id) {
static char str[200] = {0};
memset(str, 0, sizeof(str));
if (way == ISWMF || way == ISWDFName)
sprintf(str, "%s", DesfireSelectWayToStr(way));
else
sprintf(str, "%s %0*x", DesfireSelectWayToStr(way), (way == ISW6bAID) ? 6 : 4, id);
return str;
}
bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id) { bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id) {
switch (way) { switch (way) {
case ISW6bAID: case ISW6bAID:
@ -997,7 +1009,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel sec
return 202; return 202;
} }
if (verbose) if (verbose)
PrintAndLogEx(INFO, "%s %0*x is " _GREEN_("selected"), DesfireSelectWayToStr(way), way == ISW6bAID ? 6 : 4, id); PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id));
} }
if (selectfile) { if (selectfile) {
@ -1008,7 +1020,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext *dctx, DesfireSecureChannel sec
} }
if (verbose) if (verbose)
PrintAndLogEx(INFO, "Application %s %04x file iso id %04x is " _GREEN_("selected"), DesfireSelectWayToStr(way), id, isofileid); PrintAndLogEx(INFO, "Application %s file iso id %04x is " _GREEN_("selected"), DesfireWayIDStr(way, id), isofileid);
} }
if (!noauth) { if (!noauth) {
@ -1707,7 +1719,7 @@ int DesfireFillAppList(DesfireContext *dctx, PICCInfoS *PICCInfo, AppListS appLi
for (int i = 0; i < buflen; i++) { for (int i = 0; i < buflen; i++) {
int indx = AppListSearchAID(DesfireAIDByteToUint(&buf[i * 24 + 1]), appList, PICCInfo->appCount); int indx = AppListSearchAID(DesfireAIDByteToUint(&buf[i * 24 + 1]), appList, PICCInfo->appCount);
if (indx >= 0) { if (indx >= 0) {
appList[indx].appISONum = MemBeToUint2byte(&buf[i * 24 + 1 + 3]); appList[indx].appISONum = MemLeToUint2byte(&buf[i * 24 + 1 + 3]);
memcpy(appList[indx].appDFName, &buf[i * 24 + 1 + 5], strnlen((char *)&buf[i * 24 + 1 + 5], 16)); memcpy(appList[indx].appDFName, &buf[i * 24 + 1 + 5], strnlen((char *)&buf[i * 24 + 1 + 5], 16));
} }
} }
@ -2002,7 +2014,7 @@ int DesfireFillFileList(DesfireContext *dctx, FileListS FileList, size_t *filesc
if (buflen > 0) { if (buflen > 0) {
for (int i = 0; i < *filescount; i++) { for (int i = 0; i < *filescount; i++) {
if (FileList[i].fileSettings.fileType != 0x02 && FileList[i].fileSettings.fileType != 0x05) { if (FileList[i].fileSettings.fileType != 0x02 && FileList[i].fileSettings.fileType != 0x05) {
FileList[i].fileISONum = MemBeToUint2byte(&buf[isoindx * 2]); FileList[i].fileISONum = MemLeToUint2byte(&buf[isoindx * 2]);
isoindx++; isoindx++;
} }
} }
@ -2624,7 +2636,7 @@ void DesfirePrintCreateFileSettings(uint8_t filetype, uint8_t *data, size_t len)
size_t xlen = 1; size_t xlen = 1;
if (ftyperec->mayHaveISOfid) { if (ftyperec->mayHaveISOfid) {
if (isoidpresent) { if (isoidpresent) {
PrintAndLogEx(SUCCESS, "File ISO number : 0x%04x", MemBeToUint2byte(&data[xlen])); PrintAndLogEx(SUCCESS, "File ISO number : 0x%04x", MemLeToUint2byte(&data[xlen]));
xlen += 2; xlen += 2;
} else { } else {
PrintAndLogEx(SUCCESS, "File ISO number : n/a"); PrintAndLogEx(SUCCESS, "File ISO number : n/a");

View file

@ -171,6 +171,7 @@ void DesfirePrintMADAID(uint32_t appid, bool verbose);
int DesfireGetCardUID(DesfireContext *ctx); int DesfireGetCardUID(DesfireContext *ctx);
const char *DesfireSelectWayToStr(DesfireISOSelectWay way); const char *DesfireSelectWayToStr(DesfireISOSelectWay way);
char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id);
bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id); bool DesfireMFSelected(DesfireISOSelectWay way, uint32_t id);
int DesfireSelectEx(DesfireContext *ctx, bool fieldon, DesfireISOSelectWay way, uint32_t id, char *dfname); int DesfireSelectEx(DesfireContext *ctx, bool fieldon, DesfireISOSelectWay way, uint32_t id, char *dfname);
int DesfireSelect(DesfireContext *ctx, DesfireISOSelectWay way, uint32_t id, char *dfname); int DesfireSelect(DesfireContext *ctx, DesfireISOSelectWay way, uint32_t id, char *dfname);