From fef399044428a047c9f998e408770e68610a4cbd Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Thu, 21 Apr 2016 15:44:24 +0800 Subject: [PATCH 1/6] support parity attack for any block --- armsrc/appmain.c | 2 +- armsrc/apps.h | 2 +- armsrc/iso14443a.c | 6 ++++-- client/cmdhfmf.c | 19 ++++++++++++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 475b1c1b..4807bc1b 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1110,7 +1110,7 @@ void UsbPacketReceived(uint8_t *packet, int len) break; case CMD_READER_MIFARE: - ReaderMifare(c->arg[0]); + ReaderMifare(c->arg[0], c->arg[1], c->arg[2]); break; case CMD_MIFARE_READBL: MifareReadBlock(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes); diff --git a/armsrc/apps.h b/armsrc/apps.h index 4d4c8083..bef48d3e 100644 --- a/armsrc/apps.h +++ b/armsrc/apps.h @@ -113,7 +113,7 @@ void EPA_PACE_Collect_Nonce(UsbCommand * c); void EPA_PACE_Replay(UsbCommand *c); // mifarecmd.h -void ReaderMifare(bool first_try); +void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType); int32_t dist_nt(uint32_t nt1, uint32_t nt2); void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *data); void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain); diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 27574dad..5a602c79 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -2022,16 +2022,18 @@ int32_t dist_nt(uint32_t nt1, uint32_t nt2) { // Cloning MiFare Classic Rail and Building Passes, Anywhere, Anytime" // (article by Nicolas T. Courtois, 2009) //----------------------------------------------------------------------------- -void ReaderMifare(bool first_try) +void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) { // Mifare AUTH - uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b }; + uint8_t mf_auth[] = { 0x60 + (keyType & 0x01), blockNo ,0x00,0x00 }; uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; static uint8_t mf_nr_ar3; uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE]; uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE]; + AppendCrc14443a(mf_auth, 2); + if (first_try) { iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); } diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 48e78b1c..181c9316 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -19,7 +19,24 @@ int CmdHF14AMifare(const char *Cmd) uint64_t par_list = 0, ks_list = 0, r_key = 0; int16_t isOK = 0; - UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}}; + uint8_t blockNo = 0, keyType = 0; + char cmdp = 0x00; + + if (strlen(Cmd)<3) { + PrintAndLog("Usage: hf mf mifare "); + PrintAndLog(" sample: hf mf mi 0 A"); + return 0; + } + + blockNo = param_get8(Cmd, 0); + cmdp = param_getchar(Cmd, 1); + if (cmdp == 0x00) { + PrintAndLog("Key type must be A or B"); + return 1; + } + if (cmdp != 'A' && cmdp != 'a') keyType = 1; + + UsbCommand c = {CMD_READER_MIFARE, {true, blockNo, keyType}}; // message printf("-------------------------------------------------------------------------\n"); From 13b71e58fddf20c5d42b8f0af1d72c795139b86f Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Thu, 21 Apr 2016 15:50:26 +0800 Subject: [PATCH 2/6] add support for a new kind Magic Chinese UID changable Mifare Classic card --- armsrc/mifarecmd.c | 24 +++++++----------------- armsrc/mifareutil.c | 4 +++- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index a3d6609d..36a6e8f5 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -1059,10 +1059,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai break; }; - if(mifare_classic_halt(NULL, cuid)) { - if (MF_DBGLEVEL >= 1) Dbprintf("Halt error"); - break; - }; + mifare_classic_halt(NULL, cuid); }; // reset chip @@ -1079,10 +1076,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai break; }; - if(mifare_classic_halt(NULL, cuid)) { - if (MF_DBGLEVEL >= 1) Dbprintf("Halt error"); - break; - }; + mifare_classic_halt(NULL, cuid); }; // write block @@ -1115,10 +1109,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai }; if (workFlags & 0x04) { - if (mifare_classic_halt(NULL, cuid)) { - if (MF_DBGLEVEL >= 1) Dbprintf("Halt error"); - break; - }; + mifare_classic_halt(NULL, cuid); } isOK = 1; @@ -1192,10 +1183,7 @@ void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai memcpy(data, receivedAnswer, 18); if (workFlags & 0x04) { - if (mifare_classic_halt(NULL, cuid)) { - if (MF_DBGLEVEL >= 1) Dbprintf("Halt error"); - break; - }; + mifare_classic_halt(NULL, cuid); } isOK = 1; @@ -1222,6 +1210,7 @@ void MifareCIdent(){ // card commands uint8_t wupC1[] = { 0x40 }; uint8_t wupC2[] = { 0x43 }; + uint8_t halt_ret = 0; // variables byte_t isOK = 1; @@ -1239,7 +1228,8 @@ void MifareCIdent(){ isOK = 0; }; - if (mifare_classic_halt(NULL, 0)) { + halt_ret = mifare_classic_halt(NULL, 0); + if (halt_ret && halt_ret != 4) { isOK = 0; }; diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index 8ef364c2..fb54c025 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -511,7 +511,9 @@ int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL); if (len != 0) { if (MF_DBGLEVEL >= MF_DBG_ERROR) - Dbprintf("halt error. response len: %x", len); + Dbprintf("halt error. response len: %x data:%02X %02X %02X %02X", len, receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3]); + if (len == 1 && receivedAnswer[0]==0x04) + return 4; return 1; } From b75f4c6f102b1820a7205bf25d44a98afbd36f35 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Thu, 21 Apr 2016 15:51:30 +0800 Subject: [PATCH 3/6] some Mifare/CPU hybird card won't response to Mifare command any more if send RATS command --- armsrc/appmain.c | 2 +- armsrc/epa.c | 2 +- armsrc/iso14443a.c | 9 +++++---- armsrc/iso14443a.h | 2 +- armsrc/mifarecmd.c | 30 +++++++++++++++--------------- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 4807bc1b..fde20547 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -429,7 +429,7 @@ void StandAloneMode14a() SpinDelay(300); } } - if (!iso14443a_select_card(uid, &hi14a_card[selected], &cuid)) + if (!iso14443a_select_card(uid, &hi14a_card[selected], &cuid, 1)) continue; else { diff --git a/armsrc/epa.c b/armsrc/epa.c index 50c7d878..e22a5a88 100644 --- a/armsrc/epa.c +++ b/armsrc/epa.c @@ -526,7 +526,7 @@ int EPA_Setup() // power up the field iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); // select the card - return_code = iso14443a_select_card(uid, &card_select_info, NULL); + return_code = iso14443a_select_card(uid, &card_select_info, NULL, 0); if (return_code == 1) { // send the PPS request ReaderTransmit((uint8_t *)pps, sizeof(pps), NULL); diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 5a602c79..f9a33158 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -1698,7 +1698,7 @@ int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity) /* performs iso14443a anticollision procedure * fills the uid pointer unless NULL * fills resp_data unless NULL */ -int iso14443a_select_card(byte_t *uid_ptr, iso14a_card_select_t *p_hi14a_card, uint32_t *cuid_ptr) { +int iso14443a_select_card(byte_t *uid_ptr, iso14a_card_select_t *p_hi14a_card, uint32_t *cuid_ptr, uint8_t no_rats) { uint8_t wupa[] = { 0x52 }; // 0x26 - REQA 0x52 - WAKE-UP uint8_t sel_all[] = { 0x93,0x20 }; uint8_t sel_uid[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; @@ -1821,7 +1821,8 @@ int iso14443a_select_card(byte_t *uid_ptr, iso14a_card_select_t *p_hi14a_card, u } // non iso14443a compliant tag - if( (sak & 0x20) == 0) return 2; + // some Mifare/CPU hybird card won't response to Mifare command any more if send RATS command. + if( no_rats || (sak & 0x20) == 0) return 2; // Request for answer to select AppendCrc14443a(rats, 2); @@ -1926,7 +1927,7 @@ void ReaderIso14443a(UsbCommand *c) iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); if(!(param & ISO14A_NO_SELECT)) { iso14a_card_select_t *card = (iso14a_card_select_t*)buf; - arg0 = iso14443a_select_card(NULL,card,NULL); + arg0 = iso14443a_select_card(NULL,card,NULL,0); cmd_send(CMD_ACK,arg0,card->uidlen,0,buf,sizeof(iso14a_card_select_t)); } } @@ -2125,7 +2126,7 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) SpinDelay(100); } - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Mifare: Can't select card"); continue; } diff --git a/armsrc/iso14443a.h b/armsrc/iso14443a.h index ec99ab99..5bcb1fbf 100644 --- a/armsrc/iso14443a.h +++ b/armsrc/iso14443a.h @@ -82,7 +82,7 @@ extern int ReaderReceive(uint8_t *receivedAnswer, uint8_t *par); extern void iso14443a_setup(uint8_t fpga_minor_mode); extern int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, void *data); -extern int iso14443a_select_card(uint8_t *uid_ptr, iso14a_card_select_t *resp_data, uint32_t *cuid_ptr); +extern int iso14443a_select_card(uint8_t *uid_ptr, iso14a_card_select_t *resp_data, uint32_t *cuid_ptr, uint8_t no_rats); extern void iso14a_set_trigger(bool enable); #endif /* __ISO14443A_H */ diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 36a6e8f5..88f9aae4 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -53,7 +53,7 @@ void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) LED_C_OFF(); while (true) { - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); break; }; @@ -100,7 +100,7 @@ void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){ clear_trace(); - if(!iso14443a_select_card(NULL, NULL, NULL)) { + if(!iso14443a_select_card(NULL, NULL, NULL, 1)) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card"); OnError(0); return; @@ -135,7 +135,7 @@ void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain) clear_trace(); - int len = iso14443a_select_card(NULL, NULL, NULL); + int len = iso14443a_select_card(NULL, NULL, NULL, 1); if(!len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len); OnError(1); @@ -211,7 +211,7 @@ void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) LED_C_OFF(); isOK = 1; - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { isOK = 0; if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); } @@ -275,7 +275,7 @@ void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain) return; } - int len = iso14443a_select_card(NULL, NULL, NULL); + int len = iso14443a_select_card(NULL, NULL, NULL, 1); if (!len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len); OnError(1); @@ -377,7 +377,7 @@ void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) LED_C_OFF(); while (true) { - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); break; }; @@ -431,7 +431,7 @@ void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain) clear_trace(); iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); - if(!iso14443a_select_card(uid, NULL, NULL)) { + if(!iso14443a_select_card(uid, NULL, NULL, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); OnError(0); return; @@ -477,7 +477,7 @@ void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain) clear_trace(); - if(!iso14443a_select_card(NULL, NULL, NULL)) { + if(!iso14443a_select_card(NULL, NULL, NULL, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); OnError(0); return; @@ -536,7 +536,7 @@ void MifareUSetPwd(uint8_t arg0, uint8_t *datain){ clear_trace(); - if(!iso14443a_select_card(NULL, NULL, NULL)) { + if(!iso14443a_select_card(NULL, NULL, NULL, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); OnError(0); return; @@ -672,7 +672,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat continue; } - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card"); rtr--; continue; @@ -746,7 +746,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat continue; } - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card"); continue; }; @@ -861,7 +861,7 @@ void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error"); } - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card"); break; }; @@ -954,7 +954,7 @@ void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai bool isOK = true; - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { isOK = false; if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); } @@ -1054,7 +1054,7 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai // get UID from chip if (workFlags & 0x01) { - if(!iso14443a_select_card(uid, NULL, &cuid)) { + if(!iso14443a_select_card(uid, NULL, &cuid, 1)) { if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card"); break; }; @@ -1249,7 +1249,7 @@ void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){ iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); clear_trace(); - int len = iso14443a_select_card(uid, NULL, &cuid); + int len = iso14443a_select_card(uid, NULL, &cuid, 0); if(!len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card"); OnError(1); From 7034f485829db0115d9e8f3419c8235ac1737645 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Fri, 22 Apr 2016 00:14:49 +0800 Subject: [PATCH 4/6] fix bug: parity attack for any block. --- client/cmdhfmf.c | 12 +++- client/mifarehost.c | 133 +++++++++++++++++++++++++++++++++++ client/mifarehost.h | 1 + client/nonce2key/nonce2key.c | 118 +++++-------------------------- 4 files changed, 162 insertions(+), 102 deletions(-) diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 181c9316..b8eb95aa 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -88,8 +88,16 @@ start: // error if (isOK != 1) return 1; - // execute original function from util nonce2key - if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) { + if (par_list == 0) + { + //parity is all zero,try special attack!just wait for few more seconds... + isOK = magic_nonce2key(uid, nt, nr, par_list, ks_list, &r_key, blockNo, keyType); + } else { + // execute original function from util nonce2key + isOK = nonce2key(uid, nt, nr, par_list, ks_list, &r_key); + } + + if (isOK) { isOK = 2; PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce..."); diff --git a/client/mifarehost.c b/client/mifarehost.c index 4abb1137..ebe35360 100644 --- a/client/mifarehost.c +++ b/client/mifarehost.c @@ -67,6 +67,139 @@ void* nested_worker_thread(void *arg) return statelist->head.slhead; } +int compar_state(const void * a, const void * b) { + // didn't work: (the result is truncated to 32 bits) + //return (*(int64_t*)b - *(int64_t*)a); + + // better: + if (*(int64_t*)b == *(int64_t*)a) return 0; + else if (*(int64_t*)b > *(int64_t*)a) return 1; + else return -1; +} + +int magic_nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t * key, uint32_t blockNo, uint8_t keyType) +{ + struct Crypto1State *state; + uint32_t i, pos, rr, nr_diff, key_count;//, ks1, ks2; + byte_t bt, ks3x[8], par[8][8]; + uint64_t key_recovered; + int64_t *state_s; + static uint32_t last_uid; + static int64_t *last_keylist; + rr = 0; + + if (last_uid != uid && last_keylist != NULL) + { + free(last_keylist); + last_keylist = NULL; + } + last_uid = uid; + + // Reset the last three significant bits of the reader nonce + nr &= 0xffffff1f; + + PrintAndLog("\nuid(%08x) nt(%08x) par(%016"llx") ks(%016"llx") nr(%08"llx")\n\n",uid,nt,par_info,ks_info,nr); + + for (pos=0; pos<8; pos++) + { + ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f; + bt = (par_info >> (pos*8)) & 0xff; + for (i=0; i<8; i++) + { + par[7-pos][i] = (bt >> i) & 0x01; + } + } + + printf("|diff|{nr} |ks3|ks3^5|parity |\n"); + printf("+----+--------+---+-----+---------------+\n"); + for (i=0; i<8; i++) + { + nr_diff = nr | i << 5; + printf("| %02x |%08x|",i << 5, nr_diff); + printf(" %01x | %01x |",ks3x[i], ks3x[i]^5); + for (pos=0; pos<7; pos++) printf("%01x,", par[i][pos]); + printf("%01x|\n", par[i][7]); + } + + if (par_info==0) + PrintAndLog("parity is all zero,try special attack!just wait for few more seconds..."); + + state = lfsr_common_prefix(nr, rr, ks3x, par, par_info==0); + state_s = (int64_t*)state; + + //char filename[50] ; + //sprintf(filename, "nt_%08x_%d.txt", nt, nr); + //printf("name %s\n", filename); + //FILE* fp = fopen(filename,"w"); + for (i = 0; (state) && ((state + i)->odd != -1); i++) + { + lfsr_rollback_word(state+i, uid^nt, 0); + crypto1_get_lfsr(state + i, &key_recovered); + *(state_s + i) = key_recovered; + //fprintf(fp, "%012llx\n",key_recovered); + } + //fclose(fp); + + if(!state) + return 1; + + qsort(state_s, i, sizeof(*state_s), compar_state); + *(state_s + i) = -1; + + //Create the intersection: + if (par_info == 0 ) + if ( last_keylist != NULL) + { + int64_t *p1, *p2, *p3; + p1 = p3 = last_keylist; + p2 = state_s; + while ( *p1 != -1 && *p2 != -1 ) { + if (compar_state(p1, p2) == 0) { + printf("p1:%"llx" p2:%"llx" p3:%"llx" key:%012"llx"\n",(uint64_t)(p1-last_keylist),(uint64_t)(p2-state_s),(uint64_t)(p3-last_keylist),*p1); + *p3++ = *p1++; + p2++; + } + else { + while (compar_state(p1, p2) == -1) ++p1; + while (compar_state(p1, p2) == 1) ++p2; + } + } + key_count = p3 - last_keylist;; + } + else + key_count = 0; + else + { + last_keylist = state_s; + key_count = i; + } + + printf("key_count:%d\n", key_count); + + // The list may still contain several key candidates. Test each of them with mfCheckKeys + for (i = 0; i < key_count; i++) { + uint8_t keyBlock[6]; + uint64_t key64; + key64 = *(last_keylist + i); + num_to_bytes(key64, 6, keyBlock); + key64 = 0; + if (!mfCheckKeys(blockNo, keyType, false, 1, keyBlock, &key64)) { + *key = key64; + free(last_keylist); + last_keylist = NULL; + if (par_info ==0) + free(state); + return 0; + } + } + + free(last_keylist); + last_keylist = state_s; + + *key = last_keylist; + return key_count+1; +} + int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * resultKey, bool calibrate) { uint16_t i; diff --git a/client/mifarehost.h b/client/mifarehost.h index 9ccb8960..a41d74d4 100644 --- a/client/mifarehost.h +++ b/client/mifarehost.h @@ -49,6 +49,7 @@ typedef struct { extern char logHexFileName[FILE_PATH_SIZE]; +int magic_nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t * key, uint32_t blockNo, uint8_t keyType); int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * ResultKeys, bool calibrate); int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key); diff --git a/client/nonce2key/nonce2key.c b/client/nonce2key/nonce2key.c index 70d874fe..e787a265 100644 --- a/client/nonce2key/nonce2key.c +++ b/client/nonce2key/nonce2key.c @@ -15,36 +15,15 @@ #define llx PRIx64 #include "nonce2key.h" -#include "mifarehost.h" #include "ui.h" -int compar_state(const void * a, const void * b) { - // didn't work: (the result is truncated to 32 bits) - //return (*(int64_t*)b - *(int64_t*)a); - - // better: - if (*(int64_t*)b == *(int64_t*)a) return 0; - else if (*(int64_t*)b > *(int64_t*)a) return 1; - else return -1; -} - int nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t * key) { - struct Crypto1State *state; - uint32_t i, pos, rr, nr_diff, key_count;//, ks1, ks2; - byte_t bt, ks3x[8], par[8][8]; + struct Crypto1State *state, *state_s; + uint32_t pos, rr, nr_diff;//, ks1, ks2; + byte_t bt, i, ks3x[8], par[8][8]; uint64_t key_recovered; - int64_t *state_s; - static uint32_t last_uid; - static int64_t *last_keylist; rr = 0; - if (last_uid != uid && last_keylist != NULL) - { - free(last_keylist); - last_keylist = NULL; - } - last_uid = uid; - // Reset the last three significant bits of the reader nonce nr &= 0xffffff1f; @@ -71,81 +50,20 @@ int nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_ printf("%01x|\n", par[i][7]); } - if (par_info==0) - PrintAndLog("parity is all zero,try special attack!just wait for few more seconds..."); + state = lfsr_common_prefix(nr, rr, ks3x, par, 0); + state_s = 0; + for (i = 0; (state) && ((state + i)->odd != -1 || (state + i)->even != ) && (i < 10); i++) + { + printf("%08x|%08x\n",(state+i)->odd, (state+i)->even); + state_s = state + i; + } + if (!state_s) return 1; + + lfsr_rollback_word(state_s, uid^nt, 0); + crypto1_get_lfsr(state_s, &key_recovered); + if (!state) free(state); + + *key = key_recovered; - state = lfsr_common_prefix(nr, rr, ks3x, par, par_info==0); - state_s = (int64_t*)state; - - //char filename[50] ; - //sprintf(filename, "nt_%08x_%d.txt", nt, nr); - //printf("name %s\n", filename); - //FILE* fp = fopen(filename,"w"); - for (i = 0; (state) && ((state + i)->odd != -1); i++) - { - lfsr_rollback_word(state+i, uid^nt, 0); - crypto1_get_lfsr(state + i, &key_recovered); - *(state_s + i) = key_recovered; - //fprintf(fp, "%012llx\n",key_recovered); - } - //fclose(fp); - - if(!state) - return 1; - - qsort(state_s, i, sizeof(*state_s), compar_state); - *(state_s + i) = -1; - - //Create the intersection: - if (par_info == 0 ) - if ( last_keylist != NULL) - { - int64_t *p1, *p2, *p3; - p1 = p3 = last_keylist; - p2 = state_s; - while ( *p1 != -1 && *p2 != -1 ) { - if (compar_state(p1, p2) == 0) { - printf("p1:%"llx" p2:%"llx" p3:%"llx" key:%012"llx"\n",(uint64_t)(p1-last_keylist),(uint64_t)(p2-state_s),(uint64_t)(p3-last_keylist),*p1); - *p3++ = *p1++; - p2++; - } - else { - while (compar_state(p1, p2) == -1) ++p1; - while (compar_state(p1, p2) == 1) ++p2; - } - } - key_count = p3 - last_keylist;; - } - else - key_count = 0; - else - { - last_keylist = state_s; - key_count = i; - } - - printf("key_count:%d\n", key_count); - - // The list may still contain several key candidates. Test each of them with mfCheckKeys - for (i = 0; i < key_count; i++) { - uint8_t keyBlock[6]; - uint64_t key64; - key64 = *(last_keylist + i); - num_to_bytes(key64, 6, keyBlock); - key64 = 0; - if (!mfCheckKeys(0, 0, false, 1, keyBlock, &key64)) { - *key = key64; - free(last_keylist); - last_keylist = NULL; - if (par_info ==0) - free(state); - return 0; - } - } - - - free(last_keylist); - last_keylist = state_s; - - return 1; + return 0; } From 3f43a10c37521ef81b05e185d391863c877079c0 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Sun, 24 Apr 2016 15:16:34 +0800 Subject: [PATCH 5/6] fix nonce2key bug. fix debug_info array overflow bug. --- armsrc/iso14443a.c | 18 ++++++++++++------ client/nonce2key/nonce2key.c | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index f9a33158..3109a826 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -2058,7 +2058,7 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) byte_t par_list[8] = {0x00}; byte_t ks_list[8] = {0x00}; - #define PRNG_SEQUENCE_LENGTH (1 << 16); + #define PRNG_SEQUENCE_LENGTH (1 << 16) static uint32_t sync_time; static int32_t sync_cycles; int catch_up_cycles = 0; @@ -2195,12 +2195,16 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) isOK = -4; // Card's PRNG runs at an unexpected frequency or resets unexpectedly break; } else { // continue for a while, just to collect some debug info - debug_info[strategy][debug_info_nr] = nt_distance; - debug_info_nr++; - if (debug_info_nr == NUM_DEBUG_INFOS) { - strategy++; - debug_info_nr = 0; + if (-1 != debug_info_nr) + { + debug_info[strategy][debug_info_nr] = nt_distance; + if (debug_info_nr == NUM_DEBUG_INFOS) { + strategy++; + if (MF_DBGLEVEL >= 3) Dbprintf("strategy:%d", strategy); + debug_info_nr = 0; + } } + debug_info_nr++; continue; } } @@ -2214,11 +2218,13 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) continue; } } + if (MF_DBGLEVEL >= 3) Dbprintf("par: %d\n",par[0]); if ((nt != nt_attacked) && nt_attacked) { // we somehow lost sync. Try to catch up again... catch_up_cycles = -dist_nt(nt_attacked, nt); if (catch_up_cycles == 99999) { // invalid nonce received. Don't resync on that one. catch_up_cycles = 0; + if (MF_DBGLEVEL >= 3) Dbprintf("invalid nonce received. Don't resync on that one\n"); continue; } catch_up_cycles /= elapsed_prng_sequences; diff --git a/client/nonce2key/nonce2key.c b/client/nonce2key/nonce2key.c index e787a265..b12fd214 100644 --- a/client/nonce2key/nonce2key.c +++ b/client/nonce2key/nonce2key.c @@ -52,7 +52,7 @@ int nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_ state = lfsr_common_prefix(nr, rr, ks3x, par, 0); state_s = 0; - for (i = 0; (state) && ((state + i)->odd != -1 || (state + i)->even != ) && (i < 10); i++) + for (i = 0; (state) && ((state + i)->odd != -1 || (state + i)->even != -1) && (i < 10); i++) { printf("%08x|%08x\n",(state+i)->odd, (state+i)->even); state_s = state + i; From ec421250b63a62cc50286653fba071f9bfe2b3fb Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Tue, 26 Apr 2016 18:24:08 +0800 Subject: [PATCH 6/6] move debug Dbprintf avoid miss timing. --- armsrc/iso14443a.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 3109a826..53358ca7 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -2218,7 +2218,6 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) continue; } } - if (MF_DBGLEVEL >= 3) Dbprintf("par: %d\n",par[0]); if ((nt != nt_attacked) && nt_attacked) { // we somehow lost sync. Try to catch up again... catch_up_cycles = -dist_nt(nt_attacked, nt); @@ -2277,6 +2276,7 @@ void ReaderMifare(bool first_try, uint8_t blockNo, uint8_t keyType) if (nt_diff == 0 && first_try) { par[0]++; + if (MF_DBGLEVEL >= 3) Dbprintf("par: %d\n",par[0]); if (par[0] == 0x00) { // tried all 256 possible parities without success. Card doesn't send NACK. isOK = -2; break;