]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifaresniff.c
FIX: mental note to self, don't forget to adjust messages to new logic
[proxmark3-svn] / armsrc / mifaresniff.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------\r
2// Merlok - 2012\r
3//\r
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
5// at your option, any later version. See the LICENSE.txt file for the text of\r
6// the license.\r
7//-----------------------------------------------------------------------------\r
8// Routines to support mifare classic sniffer.\r
9//-----------------------------------------------------------------------------\r
10\r
11#include "mifaresniff.h"\r
12#include "apps.h"\r
13\r
14static int sniffState = SNF_INIT;\r
15static uint8_t sniffUIDType = 0;\r
16static uint8_t sniffUID[10] = {0,0,0,0,0,0,0,0,0,0};\r
17static uint8_t sniffATQA[2] = {0,0};\r
18static uint8_t sniffSAK = 0;\r
19static uint8_t sniffBuf[17];\r
20static uint32_t timerData = 0;\r
21\r
22void MfSniffInit(void){\r
23 memset(sniffUID, 0x00, sizeof(sniffUID));\r
24 memset(sniffATQA, 0x00, sizeof(sniffATQA));\r
25 memset(sniffBuf, 0x00, sizeof(sniffBuf));\r
26 sniffSAK = 0;\r
27 sniffUIDType = SNF_UID_4;\r
28}\r
29\r
30void MfSniffEnd(void){\r
31 LED_B_ON();\r
32 cmd_send(CMD_ACK,0,0,0,0,0);\r
33 LED_B_OFF();\r
34}\r
35\r
36bool RAMFUNC MfSniffLogic(const uint8_t *data, uint16_t len, uint8_t *parity, uint16_t bitCnt, bool reader) {\r
37\r
38 // reset on 7-Bit commands from reader\r
39 if (reader && (len == 1) && (bitCnt == 7)) { \r
40 sniffState = SNF_INIT;\r
41 }\r
42\r
43 switch (sniffState) {\r
44 case SNF_INIT:{\r
45 // REQA or WUPA from reader\r
46 if ((len == 1) && (reader) && (bitCnt == 7) ) {\r
47 MfSniffInit();\r
48 sniffState = SNF_WUPREQ;\r
49 }\r
50 break;\r
51 }\r
52 case SNF_WUPREQ:{\r
53 // ATQA from tag\r
54 if ((!reader) && (len == 2)) {\r
55 sniffATQA[0] = data[0];\r
56 sniffATQA[1] = data[1];\r
57 sniffState = SNF_ATQA;\r
58 }\r
59 break;\r
60 }\r
61 case SNF_ATQA:{\r
62 // Select ALL from reader\r
63 if ((reader) && (len == 2) && (data[0] == 0x93) && (data[1] == 0x20))\r
64 sniffState = SNF_ANTICOL1;\r
65 break;\r
66 }\r
67 case SNF_ANTICOL1:{\r
68 // UID from tag (CL1) \r
69 if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {\r
70 memcpy(sniffUID, data, 4);\r
71 sniffState = SNF_UID1;\r
72 }\r
73 break;\r
74 }\r
75 case SNF_UID1:{\r
76 // Select 4 Byte UID from reader\r
77 if ((reader) && (len == 9) && (data[0] == 0x93) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))\r
78 sniffState = SNF_SAK;\r
79 break;\r
80 }\r
81 case SNF_SAK:{\r
82 if ((!reader) && (len == 3) && (CheckCrc14443(CRC_14443_A, data, 3))) { // SAK from card?\r
83 sniffSAK = data[0];\r
84 if (sniffUID[0] == 0x88) // CL2/3 UID part to be expected \r
85 sniffState = (sniffState == SNF_ANTICOL2 ) ? SNF_ANTICOL3 : SNF_ANTICOL2;\r
86 else // select completed\r
87 sniffState = SNF_CARD_IDLE;\r
88 }\r
89 break;\r
90 }\r
91 case SNF_ANTICOL2:{\r
92 // CL2 UID \r
93 if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) {\r
94 sniffUID[0] = sniffUID[1];\r
95 sniffUID[1] = sniffUID[2];\r
96 sniffUID[2] = sniffUID[3];\r
97 memcpy(sniffUID+3, data, 4);\r
98 sniffUIDType = SNF_UID_7;\r
99 sniffState = SNF_UID2;\r
100 }\r
101 break;\r
102 }\r
103 case SNF_UID2:{\r
104 // Select 2nd part of 7 Byte UID\r
105 if ((reader) && (len == 9) && (data[0] == 0x95) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))\r
106 sniffState = SNF_SAK;\r
107 break;\r
108 }\r
109 case SNF_ANTICOL3:{\r
110 // CL3 UID \r
111 if ((!reader) && (len == 5) && ((data[0] ^ data[1] ^ data[2] ^ data[3]) == data[4])) { \r
112 // 3+3+4 = 10.\r
113 sniffUID[3] = sniffUID[4];\r
114 sniffUID[4] = sniffUID[5];\r
115 sniffUID[5] = sniffUID[6];\r
116 memcpy(sniffUID+6, data, 4);\r
117 sniffUIDType = SNF_UID_10;\r
118 sniffState = SNF_UID3;\r
119 }\r
120 break;\r
121 }\r
122 case SNF_UID3:{\r
123 // Select 3nd part of 10 Byte UID\r
124 if ((reader) && (len == 9) && (data[0] == 0x97) && (data[1] == 0x70) && (CheckCrc14443(CRC_14443_A, data, 9)))\r
125 sniffState = SNF_SAK;\r
126 break;\r
127 }\r
128 case SNF_CARD_IDLE:{ // trace the card select sequence\r
129 sniffBuf[0] = 0xFF;\r
130 sniffBuf[1] = 0xFF;\r
131 memcpy(sniffBuf + 2, sniffUID, sizeof(sniffUID));\r
132 memcpy(sniffBuf + 12, sniffATQA, sizeof(sniffATQA));\r
133 sniffBuf[14] = sniffSAK;\r
134 sniffBuf[15] = 0xFF;\r
135 sniffBuf[16] = 0xFF;\r
136 LogTrace(sniffBuf, sizeof(sniffBuf), 0, 0, NULL, TRUE);\r
137 } // intentionally no break;\r
138 case SNF_CARD_CMD:{ \r
139 LogTrace(data, len, 0, 0, NULL, TRUE);\r
140 sniffState = SNF_CARD_RESP;\r
141 timerData = GetTickCount();\r
142 break;\r
143 }\r
144 case SNF_CARD_RESP:{\r
145 LogTrace(data, len, 0, 0, NULL, FALSE);\r
146 sniffState = SNF_CARD_CMD;\r
147 timerData = GetTickCount();\r
148 break;\r
149 }\r
150 default:\r
151 sniffState = SNF_INIT;\r
152 break;\r
153 }\r
154 return FALSE;\r
155}\r
156\r
157bool RAMFUNC MfSniffSend(uint16_t maxTimeoutMs) {\r
158 if (BigBuf_get_traceLen() && (GetTickCount() > timerData + maxTimeoutMs)) {\r
159 return intMfSniffSend();\r
160 }\r
161 return FALSE;\r
162}\r
163\r
164// internal sending function. not a RAMFUNC.\r
165bool intMfSniffSend() {\r
166\r
167 int pckSize = 0;\r
168 int pckLen = BigBuf_get_traceLen();\r
169 int pckNum = 0;\r
170 uint8_t *data = BigBuf_get_addr();\r
171 \r
172 FpgaDisableSscDma();\r
173 while (pckLen > 0) {\r
174 pckSize = MIN(USB_CMD_DATA_SIZE, pckLen);\r
175 LED_B_ON();\r
176 cmd_send(CMD_ACK, 1, BigBuf_get_traceLen(), pckSize, data + BigBuf_get_traceLen() - pckLen, pckSize);\r
177 LED_B_OFF();\r
178 pckLen -= pckSize;\r
179 pckNum++;\r
180 }\r
181\r
182 LED_B_ON();\r
183 cmd_send(CMD_ACK,2,0,0,0,0); // 2 == data transfer is finished.\r
184 LED_B_OFF();\r
185\r
186 clear_trace();\r
187 return TRUE;\r
188}\r
Impressum, Datenschutz