2012-07-07 00:19:05 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Merlok - 2012
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Routines to support mifare classic sniffer.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mifaresniff.h"
|
|
|
|
|
2012-07-11 23:52:33 +08:00
|
|
|
static int sniffState = SNF_INIT;
|
2016-04-14 17:35:49 +08:00
|
|
|
static uint8_t sniffUIDType = 0;
|
|
|
|
static uint8_t sniffUID[10] = {0,0,0,0,0,0,0,0,0,0};
|
|
|
|
static uint8_t sniffATQA[2] = {0,0};
|
|
|
|
static uint8_t sniffSAK = 0;
|
2016-04-18 19:19:11 +08:00
|
|
|
static uint8_t sniffBuf[17];
|
2016-04-14 17:35:49 +08:00
|
|
|
static uint32_t timerData = 0;
|
|
|
|
|
|
|
|
void MfSniffInit(void){
|
|
|
|
memset(sniffUID, 0x00, sizeof(sniffUID));
|
|
|
|
memset(sniffATQA, 0x00, sizeof(sniffATQA));
|
|
|
|
memset(sniffBuf, 0x00, sizeof(sniffBuf));
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffSAK = 0;
|
|
|
|
sniffUIDType = SNF_UID_4;
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:35:49 +08:00
|
|
|
void MfSniffEnd(void){
|
2012-07-11 23:52:33 +08:00
|
|
|
LED_B_ON();
|
2014-02-20 04:35:04 +08:00
|
|
|
cmd_send(CMD_ACK,0,0,0,0,0);
|
2012-07-11 23:52:33 +08:00
|
|
|
LED_B_OFF();
|
|
|
|
}
|
|
|
|
|
2014-12-16 14:41:07 +08:00
|
|
|
bool RAMFUNC MfSniffLogic(const uint8_t *data, uint16_t len, uint8_t *parity, uint16_t bitCnt, bool reader) {
|
2012-07-11 23:52:33 +08:00
|
|
|
|
2016-04-14 17:35:49 +08:00
|
|
|
// reset on 7-Bit commands from reader
|
|
|
|
if (reader && (len == 1) && (bitCnt == 7)) {
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sniffState) {
|
|
|
|
case SNF_INIT:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// REQA or WUPA from reader
|
|
|
|
if ((len == 1) && (reader) && (bitCnt == 7) ) {
|
|
|
|
MfSniffInit();
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_WUPREQ;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_WUPREQ:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// ATQA from tag
|
|
|
|
if ((!reader) && (len == 2)) {
|
|
|
|
sniffATQA[0] = data[0];
|
|
|
|
sniffATQA[1] = data[1];
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_ATQA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_ATQA:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// Select ALL from reader
|
2016-04-18 19:19:11 +08:00
|
|
|
if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20))
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_ANTICOL1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_ANTICOL1:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// UID from tag (CL1)
|
|
|
|
if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {
|
2016-04-18 19:19:11 +08:00
|
|
|
memcpy(sniffUID, data, 4);
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_UID1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_UID1:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// Select 4 Byte UID from reader
|
2016-04-18 19:19:11 +08:00
|
|
|
if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_SAK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_SAK:{
|
2014-02-20 04:35:04 +08:00
|
|
|
if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { // SAK from card?
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffSAK = data[0];
|
2016-04-18 19:19:11 +08:00
|
|
|
if (sniffUID[0] == 0x88) // CL2/3 UID part to be expected
|
|
|
|
sniffState = (sniffState == SNF_ANTICOL2 ) ? SNF_ANTICOL3 : SNF_ANTICOL2;
|
|
|
|
else // select completed
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_CARD_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_ANTICOL2:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// CL2 UID
|
|
|
|
if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {
|
2016-04-18 19:19:11 +08:00
|
|
|
sniffUID[0] = sniffUID[1];
|
|
|
|
sniffUID[1] = sniffUID[2];
|
|
|
|
sniffUID[2] = sniffUID[3];
|
2014-03-26 04:38:24 +08:00
|
|
|
memcpy(sniffUID+3, data, 4);
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffUIDType = SNF_UID_7;
|
|
|
|
sniffState = SNF_UID2;
|
2014-02-20 04:35:04 +08:00
|
|
|
}
|
2012-07-11 23:52:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_UID2:{
|
2016-04-14 17:35:49 +08:00
|
|
|
// Select 2nd part of 7 Byte UID
|
2016-04-18 19:19:11 +08:00
|
|
|
if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))
|
2016-04-14 17:35:49 +08:00
|
|
|
sniffState = SNF_SAK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_ANTICOL3:{
|
|
|
|
// CL3 UID
|
|
|
|
if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {
|
2016-04-18 19:19:11 +08:00
|
|
|
// 3+3+4 = 10.
|
|
|
|
sniffUID[3] = sniffUID[4];
|
|
|
|
sniffUID[4] = sniffUID[5];
|
|
|
|
sniffUID[5] = sniffUID[6];
|
|
|
|
memcpy(sniffUID+6, data, 4);
|
2016-04-14 17:35:49 +08:00
|
|
|
sniffUIDType = SNF_UID_10;
|
|
|
|
sniffState = SNF_UID3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_UID3:{
|
|
|
|
// Select 3nd part of 10 Byte UID
|
2016-04-18 19:19:11 +08:00
|
|
|
if ((reader) && (len == 9) && (data[0] == 0x97) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_SAK;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-20 04:35:04 +08:00
|
|
|
case SNF_CARD_IDLE:{ // trace the card select sequence
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffBuf[0] = 0xFF;
|
|
|
|
sniffBuf[1] = 0xFF;
|
2016-04-14 17:35:49 +08:00
|
|
|
memcpy(sniffBuf + 2, sniffUID, sizeof(sniffUID));
|
2016-04-18 19:19:11 +08:00
|
|
|
memcpy(sniffBuf + 12, sniffATQA, sizeof(sniffATQA));
|
2016-04-14 17:35:49 +08:00
|
|
|
sniffBuf[14] = sniffSAK;
|
|
|
|
sniffBuf[15] = 0xFF;
|
|
|
|
sniffBuf[16] = 0xFF;
|
|
|
|
LogTrace(sniffBuf, sizeof(sniffBuf), 0, 0, NULL, TRUE);
|
2014-02-20 04:35:04 +08:00
|
|
|
} // intentionally no break;
|
|
|
|
case SNF_CARD_CMD:{
|
2014-12-16 14:41:07 +08:00
|
|
|
LogTrace(data, len, 0, 0, NULL, TRUE);
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_CARD_RESP;
|
|
|
|
timerData = GetTickCount();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SNF_CARD_RESP:{
|
2014-12-16 14:41:07 +08:00
|
|
|
LogTrace(data, len, 0, 0, NULL, FALSE);
|
2012-07-11 23:52:33 +08:00
|
|
|
sniffState = SNF_CARD_CMD;
|
|
|
|
timerData = GetTickCount();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
sniffState = SNF_INIT;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-20 04:35:04 +08:00
|
|
|
return FALSE;
|
2012-07-11 23:52:33 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:35:04 +08:00
|
|
|
bool RAMFUNC MfSniffSend(uint16_t maxTimeoutMs) {
|
2015-02-08 03:49:40 +08:00
|
|
|
if (BigBuf_get_traceLen() && (GetTickCount() > timerData + maxTimeoutMs)) {
|
2012-07-11 23:52:33 +08:00
|
|
|
return intMfSniffSend();
|
|
|
|
}
|
2014-02-20 04:35:04 +08:00
|
|
|
return FALSE;
|
2012-07-11 23:52:33 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:35:04 +08:00
|
|
|
// internal sending function. not a RAMFUNC.
|
|
|
|
bool intMfSniffSend() {
|
|
|
|
|
2012-07-11 23:52:33 +08:00
|
|
|
int pckSize = 0;
|
2015-02-08 03:49:40 +08:00
|
|
|
int pckLen = BigBuf_get_traceLen();
|
2012-07-11 23:52:33 +08:00
|
|
|
int pckNum = 0;
|
2016-04-18 19:19:11 +08:00
|
|
|
uint8_t *data = BigBuf_get_addr();
|
2015-01-16 18:00:17 +08:00
|
|
|
|
2012-07-16 22:49:51 +08:00
|
|
|
FpgaDisableSscDma();
|
2012-07-11 23:52:33 +08:00
|
|
|
while (pckLen > 0) {
|
2014-02-20 04:35:04 +08:00
|
|
|
pckSize = MIN(USB_CMD_DATA_SIZE, pckLen);
|
2012-07-11 23:52:33 +08:00
|
|
|
LED_B_ON();
|
2016-04-18 19:19:11 +08:00
|
|
|
cmd_send(CMD_ACK, 1, BigBuf_get_traceLen(), pckSize, data + BigBuf_get_traceLen() - pckLen, pckSize);
|
2012-07-11 23:52:33 +08:00
|
|
|
LED_B_OFF();
|
|
|
|
pckLen -= pckSize;
|
|
|
|
pckNum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
LED_B_ON();
|
2016-04-18 19:19:11 +08:00
|
|
|
cmd_send(CMD_ACK,2,0,0,0,0); // 2 == data transfer is finished.
|
2012-07-11 23:52:33 +08:00
|
|
|
LED_B_OFF();
|
|
|
|
|
2015-02-08 03:49:40 +08:00
|
|
|
clear_trace();
|
2014-02-20 04:35:04 +08:00
|
|
|
return TRUE;
|
2012-07-11 23:52:33 +08:00
|
|
|
}
|