fixed type confusing error when trying to load none supported .picopass files. Thanks to Jump for the suggested fixes

This commit is contained in:
iceman1001 2024-07-12 15:06:08 +02:00
commit 3461b6f803
3 changed files with 64 additions and 34 deletions

View file

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
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... 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...
## [unreleased][unreleased] ## [unreleased][unreleased]
- fixed breaking of client when trying to load a non-supported .picopass file (@iceman100) Thanks to Jump for suggested fixes!
- Changed `mf_nonce_brute` tool to handle the odd case of multiple key candidates (@iceman1001) - Changed `mf_nonce_brute` tool to handle the odd case of multiple key candidates (@iceman1001)
- Fixed a bad memory erase (@iceman1001) - Fixed a bad memory erase (@iceman1001)
- Fixed BT serial comms (@iceman1001) - Fixed BT serial comms (@iceman1001)

View file

@ -2162,14 +2162,18 @@ int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, u
int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, uint8_t keylen, uint32_t *keycnt, int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, uint8_t keylen, uint32_t *keycnt,
size_t startFilePosition, size_t *endFilePosition, bool verbose) { size_t startFilePosition, size_t *endFilePosition, bool verbose) {
if (data == NULL) return PM3_EINVARG; if (data == NULL) {
return PM3_EINVARG;
}
if (endFilePosition) if (endFilePosition) {
*endFilePosition = 0; *endFilePosition = 0;
}
char *path; char *path;
if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic", false) != PM3_SUCCESS) if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic", false) != PM3_SUCCESS) {
return PM3_EFILE; return PM3_EFILE;
}
// double up since its chars // double up since its chars
keylen <<= 1; keylen <<= 1;
@ -2201,8 +2205,9 @@ int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatale
long filepos = ftell(f); long filepos = ftell(f);
if (!fgets(line, sizeof(line), f)) { if (!fgets(line, sizeof(line), f)) {
if (endFilePosition) if (endFilePosition) {
*endFilePosition = 0; *endFilePosition = 0;
}
break; break;
} }
@ -2210,39 +2215,50 @@ int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatale
line[keylen] = 0; line[keylen] = 0;
// smaller keys than expected is skipped // smaller keys than expected is skipped
if (strlen(line) < keylen) if (strlen(line) < keylen) {
continue; continue;
}
// The line start with # is comment, skip // The line start with # is comment, skip
if (line[0] == '#') if (line[0] == '#') {
continue; continue;
}
if (!CheckStringIsHEXValue(line)) if (!CheckStringIsHEXValue(line)) {
continue; continue;
}
// cant store more data // cant store more data
if (maxdatalen && (counter + (keylen >> 1) > maxdatalen)) { if (maxdatalen && (counter + (keylen >> 1) > maxdatalen)) {
retval = 1; retval = 1;
if (endFilePosition) if (endFilePosition) {
*endFilePosition = filepos; *endFilePosition = filepos;
}
break; break;
} }
if (hex_to_bytes(line, udata + counter, keylen >> 1) != (keylen >> 1)) if (hex_to_bytes(line, udata + counter, keylen >> 1) != (keylen >> 1)) {
continue; continue;
}
vkeycnt++; vkeycnt++;
memset(line, 0, sizeof(line)); memset(line, 0, sizeof(line));
counter += (keylen >> 1); counter += (keylen >> 1);
} }
fclose(f);
if (verbose)
PrintAndLogEx(SUCCESS, "Loaded " _GREEN_("%2d") " keys from dictionary file `" _YELLOW_("%s") "`", vkeycnt, path);
if (datalen) fclose(f);
if (verbose) {
PrintAndLogEx(SUCCESS, "Loaded " _GREEN_("%2d") " keys from dictionary file `" _YELLOW_("%s") "`", vkeycnt, path);
}
if (datalen) {
*datalen = counter; *datalen = counter;
if (keycnt) }
if (keycnt) {
*keycnt = vkeycnt; *keycnt = vkeycnt;
}
out: out:
free(path); free(path);
return retval; return retval;
@ -2253,8 +2269,9 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
int retval = PM3_SUCCESS; int retval = PM3_SUCCESS;
char *path; char *path;
if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic", false) != PM3_SUCCESS) if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic", false) != PM3_SUCCESS) {
return PM3_EFILE; return PM3_EFILE;
}
// t5577 == 4bytes // t5577 == 4bytes
// mifare == 6 bytes // mifare == 6 bytes
@ -2311,15 +2328,18 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
line[keylen] = 0; line[keylen] = 0;
// smaller keys than expected is skipped // smaller keys than expected is skipped
if (strlen(line) < keylen) if (strlen(line) < keylen) {
continue; continue;
}
// The line start with # is comment, skip // The line start with # is comment, skip
if (line[0] == '#') if (line[0] == '#') {
continue; continue;
}
if (!CheckStringIsHEXValue(line)) if (!CheckStringIsHEXValue(line)) {
continue; continue;
}
uint64_t key = strtoull(line, NULL, 16); uint64_t key = strtoull(line, NULL, 16);
@ -2330,6 +2350,7 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
memset(line, 0, sizeof(line)); memset(line, 0, sizeof(line));
} }
fclose(f); fclose(f);
PrintAndLogEx(SUCCESS, "Loaded " _GREEN_("%2d") " keys from dictionary file `" _YELLOW_("%s") "`", *keycnt, path); PrintAndLogEx(SUCCESS, "Loaded " _GREEN_("%2d") " keys from dictionary file `" _YELLOW_("%s") "`", *keycnt, path);
out: out:
@ -2453,7 +2474,9 @@ mfu_df_e detect_mfu_dump_format(uint8_t **dump, bool verbose) {
return retval; return retval;
} }
nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose) { int detect_nfc_dump_format(const char *preferredName, nfc_df_e *dump_type, bool verbose) {
*dump_type = NFC_DF_UNKNOWN;
char *path; char *path;
int res = searchFile(&path, RESOURCES_SUBDIR, preferredName, "", false); int res = searchFile(&path, RESOURCES_SUBDIR, preferredName, "", false);
@ -2469,8 +2492,6 @@ nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose) {
} }
free(path); free(path);
nfc_df_e retval = NFC_DF_UNKNOWN;
char line[256]; char line[256];
memset(line, 0, sizeof(line)); memset(line, 0, sizeof(line));
@ -2492,31 +2513,31 @@ nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose) {
str_lower(line); str_lower(line);
if (str_startswith(line, "device type: ntag")) { if (str_startswith(line, "device type: ntag")) {
retval = NFC_DF_MFU; *dump_type = NFC_DF_MFU;
break; break;
} }
if (str_startswith(line, "device type: mifare classic")) { if (str_startswith(line, "device type: mifare classic")) {
retval = NFC_DF_MFC; *dump_type = NFC_DF_MFC;
break; break;
} }
if (str_startswith(line, "device type: mifare desfire")) { if (str_startswith(line, "device type: mifare desfire")) {
retval = NFC_DF_MFDES; *dump_type = NFC_DF_MFDES;
break; break;
} }
if (str_startswith(line, "device type: iso14443-3a")) { if (str_startswith(line, "device type: iso14443-3a")) {
retval = NFC_DF_14_3A; *dump_type = NFC_DF_14_3A;
break; break;
} }
if (str_startswith(line, "device type: iso14443-3b")) { if (str_startswith(line, "device type: iso14443-3b")) {
retval = NFC_DF_14_3B; *dump_type = NFC_DF_14_3B;
break; break;
} }
if (str_startswith(line, "device type: iso14443-4a")) { if (str_startswith(line, "device type: iso14443-4a")) {
retval = NFC_DF_14_4A; *dump_type = NFC_DF_14_4A;
break; break;
} }
if (str_startswith(line, "filetype: flipper picopass device")) { if (str_startswith(line, "filetype: flipper picopass device")) {
retval = NFC_DF_PICOPASS; *dump_type = NFC_DF_PICOPASS;
break; break;
} }
@ -2524,7 +2545,7 @@ nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose) {
fclose(f); fclose(f);
if (verbose) { if (verbose) {
switch (retval) { switch (*dump_type) {
case NFC_DF_MFU: case NFC_DF_MFU:
PrintAndLogEx(INFO, "Detected MIFARE Ultralight / NTAG based dump format"); PrintAndLogEx(INFO, "Detected MIFARE Ultralight / NTAG based dump format");
break; break;
@ -2551,7 +2572,7 @@ nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose) {
break; break;
} }
} }
return retval; return PM3_SUCCESS;
} }
static int convert_plain_mfu_dump(uint8_t **dump, size_t *dumplen, bool verbose) { static int convert_plain_mfu_dump(uint8_t **dump, size_t *dumplen, bool verbose) {
@ -2996,15 +3017,20 @@ int pm3_load_dump(const char *fn, void **pdump, size_t *dumplen, size_t maxdumpl
break; break;
} }
case FLIPPER: { case FLIPPER: {
nfc_df_e foo = detect_nfc_dump_format(fn, true); nfc_df_e dumptype;
if (foo == NFC_DF_MFC || foo == NFC_DF_MFU || foo == NFC_DF_PICOPASS) { res = detect_nfc_dump_format(fn, &dumptype, true);
if (res != SUCCESS) {
break;
}
if (dumptype == NFC_DF_MFC || dumptype == NFC_DF_MFU || dumptype == NFC_DF_PICOPASS) {
*pdump = calloc(maxdumplen, sizeof(uint8_t)); *pdump = calloc(maxdumplen, sizeof(uint8_t));
if (*pdump == NULL) { if (*pdump == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC; return PM3_EMALLOC;
} }
res = loadFileNFC_safe(fn, *pdump, maxdumplen, dumplen, foo); res = loadFileNFC_safe(fn, *pdump, maxdumplen, dumplen, dumptype);
if (res == PM3_SUCCESS) { if (res == PM3_SUCCESS) {
return res; return res;
} }
@ -3016,6 +3042,9 @@ int pm3_load_dump(const char *fn, void **pdump, size_t *dumplen, size_t maxdumpl
} else if (res == PM3_EMALLOC) { } else if (res == PM3_EMALLOC) {
PrintAndLogEx(WARNING, "wrong size of allocated memory. Check your parameters"); PrintAndLogEx(WARNING, "wrong size of allocated memory. Check your parameters");
} }
} else {
// unknown dump file type
res = PM3_ESOFT;
} }
break; break;
} }

View file

@ -289,7 +289,7 @@ int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya
*/ */
int convert_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose); int convert_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose);
mfu_df_e detect_mfu_dump_format(uint8_t **dump, bool verbose); mfu_df_e detect_mfu_dump_format(uint8_t **dump, bool verbose);
nfc_df_e detect_nfc_dump_format(const char *preferredName, bool verbose); int detect_nfc_dump_format(const char *preferredName, nfc_df_e *dump_type, bool verbose);
int searchAndList(const char *pm3dir, const char *ext); int searchAndList(const char *pm3dir, const char *ext);
int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix, bool silent); int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix, bool silent);