]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
"hf mf sniff" low level logics works.
authorMerlokbr@gmail.com <Merlokbr@gmail.com@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Wed, 11 Jul 2012 15:52:33 +0000 (15:52 +0000)
committerMerlokbr@gmail.com <Merlokbr@gmail.com@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Wed, 11 Jul 2012 15:52:33 +0000 (15:52 +0000)
armsrc/fpgaloader.c
armsrc/iso14443a.c
armsrc/iso14443a.h
armsrc/mifaresniff.c
armsrc/mifaresniff.h
armsrc/util.h
client/cmdhfmf.c
common/iso14443crc.c
common/iso14443crc.h
include/proxmark3.h

index caf87ba987d12788ab39aa7ac04a725a3480a2a8..c199b9a86f4b84f24c8e0b39e90b84cffb9ec9d8 100644 (file)
@@ -138,11 +138,16 @@ void FpgaSetupSsc(void)
 //-----------------------------------------------------------------------------
 void FpgaSetupSscDma(uint8_t *buf, int len)
 {
+       AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
+       
        AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf;
        AT91C_BASE_PDC_SSC->PDC_RCR = len;
        AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf;
        AT91C_BASE_PDC_SSC->PDC_RNCR = len;
-       AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
+       
+       if (buf != NULL) {
+               AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
+       }
 }
 
 static void DownloadFPGA_byte(unsigned char w)
