]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
b3f7acb2a1e1d0f12a16e2dd7341fdfc172e4d7e
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
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
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
16 #include "proxmark3.h"
20 #include "mifarehost.h"
22 #include "nonce2key/nonce2key.h"
24 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
27 static int CmdHelp ( const char * Cmd
);
29 int CmdHF14AMifare ( const char * Cmd
)
32 uint32_t nt
= 0 , nr
= 0 ;
33 uint64_t par_list
= 0 , ks_list
= 0 , r_key
= 0 ;
36 UsbCommand c
= { CMD_READER_MIFARE
, { true , 0 , 0 }};
39 printf ( "------------------------------------------------------------------------- \n " );
40 printf ( "Executing command. Expected execution time: 25sec on average :-) \n " );
41 printf ( "Press button on the proxmark3 device to abort both proxmark3 and client. \n " );
42 printf ( "------------------------------------------------------------------------- \n " );
51 int c
= getchar (); ( void ) c
;
60 printf ( " \n aborted via keyboard! \n " );
65 if ( WaitForResponseTimeout ( CMD_ACK
, & resp
, 1000 )) {
67 uid
= ( uint32_t ) bytes_to_num ( resp
. d
. asBytes
+ 0 , 4 );
68 nt
= ( uint32_t ) bytes_to_num ( resp
. d
. asBytes
+ 4 , 4 );
69 par_list
= bytes_to_num ( resp
. d
. asBytes
+ 8 , 8 );
70 ks_list
= bytes_to_num ( resp
. d
. asBytes
+ 16 , 8 );
71 nr
= bytes_to_num ( resp
. d
. asBytes
+ 24 , 4 );
74 case - 1 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
75 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests). \n " ); break ;
76 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable). \n " ); break ;
77 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
78 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour. \n " ); break ;
88 if ( isOK
!= 1 ) return 1 ;
90 // execute original function from util nonce2key
91 if ( nonce2key ( uid
, nt
, nr
, par_list
, ks_list
, & r_key
)) {
93 PrintAndLog ( "Key not found (lfsr_common_prefix list is null). Nt=%08x" , nt
);
94 PrintAndLog ( "Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce..." );
99 printf ( "------------------------------------------------------------------ \n " );
100 PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , r_key
);
107 int CmdHF14AMfWrBl ( const char * Cmd
)
111 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
112 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
117 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
118 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
122 blockNo
= param_get8 ( Cmd
, 0 );
123 cmdp
= param_getchar ( Cmd
, 1 );
125 PrintAndLog ( "Key type must be A or B" );
128 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
129 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
130 PrintAndLog ( "Key must include 12 HEX symbols" );
133 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
134 PrintAndLog ( "Block data must include 32 HEX symbols" );
137 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
138 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
140 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
141 memcpy ( c
. d
. asBytes
, key
, 6 );
142 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
146 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
147 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
148 PrintAndLog ( "isOk:%02x" , isOK
);
150 PrintAndLog ( "Command execute timeout" );
156 int CmdHF14AMfRdBl ( const char * Cmd
)
160 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
166 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
167 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
171 blockNo
= param_get8 ( Cmd
, 0 );
172 cmdp
= param_getchar ( Cmd
, 1 );
174 PrintAndLog ( "Key type must be A or B" );
177 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
178 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
179 PrintAndLog ( "Key must include 12 HEX symbols" );
182 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
184 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
185 memcpy ( c
. d
. asBytes
, key
, 6 );
189 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
190 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
191 uint8_t * data
= resp
. d
. asBytes
;
194 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
196 PrintAndLog ( "isOk:%02x" , isOK
);
198 PrintAndLog ( "Command execute timeout" );
204 int CmdHF14AMfRdSc ( const char * Cmd
)
207 uint8_t sectorNo
= 0 ;
209 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
211 uint8_t * data
= NULL
;
215 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
216 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
220 sectorNo
= param_get8 ( Cmd
, 0 );
222 PrintAndLog ( "Sector number must be less than 40" );
225 cmdp
= param_getchar ( Cmd
, 1 );
226 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
227 PrintAndLog ( "Key type must be A or B" );
230 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
231 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
232 PrintAndLog ( "Key must include 12 HEX symbols" );
235 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
237 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
238 memcpy ( c
. d
. asBytes
, key
, 6 );
243 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
244 isOK
= resp
. arg
[ 0 ] & 0xff ;
245 data
= resp
. d
. asBytes
;
247 PrintAndLog ( "isOk:%02x" , isOK
);
249 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
250 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
252 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
255 PrintAndLog ( "Command execute timeout" );
261 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
266 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
270 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
279 int CmdHF14AMfDump ( const char * Cmd
)
281 uint8_t sectorNo
, blockNo
;
285 uint8_t rights
[ 40 ][ 4 ];
286 uint8_t carddata
[ 256 ][ 16 ];
287 uint8_t numSectors
= 16 ;
294 char cmdp
= param_getchar ( Cmd
, 0 );
296 case '0' : numSectors
= 5 ; break ;
298 case '\0' : numSectors
= 16 ; break ;
299 case '2' : numSectors
= 32 ; break ;
300 case '4' : numSectors
= 40 ; break ;
301 default : numSectors
= 16 ;
304 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
305 PrintAndLog ( "Usage: hf mf dump [card memory]" );
306 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
308 PrintAndLog ( "Samples: hf mf dump" );
309 PrintAndLog ( " hf mf dump 4" );
313 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
314 PrintAndLog ( "Could not find file dumpkeys.bin" );
318 // Read keys A from file
319 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
320 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fin
);
321 if ( bytes_read
!= 6 ) {
322 PrintAndLog ( "File reading error." );
328 // Read keys B from file
329 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
330 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fin
);
331 if ( bytes_read
!= 6 ) {
332 PrintAndLog ( "File reading error." );
340 PrintAndLog ( "|-----------------------------------------|" );
341 PrintAndLog ( "|------ Reading sector access bits...-----|" );
342 PrintAndLog ( "|-----------------------------------------|" );
344 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
345 for ( tries
= 0 ; tries
< 3 ; tries
++) {
346 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
347 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
350 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
351 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
352 uint8_t * data
= resp
. d
. asBytes
;
354 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
355 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
356 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
357 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
359 } else if ( tries
== 2 ) { // on last try set defaults
360 PrintAndLog ( "Could not get access rights for sector %2d. Trying with defaults..." , sectorNo
);
361 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
362 rights
[ sectorNo
][ 3 ] = 0x01 ;
365 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
366 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
367 rights
[ sectorNo
][ 3 ] = 0x01 ;
372 PrintAndLog ( "|-----------------------------------------|" );
373 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
374 PrintAndLog ( "|-----------------------------------------|" );
377 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
378 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
379 bool received
= false ;
380 for ( tries
= 0 ; tries
< 3 ; tries
++) {
381 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
382 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
383 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
385 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
386 } else { // data block. Check if it can be read with key A or key B
387 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
388 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
389 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
390 memcpy ( c
. d
. asBytes
, keyB
[ sectorNo
], 6 );
392 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
393 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
395 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
397 } else { // key A would work
398 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
399 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
401 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
405 isOK
= resp
. arg
[ 0 ] & 0xff ;
411 isOK
= resp
. arg
[ 0 ] & 0xff ;
412 uint8_t * data
= resp
. d
. asBytes
;
413 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
414 data
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
415 data
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
416 data
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
417 data
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
418 data
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
419 data
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
420 data
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
421 data
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
422 data
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
423 data
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
424 data
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
425 data
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
428 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
429 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
431 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
437 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
444 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
445 PrintAndLog ( "Could not create file name dumpdata.bin" );
448 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
449 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
451 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
457 int CmdHF14AMfRestore ( const char * Cmd
)
459 uint8_t sectorNo
, blockNo
;
461 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
462 uint8_t bldata
[ 16 ] = { 0x00 };
470 char cmdp
= param_getchar ( Cmd
, 0 );
472 case '0' : numSectors
= 5 ; break ;
474 case '\0' : numSectors
= 16 ; break ;
475 case '2' : numSectors
= 32 ; break ;
476 case '4' : numSectors
= 40 ; break ;
477 default : numSectors
= 16 ;
480 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
481 PrintAndLog ( "Usage: hf mf restore [card memory]" );
482 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
484 PrintAndLog ( "Samples: hf mf restore" );
485 PrintAndLog ( " hf mf restore 4" );
489 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
490 PrintAndLog ( "Could not find file dumpkeys.bin" );
494 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
495 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
496 if ( bytes_read
!= 6 ) {
497 PrintAndLog ( "File reading error (dumpkeys.bin)." );
503 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
504 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
505 if ( bytes_read
!= 6 ) {
506 PrintAndLog ( "File reading error (dumpkeys.bin)." );
514 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
515 PrintAndLog ( "Could not find file dumpdata.bin" );
518 PrintAndLog ( "Restoring dumpdata.bin to card" );
520 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
521 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
522 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
523 memcpy ( c
. d
. asBytes
, key
, 6 );
525 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
526 if ( bytes_read
!= 16 ) {
527 PrintAndLog ( "File reading error (dumpdata.bin)." );
532 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
533 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
534 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
535 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
536 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
537 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
538 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
539 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
540 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
541 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
542 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
543 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
544 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
547 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
549 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
553 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
554 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
555 PrintAndLog ( "isOk:%02x" , isOK
);
557 PrintAndLog ( "Command execute timeout" );
573 int CmdHF14AMfNested ( const char * Cmd
)
575 int i
, j
, res
, iterations
;
576 sector_t
* e_sector
= NULL
;
579 uint8_t trgBlockNo
= 0 ;
580 uint8_t trgKeyType
= 0 ;
581 uint8_t SectorsCnt
= 0 ;
582 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
583 uint8_t keyBlock
[ 14 * 6 ];
585 bool transferToEml
= false ;
587 bool createDumpFile
= false ;
589 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
590 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
595 PrintAndLog ( "Usage:" );
596 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]" );
597 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
598 PrintAndLog ( " <target block number> <target key A/B> [t]" );
599 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
600 PrintAndLog ( "t - transfer keys into emulator memory" );
601 PrintAndLog ( "d - write keys to binary file" );
603 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
604 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
605 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
606 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
610 cmdp
= param_getchar ( Cmd
, 0 );
611 blockNo
= param_get8 ( Cmd
, 1 );
612 ctmp
= param_getchar ( Cmd
, 2 );
614 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
615 PrintAndLog ( "Key type must be A or B" );
619 if ( ctmp
!= 'A' && ctmp
!= 'a' )
622 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
623 PrintAndLog ( "Key must include 12 HEX symbols" );
627 if ( cmdp
== 'o' || cmdp
== 'O' ) {
629 trgBlockNo
= param_get8 ( Cmd
, 4 );
630 ctmp
= param_getchar ( Cmd
, 5 );
631 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
632 PrintAndLog ( "Target key type must be A or B" );
635 if ( ctmp
!= 'A' && ctmp
!= 'a' )
640 case '0' : SectorsCnt
= 05 ; break ;
641 case '1' : SectorsCnt
= 16 ; break ;
642 case '2' : SectorsCnt
= 32 ; break ;
643 case '4' : SectorsCnt
= 40 ; break ;
644 default : SectorsCnt
= 16 ;
648 ctmp
= param_getchar ( Cmd
, 4 );
649 if ( ctmp
== 't' || ctmp
== 'T' ) transferToEml
= true ;
650 else if ( ctmp
== 'd' || ctmp
== 'D' ) createDumpFile
= true ;
652 ctmp
= param_getchar ( Cmd
, 6 );
653 transferToEml
|= ( ctmp
== 't' || ctmp
== 'T' );
654 transferToEml
|= ( ctmp
== 'd' || ctmp
== 'D' );
657 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
658 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
661 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
662 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
663 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
664 default : PrintAndLog ( "Unknown Error. \n " );
668 key64
= bytes_to_num ( keyBlock
, 6 );
670 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
672 // transfer key to the emulator
674 uint8_t sectortrailer
;
675 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
676 sectortrailer
= ( trgBlockNo
& 0x03 ) + 3 ;
677 } else { // 16 block sector
678 sectortrailer
= ( trgBlockNo
& 0x0f ) + 15 ;
680 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
683 num_to_bytes ( key64
, 6 , keyBlock
);
685 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
686 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
689 PrintAndLog ( "No valid key found" );
692 else { // ------------------------------------ multiple sectors working
694 msclock1
= msclock ();
696 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
697 if ( e_sector
== NULL
) return 1 ;
699 //test current key and additional standard keys first
700 memcpy ( keyBlock
, key
, 6 );
701 num_to_bytes ( 0xffffffffffff , 6 , ( uint8_t *)( keyBlock
+ 1 * 6 ));
702 num_to_bytes ( 0x000000000000 , 6 , ( uint8_t *)( keyBlock
+ 2 * 6 ));
703 num_to_bytes ( 0xa0a1a2a3a4a5 , 6 , ( uint8_t *)( keyBlock
+ 3 * 6 ));
704 num_to_bytes ( 0xb0b1b2b3b4b5 , 6 , ( uint8_t *)( keyBlock
+ 4 * 6 ));
705 num_to_bytes ( 0xaabbccddeeff , 6 , ( uint8_t *)( keyBlock
+ 5 * 6 ));
706 num_to_bytes ( 0x4d3a99c351dd , 6 , ( uint8_t *)( keyBlock
+ 6 * 6 ));
707 num_to_bytes ( 0x1a982c7e459a , 6 , ( uint8_t *)( keyBlock
+ 7 * 6 ));
708 num_to_bytes ( 0xd3f7d3f7d3f7 , 6 , ( uint8_t *)( keyBlock
+ 8 * 6 ));
709 num_to_bytes ( 0x714c5c886e97 , 6 , ( uint8_t *)( keyBlock
+ 9 * 6 ));
710 num_to_bytes ( 0x587ee5f9350f , 6 , ( uint8_t *)( keyBlock
+ 10 * 6 ));
711 num_to_bytes ( 0xa0478cc39091 , 6 , ( uint8_t *)( keyBlock
+ 11 * 6 ));
712 num_to_bytes ( 0x533cb6c723f6 , 6 , ( uint8_t *)( keyBlock
+ 12 * 6 ));
713 num_to_bytes ( 0x8fd0a4f256e9 , 6 , ( uint8_t *)( keyBlock
+ 13 * 6 ));
715 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
716 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
717 for ( j
= 0 ; j
< 2 ; j
++) {
718 if ( e_sector
[ i
]. foundKey
[ j
]) continue ;
720 res
= mfCheckKeys ( FirstBlockOfSector ( i
), j
, true , 6 , keyBlock
, & key64
);
723 e_sector
[ i
]. Key
[ j
] = key64
;
724 e_sector
[ i
]. foundKey
[ j
] = 1 ;
731 PrintAndLog ( "nested..." );
732 bool calibrate
= true ;
733 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
734 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
735 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
736 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
737 PrintAndLog ( "-----------------------------------------------" );
738 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
741 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
742 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
743 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
744 default : PrintAndLog ( "Unknown Error. \n " );
754 key64
= bytes_to_num ( keyBlock
, 6 );
756 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
757 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
758 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
764 printf ( "Time in nested: %1.3f (%1.3f sec per key) \n\n " , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
766 PrintAndLog ( "----------------------------------------------- \n Iterations count: %d \n\n " , iterations
);
768 PrintAndLog ( "|---|----------------|---|----------------|---|" );
769 PrintAndLog ( "|sec|key A |res|key B |res|" );
770 PrintAndLog ( "|---|----------------|---|----------------|---|" );
771 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
772 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
773 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
775 PrintAndLog ( "|---|----------------|---|----------------|---|" );
777 // transfer them to the emulator
779 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
780 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
781 if ( e_sector
[ i
]. foundKey
[ 0 ])
782 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
783 if ( e_sector
[ i
]. foundKey
[ 1 ])
784 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
785 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
790 if ( createDumpFile
) {
791 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
792 PrintAndLog ( "Could not create file dumpkeys.bin" );
796 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
797 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
798 if ( e_sector
[ i
]. foundKey
[ 0 ]){
799 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
800 fwrite ( tempkey
, 1 , 6 , fkeys
);
803 fwrite ( & standart
, 1 , 6 , fkeys
);
806 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
807 if ( e_sector
[ i
]. foundKey
[ 1 ]){
808 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
809 fwrite ( tempkey
, 1 , 6 , fkeys
);
812 fwrite ( & standart
, 1 , 6 , fkeys
);
823 int CmdHF14AMfChk ( const char * Cmd
)
826 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
827 PrintAndLog ( " * - all sectors" );
828 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
829 PrintAndLog ( "d - write keys to binary file \n " );
830 PrintAndLog ( "t - write keys to emulator memory" );
831 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
832 PrintAndLog ( " hf mf chk *1 ? t" );
833 PrintAndLog ( " hf mf chk *1 ? d" );
838 char filename
[ FILE_PATH_SIZE
]={ 0 };
840 uint8_t * keyBlock
= NULL
, * p
;
841 uint8_t stKeyBlock
= 20 ;
847 uint8_t SectorsCnt
= 1 ;
851 int transferToEml
= 0 ;
852 int createDumpFile
= 0 ;
854 keyBlock
= calloc ( stKeyBlock
, 6 );
855 if ( keyBlock
== NULL
) return 1 ;
857 uint64_t defaultKeys
[] =
859 0xffffffffffff , // Default key (first key used by program if no user defined key)
860 0x000000000000 , // Blank key
861 0xa0a1a2a3a4a5 , // NFCForum MAD key
873 int defaultKeysSize
= sizeof ( defaultKeys
) / sizeof ( uint64_t );
875 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
877 num_to_bytes ( defaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
880 if ( param_getchar ( Cmd
, 0 )== '*' ) {
882 switch ( param_getchar ( Cmd
+ 1 , 0 )) {
883 case '0' : SectorsCnt
= 5 ; break ;
884 case '1' : SectorsCnt
= 16 ; break ;
885 case '2' : SectorsCnt
= 32 ; break ;
886 case '4' : SectorsCnt
= 40 ; break ;
887 default : SectorsCnt
= 16 ;
891 blockNo
= param_get8 ( Cmd
, 0 );
893 ctmp
= param_getchar ( Cmd
, 1 );
905 PrintAndLog ( "Key type must be A , B or ?" );
909 ctmp
= param_getchar ( Cmd
, 2 );
910 if ( ctmp
== 't' || ctmp
== 'T' ) transferToEml
= 1 ;
911 else if ( ctmp
== 'd' || ctmp
== 'D' ) createDumpFile
= 1 ;
913 for ( i
= transferToEml
|| createDumpFile
; param_getchar ( Cmd
, 2 + i
); i
++) {
914 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
915 if ( stKeyBlock
- keycnt
< 2 ) {
916 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
918 PrintAndLog ( "Cannot allocate memory for Keys" );
924 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
925 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
926 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
930 if ( param_getstr ( Cmd
, 2 + i
, filename
) >= FILE_PATH_SIZE
) {
931 PrintAndLog ( "File name too long" );
936 if ( ( f
= fopen ( filename
, "r" )) ) {
937 while ( fgets ( buf
, sizeof ( buf
), f
) ){
938 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
941 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
943 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
945 if (! isxdigit ( buf
[ 0 ])){
946 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
952 if ( stKeyBlock
- keycnt
< 2 ) {
953 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
955 PrintAndLog ( "Cannot allocate memory for defKeys" );
962 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
963 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
964 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
966 memset ( buf
, 0 , sizeof ( buf
));
970 PrintAndLog ( "File: %s: not found or locked." , filename
);
979 PrintAndLog ( "No key specified, trying default keys" );
980 for (; keycnt
< defaultKeysSize
; keycnt
++)
981 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
982 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
983 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
986 // initialize storage for found keys
987 bool validKey
[ 2 ][ 40 ];
988 uint8_t foundKey
[ 2 ][ 40 ][ 6 ];
989 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
990 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
991 validKey
[ t
][ sectorNo
] = false ;
992 for ( uint16_t i
= 0 ; i
< 6 ; i
++) {
993 foundKey
[ t
][ sectorNo
][ i
] = 0xff ;
998 for ( int t
= ! keyType
; t
< 2 ; keyType
== 2 ?( t
++):( t
= 2 ) ) {
1000 for ( int i
= 0 ; i
< SectorsCnt
; ++ i
) {
1001 PrintAndLog ( "--sector:%2d, block:%3d, key type:%C, key count:%2d " , i
, b
, t
? 'B' : 'A' , keycnt
);
1002 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1003 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1004 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1005 res
= mfCheckKeys ( b
, t
, true , size
, & keyBlock
[ 6 * c
], & key64
);
1008 PrintAndLog ( "Found valid key:[%012" PRIx64
"]" , key64
);
1009 num_to_bytes ( key64
, 6 , foundKey
[ t
][ i
]);
1010 validKey
[ t
][ i
] = true ;
1013 PrintAndLog ( "Command execute timeout" );
1016 b
< 127 ?( b
+= 4 ):( b
+= 16 );
1020 if ( transferToEml
) {
1022 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1023 if ( validKey
[ 0 ][ sectorNo
] || validKey
[ 1 ][ sectorNo
]) {
1024 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1025 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1026 if ( validKey
[ t
][ sectorNo
]) {
1027 memcpy ( block
+ t
* 10 , foundKey
[ t
][ sectorNo
], 6 );
1030 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1033 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1036 if ( createDumpFile
) {
1037 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1038 if ( fkeys
== NULL
) {
1039 PrintAndLog ( "Could not create file dumpkeys.bin" );
1043 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1044 fwrite ( foundKey
[ t
], 1 , 6 * SectorsCnt
, fkeys
);
1047 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1055 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1056 #define ATTACK_KEY_COUNT 8 // keep same as define in iso14443a.c -> Mifare1ksim()
1062 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1063 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1065 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1066 memset ( stSector
, 0x00 , sizeof ( stSector
));
1067 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1068 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1070 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1071 if ( ar_resp
[ i
]. ar2
> 0 ) {
1072 //PrintAndLog("DEBUG: Trying sector %d, cuid %08x, nt %08x, ar %08x, nr %08x, ar2 %08x, nr2 %08x",ar_resp[i].sector, ar_resp[i].cuid,ar_resp[i].nonce,ar_resp[i].ar,ar_resp[i].nr,ar_resp[i].ar2,ar_resp[i].nr2);
1073 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1074 PrintAndLog ( " Found Key%s for sector %02d: [%04x%08x]" , ( ar_resp
[ i
]. keytype
) ? "B" : "A" , ar_resp
[ i
]. sector
, ( uint32_t ) ( key
>> 32 ), ( uint32_t ) ( key
& 0xFFFFFFFF ));
1076 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1077 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1078 if ( ar_resp
[ i
]. keytype
== 0 ) {
1080 sector_trailer
[ ii
]. keyA
= key
;
1081 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1086 sector_trailer
[ ii
]. keyB
= key
;
1087 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1093 } else if ( tryMfk32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1094 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1095 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1097 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1098 , keyType
? "B" : "A"
1103 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1104 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1107 sector_trailer
[ ii
]. keyA
= key
;
1108 stSector
[ ii
] = sectorNum
;
1113 sector_trailer
[ ii
]. keyB
= key
;
1114 stSector
[ ii
] = sectorNum
;
1124 //set emulator memory for keys
1125 if ( setEmulatorMem
) {
1126 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1128 uint8_t memBlock
[ 16 ];
1129 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1131 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1132 snprintf ( cmd1
, sizeof ( cmd1
), "%04x%08xFF078069%04x%08x" ,( uint32_t ) ( sector_trailer
[ i
]. keyA
>> 32 ), ( uint32_t ) ( sector_trailer
[ i
]. keyA
& 0xFFFFFFFF ),( uint32_t ) ( sector_trailer
[ i
]. keyB
>> 32 ), ( uint32_t ) ( sector_trailer
[ i
]. keyB
& 0xFFFFFFFF ));
1133 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1134 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1135 PrintAndLog ( "block data must include 32 HEX symbols" );
1139 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1140 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1141 clearCommandBuffer ();
1147 //un-comment to use as well moebius attack
1148 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1149 if (ar_resp[i].ar2 > 0) {
1150 if (tryMfk32_moebius(ar_resp[i], &key)) {
1151 PrintAndLog("M-Found Key%s for sector %02d: [%04x%08x]", (ar_resp[i].keytype) ? "B" : "A", ar_resp[i].sector, (uint32_t) (key>>32), (uint32_t) (key &0xFFFFFFFF));
1157 int usage_hf14_mf1ksim ( void ) {
1158 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1159 PrintAndLog ( "options:" );
1160 PrintAndLog ( " h this help" );
1161 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1162 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1163 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1164 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1165 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1166 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1167 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1168 PrintAndLog ( "samples:" );
1169 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1170 PrintAndLog ( " hf mf sim u 11223344556677" );
1171 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1172 PrintAndLog ( " hf mf sim f uids.txt" );
1173 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1178 int CmdHF14AMf1kSim ( const char * Cmd
) {
1180 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1181 uint8_t exitAfterNReads
= 0 ;
1185 bool setEmulatorMem
= false ;
1186 bool attackFromFile
= false ;
1188 char filename
[ FILE_PATH_SIZE
];
1189 memset ( filename
, 0x00 , sizeof ( filename
));
1194 bool errors
= false ;
1196 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1197 switch ( param_getchar ( Cmd
, cmdp
)) {
1200 setEmulatorMem
= true ;
1202 flags
|= FLAG_INTERACTIVE
;
1203 flags
|= FLAG_NR_AR_ATTACK
;
1208 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
);
1210 PrintAndLog ( "error no filename found" );
1213 attackFromFile
= true ;
1215 flags
|= FLAG_INTERACTIVE
;
1216 flags
|= FLAG_NR_AR_ATTACK
;
1221 return usage_hf14_mf1ksim ();
1224 flags
|= FLAG_INTERACTIVE
;
1229 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1234 flags
|= FLAG_RANDOM_NONCE
;
1239 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1241 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1242 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1243 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1244 default : return usage_hf14_mf1ksim ();
1250 flags
|= FLAG_NR_AR_ATTACK
;
1254 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1261 if ( errors
) return usage_hf14_mf1ksim ();
1264 if ( attackFromFile
) {
1267 f
= fopen ( filename
, "r" );
1269 PrintAndLog ( "File %s not found or locked" , filename
);
1272 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1273 while (! feof ( f
) && ! ukbhit ()){
1274 memset ( buf
, 0 , sizeof ( buf
));
1275 memset ( uid
, 0 , sizeof ( uid
));
1277 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1278 if ( count
> 0 ) break ;
1280 PrintAndLog ( "File reading error." );
1284 if (! strlen ( buf
) && feof ( f
)) break ;
1286 uidlen
= strlen ( buf
)- 1 ;
1288 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1289 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1290 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1292 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1297 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1298 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1301 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1302 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1303 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1304 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1305 , exitAfterNReads
, flags
, flags
);
1307 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1308 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1309 clearCommandBuffer ();
1312 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1313 //We're waiting only 1.5 s at a time, otherwise we get the
1314 // annoying message about "Waiting for a response... "
1317 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1318 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1319 // We can skip the standard attack if we have RANDOM_NONCE set.
1320 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1321 if (( bool ) resp
. arg
[ 1 ]) {
1322 PrintAndLog ( "Device button pressed - quitting" );
1329 } else { //not from file
1331 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1332 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1333 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1334 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1335 , exitAfterNReads
, flags
, flags
);
1337 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1338 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1339 clearCommandBuffer ();
1342 if ( flags
& FLAG_INTERACTIVE
) {
1343 PrintAndLog ( "Press pm3-button to abort simulation" );
1344 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1345 //We're waiting only 1.5 s at a time, otherwise we get the
1346 // annoying message about "Waiting for a response... "
1349 if ( flags
& FLAG_NR_AR_ATTACK
) {
1350 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1351 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1352 // We can skip the standard attack if we have RANDOM_NONCE set.
1353 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1361 int CmdHF14AMfDbg ( const char * Cmd
)
1363 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1365 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1368 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1369 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1370 PrintAndLog ( " 0 - no debug messages" );
1371 PrintAndLog ( " 1 - error messages" );
1372 PrintAndLog ( " 2 - plus information messages" );
1373 PrintAndLog ( " 3 - plus debug messages" );
1374 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1375 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1379 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1385 int CmdHF14AMfEGet ( const char * Cmd
)
1387 uint8_t blockNo
= 0 ;
1388 uint8_t data
[ 16 ] = { 0x00 };
1390 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1391 PrintAndLog ( "Usage: hf mf eget <block number>" );
1392 PrintAndLog ( " sample: hf mf eget 0 " );
1396 blockNo
= param_get8 ( Cmd
, 0 );
1399 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1400 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1402 PrintAndLog ( "Command execute timeout" );
1408 int CmdHF14AMfEClear ( const char * Cmd
)
1410 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1411 PrintAndLog ( "Usage: hf mf eclr" );
1412 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1416 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1422 int CmdHF14AMfESet ( const char * Cmd
)
1424 uint8_t memBlock
[ 16 ];
1425 uint8_t blockNo
= 0 ;
1427 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1429 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1430 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1431 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1435 blockNo
= param_get8 ( Cmd
, 0 );
1437 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1438 PrintAndLog ( "block data must include 32 HEX symbols" );
1443 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, { blockNo
, 1 , 0 }};
1444 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1450 int CmdHF14AMfELoad ( const char * Cmd
)
1453 char filename
[ FILE_PATH_SIZE
];
1454 char * fnameptr
= filename
;
1455 char buf
[ 64 ] = { 0x00 };
1456 uint8_t buf8
[ 64 ] = { 0x00 };
1457 int i
, len
, blockNum
, numBlocks
;
1458 int nameParamNo
= 1 ;
1460 char ctmp
= param_getchar ( Cmd
, 0 );
1462 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1463 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1464 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1465 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1467 PrintAndLog ( " sample: hf mf eload filename" );
1468 PrintAndLog ( " hf mf eload 4 filename" );
1473 case '0' : numBlocks
= 5 * 4 ; break ;
1475 case '\0' : numBlocks
= 16 * 4 ; break ;
1476 case '2' : numBlocks
= 32 * 4 ; break ;
1477 case '4' : numBlocks
= 256 ; break ;
1484 len
= param_getstr ( Cmd
, nameParamNo
, filename
);
1486 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1490 sprintf ( fnameptr
, ".eml" );
1493 f
= fopen ( filename
, "r" );
1495 PrintAndLog ( "File %s not found or locked" , filename
);
1501 memset ( buf
, 0 , sizeof ( buf
));
1503 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1505 if ( blockNum
>= numBlocks
) break ;
1507 PrintAndLog ( "File reading error." );
1512 if ( strlen ( buf
) < 32 ){
1513 if ( strlen ( buf
) && feof ( f
))
1515 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1520 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1521 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1524 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1525 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1532 if ( blockNum
>= numBlocks
) break ;
1537 if (( blockNum
!= numBlocks
)) {
1538 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1541 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1546 int CmdHF14AMfESave ( const char * Cmd
)
1549 char filename
[ FILE_PATH_SIZE
];
1550 char * fnameptr
= filename
;
1552 int i
, j
, len
, numBlocks
;
1553 int nameParamNo
= 1 ;
1555 memset ( filename
, 0 , sizeof ( filename
));
1556 memset ( buf
, 0 , sizeof ( buf
));
1558 char ctmp
= param_getchar ( Cmd
, 0 );
1560 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1561 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1562 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1563 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1565 PrintAndLog ( " sample: hf mf esave " );
1566 PrintAndLog ( " hf mf esave 4" );
1567 PrintAndLog ( " hf mf esave 4 filename" );
1572 case '0' : numBlocks
= 5 * 4 ; break ;
1574 case '\0' : numBlocks
= 16 * 4 ; break ;
1575 case '2' : numBlocks
= 32 * 4 ; break ;
1576 case '4' : numBlocks
= 256 ; break ;
1583 len
= param_getstr ( Cmd
, nameParamNo
, filename
);
1585 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1587 // user supplied filename?
1589 // get filename (UID from memory)
1590 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1591 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1592 len
= sprintf ( fnameptr
, "dump" );
1596 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1597 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1603 // add file extension
1604 sprintf ( fnameptr
, ".eml" );
1607 f
= fopen ( filename
, "w+" );
1610 PrintAndLog ( "Can't open file %s " , filename
);
1615 for ( i
= 0 ; i
< numBlocks
; i
++) {
1616 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1617 PrintAndLog ( "Cant get block: %d" , i
);
1620 for ( j
= 0 ; j
< 16 ; j
++)
1621 fprintf ( f
, "%02X" , buf
[ j
]);
1626 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1632 int CmdHF14AMfECFill ( const char * Cmd
)
1634 uint8_t keyType
= 0 ;
1635 uint8_t numSectors
= 16 ;
1637 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1638 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1639 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1641 PrintAndLog ( "samples: hf mf ecfill A" );
1642 PrintAndLog ( " hf mf ecfill A 4" );
1643 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1644 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1648 char ctmp
= param_getchar ( Cmd
, 0 );
1649 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1650 PrintAndLog ( "Key type must be A or B" );
1653 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1655 ctmp
= param_getchar ( Cmd
, 1 );
1657 case '0' : numSectors
= 5 ; break ;
1659 case '\0' : numSectors
= 16 ; break ;
1660 case '2' : numSectors
= 32 ; break ;
1661 case '4' : numSectors
= 40 ; break ;
1662 default : numSectors
= 16 ;
1665 printf ( "--params: numSectors: %d, keyType:%d" , numSectors
, keyType
);
1666 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1672 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1677 uint64_t keyA
, keyB
;
1679 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1680 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1681 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1682 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1684 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1688 char cmdp
= param_getchar ( Cmd
, 0 );
1691 case '0' : numSectors
= 5 ; break ;
1693 case '\0' : numSectors
= 16 ; break ;
1694 case '2' : numSectors
= 32 ; break ;
1695 case '4' : numSectors
= 40 ; break ;
1696 default : numSectors
= 16 ;
1699 PrintAndLog ( "|---|----------------|----------------|" );
1700 PrintAndLog ( "|sec|key A |key B |" );
1701 PrintAndLog ( "|---|----------------|----------------|" );
1702 for ( i
= 0 ; i
< numSectors
; i
++) {
1703 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1704 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1707 keyA
= bytes_to_num ( data
, 6 );
1708 keyB
= bytes_to_num ( data
+ 10 , 6 );
1709 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1711 PrintAndLog ( "|---|----------------|----------------|" );
1717 int CmdHF14AMfCSetUID ( const char * Cmd
)
1719 uint8_t wipeCard
= 0 ;
1720 uint8_t uid
[ 8 ] = { 0x00 };
1721 uint8_t oldUid
[ 8 ] = { 0x00 };
1722 uint8_t atqa
[ 2 ] = { 0x00 };
1723 uint8_t sak
[ 1 ] = { 0x00 };
1724 uint8_t atqaPresent
= 1 ;
1729 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, argi
) == 'h' ) {
1730 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]" );
1731 PrintAndLog ( "sample: hf mf csetuid 01020304" );
1732 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08 w" );
1733 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
1734 PrintAndLog ( "If you also want to wipe the card then add 'w' at the end of the command line." );
1738 if ( param_getchar ( Cmd
, argi
) && param_gethex ( Cmd
, argi
, uid
, 8 )) {
1739 PrintAndLog ( "UID must include 8 HEX symbols" );
1744 ctmp
= param_getchar ( Cmd
, argi
);
1745 if ( ctmp
== 'w' || ctmp
== 'W' ) {
1751 if ( param_getchar ( Cmd
, argi
)) {
1752 if ( param_gethex ( Cmd
, argi
, atqa
, 4 )) {
1753 PrintAndLog ( "ATQA must include 4 HEX symbols" );
1757 if (! param_getchar ( Cmd
, argi
) || param_gethex ( Cmd
, argi
, sak
, 2 )) {
1758 PrintAndLog ( "SAK must include 2 HEX symbols" );
1767 ctmp
= param_getchar ( Cmd
, argi
);
1768 if ( ctmp
== 'w' || ctmp
== 'W' ) {
1773 PrintAndLog ( "--wipe card:%s uid:%s" , ( wipeCard
)? "YES" : "NO" , sprint_hex ( uid
, 4 ));
1775 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
, wipeCard
);
1777 PrintAndLog ( "Can't set UID. error=%d" , res
);
1781 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
1782 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
1786 int CmdHF14AMfCSetBlk ( const char * Cmd
)
1788 uint8_t memBlock
[ 16 ] = { 0x00 };
1789 uint8_t blockNo
= 0 ;
1790 bool wipeCard
= false ;
1793 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1794 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
1795 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
1796 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
1797 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
1801 blockNo
= param_get8 ( Cmd
, 0 );
1803 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1804 PrintAndLog ( "block data must include 32 HEX symbols" );
1808 char ctmp
= param_getchar ( Cmd
, 2 );
1809 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
1810 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
1812 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1814 PrintAndLog ( "Can't write block. error=%d" , res
);
1821 int CmdHF14AMfCLoad ( const char * Cmd
)
1824 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
1825 char * fnameptr
= filename
;
1826 char buf
[ 64 ] = { 0x00 };
1827 uint8_t buf8
[ 64 ] = { 0x00 };
1828 uint8_t fillFromEmulator
= 0 ;
1829 int i
, len
, blockNum
, flags
= 0 ;
1831 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
1832 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
1833 PrintAndLog ( "or from emulator memory (option `e`)" );
1834 PrintAndLog ( "Usage: hf mf cload <file name w/o `.eml`>" );
1835 PrintAndLog ( " or: hf mf cload e " );
1836 PrintAndLog ( " sample: hf mf cload filename" );
1840 char ctmp
= param_getchar ( Cmd
, 0 );
1841 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
1843 if ( fillFromEmulator
) {
1844 for ( blockNum
= 0 ; blockNum
< 16 * 4 ; blockNum
+= 1 ) {
1845 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
1846 PrintAndLog ( "Cant get block: %d" , blockNum
);
1849 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1850 if ( blockNum
== 1 ) flags
= 0 ; // just write
1851 if ( blockNum
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1853 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
1854 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
1861 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1863 memcpy ( filename
, Cmd
, len
);
1866 sprintf ( fnameptr
, ".eml" );
1869 f
= fopen ( filename
, "r" );
1871 PrintAndLog ( "File not found or locked." );
1878 memset ( buf
, 0 , sizeof ( buf
));
1880 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1882 PrintAndLog ( "File reading error." );
1886 if ( strlen ( buf
) < 32 ) {
1887 if ( strlen ( buf
) && feof ( f
))
1889 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1893 for ( i
= 0 ; i
< 32 ; i
+= 2 )
1894 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1896 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1897 if ( blockNum
== 1 ) flags
= 0 ; // just write
1898 if ( blockNum
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1900 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
1901 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
1907 if ( blockNum
>= 16 * 4 ) break ; // magic card type - mifare 1K
1911 if ( blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16 ){
1912 PrintAndLog ( "File content error. There must be 64 blocks" );
1915 PrintAndLog ( "Loaded from file: %s" , filename
);
1921 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
1922 uint8_t memBlock
[ 16 ];
1923 uint8_t blockNo
= 0 ;
1925 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1927 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1928 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
1929 PrintAndLog ( "sample: hf mf cgetblk 1" );
1930 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
1934 blockNo
= param_get8 ( Cmd
, 0 );
1936 PrintAndLog ( "--block number:%2d " , blockNo
);
1938 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
1940 PrintAndLog ( "Can't read block. error=%d" , res
);
1944 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
1949 int CmdHF14AMfCGetSc ( const char * Cmd
) {
1950 uint8_t memBlock
[ 16 ] = { 0x00 };
1951 uint8_t sectorNo
= 0 ;
1954 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1955 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
1956 PrintAndLog ( "sample: hf mf cgetsc 0" );
1957 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
1961 sectorNo
= param_get8 ( Cmd
, 0 );
1962 if ( sectorNo
> 15 ) {
1963 PrintAndLog ( "Sector number must be in [0..15] as in MIFARE classic." );
1967 PrintAndLog ( "--sector number:%d " , sectorNo
);
1969 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
1970 for ( i
= 0 ; i
< 4 ; i
++) {
1971 if ( i
== 1 ) flags
= 0 ;
1972 if ( i
== 3 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
1974 res
= mfCGetBlock ( sectorNo
* 4 + i
, memBlock
, flags
);
1976 PrintAndLog ( "Can't read block. %d error=%d" , sectorNo
* 4 + i
, res
);
1980 PrintAndLog ( "block %3d data:%s" , sectorNo
* 4 + i
, sprint_hex ( memBlock
, 16 ));
1986 int CmdHF14AMfCSave ( const char * Cmd
) {
1989 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
1990 char * fnameptr
= filename
;
1991 uint8_t fillFromEmulator
= 0 ;
1992 uint8_t buf
[ 64 ] = { 0x00 };
1993 int i
, j
, len
, flags
;
1995 // memset(filename, 0, sizeof(filename));
1996 // memset(buf, 0, sizeof(buf));
1998 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1999 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2000 PrintAndLog ( "or into emulator memory (option `e`)" );
2001 PrintAndLog ( "Usage: hf mf esave [file name w/o `.eml`][e]" );
2002 PrintAndLog ( " sample: hf mf esave " );
2003 PrintAndLog ( " hf mf esave filename" );
2004 PrintAndLog ( " hf mf esave e \n " );
2008 char ctmp
= param_getchar ( Cmd
, 0 );
2009 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2011 if ( fillFromEmulator
) {
2012 // put into emulator
2013 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2014 for ( i
= 0 ; i
< 16 * 4 ; i
++) {
2015 if ( i
== 1 ) flags
= 0 ;
2016 if ( i
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2018 if ( mfCGetBlock ( i
, buf
, flags
)) {
2019 PrintAndLog ( "Cant get block: %d" , i
);
2023 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2024 PrintAndLog ( "Cant set emul block: %d" , i
);
2031 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2035 if ( mfCGetBlock ( 0 , buf
, CSETBLOCK_SINGLE_OPER
)) {
2036 PrintAndLog ( "Cant get block: %d" , 0 );
2037 len
= sprintf ( fnameptr
, "dump" );
2041 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2042 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2045 memcpy ( filename
, Cmd
, len
);
2049 sprintf ( fnameptr
, ".eml" );
2052 f
= fopen ( filename
, "w+" );
2055 PrintAndLog ( "File not found or locked." );
2060 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2061 for ( i
= 0 ; i
< 16 * 4 ; i
++) {
2062 if ( i
== 1 ) flags
= 0 ;
2063 if ( i
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2065 if ( mfCGetBlock ( i
, buf
, flags
)) {
2066 PrintAndLog ( "Cant get block: %d" , i
);
2069 for ( j
= 0 ; j
< 16 ; j
++)
2070 fprintf ( f
, "%02x" , buf
[ j
]);
2075 PrintAndLog ( "Saved to file: %s" , filename
);
2082 int CmdHF14AMfSniff ( const char * Cmd
){
2084 bool wantLogToFile
= 0 ;
2085 bool wantDecrypt
= 0 ;
2086 //bool wantSaveToEml = 0; TODO
2087 bool wantSaveToEmlFile
= 0 ;
2097 uint8_t atqa
[ 2 ] = { 0x00 };
2100 uint8_t * buf
= NULL
;
2101 uint16_t bufsize
= 0 ;
2102 uint8_t * bufPtr
= NULL
;
2104 char ctmp
= param_getchar ( Cmd
, 0 );
2105 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2106 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2107 PrintAndLog ( "You can specify:" );
2108 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2109 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2110 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2111 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2112 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2113 PrintAndLog ( " sample: hf mf sniff l d e" );
2117 for ( int i
= 0 ; i
< 4 ; i
++) {
2118 ctmp
= param_getchar ( Cmd
, i
);
2119 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2120 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2121 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2122 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2125 printf ( "------------------------------------------------------------------------- \n " );
2126 printf ( "Executing command. \n " );
2127 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2128 printf ( "Press the key on pc keyboard to abort the client. \n " );
2129 printf ( "------------------------------------------------------------------------- \n " );
2131 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2132 clearCommandBuffer ();
2141 printf ( " \n aborted via keyboard! \n " );
2146 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 2000 )) {
2147 res
= resp
. arg
[ 0 ] & 0xff ;
2148 uint16_t traceLen
= resp
. arg
[ 1 ];
2151 if ( res
== 0 ) { // we are done
2156 if ( res
== 1 ) { // there is (more) data to be transferred
2157 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2158 if ( traceLen
> bufsize
|| buf
== NULL
) {
2160 if ( buf
== NULL
) { // not yet allocated
2161 p
= malloc ( traceLen
);
2162 } else { // need more memory
2163 p
= realloc ( buf
, traceLen
);
2166 PrintAndLog ( "Cannot allocate memory for trace" );
2174 memset ( buf
, 0x00 , traceLen
);
2176 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2181 if ( res
== 2 ) { // received all data, start displaying
2182 blockLen
= bufPtr
- buf
;
2185 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2186 while ( bufPtr
- buf
< blockLen
) {
2187 bufPtr
+= 6 ; // skip (void) timing information
2188 len
= *(( uint16_t *) bufPtr
);
2196 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2197 memcpy ( uid
, bufPtr
+ 2 , 7 );
2198 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2199 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2201 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2202 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2206 if ( wantLogToFile
|| wantDecrypt
) {
2207 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2208 AddLogCurrentDT ( logHexFileName
);
2211 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2213 PrintAndLog ( "%s(%d):%s" , isTag
? "TAG" : "RDR" , num
, sprint_hex ( bufPtr
, len
));
2215 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2217 mfTraceDecode ( bufPtr
, len
, wantSaveToEmlFile
);
2221 bufPtr
+= (( len
- 1 )/ 8 + 1 ); // ignore parity
2232 //needs nt, ar, at, Data to decrypt
2233 int CmdDecryptTraceCmds ( const char * Cmd
){
2236 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2237 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2240 static command_t CommandTable
[] =
2242 { "help" , CmdHelp
, 1 , "This help" },
2243 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2244 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2245 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2246 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2247 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2248 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2249 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2250 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2251 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2252 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2253 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2254 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2255 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2256 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2257 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2258 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2259 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2260 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2261 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2262 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2263 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2264 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2265 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2266 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2267 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2268 { NULL
, NULL
, 0 , NULL
}
2271 int CmdHFMF ( const char * Cmd
)
2274 WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2276 CmdsParse ( CommandTable
, Cmd
);
2280 int CmdHelp ( const char * Cmd
)
2282 CmdsHelp ( CommandTable
);