From 5de4dd68e59b4a26d3339318e4b40d366492584e Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 22 Jun 2025 20:34:54 +0200 Subject: [PATCH] text --- armsrc/sam_picopass.c | 4 +-- client/src/cmdhficlass.c | 16 ++++++------ client/src/cmdhfmf.c | 15 +++++------ client/src/cmdhfmfdes.c | 6 ++--- client/src/cmdhfmfhard.c | 2 +- client/src/cmdparser.c | 2 +- client/src/cmdtrace.c | 2 +- client/src/fileutils.c | 19 +++++++------- client/src/fileutils.h | 2 +- client/src/mifare/mifarehost.c | 4 +-- client/src/pthread_spin_lock_shim.h | 40 ++++++++++++++--------------- common/commonutil.c | 37 ++++++++++++++++++++++++++ common/commonutil.h | 3 +++ doc/commands.json | 19 +++++++------- include/pm3_cmd.h | 2 ++ 15 files changed, 107 insertions(+), 66 deletions(-) diff --git a/armsrc/sam_picopass.c b/armsrc/sam_picopass.c index 1504f4104..dc794a7da 100644 --- a/armsrc/sam_picopass.c +++ b/armsrc/sam_picopass.c @@ -238,9 +238,9 @@ static int sam_send_request_iso15(const uint8_t *const request, const uint8_t re } } - if (sam_rx_buf[6] == 0x81 && sam_rx_buf[8] == 0x8a && sam_rx_buf[9] == 0x81 ){ //check if the response is an SNMP message + if (sam_rx_buf[6] == 0x81 && sam_rx_buf[8] == 0x8a && sam_rx_buf[9] == 0x81) { //check if the response is an SNMP message *response_len = sam_rx_buf[5 + 2] + 3; - }else{ //if not, use the old logic + } else { //if not, use the old logic *response_len = sam_rx_buf[5 + 1] + 2; } diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index 2bb6b3f16..ffa5d8f7d 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -5971,10 +5971,10 @@ static int CmdHFiClassSAM(const char *Cmd) { } if (snmp_data) { - uint8_t header[4] = {0xa0, cmdlen+2 , 0x94, cmdlen }; - memmove(data + 4, data, cmdlen+1); + uint8_t header[4] = {0xa0, cmdlen + 2, 0x94, cmdlen }; + memmove(data + 4, data, cmdlen + 1); data[0] = flags; - memcpy(data+1, header, 4); + memcpy(data + 1, header, 4); cmdlen += 4; } @@ -6049,21 +6049,21 @@ static int CmdHFiClassSAM(const char *Cmd) { } else { //if it is an error decode it if (memcmp(d, "\xBE\x07\x80\x01", 4) == 0) { //if it the string is 0xbe 0x07 0x80 0x01 the next byte will indicate the error code - PrintAndLogEx(ERR,_RED_("Sam Error Code: %02x"), d[4]); + PrintAndLogEx(ERR, _RED_("Sam Error Code: %02x"), d[4]); print_hex(d, resp.length); - }else if (match_with_wildcard(d, snmp_pattern, snmp_mask, 6)){ + } else if (match_with_wildcard(d, snmp_pattern, snmp_mask, 6)) { is_snmp = true; PrintAndLogEx(SUCCESS, _YELLOW_("[samSNMPMessageResponse] ")"%s", sprint_hex(d + 6, resp.length - 6)); - }else if (match_with_wildcard(d,ok_pattern, ok_mask, 3)){ + } else if (match_with_wildcard(d, ok_pattern, ok_mask, 3)) { PrintAndLogEx(SUCCESS, _YELLOW_("[samResponseAcknowledge] ")"%s", sprint_hex(d + 4, resp.length - 4)); - }else{ + } else { print_hex(d, resp.length); } } if (decodeTLV && is_snmp == false) { asn1_print(d, d[1] + 2, " "); - } else if (decodeTLV && is_snmp){ + } else if (decodeTLV && is_snmp) { asn1_print(d + 6, resp.length - 6, " "); } diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 269a06fab..038180ff1 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -433,9 +433,10 @@ static int mf_read_uid(uint8_t *uid, int *uidlen, int *nxptype) { } static char *GenerateFilename(const char *prefix, const char *suffix) { - if (! IfPm3Iso14443a()) { + if (IfPm3Iso14443a() == false) { return NULL; } + uint8_t uid[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int uidlen = 0; char *fptr = calloc(sizeof(char) * (strlen(prefix) + strlen(suffix)) + sizeof(uid) * 2 + 1, sizeof(uint8_t)); @@ -924,7 +925,7 @@ static int mfc_read_tag(iso14a_card_select_t *card, uint8_t *carddata, uint8_t n size_t alen = 0, blen = 0; uint8_t *keyA = NULL, *keyB = NULL; - if (loadFileBinaryKey(keyfn, "", (void **)&keyA, (void **)&keyB, &alen, &blen) != PM3_SUCCESS) { + if (loadFileBinaryKey(keyfn, "", (void **)&keyA, (void **)&keyB, &alen, &blen, true) != PM3_SUCCESS) { free(fptr); return PM3_ESOFT; } @@ -1565,7 +1566,7 @@ static int FastDumpWithEcFill(uint8_t numsectors) { } if (resp.status != PM3_SUCCESS) { - PrintAndLogEx(FAILED, "fast dump reported back failure w KEY A, swapping to KEY B"); + PrintAndLogEx(FAILED, "fast dump reported back failure w KEY A. Swapping to KEY B"); // ecfill key B payload.keytype = MF_KEY_B; @@ -1823,12 +1824,10 @@ static int CmdHF14AMfRestore(const char *Cmd) { // size_t alen = 0, blen = 0; uint8_t *keyA, *keyB; - if (loadFileBinaryKey(keyfilename, "", (void **)&keyA, (void **)&keyB, &alen, &blen) != PM3_SUCCESS) { + if (loadFileBinaryKey(keyfilename, "", (void **)&keyA, (void **)&keyB, &alen, &blen, true) != PM3_SUCCESS) { return PM3_ESOFT; } - PrintAndLogEx(INFO, "Using key file `" _YELLOW_("%s") "`", keyfilename); - // try reading card uid and create filename if (datafnlen == 0) { char *fptr = GenerateFilename("hf-mf-", "-dump.bin"); @@ -7311,12 +7310,10 @@ int CmdHFMFNDEFFormat(const char *Cmd) { // size_t alen = 0, blen = 0; uint8_t *tmpA, *tmpB; - if (loadFileBinaryKey(keyFilename, "", (void **)&tmpA, (void **)&tmpB, &alen, &blen) != PM3_SUCCESS) { + if (loadFileBinaryKey(keyFilename, "", (void **)&tmpA, (void **)&tmpB, &alen, &blen, true) != PM3_SUCCESS) { goto skipfile; } - PrintAndLogEx(INFO, "Using `" _YELLOW_("%s") "`", keyFilename); - for (int i = 0; i < numSectors; i++) { memcpy(keyA[i], tmpA + (i * MIFARE_KEY_SIZE), MIFARE_KEY_SIZE); memcpy(keyB[i], tmpB + (i * MIFARE_KEY_SIZE), MIFARE_KEY_SIZE); diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index a6f4f934c..341ced612 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1059,7 +1059,7 @@ static int AuthCheckDesfire(DesfireContext_t *dctx, DesfireSetKeyNoClear(dctx, keyno, T_3DES, aeskeyList[curkey]); res = DesfireAuthenticate(dctx, secureChannel, false); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "AID 0x%06X, Found 2TDEA Key %02u : " _GREEN_("%s"), curaid, keyno, sprint_hex(aeskeyList[curkey], 16)); + PrintAndLogEx(SUCCESS, "AID 0x%06X, Found 2TDEA Key %02u... " _GREEN_("%s"), curaid, keyno, sprint_hex_inrow(aeskeyList[curkey], 16)); foundKeys[1][keyno][0] = 0x01; *result = true; memcpy(&foundKeys[1][keyno][1], aeskeyList[curkey], 16); @@ -1091,7 +1091,7 @@ static int AuthCheckDesfire(DesfireContext_t *dctx, DesfireSetKeyNoClear(dctx, keyno, T_AES, aeskeyList[curkey]); res = DesfireAuthenticate(dctx, secureChannel, false); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "AID 0x%06X, Found AES Key %02u : " _GREEN_("%s"), curaid, keyno, sprint_hex(aeskeyList[curkey], 16)); + PrintAndLogEx(SUCCESS, "AID 0x%06X, Found AES Key %02u... " _GREEN_("%s"), curaid, keyno, sprint_hex_inrow(aeskeyList[curkey], 16)); foundKeys[2][keyno][0] = 0x01; *result = true; memcpy(&foundKeys[2][keyno][1], aeskeyList[curkey], 16); @@ -1123,7 +1123,7 @@ static int AuthCheckDesfire(DesfireContext_t *dctx, DesfireSetKeyNoClear(dctx, keyno, T_3K3DES, k3kkeyList[curkey]); res = DesfireAuthenticate(dctx, secureChannel, false); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "AID 0x%06X, Found 3TDEA Key %02u : " _GREEN_("%s"), curaid, keyno, sprint_hex(k3kkeyList[curkey], 24)); + PrintAndLogEx(SUCCESS, "AID 0x%06X, Found 3TDEA Key %02u... " _GREEN_("%s"), curaid, keyno, sprint_hex_inrow(k3kkeyList[curkey], 24)); foundKeys[3][keyno][0] = 0x01; *result = true; memcpy(&foundKeys[3][keyno][1], k3kkeyList[curkey], 16); diff --git a/client/src/cmdhfmfhard.c b/client/src/cmdhfmfhard.c index a04db22ec..885162437 100644 --- a/client/src/cmdhfmfhard.c +++ b/client/src/cmdhfmfhard.c @@ -508,7 +508,7 @@ static void init_bitflip_bitarrays(void) { { char progress_text[100]; memset(progress_text, 0, sizeof(progress_text)); - snprintf(progress_text, sizeof(progress_text), "Loaded " _YELLOW_("%u") " RAW / " _YELLOW_("%u") " LZ4 / " _YELLOW_("%u") " BZ2 in %"PRIu64" ms" + snprintf(progress_text, sizeof(progress_text), "Loaded " _YELLOW_("%u") " RAW / " _YELLOW_("%u") " LZ4 / " _YELLOW_("%u") " BZ2 in %4"PRIu64" ms" , nraw , nlz4 , nbz2 diff --git a/client/src/cmdparser.c b/client/src/cmdparser.c index d170c6828..fc628b947 100644 --- a/client/src/cmdparser.c +++ b/client/src/cmdparser.c @@ -231,7 +231,7 @@ static int execute_system_command(const char *command) { int ret; - #if defined(_WIN32) +#if defined(_WIN32) char wrapped_command[255]; strncat(wrapped_command, "cmd /C \"", 9); strncat(wrapped_command, command, strlen(command)); diff --git a/client/src/cmdtrace.c b/client/src/cmdtrace.c index 0e83bf128..0f3242345 100644 --- a/client/src/cmdtrace.c +++ b/client/src/cmdtrace.c @@ -181,7 +181,7 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t } */ - // extract MFU-C KEY when written. + // extract UL-C KEY when written. switch (frame[0]) { case MIFARE_ULC_AUTH_1: { diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 3bea9f3c4..f4c76f049 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -2353,7 +2353,7 @@ int loadFileDICTIONARY_safe_ex(const char *preferredName, const char *suffix, vo keylen = 6; } - size_t block_size = 10 * keylen; + size_t block_size = 1000 * keylen; // double up since its chars keylen <<= 1; @@ -2428,10 +2428,9 @@ int loadFileDICTIONARY_safe_ex(const char *preferredName, const char *suffix, vo continue; } - if (hex_to_bytes( - line, - (uint8_t *)*pdata + (*keycnt * (keylen >> 1)), - keylen >> 1) != (keylen >> 1)) { + int ret = hex_to_bytes(line, (uint8_t *)*pdata + (*keycnt * (keylen >> 1)), keylen >> 1); + if (ret != (keylen >> 1)) { + PrintAndLogEx(INFO, "hex to bytes wrong %i", ret); continue; } @@ -2450,16 +2449,16 @@ out: return retval; } -int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen) { +int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen, bool verbose) { char *path; int res = searchFile(&path, RESOURCES_SUBDIR, preferredName, suffix, false); if (res != PM3_SUCCESS) { - return PM3_EFILE; + return PM3_ENOFILE; } FILE *f = fopen(path, "rb"); - if (!f) { + if (f == NULL) { PrintAndLogEx(WARNING, "file not found or locked `" _YELLOW_("%s") "`", path); free(path); return PM3_EFILE; @@ -2502,7 +2501,9 @@ int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya *blen = fread(*keyb, 1, fsize, f); fclose(f); - PrintAndLogEx(SUCCESS, "Loaded binary key file `" _YELLOW_("%s") "`", path); + if (verbose) { + PrintAndLogEx(SUCCESS, "Loaded binary key file `" _YELLOW_("%s") "`", path); + } free(path); return PM3_SUCCESS; } diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 3e8b022c1..cc6d9ee14 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -307,7 +307,7 @@ int loadFileDICTIONARY_safe_ex(const char *preferredName, const char *suffix, vo */ int loadFileXML_safe(const char *preferredName, const char *suffix, void **pdata, size_t *datalen); -int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen); +int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen, bool verbose); /** * @brief Utility function to check and convert plain mfu dump format to new mfu binary format. diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index f6fc0b07a..bdac928d5 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -64,7 +64,7 @@ int mf_dark_side(uint8_t blockno, uint8_t key_type, uint64_t *key) { //flush queue while (kbd_enter_pressed()) { SendCommandNG(CMD_BREAK_LOOP, NULL, 0); - PrintAndLogEx(WARNING, "Aborted via keyboard"); + PrintAndLogEx(WARNING, "aborted via keyboard"); return PM3_EOPABORTED; } @@ -90,7 +90,7 @@ int mf_dark_side(uint8_t blockno, uint8_t key_type, uint64_t *key) { //TODO: Not really stopping the command in time. if (kbd_enter_pressed()) { SendCommandNG(CMD_BREAK_LOOP, NULL, 0); - PrintAndLogEx(WARNING, "\nAborted via keyboard"); + PrintAndLogEx(WARNING, "\naborted via keyboard"); return PM3_EOPABORTED; } diff --git a/client/src/pthread_spin_lock_shim.h b/client/src/pthread_spin_lock_shim.h index 131598b02..243771a6c 100644 --- a/client/src/pthread_spin_lock_shim.h +++ b/client/src/pthread_spin_lock_shim.h @@ -18,38 +18,38 @@ typedef int pthread_spinlock_t; #endif static inline int pthread_spin_init(pthread_spinlock_t *lock, int pshared) { - __asm__ __volatile__ ("" ::: "memory"); - *lock = 0; - return 0; + __asm__ __volatile__("" ::: "memory"); + *lock = 0; + return 0; } static inline int pthread_spin_destroy(pthread_spinlock_t *lock) { - return 0; + return 0; } static inline int pthread_spin_lock(pthread_spinlock_t *lock) { - while (1) { - int i; - for (i=0; i < 10000; i++) { - if (__sync_bool_compare_and_swap(lock, 0, 1)) { - return 0; - } - } - sched_yield(); - } + while (1) { + int i; + for (i = 0; i < 10000; i++) { + if (__sync_bool_compare_and_swap(lock, 0, 1)) { + return 0; + } + } + sched_yield(); + } } static inline int pthread_spin_trylock(pthread_spinlock_t *lock) { - if (__sync_bool_compare_and_swap(lock, 0, 1)) { - return 0; - } - return 16; // EBUSY; + if (__sync_bool_compare_and_swap(lock, 0, 1)) { + return 0; + } + return 16; // EBUSY; } static inline int pthread_spin_unlock(pthread_spinlock_t *lock) { - __asm__ __volatile__ ("" ::: "memory"); - *lock = 0; - return 0; + __asm__ __volatile__("" ::: "memory"); + *lock = 0; + return 0; } #endif diff --git a/common/commonutil.c b/common/commonutil.c index 48ce64bfa..ff0782514 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -450,6 +450,43 @@ void lslx(uint8_t *d, size_t n, uint8_t shifts) { } } +// right shift an array of length one bit +void rsl(uint8_t *d, size_t n) { + + uint8_t carry = 0; + + for (size_t i = 0; i < n; i++) { + + // Save the LSB before shifting + uint8_t new_carry = d[i] & 0x1; + + // Shift current byte right and incorporate previous carry + d[i] = (d[i] >> 1) | (carry ? 0x80 : 0); + + // Update carry for next byte + carry = new_carry; + } +} + +void rslx(uint8_t *d, size_t n, uint8_t shifts) { + + uint8_t carry = 0; + for (uint8_t j = 0; j < shifts; j++) { + + for (size_t i = 0; i < n; i++) { + + // Save the LSB before shifting + uint8_t new_carry = d[i] & 0x1; + + // Shift current byte right and incorporate previous carry + d[i] = (d[i] >> 1) | (carry ? 0x80 : 0); + + // Update carry for next byte + carry = new_carry; + } + } +} + // BSWAP24 of array[3] uint32_t le24toh(const uint8_t data[3]) { diff --git a/common/commonutil.h b/common/commonutil.h index f963805bb..00949b851 100644 --- a/common/commonutil.h +++ b/common/commonutil.h @@ -135,6 +135,9 @@ void xor(uint8_t *dest, const uint8_t *src, size_t n); void lsl(uint8_t *d, size_t n); void lslx(uint8_t *d, size_t n, uint8_t shifts); +void rsl(uint8_t *d, size_t n); +void rslx(uint8_t *d, size_t n, uint8_t shifts); + uint32_t le24toh(const uint8_t data[3]); void htole24(uint32_t val, uint8_t data[3]); diff --git a/doc/commands.json b/doc/commands.json index 034e055b1..4ef2a2aa0 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -3686,9 +3686,10 @@ "-p, --prevent fake epurse update", "--shallow shallow mod", "-d, --data DER encoded command to send to SAM", + "-s, --snmp data is in snmp format without headers", "--info get SAM infos (version, serial number)" ], - "usage": "hf iclass sam [-hvkntp] [--break] [--shallow] [-d ]... [--info]" + "usage": "hf iclass sam [-hvkntps] [--break] [--shallow] [-d ]... [--info]" }, "hf iclass sim": { "command": "hf iclass sim", @@ -12123,7 +12124,7 @@ }, "mem load": { "command": "mem load", - "description": "Loads binary file into flash memory on device Warning: mem area to be written must have been wiped first ( dictionaries are serviced as files in spiffs so no wipe is needed )", + "description": "Loads binary file into flash memory on device Warning! - mem area to be written must have been wiped first OBS! - dictionaries are serviced as files in spiffs so no wipe is needed", "notes": [ "mem load -f myfile -> upload file myfile values at default offset 0", "mem load -f myfile -o 1024 -> upload file myfile values at offset 1024", @@ -12136,14 +12137,14 @@ "options": [ "-h, --help This help", "-o, --offset offset in memory", - "-m, --mifare, --mfc upload 6 bytes keys (mifare key dictionary)", - "-i, --iclass upload 8 bytes keys (iClass key dictionary)", - "-t, --t55xx upload 4 bytes keys (password dictionary)", - "--ulc upload 16 bytes keys (mifare UL-C key dictionary)", - "--ulaes upload 16 bytes keys (mifare UL-AES key dictionary)", + "-m, --mfc upload 6 bytes keys (MIFARE Classic dictionary)", + "-i, --iclass upload 8 bytes keys (iClass dictionary)", + "-t, --t55xx upload 4 bytes keys (T55xx dictionary)", + "--ulc upload 16 bytes keys (MIFARE UL-C dictionary)", + "--aes upload 16 bytes keys (MIFARE UL-AES dictionary)", "-f, --file file name" ], - "usage": "mem load [-hmit] [-o ] [--ulc] [--ulaes] -f " + "usage": "mem load [-hmit] [-o ] [--ulc] [--aes] -f " }, "mem spiffs check": { "command": "mem spiffs check", @@ -13374,6 +13375,6 @@ "metadata": { "commands_extracted": 768, "extracted_by": "PM3Help2JSON v1.00", - "extracted_on": "2025-06-19T15:01:51" + "extracted_on": "2025-06-22T18:30:29" } } diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index bb9123d7d..cbce45a24 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -964,6 +964,8 @@ typedef struct { // Cryptographic error client/pm3: cryptographic operation failed #define PM3_ECRYPTO -29 +// File error client: error related to file does not exist in search paths +#define PM3_ENOFILE -30 // No data client/pm3: no data available, no host frame available (not really an error) #define PM3_ENODATA -98 // Quit program client: reserved, order to quit the program