X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/39864b0bd41dd5e896bcb8eeabcf2c3932f2203a..55acbb2a3903f7940d0db5fae2e8f4fdab51d1b8:/client/mifarehost.c diff --git a/client/mifarehost.c b/client/mifarehost.c index fb6a4bdb..cfcc17ba 100644 --- a/client/mifarehost.c +++ b/client/mifarehost.c @@ -5,7 +5,7 @@ // at your option, any later version. See the LICENSE.txt file for the text of // the license. //----------------------------------------------------------------------------- -// High frequency ISO14443A commands +// mifare commands //----------------------------------------------------------------------------- #include @@ -13,6 +13,7 @@ #include #include "mifarehost.h" +// MIFARE int compar_int(const void * a, const void * b) { return (*(uint64_t*)b - *(uint64_t*)a); @@ -197,6 +198,8 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, uint8_t keycnt, uint8_t * key return 0; } +// EMULATOR + int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) { UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}}; @@ -216,6 +219,8 @@ int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) { return 0; } +// "MAGIC" CARD + int mfCSetUID(uint8_t *uid, uint8_t *oldUID, int wantWipe) { uint8_t block0[16]; memset(block0, 0, 16); @@ -267,3 +272,236 @@ int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) { } return 0; } + +// SNIFFER + +// variables +char logHexFileName[200] = {0x00}; +static uint8_t traceCard[4096]; +static int traceState = TRACE_IDLE; +static uint8_t traceCurBlock = 0; +static uint8_t traceCurKey = 0; + +struct Crypto1State *traceCrypto1 = NULL; + +struct Crypto1State *revstate; +uint64_t lfsr; +uint32_t ks2; +uint32_t ks3; + +uint32_t uid; // serial number +uint32_t nt; // tag challenge +uint32_t nr_enc; // encrypted reader challenge +uint32_t ar_enc; // encrypted reader response +uint32_t at_enc; // encrypted tag response + +int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak) { + + if (traceCrypto1) crypto1_destroy(traceCrypto1); + traceCrypto1 = NULL; + + memset(traceCard, 0x00, 4096); + memcpy(traceCard, tuid + 3, 4); + traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3]; + traceCard[5] = sak; + memcpy(&traceCard[6], atqa, 2); + traceCurBlock = 0; + uid = bytes_to_num(tuid + 3, 4); + + traceState = TRACE_IDLE; + + return 0; +} + +void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){ + uint8_t bt = 0; + int i; + + if (len != 1) { + for (i = 0; i < len; i++) + data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i]; + } else { + bt = 0; + for (i = 0; i < 4; i++) + bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i; + + data[0] = bt; + } + return; +} + + +int mfTraceDecode(uint8_t *data_src, int len) { + uint8_t data[64]; + + if (traceState == TRACE_ERROR) return 1; + if (len > 64) { + traceState = TRACE_ERROR; + return 1; + } + + memcpy(data, data_src, len); + if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) { + mf_crypto1_decrypt(traceCrypto1, data, len, 0); + PrintAndLog("dec> %s", sprint_hex(data, len)); + AddLogHex(logHexFileName, "dec> ", data, len); + } + + switch (traceState) { + case TRACE_IDLE: + // TODO: check packet crc16! + + // AUTHENTICATION + if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) { + traceState = TRACE_AUTH1; + traceCurBlock = data[1]; + traceCurKey = data[0] == 60 ? 1:0; + return 0; + } + + // READ + if ((len ==4) && ((data[0] == 0x30))) { + traceState = TRACE_READ_DATA; + traceCurBlock = data[1]; + return 0; + } + + // WRITE + if ((len ==4) && ((data[0] == 0xA0))) { + traceState = TRACE_WRITE_OK; + traceCurBlock = data[1]; + return 0; + } + + // HALT + if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) { + traceState = TRACE_ERROR; // do not decrypt the next commands + return 0; + } + + return 0; + break; + + case TRACE_READ_DATA: + if (len == 18) { + traceState = TRACE_IDLE; + + memcpy(traceCard + traceCurBlock * 16, data, 16); + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + case TRACE_WRITE_OK: + if ((len == 1) && (data[0] = 0x0a)) { + traceState = TRACE_WRITE_DATA; + + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + case TRACE_WRITE_DATA: + if (len == 18) { + traceState = TRACE_IDLE; + + memcpy(traceCard + traceCurBlock * 16, data, 16); + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + case TRACE_AUTH1: + if (len == 4) { + traceState = TRACE_AUTH2; + + nt = bytes_to_num(data, 4); + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + case TRACE_AUTH2: + if (len == 8) { + traceState = TRACE_AUTH_OK; + + nr_enc = bytes_to_num(data, 4); + ar_enc = bytes_to_num(data + 4, 4); + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + case TRACE_AUTH_OK: + if (len ==4) { + traceState = TRACE_IDLE; + + at_enc = bytes_to_num(data, 4); + + // decode key here) + if (!traceCrypto1) { + ks2 = ar_enc ^ prng_successor(nt, 64); + ks3 = at_enc ^ prng_successor(nt, 96); + revstate = lfsr_recovery64(ks2, ks3); + lfsr_rollback_word(revstate, 0, 0); + lfsr_rollback_word(revstate, 0, 0); + lfsr_rollback_word(revstate, nr_enc, 1); + lfsr_rollback_word(revstate, uid ^ nt, 0); + }else{ + ks2 = ar_enc ^ prng_successor(nt, 64); + ks3 = at_enc ^ prng_successor(nt, 96); + revstate = lfsr_recovery64(ks2, ks3); + lfsr_rollback_word(revstate, 0, 0); + lfsr_rollback_word(revstate, 0, 0); + lfsr_rollback_word(revstate, nr_enc, 1); + lfsr_rollback_word(revstate, uid ^ nt, 0); + } + crypto1_get_lfsr(revstate, &lfsr); + printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF)); + AddLogUint64(logHexFileName, "key> ", lfsr); + + if (traceCurKey) { + num_to_bytes(lfsr, 6, traceCard + traceCurBlock * 16 + 10); + } else { + num_to_bytes(lfsr, 6, traceCard + traceCurBlock * 16); + } + + if (traceCrypto1) { + crypto1_destroy(traceCrypto1); + } + + // set cryptosystem state + traceCrypto1 = lfsr_recovery64(ks2, ks3); + +// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt; + + /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr + crypto1_word(traceCrypto1, nt ^ uid, 0); + crypto1_word(traceCrypto1, ar, 1); + crypto1_word(traceCrypto1, 0, 0); + crypto1_word(traceCrypto1, 0, 0);*/ + + return 0; + } else { + traceState = TRACE_ERROR; + return 1; + } + break; + + default: + traceState = TRACE_ERROR; + return 1; + } + + return 0; +}