index 4bdbb8104fa20243abf80fa3b781c6bb178a486f..a564a32ffcc345b9630f61cf77b4ee0ca6f355aa 100644 (file)
@@ -656,7 +656,6 @@ void RAMFUNC SnoopIso14443a(uint8_t param) {
                if (!AT91C_BASE_PDC_SSC->PDC_RCR) {
                        AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) dmaBuf;
                        AT91C_BASE_PDC_SSC->PDC_RCR = DMA_BUFFER_SIZE;
-                       Dbprintf("RxEmpty ERROR!!! %d", dataLen); // temporary
                }
                // secondary buffer sets as primary, secondary buffer was stopped
                if (!AT91C_BASE_PDC_SSC->PDC_RNCR) {
@@ -2356,7 +2355,8 @@ void RAMFUNC SniffMifare(uint8_t param) {
        // param:
        // bit 0 - trigger from first card answer
        // bit 1 - trigger from first reader 7-bit request
-       
+
+       // C(red) A(yellow) B(green)
        LEDsoff();
        // init trace buffer
        traceLen = 0;
@@ -2378,7 +2378,6 @@ void RAMFUNC SniffMifare(uint8_t param) {
        int8_t *data = dmaBuf;
        int maxDataLen = 0;
        int dataLen = 0;
-//     data = dmaBuf;
 
        // Set up the demodulator for tag -> reader responses.
        Demod.output = receivedResponse;
@@ -2400,10 +2399,11 @@ void RAMFUNC SniffMifare(uint8_t param) {
        LED_D_OFF();
        FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER);
        SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+       
+       // init sniffer
+       MfSniffInit();
+       int sniffCounter = 0;
 
-       // Count of samples received so far, so that we can include timing
-       // information in the trace buffer.
-       rsamples = 0;
        // And now we loop, receiving samples.
        while(true) {
                if(BUTTON_PRESS()) {
@@ -2413,6 +2413,13 @@ void RAMFUNC SniffMifare(uint8_t param) {
 
                LED_A_ON();
                WDT_HIT();
+               
+               if (++sniffCounter > 65) {
+                       if (MfSniffSend(2000)) {
+                               AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
+                       }
+                       sniffCounter = 0;
+               }
 
                int register readBufDataP = data - dmaBuf;
                int register dmaBufDataP = DMA_BUFFER_SIZE - AT91C_BASE_PDC_SSC->PDC_RCR;
@@ -2445,33 +2452,30 @@ void RAMFUNC SniffMifare(uint8_t param) {
 
                LED_A_OFF();
                
-               rsamples += 4;
                if(MillerDecoding((data[0] & 0xF0) >> 4)) {
-                       LED_C_ON();
+                       LED_C_INV();
                        // check - if there is a short 7bit request from reader
-                       if ((Uart.byteCnt == 1) && (Uart.bitCnt = 9)) { 
-
-                       }
-                       if (!LogTrace(receivedCmd, Uart.byteCnt, 0 - Uart.samples, Uart.parityBits, TRUE)) break;
+                       if (MfSniffLogic(receivedCmd, Uart.byteCnt, Uart.bitCnt, TRUE)) break;
 
                        /* And ready to receive another command. */
                        Uart.state = STATE_UNSYNCD;
-                       /* And also reset the demod code, which might have been */
-                       /* false-triggered by the commands from the reader. */
+                       
+                       /* And also reset the demod code */
                        Demod.state = DEMOD_UNSYNCD;
-                       LED_B_OFF();
                }
 
                if(ManchesterDecoding(data[0] & 0x0F)) {
-                       LED_B_ON();
+                       LED_C_INV();
 
-                       if (!LogTrace(receivedResponse, Demod.len, 0 - Demod.samples, Demod.parityBits, FALSE)) break;
+                       if (MfSniffLogic(receivedResponse, Demod.len, Uart.bitCnt, FALSE)) break;
 
                        // And ready to receive another response.
                        memset(&Demod, 0, sizeof(Demod));
                        Demod.output = receivedResponse;
                        Demod.state = DEMOD_UNSYNCD;
-                       LED_C_OFF();
+
+                       /* And also reset the uart code */
+                       Uart.state = STATE_UNSYNCD;
                }
 
                data++;
@@ -2484,7 +2488,9 @@ void RAMFUNC SniffMifare(uint8_t param) {
 
 done:
        AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
+       MfSniffEnd();
+       
        Dbprintf("maxDataLen=%x, Uart.state=%x, Uart.byteCnt=%x", maxDataLen, Uart.state, Uart.byteCnt);
-       Dbprintf("Uart.byteCntMax=%x, traceLen=%x, Uart.output[0]=%x", Uart.byteCntMax, traceLen, (int)Uart.output[0]);
+       Dbprintf("Uart.byteCntMax=%x, traceLen=%x", Uart.byteCntMax, traceLen);
        LEDsoff();
 }
\ No newline at end of file
index d35d8f200953a5c236dc5429c7b9a00aab532ddc..1a5766c8fdaa7a1a43721dfcba1ac7d7e4c65405 100644 (file)
@@ -13,6 +13,7 @@
 #ifndef __ISO14443A_H
 #define __ISO14443A_H
 #include "common.h"
+#include "mifaresniff.h"
 
 // mifare reader                      over DMA buffer (SnoopIso14443a())!!!
 #define MIFARE_BUFF_OFFSET 3560  //              \/   \/   \/
index 25c7780d49fda4a7539c7c8ea79a8f8d76acad5e..aefe6962483315c992c5a61682f29024083e2a42 100644 (file)
 #include "mifaresniff.h"\r
 #include "apps.h"\r
 \r
+static int sniffState = SNF_INIT;\r
+static uint8_t sniffUIDType;\r
+static uint8_t sniffUID[8];\r
+static uint8_t sniffATQA[2];\r
+static uint8_t sniffSAK;\r
+static uint8_t sniffBuf[16];\r
+static int timerData = 0;\r
 \r
 \r
+int MfSniffInit(void){\r
+       rsamples = 0;\r
+       memset(sniffUID, 0x00, 8);\r
+       memset(sniffATQA, 0x00, 2);\r
+       sniffSAK = 0;\r
+       sniffUIDType = SNF_UID_4;\r
+\r
+       return 0;\r
+}\r
+\r
+int MfSniffEnd(void){\r
+       UsbCommand ack = {CMD_ACK, {0, 0, 0}};\r
+\r
+       LED_B_ON();\r
+       UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
+       LED_B_OFF();\r
+\r
+       return 0;\r
+}\r
+\r
+int RAMFUNC MfSniffLogic(const uint8_t * data, int len, int bitCnt, int reader) {\r
+\r
+       if ((len == 1) && (bitCnt = 9)) { \r
+               sniffState = SNF_INIT;\r
+       }\r
+\r
+       switch (sniffState) {\r
+               case SNF_INIT:{\r
+                       if ((reader) && (len == 1) && (bitCnt == 9) && ((data[0] == 0x26) || (data[0] == 0x52))) { \r
+                               sniffUIDType = SNF_UID_4;\r
+                               memset(sniffUID, 0x00, 8);\r
+                               memset(sniffATQA, 0x00, 2);\r
+                               sniffSAK = 0;\r
+\r
+                               sniffState = SNF_WUPREQ;\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_WUPREQ:{\r
+                       if ((!reader) && (len == 2)) { \r
+                               memcpy(sniffATQA, data, 2);\r
+\r
+                               sniffState = SNF_ATQA;\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_ATQA:{\r
+                       if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20)) { \r
+                               sniffState = SNF_ANTICOL1;\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_ANTICOL1:{\r
+                       if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { \r
+                               memcpy(sniffUID + 3, data, 4);\r
+                       \r
+                               sniffState = SNF_UID1;\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_UID1:{\r
+                       if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) { \r
+                               sniffState = SNF_SAK;\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_SAK:{\r
+                       if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { \r
+                               sniffSAK = data[0];\r
+                               if (sniffUID[3] == 0x88) {\r
+                                       sniffState = SNF_ANTICOL2;\r
+                               } else {\r
+                                       sniffState = SNF_CARD_IDLE;\r
+                               }\r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_ANTICOL2:{\r
+                       if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { \r
+                               memcpy(sniffUID, data, 4);\r
+                               sniffUIDType = SNF_UID_7;\r
+                       \r
+                               sniffState = SNF_UID2;\r
+               }\r
+                       break;\r
+               }\r
+               case SNF_UID2:{\r
+                       if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9))) { \r
+                               sniffState = SNF_SAK;\r
+       Dbprintf("SNF_SAK");                            \r
+                       }\r
+                       break;\r
+               }\r
+               case SNF_CARD_IDLE:{\r
+                       sniffBuf[0] = 0xFF;\r
+                       sniffBuf[1] = 0xFF;\r
+                       memcpy(sniffBuf + 2, sniffUID, 7);\r
+                       memcpy(sniffBuf + 9, sniffATQA, 2);\r
+                       sniffBuf[11] = sniffSAK;\r
+                       sniffBuf[12] = 0xFF;\r
+                       sniffBuf[13] = 0xFF;\r
+                       LogTrace(sniffBuf, 14, 0, 0, true);\r
+                       timerData = GetTickCount();\r
+               }\r
+               case SNF_CARD_CMD:{\r
+                       LogTrace(data, len, 0, 0, true);\r
+\r
+                       sniffState = SNF_CARD_RESP;\r
+                       timerData = GetTickCount();\r
+                       break;\r
+               }\r
+               case SNF_CARD_RESP:{\r
+                       LogTrace(data, len, 0, 0, false);\r
+\r
+                       sniffState = SNF_CARD_CMD;\r
+                       timerData = GetTickCount();\r
+                       break;\r
+               }\r
+       \r
+               default:\r
+                       sniffState = SNF_INIT;\r
+               break;\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+int RAMFUNC MfSniffSend(int maxTimeoutMs) {\r
+       if (traceLen && (timerData + maxTimeoutMs < GetTickCount())) {\r
+               return intMfSniffSend();\r
+       }\r
+       return 0;\r
+}\r
+\r
+// internal seding function. not a RAMFUNC.\r
+int intMfSniffSend() {\r
+       \r
+       int pckSize = 0;\r
+       int pckLen = traceLen;\r
+       int pckNum = 0;\r
+       \r
+       if (!traceLen) return 0;\r
+\r
+       AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;\r
+\r
+       while (pckLen > 0) {\r
+               pckSize = min(32, pckLen);\r
+               UsbCommand ack = {CMD_ACK, {1, pckSize, pckNum}};\r
+               memcpy(ack.d.asBytes, trace + traceLen - pckLen, pckSize);\r
+       \r
+               LED_B_ON();\r
+               UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
+               SpinDelay(20);\r
+               LED_B_OFF();\r
+\r
+               pckLen -= pckSize;\r
+               pckNum++;\r
+       }\r
+\r
+       UsbCommand ack = {CMD_ACK, {2, 0, 0}};\r
+\r
+       LED_B_ON();\r
+       UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
+       LED_B_OFF();\r
+\r
+       traceLen = 0;\r
+       memset(trace, 0x44, TRACE_SIZE);\r
+       \r
+       return 1;\r
+}\r
index 21692aec94e16d96b484e979ef1e3c1ac01f698b..2012f9d74fdc8a27835d59a18859243860d1941d 100644 (file)
 #include "mifareutil.h"\r
 #include "common.h"\r
 \r
+#define SNF_INIT                               0\r
+#define SNF_NO_FIELD           1\r
+#define SNF_WUPREQ                     2\r
+#define SNF_ATQA                               3\r
+#define SNF_ANTICOL1           4\r
+#define SNF_UID1                               5\r
+#define SNF_ANTICOL2           6\r
+#define SNF_UID2                               7\r
+#define SNF_SAK                                        8\r
+#define SNF_CARD_IDLE          9\r
+#define SNF_CARD_CMD           10\r
+#define SNF_CARD_RESP          11\r
+\r
+#define SNF_UID_4                              0\r
+#define SNF_UID_7                              0\r
+\r
+int MfSniffInit(void);\r
+int RAMFUNC MfSniffLogic(const uint8_t * data, int len, int bitCnt, int reader);\r
+int RAMFUNC MfSniffSend(int maxTimeoutMs);\r
+int intMfSniffSend();\r
+int MfSniffEnd(void);\r
 \r
 #endif
\ No newline at end of file
index 661b472d1439d273f22bdbf2c71c4da8f54d198c..21a9b7577f2d1f79c0ef3a8d41462bac6e0994a4 100644 (file)
@@ -17,6 +17,7 @@
 #define RAMFUNC __attribute((long_call, section(".ramfunc")))
 
 #define BYTEx(x, n) (((x) >> (n * 8)) & 0xff )
+#define min(a, b) (((a) > (b)) ? (b) : (a))
 
 #define LED_RED 1
 #define LED_ORANGE 2
index 8dc59f49a0e08e23f5f6c76140829ebe06faafe1..98006d4165c2bef9c630f285110d5496e0b6c6eb 100644 (file)
@@ -37,7 +37,6 @@ start:
        // message\r
        printf("-------------------------------------------------------------------------\n");\r
        printf("Executing command. It may take up to 30 min.\n");\r
-       printf("Press the key on proxmark3 device to abort proxmark3.\n");\r
        printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
        printf("-------------------------------------------------------------------------\n");\r
        \r
@@ -1543,6 +1542,18 @@ int CmdHF14AMfCSave(const char *Cmd) {
 }\r
 \r
 int CmdHF14AMfSniff(const char *Cmd){\r
+       int res = 0;\r
+       int len = 0;\r
+       int blockLen = 0;\r
+       int num = 0;\r
+       int pckNum = 0;\r
+       uint8_t uid[8];\r
+       uint8_t atqa[2];\r
+       uint8_t sak;\r
+       bool isTag;\r
+       uint8_t buf[3000];\r
+       uint8_t * bufPtr = buf;\r
+       memset(buf, 0x00, 3000);\r
        \r
        if (param_getchar(Cmd, 0) == 'h') {\r
                PrintAndLog("Usage:  hf mf sniff ");\r
@@ -1550,9 +1561,66 @@ int CmdHF14AMfSniff(const char *Cmd){
                return 0;\r
        }       \r
        \r
+       printf("-------------------------------------------------------------------------\n");\r
+       printf("Executing command. \n");\r
+       printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
+       printf("Press the key on pc keyboard to abort the client.\n");\r
+       printf("-------------------------------------------------------------------------\n");\r
+\r
   UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
   SendCommand(&c);\r
 \r
+       // wait cycle\r
+       while (true) {\r
+               printf(".");\r
+               fflush(stdout);\r
+               if (ukbhit()) {\r
+                       getchar();\r
+                       printf("\naborted via keyboard!\n");\r
+                       break;\r
+               }\r
+               \r
+               UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);\r
+               if (resp != NULL) {\r
+                       res = resp->arg[0] & 0xff;\r
+                       len = resp->arg[1];\r
+                       num = resp->arg[2];\r
+                       \r
+                       if (res == 0) return 0;\r
+                       if (res == 1) {\r
+                               if (num ==0) {\r
+                                       bufPtr = buf;\r
+                                       memset(buf, 0x00, 3000);\r
+                               }\r
+                               memcpy(bufPtr, resp->d.asBytes, len);\r
+                               bufPtr += len;\r
+                               pckNum++;\r
+                       }\r
+                       if (res == 2) {\r
+                               blockLen = bufPtr - buf;\r
+                               bufPtr = buf;\r
+                               printf(">\n");\r
+                               PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
+                               num = 0;\r
+                               while (bufPtr - buf + 9 < blockLen) {\r
+                                 isTag = bufPtr[3] & 0x80 ? true:false;\r
+                                       bufPtr += 8;\r
+                                       len = bufPtr[0];\r
+                                       bufPtr++;\r
+                                       if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) {\r
+                                               memcpy(uid, bufPtr + 2, 7);\r
+                                               memcpy(atqa, bufPtr + 2 + 7, 2);\r
+                                               sak = bufPtr[11];\r
+                                               PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);\r
+                                       } else {\r
+                                               PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
+                                       }\r
+                                       bufPtr += len;\r
+                                       num++;\r
+                               }\r
+                       }\r
+               } // resp not NILL\r
+       } // while (true)\r
   return 0;\r
 }\r
 \r
index b5ffc92d7a46614dec7b7bd2b427937aabb7f2ac..a6def1a91be2df84675660c67ec636ecc3ba4b55 100644 (file)
@@ -18,7 +18,7 @@ static unsigned short UpdateCrc14443(unsigned char ch, unsigned short *lpwCrc)
 }
 
 void ComputeCrc14443(int CrcType,
-                     unsigned char *Data, int Length,
+                     const unsigned char *Data, int Length,
                      unsigned char *TransmitFirst,
                      unsigned char *TransmitSecond)
 {
@@ -37,3 +37,12 @@ void ComputeCrc14443(int CrcType,
     *TransmitSecond = (unsigned char) ((wCrc >> 8) & 0xFF);
     return;
 }
+
+int CheckCrc14443(int CrcType, const unsigned char *Data, int Length) {
+       unsigned char b1;
+       unsigned char b2;
+       if (Length < 3) return 0;
+       ComputeCrc14443(CrcType, Data, Length - 2, &b1, &b2);
+       if ((b1 == Data[Length - 2]) && (b2 == Data[Length - 1])) return 1;
+       return 0;
+}
index e70573efdd67c0425a66f595c5f47943126ae5ef..87347714dbe6be95f1570320efe2512e0f79d05f 100644 (file)
@@ -8,6 +8,7 @@
 
 #ifndef __ISO14443CRC_H
 #define __ISO14443CRC_H
+#include "common.h"
 
 //-----------------------------------------------------------------------------
 // Routines to compute the CRCs (two different flavours, just for confusion)
@@ -18,8 +19,9 @@
 #define CRC_ICLASS     0xE012  /* ICLASS PRERFIX */
 
 void ComputeCrc14443(int CrcType,
-                     unsigned char *Data, int Length,
+                     const unsigned char *Data, int Length,
                      unsigned char *TransmitFirst,
                      unsigned char *TransmitSecond);
+int CheckCrc14443(int CrcType, const unsigned char *Data, int Length);
 
 #endif
index a2bad2c516d5d4ed44f52c910befd892d21896ac..0431dc802dfbf47c0fd227350f70348b1ec36e83 100644 (file)
 #define UDP_CSR_BYTES_RECEIVED(x)                              (((x) >> 16) & 0x7ff)
 //**************************************************************
 
-#define LOW(x) AT91C_BASE_PIOA->PIO_CODR = (x)
-#define HIGH(x)        AT91C_BASE_PIOA->PIO_SODR = (x)
+#define LOW(x)  AT91C_BASE_PIOA->PIO_CODR = (x)
+#define HIGH(x)         AT91C_BASE_PIOA->PIO_SODR = (x)
+#define GETBIT(x) (AT91C_BASE_PIOA->PIO_ODSR & (x)) ? 1:0
+#define SETBIT(x, y) (y) ? (HIGH(x)):(LOW(x))
+#define INVBIT(x) SETBIT((x), !(GETBIT(x)))
 
 #define SPI_FPGA_MODE  0
 #define SPI_LCD_MODE   1
 
 #define LED_A_ON()             HIGH(GPIO_LED_A)
 #define LED_A_OFF()            LOW(GPIO_LED_A)
+#define LED_A_INV()            INVBIT(GPIO_LED_A)
 #define LED_B_ON()             HIGH(GPIO_LED_B)
 #define LED_B_OFF()            LOW(GPIO_LED_B)
+#define LED_B_INV()            INVBIT(GPIO_LED_B)
 #define LED_C_ON()             HIGH(GPIO_LED_C)
 #define LED_C_OFF()            LOW(GPIO_LED_C)
+#define LED_C_INV()            INVBIT(GPIO_LED_C)
 #define LED_D_ON()             HIGH(GPIO_LED_D)
 #define LED_D_OFF()            LOW(GPIO_LED_D)
+#define LED_D_INV()            INVBIT(GPIO_LED_D)
 #define RELAY_ON()             HIGH(GPIO_RELAY)
 #define RELAY_OFF()            LOW(GPIO_RELAY)
 #define BUTTON_PRESS() !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_BUTTON)
Impressum, Datenschutz