]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflist.c
43e0e56258565a97bac788f53c11ad7e932b45c9
[proxmark3-svn] / client / cmdhflist.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Merlok - 2017
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Command: hf mf list. It shows data from arm buffer.
9 //-----------------------------------------------------------------------------
10
11 #include "cmdhflist.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include "util.h"
19 #include "data.h"
20 #include "ui.h"
21 #include "iso14443crc.h"
22 #include "parity.h"
23 #include "protocols.h"
24 #include "crapto1/crapto1.h"
25 #include "mifarehost.h"
26
27
28 enum MifareAuthSeq {
29 masNone,
30 masNt,
31 masNrAr,
32 masAt,
33 masAuthComplete,
34 masFirstData,
35 masData,
36 masError,
37 };
38 static enum MifareAuthSeq MifareAuthState;
39 static TAuthData AuthData;
40
41 void ClearAuthData() {
42 AuthData.uid = 0;
43 AuthData.nt = 0;
44 AuthData.first_auth = true;
45 }
46
47 /**
48 * @brief iso14443A_CRC_check Checks CRC in command or response
49 * @param isResponse
50 * @param data
51 * @param len
52 * @return 0 : CRC-command, CRC not ok
53 * 1 : CRC-command, CRC ok
54 * 2 : Not crc-command
55 */
56 uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
57 {
58 uint8_t b1,b2;
59
60 if(len <= 2) return 2;
61
62 if(isResponse & (len < 6)) return 2;
63
64 ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2);
65 if (b1 != data[len-2] || b2 != data[len-1]) {
66 return 0;
67 } else {
68 return 1;
69 }
70 }
71
72 uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
73 {
74 switch(MifareAuthState) {
75 case masNone:
76 case masError:
77 return iso14443A_CRC_check(isResponse, data, len);
78 default:
79 return 2;
80 }
81 }
82
83 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
84 {
85 switch(cmd[0])
86 {
87 case ISO14443A_CMD_WUPA:
88 snprintf(exp,size,"WUPA");
89 MifareAuthState = masNone;
90 break;
91 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
92 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
93 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
94 if(cmd[1] == 0x70)
95 {
96 snprintf(exp,size,"SELECT_UID"); break;
97 }else
98 {
99 snprintf(exp,size,"ANTICOLL"); break;
100 }
101 }
102 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
103 //95 20 = Anticollision of cascade level2
104 //95 70 = Select of cascade level2
105 if(cmd[2] == 0x70)
106 {
107 snprintf(exp,size,"SELECT_UID-2"); break;
108 }else
109 {
110 snprintf(exp,size,"ANTICOLL-2"); break;
111 }
112 }
113 case ISO14443A_CMD_REQA:
114 snprintf(exp,size,"REQA");
115 MifareAuthState = masNone;
116 break;
117 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
118 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
119 case ISO14443A_CMD_HALT:
120 snprintf(exp,size,"HALT");
121 MifareAuthState = masNone;
122 break;
123 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
124 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
125 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
126 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
127 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
128 case MIFARE_AUTH_KEYA:
129 if ( cmdsize > 3) {
130 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
131 MifareAuthState = masNt;
132 } else {
133 // case MIFARE_ULEV1_VERSION : both 0x60.
134 snprintf(exp,size,"EV1 VERSION");
135 }
136 break;
137 case MIFARE_AUTH_KEYB:
138 MifareAuthState = masNt;
139 snprintf(exp,size,"AUTH-B(%d)",cmd[1]);
140 break;
141 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
142 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
143 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
144 case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break;
145 case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break;
146 case MIFARE_ULEV1_AUTH:
147 if ( cmdsize == 7 )
148 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
149 else
150 snprintf(exp,size,"PWD-AUTH");
151 break;
152 case MIFARE_ULEV1_FASTREAD:{
153 if ( cmdsize >=3 && cmd[2] <= 0xE6)
154 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
155 else
156 snprintf(exp,size,"?");
157 break;
158 }
159 case MIFARE_ULC_WRITE:{
160 if ( cmd[1] < 0x21 )
161 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
162 else
163 snprintf(exp,size,"?");
164 break;
165 }
166 case MIFARE_ULEV1_READ_CNT:{
167 if ( cmd[1] < 5 )
168 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
169 else
170 snprintf(exp,size,"?");
171 break;
172 }
173 case MIFARE_ULEV1_INCR_CNT:{
174 if ( cmd[1] < 5 )
175 snprintf(exp,size,"INCR(%d)",cmd[1]);
176 else
177 snprintf(exp,size,"?");
178 break;
179 }
180 case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break;
181 case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
182 case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break;
183 default: snprintf(exp,size,"?"); break;
184 }
185 return;
186 }
187
188 void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) {
189 // get UID
190 if (MifareAuthState == masNone) {
191 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
192 ClearAuthData();
193 AuthData.uid = bytes_to_num(&cmd[2], 4);
194 }
195 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
196 ClearAuthData();
197 AuthData.uid = bytes_to_num(&cmd[2], 4);
198 }
199 }
200
201 switch(MifareAuthState) {
202 case masNt:
203 if (cmdsize == 4 && isResponse) {
204 snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)");
205 MifareAuthState = masNrAr;
206 if (AuthData.first_auth)
207 AuthData.nt = bytes_to_num(cmd, 4);
208 else
209 AuthData.nt_enc = bytes_to_num(cmd, 4);
210 AuthData.nt_enc_par = parity[0];
211 return;
212 } else {
213 MifareAuthState = masError;
214 }
215 break;
216 case masNrAr:
217 if (cmdsize == 8 && !isResponse) {
218 snprintf(exp,size,"AUTH: nr ar (enc)");
219 MifareAuthState = masAt;
220 AuthData.nr_enc = bytes_to_num(cmd, 4);
221 AuthData.ar_enc = bytes_to_num(&cmd[4], 4);
222 AuthData.ar_enc_par = parity[0] << 4;
223 return;
224 } else {
225 MifareAuthState = masError;
226 }
227 break;
228 case masAt:
229 if (cmdsize == 4 && isResponse) {
230 snprintf(exp,size,"AUTH: at (enc)");
231 MifareAuthState = masAuthComplete;
232 AuthData.at_enc = bytes_to_num(cmd, 4);
233 AuthData.at_enc_par = parity[0];
234 return;
235 } else {
236 MifareAuthState = masError;
237 }
238 break;
239 default:
240 break;
241 }
242
243 if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError)))
244 annotateIso14443a(exp, size, cmd, cmdsize);
245
246 }
247
248 bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *mfData, size_t *mfDataLen) {
249 static struct Crypto1State *traceCrypto1;
250 uint64_t mfLastKey;
251
252 *mfDataLen = 0;
253
254 if (MifareAuthState == masAuthComplete) {
255 if (traceCrypto1) {
256 crypto1_destroy(traceCrypto1);
257 }
258
259 MifareAuthState = masFirstData;
260 return false;
261 }
262
263 if (cmdsize > 32)
264 return false;
265
266 if (MifareAuthState == masFirstData) {
267 if (AuthData.first_auth) {
268 uint32_t ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64);
269 uint32_t ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96);
270 struct Crypto1State *revstate = lfsr_recovery64(ks2, ks3);
271 lfsr_rollback_word(revstate, 0, 0);
272 lfsr_rollback_word(revstate, 0, 0);
273 lfsr_rollback_word(revstate, AuthData.nr_enc, 1);
274 lfsr_rollback_word(revstate, AuthData.uid ^ AuthData.nt, 0);
275
276 uint64_t lfsr = 0;
277 crypto1_get_lfsr(revstate, &lfsr);
278 crypto1_destroy(revstate);
279 mfLastKey = lfsr;
280 PrintAndLog(" | * | key | probable key:%x%x Prng:%s ks2:%08x ks3:%08x | |",
281 (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),
282 validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD",
283 ks2,
284 ks3);
285
286 AuthData.first_auth = false;
287
288 traceCrypto1 = lfsr_recovery64(ks2, ks3);
289 } else {
290 printf("uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc);
291
292 // check last used key
293 if (mfLastKey) {
294
295 }
296
297 // check default keys
298
299 // nested
300 if (validate_prng_nonce(AuthData.nt)) {
301 }
302
303 //hardnested
304 }
305
306
307
308 MifareAuthState = masData;
309 }
310
311 if (MifareAuthState == masData && traceCrypto1) {
312 memcpy(mfData, cmd, cmdsize);
313 mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0);
314 *mfDataLen = cmdsize;
315 }
316
317 return *mfDataLen > 0;
318 }
319
Impressum, Datenschutz