]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflist.c
nested works
[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 #include "mifaredefault.h"
27
28
29 enum MifareAuthSeq {
30 masNone,
31 masNt,
32 masNrAr,
33 masAt,
34 masAuthComplete,
35 masFirstData,
36 masData,
37 masError,
38 };
39 static enum MifareAuthSeq MifareAuthState;
40 static TAuthData AuthData;
41
42 void ClearAuthData() {
43 AuthData.uid = 0;
44 AuthData.nt = 0;
45 AuthData.first_auth = true;
46 AuthData.ks2 = 0;
47 AuthData.ks3 = 0;
48 }
49
50 /**
51 * @brief iso14443A_CRC_check Checks CRC in command or response
52 * @param isResponse
53 * @param data
54 * @param len
55 * @return 0 : CRC-command, CRC not ok
56 * 1 : CRC-command, CRC ok
57 * 2 : Not crc-command
58 */
59 uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
60 {
61 uint8_t b1,b2;
62
63 if(len <= 2) return 2;
64
65 if(isResponse & (len < 6)) return 2;
66
67 ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2);
68 if (b1 != data[len-2] || b2 != data[len-1]) {
69 return 0;
70 } else {
71 return 1;
72 }
73 }
74
75 uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
76 {
77 switch(MifareAuthState) {
78 case masNone:
79 case masError:
80 return iso14443A_CRC_check(isResponse, data, len);
81 default:
82 return 2;
83 }
84 }
85
86 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
87 {
88 switch(cmd[0])
89 {
90 case ISO14443A_CMD_WUPA:
91 snprintf(exp,size,"WUPA");
92 break;
93 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
94 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
95 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
96 if(cmd[1] == 0x70)
97 {
98 snprintf(exp,size,"SELECT_UID"); break;
99 }else
100 {
101 snprintf(exp,size,"ANTICOLL"); break;
102 }
103 }
104 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
105 //95 20 = Anticollision of cascade level2
106 //95 70 = Select of cascade level2
107 if(cmd[2] == 0x70)
108 {
109 snprintf(exp,size,"SELECT_UID-2"); break;
110 }else
111 {
112 snprintf(exp,size,"ANTICOLL-2"); break;
113 }
114 }
115 case ISO14443A_CMD_REQA:
116 snprintf(exp,size,"REQA");
117 break;
118 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
119 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
120 case ISO14443A_CMD_HALT:
121 snprintf(exp,size,"HALT");
122 MifareAuthState = masNone;
123 break;
124 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
125 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
126 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
127 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
128 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
129 case MIFARE_AUTH_KEYA:
130 if ( cmdsize > 3) {
131 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
132 MifareAuthState = masNt;
133 } else {
134 // case MIFARE_ULEV1_VERSION : both 0x60.
135 snprintf(exp,size,"EV1 VERSION");
136 }
137 break;
138 case MIFARE_AUTH_KEYB:
139 MifareAuthState = masNt;
140 snprintf(exp,size,"AUTH-B(%d)",cmd[1]);
141 break;
142 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
143 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
144 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
145 case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break;
146 case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break;
147 case MIFARE_ULEV1_AUTH:
148 if ( cmdsize == 7 )
149 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
150 else
151 snprintf(exp,size,"PWD-AUTH");
152 break;
153 case MIFARE_ULEV1_FASTREAD:{
154 if ( cmdsize >=3 && cmd[2] <= 0xE6)
155 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
156 else
157 snprintf(exp,size,"?");
158 break;
159 }
160 case MIFARE_ULC_WRITE:{
161 if ( cmd[1] < 0x21 )
162 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
163 else
164 snprintf(exp,size,"?");
165 break;
166 }
167 case MIFARE_ULEV1_READ_CNT:{
168 if ( cmd[1] < 5 )
169 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
170 else
171 snprintf(exp,size,"?");
172 break;
173 }
174 case MIFARE_ULEV1_INCR_CNT:{
175 if ( cmd[1] < 5 )
176 snprintf(exp,size,"INCR(%d)",cmd[1]);
177 else
178 snprintf(exp,size,"?");
179 break;
180 }
181 case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break;
182 case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
183 case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break;
184 default: snprintf(exp,size,"?"); break;
185 }
186 return;
187 }
188
189 void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) {
190 if (!isResponse && cmdsize == 1) {
191 switch(cmd[0]) {
192 case ISO14443A_CMD_WUPA:
193 case ISO14443A_CMD_REQA:
194 MifareAuthState = masNone;
195 break;
196 default:
197 break;
198 }
199 }
200
201 // get UID
202 if (MifareAuthState == masNone) {
203 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
204 ClearAuthData();
205 AuthData.uid = bytes_to_num(&cmd[2], 4);
206 }
207 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
208 ClearAuthData();
209 AuthData.uid = bytes_to_num(&cmd[2], 4);
210 }
211 }
212
213 switch(MifareAuthState) {
214 case masNt:
215 if (cmdsize == 4 && isResponse) {
216 snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)");
217 MifareAuthState = masNrAr;
218 if (AuthData.first_auth)
219 AuthData.nt = bytes_to_num(cmd, 4);
220 else
221 AuthData.nt_enc = bytes_to_num(cmd, 4);
222 AuthData.nt_enc_par = parity[0];
223 return;
224 } else {
225 MifareAuthState = masError;
226 }
227 break;
228 case masNrAr:
229 if (cmdsize == 8 && !isResponse) {
230 snprintf(exp,size,"AUTH: nr ar (enc)");
231 MifareAuthState = masAt;
232 AuthData.nr_enc = bytes_to_num(cmd, 4);
233 AuthData.ar_enc = bytes_to_num(&cmd[4], 4);
234 AuthData.ar_enc_par = parity[0] << 4;
235 return;
236 } else {
237 MifareAuthState = masError;
238 }
239 break;
240 case masAt:
241 if (cmdsize == 4 && isResponse) {
242 snprintf(exp,size,"AUTH: at (enc)");
243 MifareAuthState = masAuthComplete;
244 AuthData.at_enc = bytes_to_num(cmd, 4);
245 AuthData.at_enc_par = parity[0];
246 return;
247 } else {
248 MifareAuthState = masError;
249 }
250 break;
251 default:
252 break;
253 }
254
255 if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError)))
256 annotateIso14443a(exp, size, cmd, cmdsize);
257
258 }
259
260 bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen) {
261 static struct Crypto1State *traceCrypto1;
262 static uint64_t mfLastKey;
263
264 *mfDataLen = 0;
265
266 if (MifareAuthState == masAuthComplete) {
267 if (traceCrypto1) {
268 crypto1_destroy(traceCrypto1);
269 traceCrypto1 = NULL;
270 }
271
272 MifareAuthState = masFirstData;
273 return false;
274 }
275
276 if (cmdsize > 32)
277 return false;
278
279 if (MifareAuthState == masFirstData) {
280 if (AuthData.first_auth) {
281 AuthData.ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64);
282 AuthData.ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96);
283
284 mfLastKey = GetCrypto1ProbableKey(&AuthData);
285 PrintAndLog(" | * | key | probable key:%010"PRIx64" Prng:%s ks2:%08x ks3:%08x | |",
286 mfLastKey,
287 validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD",
288 AuthData.ks2,
289 AuthData.ks3);
290
291 AuthData.first_auth = false;
292
293 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
294 } else {
295 if (traceCrypto1) {
296 crypto1_destroy(traceCrypto1);
297 traceCrypto1 = NULL;
298 }
299
300 // check last used key
301 if (false && mfLastKey) {
302 if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize, parity)) {
303 PrintAndLog(" | * | key | last used key:%010"PRIx64" ks2:%08x ks3:%08x | |",
304 mfLastKey,
305 AuthData.ks2,
306 AuthData.ks3);
307
308 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
309 };
310 }
311
312 // check default keys
313 if (false && !traceCrypto1) {
314 for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){
315 if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize, parity)) {
316 PrintAndLog(" | * | key | default key:%010"PRIx64" ks2:%08x ks3:%08x | |",
317 MifareDefaultKeys[defaultKeyCounter],
318 AuthData.ks2,
319 AuthData.ks3);
320
321 mfLastKey = MifareDefaultKeys[defaultKeyCounter];
322 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
323 break;
324 };
325 }
326 }
327
328 // nested
329 if (!traceCrypto1 && validate_prng_nonce(AuthData.nt)) {
330 uint32_t ntx = prng_successor(AuthData.nt, 90);
331 for (int i = 0; i < 16383; i++) {
332 ntx = prng_successor(ntx, 1);
333 if (NTParityChk(&AuthData, ntx)){
334
335 uint32_t ks2 = AuthData.ar_enc ^ prng_successor(ntx, 64);
336 uint32_t ks3 = AuthData.at_enc ^ prng_successor(ntx, 96);
337 struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3);
338 memcpy(mfData, cmd, cmdsize);
339 mf_crypto1_decrypt(pcs, mfData, cmdsize, 0);
340
341 crypto1_destroy(pcs);
342 if (CheckCrypto1Parity(cmd, cmdsize, mfData, parity) && CheckCrc14443(CRC_14443_A, mfData, cmdsize)) {
343 AuthData.ks2 = ks2;
344 AuthData.ks3 = ks3;
345
346 AuthData.nt = ntx;
347 mfLastKey = GetCrypto1ProbableKey(&AuthData);
348 PrintAndLog(" | * | key | nested probable key:%010"PRIx64" ks2:%08x ks3:%08x | |",
349 mfLastKey,
350 AuthData.ks2,
351 AuthData.ks3);
352
353 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
354 break;
355 }
356 }
357 }
358 }
359
360 //hardnested
361 if (!traceCrypto1) {
362 printf("hardnested not implemented. uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc);
363 }
364 }
365
366
367
368 MifareAuthState = masData;
369 }
370
371 if (MifareAuthState == masData && traceCrypto1) {
372 memcpy(mfData, cmd, cmdsize);
373 mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0);
374 *mfDataLen = cmdsize;
375 }
376
377 return *mfDataLen > 0;
378 }
379
380 bool NTParityChk(TAuthData *ad, uint32_t ntx) {
381 if (
382 (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) ||
383 (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) ||
384 (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01))
385 )
386 return false;
387
388 uint32_t ar = prng_successor(ntx, 64);
389 if (
390 (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) ||
391 (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) ||
392 (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01))
393 )
394 return false;
395
396 uint32_t at = prng_successor(ntx, 96);
397 if (
398 (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) ||
399 (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) ||
400 (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) ||
401 (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01))
402 )
403 return false;
404
405 return true;
406 }
407
408 bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity) {
409 uint8_t buf[32] = {0};
410 struct Crypto1State *pcs;
411
412 AuthData.ks2 = 0;
413 AuthData.ks3 = 0;
414
415 pcs = crypto1_create(key);
416 uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc;
417 uint32_t ar = prng_successor(nt1, 64);
418 uint32_t at = prng_successor(nt1, 96);
419
420 crypto1_word(pcs, ad->nr_enc, 1);
421 // uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc; // if needs deciphered nr
422 uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc;
423 uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc;
424
425 if (!(ar == ar1 && at == at1 && NTParityChk(ad, nt1)))
426 return false;
427
428 memcpy(buf, cmd, cmdsize);
429 mf_crypto1_decrypt(pcs, buf, cmdsize, 0);
430
431 crypto1_destroy(pcs);
432
433 if (!CheckCrypto1Parity(cmd, cmdsize, buf, parity))
434 return false;
435
436 if(!CheckCrc14443(CRC_14443_A, buf, cmdsize))
437 return false;
438
439 AuthData.nt = nt1;
440 AuthData.ks2 = AuthData.ar_enc ^ ar;
441 AuthData.ks3 = AuthData.at_enc ^ at;
442
443 return true;
444 }
445
446 bool CheckCrypto1Parity(uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, uint8_t *parity_enc) {
447 for (int i = 0; i < cmdsize - 1; i++) {
448 if (oddparity8(cmd[i]) ^ (cmd[i + 1] & 0x01) ^ ((parity_enc[i / 8] >> (7 - i % 8)) & 0x01) ^ (cmd_enc[i + 1] & 0x01))
449 return false;
450 }
451
452 return true;
453 }
454
455 uint64_t GetCrypto1ProbableKey(TAuthData *ad) {
456 struct Crypto1State *revstate = lfsr_recovery64(ad->ks2, ad->ks3);
457 lfsr_rollback_word(revstate, 0, 0);
458 lfsr_rollback_word(revstate, 0, 0);
459 lfsr_rollback_word(revstate, ad->nr_enc, 1);
460 lfsr_rollback_word(revstate, ad->uid ^ ad->nt, 0);
461
462 uint64_t lfsr = 0;
463 crypto1_get_lfsr(revstate, &lfsr);
464 crypto1_destroy(revstate);
465
466 return lfsr;
467 }
Impressum, Datenschutz