]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflist.c
7b3fdca505a2427f4c4d06fec0adf99b4c6451bf
[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 MifareAuthState = masNone;
93 break;
94 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
95 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
96 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
97 if(cmd[1] == 0x70)
98 {
99 snprintf(exp,size,"SELECT_UID"); break;
100 }else
101 {
102 snprintf(exp,size,"ANTICOLL"); break;
103 }
104 }
105 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
106 //95 20 = Anticollision of cascade level2
107 //95 70 = Select of cascade level2
108 if(cmd[2] == 0x70)
109 {
110 snprintf(exp,size,"SELECT_UID-2"); break;
111 }else
112 {
113 snprintf(exp,size,"ANTICOLL-2"); break;
114 }
115 }
116 case ISO14443A_CMD_REQA:
117 snprintf(exp,size,"REQA");
118 MifareAuthState = masNone;
119 break;
120 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
121 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
122 case ISO14443A_CMD_HALT:
123 snprintf(exp,size,"HALT");
124 MifareAuthState = masNone;
125 break;
126 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
127 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
128 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
129 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
130 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
131 case MIFARE_AUTH_KEYA:
132 if ( cmdsize > 3) {
133 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
134 MifareAuthState = masNt;
135 } else {
136 // case MIFARE_ULEV1_VERSION : both 0x60.
137 snprintf(exp,size,"EV1 VERSION");
138 }
139 break;
140 case MIFARE_AUTH_KEYB:
141 MifareAuthState = masNt;
142 snprintf(exp,size,"AUTH-B(%d)",cmd[1]);
143 break;
144 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
145 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
146 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
147 case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break;
148 case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break;
149 case MIFARE_ULEV1_AUTH:
150 if ( cmdsize == 7 )
151 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
152 else
153 snprintf(exp,size,"PWD-AUTH");
154 break;
155 case MIFARE_ULEV1_FASTREAD:{
156 if ( cmdsize >=3 && cmd[2] <= 0xE6)
157 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
158 else
159 snprintf(exp,size,"?");
160 break;
161 }
162 case MIFARE_ULC_WRITE:{
163 if ( cmd[1] < 0x21 )
164 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
165 else
166 snprintf(exp,size,"?");
167 break;
168 }
169 case MIFARE_ULEV1_READ_CNT:{
170 if ( cmd[1] < 5 )
171 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
172 else
173 snprintf(exp,size,"?");
174 break;
175 }
176 case MIFARE_ULEV1_INCR_CNT:{
177 if ( cmd[1] < 5 )
178 snprintf(exp,size,"INCR(%d)",cmd[1]);
179 else
180 snprintf(exp,size,"?");
181 break;
182 }
183 case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break;
184 case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
185 case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break;
186 default: snprintf(exp,size,"?"); break;
187 }
188 return;
189 }
190
191 void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) {
192 // get UID
193 if (MifareAuthState == masNone) {
194 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
195 ClearAuthData();
196 AuthData.uid = bytes_to_num(&cmd[2], 4);
197 }
198 if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
199 ClearAuthData();
200 AuthData.uid = bytes_to_num(&cmd[2], 4);
201 }
202 }
203
204 switch(MifareAuthState) {
205 case masNt:
206 if (cmdsize == 4 && isResponse) {
207 snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)");
208 MifareAuthState = masNrAr;
209 if (AuthData.first_auth)
210 AuthData.nt = bytes_to_num(cmd, 4);
211 else
212 AuthData.nt_enc = bytes_to_num(cmd, 4);
213 AuthData.nt_enc_par = parity[0];
214 return;
215 } else {
216 MifareAuthState = masError;
217 }
218 break;
219 case masNrAr:
220 if (cmdsize == 8 && !isResponse) {
221 snprintf(exp,size,"AUTH: nr ar (enc)");
222 MifareAuthState = masAt;
223 AuthData.nr_enc = bytes_to_num(cmd, 4);
224 AuthData.ar_enc = bytes_to_num(&cmd[4], 4);
225 AuthData.ar_enc_par = parity[0] << 4;
226 return;
227 } else {
228 MifareAuthState = masError;
229 }
230 break;
231 case masAt:
232 if (cmdsize == 4 && isResponse) {
233 snprintf(exp,size,"AUTH: at (enc)");
234 MifareAuthState = masAuthComplete;
235 AuthData.at_enc = bytes_to_num(cmd, 4);
236 AuthData.at_enc_par = parity[0];
237 return;
238 } else {
239 MifareAuthState = masError;
240 }
241 break;
242 default:
243 break;
244 }
245
246 if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError)))
247 annotateIso14443a(exp, size, cmd, cmdsize);
248
249 }
250
251 bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *mfData, size_t *mfDataLen) {
252 static struct Crypto1State *traceCrypto1;
253 static uint64_t mfLastKey;
254
255 *mfDataLen = 0;
256
257 if (MifareAuthState == masAuthComplete) {
258 if (traceCrypto1) {
259 crypto1_destroy(traceCrypto1);
260 }
261
262 MifareAuthState = masFirstData;
263 return false;
264 }
265
266 if (cmdsize > 32)
267 return false;
268
269 if (MifareAuthState == masFirstData) {
270 if (AuthData.first_auth) {
271 AuthData.ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64);
272 AuthData.ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96);
273 struct Crypto1State *revstate = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
274 lfsr_rollback_word(revstate, 0, 0);
275 lfsr_rollback_word(revstate, 0, 0);
276 lfsr_rollback_word(revstate, AuthData.nr_enc, 1);
277 lfsr_rollback_word(revstate, AuthData.uid ^ AuthData.nt, 0);
278
279 uint64_t lfsr = 0;
280 crypto1_get_lfsr(revstate, &lfsr);
281 crypto1_destroy(revstate);
282 mfLastKey = lfsr;
283 PrintAndLog(" | * | key | probable key:%x%x Prng:%s ks2:%08x ks3:%08x | |",
284 (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),
285 validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD",
286 AuthData.ks2,
287 AuthData.ks3);
288
289 AuthData.first_auth = false;
290
291 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
292 } else {
293 printf("uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc);
294
295 // check last used key
296 if (mfLastKey) {
297 if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize)) {
298 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
299 };
300 }
301
302 // check default keys
303 if (!traceCrypto1) {
304 for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){
305 if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize)) {
306 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
307 break;
308 };
309 }
310 }
311
312 // nested
313 if (!traceCrypto1 && validate_prng_nonce(AuthData.nt)) {
314 uint32_t ntx = prng_successor(AuthData.nt, 90);
315 for (int i = 0; i < 16383; i++) {
316 ntx = prng_successor(ntx, 1);
317 if (NTParityChk(&AuthData, ntx)){
318
319 uint32_t ks2 = AuthData.ar_enc ^ prng_successor(ntx, 64);
320 uint32_t ks3 = AuthData.at_enc ^ prng_successor(ntx, 96);
321 struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3);
322 memcpy(mfData, cmd, cmdsize);
323 mf_crypto1_decrypt(pcs, mfData, cmdsize, 0);
324
325 crypto1_destroy(pcs);
326 if (CheckCrc14443(CRC_14443_A, mfData, cmdsize)) {
327 AuthData.ks2 = ks2;
328 AuthData.ks3 = ks3;
329 traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3);
330 break;
331 }
332 }
333 }
334 if (traceCrypto1)
335 printf("key> nt=%08x nonce distance=%d \n", ntx, nonce_distance(AuthData.nt, ntx));
336 else
337 printf("key> don't have any valid nt( \n");
338 }
339
340 //hardnested
341 if (!traceCrypto1) {
342 }
343 }
344
345
346
347 MifareAuthState = masData;
348 }
349
350 if (MifareAuthState == masData && traceCrypto1) {
351 memcpy(mfData, cmd, cmdsize);
352 mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0);
353 *mfDataLen = cmdsize;
354 }
355
356 return *mfDataLen > 0;
357 }
358
359 bool NTParityChk(TAuthData *ad, uint32_t ntx) {
360 if (
361 (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) ||
362 (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) ||
363 (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01))
364 )
365 return false;
366
367 uint32_t ar = prng_successor(ntx, 64);
368 if (
369 (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) ||
370 (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) ||
371 (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01))
372 )
373 return false;
374
375 uint32_t at = prng_successor(ntx, 96);
376 if (
377 (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) ||
378 (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) ||
379 (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) ||
380 (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01))
381 )
382 return false;
383
384 return true;
385 }
386
387 bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize) {
388 uint8_t buf[32] = {0};
389 struct Crypto1State *pcs;
390
391 pcs = crypto1_create(key);
392 uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc;
393 uint32_t ar = prng_successor(nt1, 64);
394 uint32_t at = prng_successor(nt1, 96);
395 printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", ad->uid, nt1, printBitsPar(&ad->nt_enc_par, 4), ar, at);
396 uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc;
397 uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc;
398 uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc;
399 printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1);
400
401 if (NTParityChk(ad, nt1))
402 printf("key> the same key test OK. key=%x%x\n", (unsigned int)((key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(key & 0xFFFFFFFF));
403 else {
404 printf("key> the same key test. check nt parity error.\n");
405 return false;
406 }
407
408 memcpy(buf, cmd, cmdsize);
409 mf_crypto1_decrypt(pcs, buf, cmdsize, 0);
410
411 crypto1_destroy(pcs);
412
413 if(CheckCrc14443(CRC_14443_A, buf, cmdsize)) {
414 AuthData.ks2 = AuthData.ar_enc ^ ar;
415 AuthData.ks3 = AuthData.at_enc ^ at;
416 return true;
417 } else {
418 return false;
419 }
420 }
Impressum, Datenschutz