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