]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
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 //-----------------------------------------------------------------------------
20 #include "cmdhfmfhard.h"
23 #include "util_posix.h"
26 #include "mifare/mifarehost.h"
28 #include "mifare/mfkey.h"
29 #include "hardnested/hardnested_bf_core.h"
30 #include "cliparser/cliparser.h"
32 #include "mifare/mifare4.h"
33 #include "mifare/mad.h"
34 #include "mifare/ndef.h"
37 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
39 static int CmdHelp ( const char * Cmd
);
41 int CmdHF14AMifare ( const char * Cmd
)
45 isOK
= mfDarkside (& key
);
47 case - 1 : PrintAndLog ( "Button pressed. Aborted." ); return 1 ;
48 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)." ); return 1 ;
49 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable)." ); return 1 ;
50 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
51 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour." ); return 1 ;
52 case - 5 : PrintAndLog ( "Aborted via keyboard." ); return 1 ;
53 default : PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , key
);
61 int CmdHF14AMfWrBl ( const char * Cmd
)
65 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
66 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
71 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
72 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
76 blockNo
= param_get8 ( Cmd
, 0 );
77 cmdp
= param_getchar ( Cmd
, 1 );
79 PrintAndLog ( "Key type must be A or B" );
82 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
83 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
84 PrintAndLog ( "Key must include 12 HEX symbols" );
87 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
88 PrintAndLog ( "Block data must include 32 HEX symbols" );
91 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
92 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
94 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
95 memcpy ( c
. d
. asBytes
, key
, 6 );
96 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
100 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
101 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
102 PrintAndLog ( "isOk:%02x" , isOK
);
104 PrintAndLog ( "Command execute timeout" );
110 int CmdHF14AMfRdBl ( const char * Cmd
)
114 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
120 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
121 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
125 blockNo
= param_get8 ( Cmd
, 0 );
126 cmdp
= param_getchar ( Cmd
, 1 );
128 PrintAndLog ( "Key type must be A or B" );
131 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
132 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
133 PrintAndLog ( "Key must include 12 HEX symbols" );
136 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
138 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
139 memcpy ( c
. d
. asBytes
, key
, 6 );
143 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
144 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
145 uint8_t * data
= resp
. d
. asBytes
;
148 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
150 PrintAndLog ( "isOk:%02x" , isOK
);
154 if ( mfIsSectorTrailer ( blockNo
) && ( data
[ 6 ] || data
[ 7 ] || data
[ 8 ])) {
155 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
156 int bln
= mfFirstBlockOfSector ( mfSectorNum ( blockNo
));
157 int blinc
= ( mfNumBlocksPerSector ( mfSectorNum ( blockNo
)) > 4 ) ? 5 : 1 ;
158 for ( int i
= 0 ; i
< 4 ; i
++) {
159 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & data
[ 6 ]));
162 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& data
[ 9 ], 1 ));
165 PrintAndLog ( "Command execute timeout" );
172 int CmdHF14AMfRdSc ( const char * Cmd
)
175 uint8_t sectorNo
= 0 ;
177 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
179 uint8_t * data
= NULL
;
183 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
184 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
188 sectorNo
= param_get8 ( Cmd
, 0 );
190 PrintAndLog ( "Sector number must be less than 40" );
193 cmdp
= param_getchar ( Cmd
, 1 );
194 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
195 PrintAndLog ( "Key type must be A or B" );
198 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
199 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
200 PrintAndLog ( "Key must include 12 HEX symbols" );
203 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
205 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
206 memcpy ( c
. d
. asBytes
, key
, 6 );
211 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
212 isOK
= resp
. arg
[ 0 ] & 0xff ;
213 data
= resp
. d
. asBytes
;
215 PrintAndLog ( "isOk:%02x" , isOK
);
217 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
218 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
220 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
222 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
223 int bln
= mfFirstBlockOfSector ( sectorNo
);
224 int blinc
= ( mfNumBlocksPerSector ( sectorNo
) > 4 ) ? 5 : 1 ;
225 for ( i
= 0 ; i
< 4 ; i
++) {
226 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, &( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 )[ 6 ]));
229 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (&( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 )[ 9 ], 1 ));
232 PrintAndLog ( "Command execute timeout" );
238 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
243 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
247 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
256 static int ParamCardSizeSectors ( const char c
) {
259 case '0' : numSectors
= 5 ; break ;
260 case '2' : numSectors
= 32 ; break ;
261 case '4' : numSectors
= 40 ; break ;
262 default : numSectors
= 16 ;
267 static int ParamCardSizeBlocks ( const char c
) {
268 int numBlocks
= 16 * 4 ;
270 case '0' : numBlocks
= 5 * 4 ; break ;
271 case '2' : numBlocks
= 32 * 4 ; break ;
272 case '4' : numBlocks
= 32 * 4 + 8 * 16 ; break ;
273 default : numBlocks
= 16 * 4 ;
278 int CmdHF14AMfDump ( const char * Cmd
)
280 uint8_t sectorNo
, blockNo
;
282 uint8_t keys
[ 2 ][ 40 ][ 6 ];
283 uint8_t rights
[ 40 ][ 4 ];
284 uint8_t carddata
[ 256 ][ 16 ];
285 uint8_t numSectors
= 16 ;
292 char cmdp
= param_getchar ( Cmd
, 0 );
293 numSectors
= ParamCardSizeSectors ( cmdp
);
295 if ( strlen ( Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H' ) {
296 PrintAndLog ( "Usage: hf mf dump [card memory] [k|m]" );
297 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
298 PrintAndLog ( " k: Always try using both Key A and Key B for each sector, even if access bits would prohibit it" );
299 PrintAndLog ( " m: When missing access bits or keys, replace that block with NULL" );
301 PrintAndLog ( "Samples: hf mf dump" );
302 PrintAndLog ( " hf mf dump 4" );
303 PrintAndLog ( " hf mf dump 4 m" );
307 char opts
= param_getchar ( Cmd
, 1 );
308 bool useBothKeysAlways
= false ;
309 if ( opts
== 'k' || opts
== 'K' ) useBothKeysAlways
= true ;
310 bool nullMissingKeys
= false ;
311 if ( opts
== 'm' || opts
== 'M' ) nullMissingKeys
= true ;
313 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
314 PrintAndLog ( "Could not find file dumpkeys.bin" );
318 // Read keys from file
319 for ( int group
= 0 ; group
<= 1 ; group
++) {
320 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
321 size_t bytes_read
= fread ( keys
[ group
][ sectorNo
], 1 , 6 , fin
);
322 if ( bytes_read
!= 6 ) {
323 PrintAndLog ( "File reading error." );
332 PrintAndLog ( "|-----------------------------------------|" );
333 PrintAndLog ( "|------ Reading sector access bits...-----|" );
334 PrintAndLog ( "|-----------------------------------------|" );
336 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
337 for ( tries
= 0 ; tries
< 3 ; tries
++) {
338 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
339 // At least the Access Conditions can always be read with key A.
340 memcpy ( c
. d
. asBytes
, keys
[ 0 ][ sectorNo
], 6 );
343 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
344 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
345 uint8_t * data
= resp
. d
. asBytes
;
347 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
348 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
349 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
350 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
352 } else if ( tries
== 2 ) { // on last try set defaults
353 PrintAndLog ( "Could not get access rights for sector %2d. Trying with defaults..." , sectorNo
);
354 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
355 rights
[ sectorNo
][ 3 ] = 0x01 ;
358 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
359 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
360 rights
[ sectorNo
][ 3 ] = 0x01 ;
365 PrintAndLog ( "|-----------------------------------------|" );
366 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
367 PrintAndLog ( "|-----------------------------------------|" );
370 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
371 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
372 bool received
= false ;
373 for ( tries
= 0 ; tries
< 3 ; tries
++) {
374 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
375 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
376 memcpy ( c
. d
. asBytes
, keys
[ 0 ][ sectorNo
], 6 );
378 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
379 } else if ( useBothKeysAlways
) {
380 // Always try both keys, even if access conditions wouldn't work.
381 for ( int k
= 0 ; k
<= 1 ; k
++) {
382 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
383 memcpy ( c
. d
. asBytes
, keys
[ k
][ sectorNo
], 6 );
385 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
387 // Don't try the other one on success.
388 if ( resp
. arg
[ 0 ] & 0xff ) break ;
390 } else { // data block. Check if it can be read with key A or key B
391 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
392 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
393 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
394 memcpy ( c
. d
. asBytes
, keys
[ 1 ][ sectorNo
], 6 );
396 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
397 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
398 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
399 if ( nullMissingKeys
) {
400 memset ( resp
. d
. asBytes
, 0 , 16 );
402 PrintAndLog ( " ... filling the block with NULL" );
408 } else { // key A would work
409 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
410 memcpy ( c
. d
. asBytes
, keys
[ 0 ][ sectorNo
], 6 );
412 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
416 isOK
= resp
. arg
[ 0 ] & 0xff ;
422 isOK
= resp
. arg
[ 0 ] & 0xff ;
423 uint8_t * data
= resp
. d
. asBytes
;
424 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
425 memcpy ( data
, keys
[ 0 ][ sectorNo
], 6 );
426 memcpy ( data
+ 10 , keys
[ 1 ][ sectorNo
], 6 );
429 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
430 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
432 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
438 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
445 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
446 PrintAndLog ( "Could not create file name dumpdata.bin" );
449 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
450 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
452 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
458 int CmdHF14AMfRestore ( const char * Cmd
)
460 uint8_t sectorNo
, blockNo
;
462 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
463 uint8_t bldata
[ 16 ] = { 0x00 };
471 char cmdp
= param_getchar ( Cmd
, 0 );
473 case '0' : numSectors
= 5 ; break ;
475 case '\0' : numSectors
= 16 ; break ;
476 case '2' : numSectors
= 32 ; break ;
477 case '4' : numSectors
= 40 ; break ;
478 default : numSectors
= 16 ;
481 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
482 PrintAndLog ( "Usage: hf mf restore [card memory]" );
483 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
485 PrintAndLog ( "Samples: hf mf restore" );
486 PrintAndLog ( " hf mf restore 4" );
490 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
491 PrintAndLog ( "Could not find file dumpkeys.bin" );
495 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
496 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
497 if ( bytes_read
!= 6 ) {
498 PrintAndLog ( "File reading error (dumpkeys.bin)." );
504 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
505 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
506 if ( bytes_read
!= 6 ) {
507 PrintAndLog ( "File reading error (dumpkeys.bin)." );
515 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
516 PrintAndLog ( "Could not find file dumpdata.bin" );
519 PrintAndLog ( "Restoring dumpdata.bin to card" );
521 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
522 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
523 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
524 memcpy ( c
. d
. asBytes
, key
, 6 );
526 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
527 if ( bytes_read
!= 16 ) {
528 PrintAndLog ( "File reading error (dumpdata.bin)." );
533 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
534 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
535 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
536 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
537 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
538 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
539 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
540 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
541 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
542 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
543 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
544 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
545 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
548 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
550 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
554 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
555 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
556 PrintAndLog ( "isOk:%02x" , isOK
);
558 PrintAndLog ( "Command execute timeout" );
567 //----------------------------------------------
569 //----------------------------------------------
571 static void parseParamTDS ( const char * Cmd
, const uint8_t indx
, bool * paramT
, bool * paramD
, uint8_t * timeout
) {
573 int len
= param_getlength ( Cmd
, indx
);
574 if ( len
> 0 && len
< 4 ){
575 param_getstr ( Cmd
, indx
, ctmp3
, sizeof ( ctmp3
));
577 * paramT
|= ( ctmp3
[ 0 ] == 't' || ctmp3
[ 0 ] == 'T' );
578 * paramD
|= ( ctmp3
[ 0 ] == 'd' || ctmp3
[ 0 ] == 'D' );
579 bool paramS1
= * paramT
|| * paramD
;
581 // slow and very slow
582 if ( ctmp3
[ 0 ] == 's' || ctmp3
[ 0 ] == 'S' || ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' ) {
583 * timeout
= 11 ; // slow
585 if (! paramS1
&& ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
586 * timeout
= 53 ; // very slow
588 if ( paramS1
&& ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
589 * timeout
= 53 ; // very slow
595 int CmdHF14AMfNested ( const char * Cmd
)
597 int i
, j
, res
, iterations
;
598 sector_t
* e_sector
= NULL
;
601 uint8_t trgBlockNo
= 0 ;
602 uint8_t trgKeyType
= 0 ;
603 uint8_t SectorsCnt
= 0 ;
604 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
605 uint8_t keyBlock
[ MifareDefaultKeysSize
* 6 ];
607 // timeout in units. (ms * 106)/10 or us*0.0106
608 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
610 bool autosearchKey
= false ;
612 bool transferToEml
= false ;
613 bool createDumpFile
= false ;
615 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
616 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
621 PrintAndLog ( "Usage:" );
622 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]" );
623 PrintAndLog ( " all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]" );
624 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
625 PrintAndLog ( " <target block number> <target key A/B> [t]" );
627 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
628 PrintAndLog ( "t - transfer keys to emulator memory" );
629 PrintAndLog ( "d - write keys to binary file dumpkeys.bin" );
630 PrintAndLog ( "s - Slow (1ms) check keys (required by some non standard cards)" );
631 PrintAndLog ( "ss - Very slow (5ms) check keys" );
633 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
634 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
635 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
636 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
637 PrintAndLog ( " sample5: hf mf nested 1 * t" );
638 PrintAndLog ( " sample6: hf mf nested 1 * ss" );
643 cmdp
= param_getchar ( Cmd
, 0 );
644 if ( cmdp
== 'o' || cmdp
== 'O' ) {
648 SectorsCnt
= ParamCardSizeSectors ( cmdp
);
651 // <block number>. number or autosearch key (*)
652 if ( param_getchar ( Cmd
, 1 ) == '*' ) {
653 autosearchKey
= true ;
655 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
657 PrintAndLog ( "--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us" ,
658 SectorsCnt
, transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
660 blockNo
= param_get8 ( Cmd
, 1 );
662 ctmp
= param_getchar ( Cmd
, 2 );
663 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
664 PrintAndLog ( "Key type must be A or B" );
668 if ( ctmp
!= 'A' && ctmp
!= 'a' )
671 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
672 PrintAndLog ( "Key must include 12 HEX symbols" );
676 // check if we can authenticate to sector
677 res
= mfCheckKeys ( blockNo
, keyType
, true , 1 , key
, & key64
);
679 PrintAndLog ( "Can't authenticate to block:%3d key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
685 trgBlockNo
= param_get8 ( Cmd
, 4 );
687 ctmp
= param_getchar ( Cmd
, 5 );
688 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
689 PrintAndLog ( "Target key type must be A or B" );
692 if ( ctmp
!= 'A' && ctmp
!= 'a' )
695 parseParamTDS ( Cmd
, 6 , & transferToEml
, & createDumpFile
, & btimeout14a
);
697 parseParamTDS ( Cmd
, 4 , & transferToEml
, & createDumpFile
, & btimeout14a
);
700 PrintAndLog ( "--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
701 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
705 if ( cmdp
== 'o' ) { // ------------------------------------ one sector working
706 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
707 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
710 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
711 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
712 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
713 default : PrintAndLog ( "Unknown Error. \n " );
717 key64
= bytes_to_num ( keyBlock
, 6 );
719 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
721 // transfer key to the emulator
723 uint8_t sectortrailer
;
724 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
725 sectortrailer
= trgBlockNo
| 0x03 ;
726 } else { // 16 block sector
727 sectortrailer
= trgBlockNo
| 0x0f ;
729 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
732 num_to_bytes ( key64
, 6 , keyBlock
);
734 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
735 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
736 PrintAndLog ( "Key transferred to emulator memory." );
739 PrintAndLog ( "No valid key found" );
742 else { // ------------------------------------ multiple sectors working
744 msclock1
= msclock ();
746 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
747 if ( e_sector
== NULL
) return 1 ;
749 //test current key and additional standard keys first
750 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< MifareDefaultKeysSize
; defaultKeyCounter
++){
751 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
754 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
755 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , MifareDefaultKeysSize
, keyBlock
, e_sector
);
757 // get known key from array
758 bool keyFound
= false ;
760 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
761 for ( j
= 0 ; j
< 2 ; j
++) {
762 if ( e_sector
[ i
]. foundKey
[ j
]) {
766 num_to_bytes ( e_sector
[ i
]. Key
[ j
], 6 , key
);
774 // Can't found a key....
776 PrintAndLog ( "Can't found any of the known keys." );
780 PrintAndLog ( "--auto key. block no:%3d, key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
785 PrintAndLog ( "nested..." );
786 bool calibrate
= true ;
787 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
788 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
789 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
790 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
791 PrintAndLog ( "-----------------------------------------------" );
792 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
795 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
796 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
797 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
798 default : PrintAndLog ( "Unknown Error. \n " );
808 key64
= bytes_to_num ( keyBlock
, 6 );
810 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
811 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
812 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
814 // try to check this key as a key to the other sectors
815 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , 1 , keyBlock
, e_sector
);
821 // print nested statistic
822 PrintAndLog ( " \n\n ----------------------------------------------- \n Nested statistic: \n Iterations count: %d" , iterations
);
823 PrintAndLog ( "Time in nested: %1.3f (%1.3f sec per key)" , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
826 PrintAndLog ( "|---|----------------|---|----------------|---|" );
827 PrintAndLog ( "|sec|key A |res|key B |res|" );
828 PrintAndLog ( "|---|----------------|---|----------------|---|" );
829 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
830 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
831 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
833 PrintAndLog ( "|---|----------------|---|----------------|---|" );
835 // transfer keys to the emulator memory
837 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
838 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
839 if ( e_sector
[ i
]. foundKey
[ 0 ])
840 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
841 if ( e_sector
[ i
]. foundKey
[ 1 ])
842 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
843 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
845 PrintAndLog ( "Keys transferred to emulator memory." );
849 if ( createDumpFile
) {
850 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
851 PrintAndLog ( "Could not create file dumpkeys.bin" );
855 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
856 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
857 if ( e_sector
[ i
]. foundKey
[ 0 ]){
858 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
859 fwrite ( tempkey
, 1 , 6 , fkeys
);
862 fwrite ( & standart
, 1 , 6 , fkeys
);
865 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
866 if ( e_sector
[ i
]. foundKey
[ 1 ]){
867 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
868 fwrite ( tempkey
, 1 , 6 , fkeys
);
871 fwrite ( & standart
, 1 , 6 , fkeys
);
883 int CmdHF14AMfNestedHard ( const char * Cmd
)
887 uint8_t trgBlockNo
= 0 ;
888 uint8_t trgKeyType
= 0 ;
889 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
890 uint8_t trgkey
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
893 ctmp
= param_getchar ( Cmd
, 0 );
895 if ( ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen ( Cmd
) < 20 ) {
896 PrintAndLog ( "Usage:" );
897 PrintAndLog ( " hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>" );
898 PrintAndLog ( " <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]" );
899 PrintAndLog ( " or hf mf hardnested r [known target key]" );
901 PrintAndLog ( "Options: " );
902 PrintAndLog ( " w: Acquire nonces and write them to binary file nonces.bin" );
903 PrintAndLog ( " s: Slower acquisition (required by some non standard cards)" );
904 PrintAndLog ( " r: Read nonces.bin and start attack" );
905 PrintAndLog ( " iX: set type of SIMD instructions. Without this flag programs autodetect it." );
906 PrintAndLog ( " i5: AVX512" );
907 PrintAndLog ( " i2: AVX2" );
908 PrintAndLog ( " ia: AVX" );
909 PrintAndLog ( " is: SSE2" );
910 PrintAndLog ( " im: MMX" );
911 PrintAndLog ( " in: none (use CPU regular instruction set)" );
913 PrintAndLog ( " sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A" );
914 PrintAndLog ( " sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w" );
915 PrintAndLog ( " sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s" );
916 PrintAndLog ( " sample4: hf mf hardnested r" );
918 PrintAndLog ( "Add the known target key to check if it is present in the remaining key space:" );
919 PrintAndLog ( " sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF" );
923 bool know_target_key
= false ;
924 bool nonce_file_read
= false ;
925 bool nonce_file_write
= false ;
931 if ( ctmp
== 'R' || ctmp
== 'r' ) {
932 nonce_file_read
= true ;
934 if (! param_gethex ( Cmd
, 1 , trgkey
, 12 )) {
935 know_target_key
= true ;
938 } else if ( ctmp
== 'T' || ctmp
== 't' ) {
939 tests
= param_get32ex ( Cmd
, 1 , 100 , 10 );
941 if (! param_gethex ( Cmd
, 2 , trgkey
, 12 )) {
942 know_target_key
= true ;
946 blockNo
= param_get8 ( Cmd
, 0 );
947 ctmp
= param_getchar ( Cmd
, 1 );
948 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
949 PrintAndLog ( "Key type must be A or B" );
952 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
956 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
957 PrintAndLog ( "Key must include 12 HEX symbols" );
961 trgBlockNo
= param_get8 ( Cmd
, 3 );
962 ctmp
= param_getchar ( Cmd
, 4 );
963 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
964 PrintAndLog ( "Target key type must be A or B" );
967 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
973 if (! param_gethex ( Cmd
, 5 , trgkey
, 12 )) {
974 know_target_key
= true ;
979 while (( ctmp
= param_getchar ( Cmd
, i
))) {
980 if ( ctmp
== 's' || ctmp
== 'S' ) {
982 } else if ( ctmp
== 'w' || ctmp
== 'W' ) {
983 nonce_file_write
= true ;
984 } else if ( param_getlength ( Cmd
, i
) == 2 && ctmp
== 'i' ) {
987 PrintAndLog ( "Possible options are w , s and/or iX" );
994 SetSIMDInstr ( SIMD_AUTO
);
996 while (( ctmp
= param_getchar ( Cmd
, iindx
))) {
997 if ( param_getlength ( Cmd
, iindx
) == 2 && ctmp
== 'i' ) {
998 switch ( param_getchar_indx ( Cmd
, 1 , iindx
)) {
1000 SetSIMDInstr ( SIMD_AVX512
);
1003 SetSIMDInstr ( SIMD_AVX2
);
1006 SetSIMDInstr ( SIMD_AVX
);
1009 SetSIMDInstr ( SIMD_SSE2
);
1012 SetSIMDInstr ( SIMD_MMX
);
1015 SetSIMDInstr ( SIMD_NONE
);
1018 PrintAndLog ( "Unknown SIMD type. %c" , param_getchar_indx ( Cmd
, 1 , iindx
));
1026 PrintAndLog ( "--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d " ,
1029 trgkey
[ 0 ], trgkey
[ 1 ], trgkey
[ 2 ], trgkey
[ 3 ], trgkey
[ 4 ], trgkey
[ 5 ],
1030 know_target_key
? "" : " (not set)" ,
1031 nonce_file_write
? "write" : nonce_file_read
? "read" : "none" ,
1035 int16_t isOK
= mfnestedhard ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1039 case 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
1040 case 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
1050 int CmdHF14AMfChk ( const char * Cmd
)
1052 if ( strlen ( Cmd
)< 3 ) {
1053 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
1054 PrintAndLog ( " * - all sectors" );
1055 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
1056 PrintAndLog ( "d - write keys to binary file \n " );
1057 PrintAndLog ( "t - write keys to emulator memory" );
1058 PrintAndLog ( "s - slow execute. timeout 1ms" );
1059 PrintAndLog ( "ss - very slow execute. timeout 5ms" );
1060 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
1061 PrintAndLog ( " hf mf chk *1 ? t" );
1062 PrintAndLog ( " hf mf chk *1 ? d" );
1063 PrintAndLog ( " hf mf chk *1 ? s" );
1064 PrintAndLog ( " hf mf chk *1 ? dss" );
1069 char filename
[ FILE_PATH_SIZE
]={ 0 };
1071 uint8_t * keyBlock
= NULL
, * p
;
1072 uint16_t stKeyBlock
= 20 ;
1078 uint8_t blockNo
= 0 ;
1079 uint8_t SectorsCnt
= 0 ;
1080 uint8_t keyType
= 0 ;
1082 // timeout in units. (ms * 106)/10 or us*0.0106
1083 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
1084 bool param3InUse
= false ;
1086 bool transferToEml
= 0 ;
1087 bool createDumpFile
= 0 ;
1089 sector_t
* e_sector
= NULL
;
1091 keyBlock
= calloc ( stKeyBlock
, 6 );
1092 if ( keyBlock
== NULL
) return 1 ;
1094 int defaultKeysSize
= MifareDefaultKeysSize
;
1095 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++){
1096 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
1099 if ( param_getchar ( Cmd
, 0 )== '*' ) {
1100 SectorsCnt
= ParamCardSizeSectors ( param_getchar ( Cmd
+ 1 , 0 ));
1103 blockNo
= param_get8 ( Cmd
, 0 );
1105 ctmp
= param_getchar ( Cmd
, 1 );
1106 clen
= param_getlength ( Cmd
, 1 );
1119 PrintAndLog ( "Key type must be A , B or ?" );
1125 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
1127 param3InUse
= transferToEml
| createDumpFile
| ( btimeout14a
!= MF_CHKKEYS_DEFTIMEOUT
);
1129 PrintAndLog ( "--chk keys. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
1130 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
1132 for ( i
= param3InUse
; param_getchar ( Cmd
, 2 + i
); i
++) {
1133 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
1134 if ( stKeyBlock
- keycnt
< 2 ) {
1135 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1137 PrintAndLog ( "Cannot allocate memory for Keys" );
1143 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1144 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1145 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1148 // May be a dic file
1149 if ( param_getstr ( Cmd
, 2 + i
, filename
, sizeof ( filename
)) >= FILE_PATH_SIZE
) {
1150 PrintAndLog ( "File name too long" );
1155 if ( ( f
= fopen ( filename
, "r" )) ) {
1156 while ( fgets ( buf
, sizeof ( buf
), f
) ){
1157 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
1160 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
1162 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
1164 if (! isxdigit (( unsigned char ) buf
[ 0 ])){
1165 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
1171 if ( stKeyBlock
- keycnt
< 2 ) {
1172 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1174 PrintAndLog ( "Cannot allocate memory for defKeys" );
1181 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
1182 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
1183 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
1185 memset ( buf
, 0 , sizeof ( buf
));
1189 PrintAndLog ( "File: %s: not found or locked." , filename
);
1197 // fill with default keys
1199 PrintAndLog ( "No key specified, trying default keys" );
1200 for (; keycnt
< defaultKeysSize
; keycnt
++)
1201 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1202 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1203 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1206 // initialize storage for found keys
1207 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
1208 if ( e_sector
== NULL
) {
1212 for ( uint8_t keyAB
= 0 ; keyAB
< 2 ; keyAB
++) {
1213 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1214 e_sector
[ sectorNo
]. Key
[ keyAB
] = 0xffffffffffff ;
1215 e_sector
[ sectorNo
]. foundKey
[ keyAB
] = 0 ;
1220 bool foundAKey
= false ;
1221 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1223 PrintAndLog ( "To cancel this operation press the button on the proxmark..." );
1225 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1227 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1228 res
= mfCheckKeysSec ( SectorsCnt
, keyType
, btimeout14a
, true , size
, & keyBlock
[ 6 * c
], e_sector
); // timeout is (ms * 106)/10 or us*0.0106
1239 PrintAndLog ( "Command execute timeout" );
1243 int keyAB
= keyType
;
1245 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1247 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1248 res
= mfCheckKeys ( blockNo
, keyAB
& 0x01 , true , size
, & keyBlock
[ 6 * c
], & key64
);
1252 PrintAndLog ( "Found valid key:[%d:%c]%012" PRIx64
, blockNo
, ( keyAB
& 0x01 )? 'B' : 'A' , key64
);
1256 PrintAndLog ( "Command execute timeout" );
1259 } while (-- keyAB
> 0 );
1266 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1267 PrintAndLog ( "|sec|key A |res|key B |res|" );
1268 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1269 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
1270 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
1271 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
1273 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1277 PrintAndLog ( "No valid keys found." );
1280 if ( transferToEml
) {
1282 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1283 if ( e_sector
[ sectorNo
]. foundKey
[ 0 ] || e_sector
[ sectorNo
]. foundKey
[ 1 ]) {
1284 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1285 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1286 if ( e_sector
[ sectorNo
]. foundKey
[ t
]) {
1287 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , block
+ t
* 10 );
1290 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1293 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1296 if ( createDumpFile
) {
1297 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1298 if ( fkeys
== NULL
) {
1299 PrintAndLog ( "Could not create file dumpkeys.bin" );
1305 for ( uint8_t t
= 0 ; t
< 2 ; t
++) {
1306 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1307 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , mkey
);
1308 fwrite ( mkey
, 1 , 6 , fkeys
);
1312 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1321 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1322 #define ATTACK_KEY_COUNT 7 // keep same as define in iso14443a.c -> Mifare1ksim()
1323 // cannot be more than 7 or it will overrun c.d.asBytes(512)
1329 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1330 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1332 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1333 memset ( stSector
, 0x00 , sizeof ( stSector
));
1334 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1335 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1337 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1338 if ( ar_resp
[ i
]. ar2
> 0 ) {
1339 //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);
1340 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1341 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 ));
1343 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1344 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1345 if ( ar_resp
[ i
]. keytype
== 0 ) {
1347 sector_trailer
[ ii
]. keyA
= key
;
1348 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1353 sector_trailer
[ ii
]. keyB
= key
;
1354 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1360 } else if ( mfkey32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1361 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1362 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1364 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1365 , keyType
? "B" : "A"
1370 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1371 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1374 sector_trailer
[ ii
]. keyA
= key
;
1375 stSector
[ ii
] = sectorNum
;
1380 sector_trailer
[ ii
]. keyB
= key
;
1381 stSector
[ ii
] = sectorNum
;
1391 //set emulator memory for keys
1392 if ( setEmulatorMem
) {
1393 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1395 uint8_t memBlock
[ 16 ];
1396 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1398 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1399 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 ));
1400 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1401 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1402 PrintAndLog ( "block data must include 32 HEX symbols" );
1406 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1407 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1408 clearCommandBuffer ();
1414 //un-comment to use as well moebius attack
1415 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1416 if (ar_resp[i].ar2 > 0) {
1417 if (tryMfk32_moebius(ar_resp[i], &key)) {
1418 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));
1424 int usage_hf14_mfsim ( void ) {
1425 PrintAndLog ( "Usage: hf mf sim [h] [*<card memory>] [u <uid (8, 14, or 20 hex symbols)>] [n <numreads>] [i] [x]" );
1426 PrintAndLog ( "options:" );
1427 PrintAndLog ( " h (Optional) this help" );
1428 PrintAndLog ( " card memory: 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other, default> - 1K" );
1429 PrintAndLog ( " u (Optional) UID 4 or 7 bytes. If not specified, the UID 4B from emulator memory will be used" );
1430 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1431 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1432 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1433 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1434 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1435 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1436 PrintAndLog ( "samples:" );
1437 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1438 PrintAndLog ( " hf mf sim *4" );
1439 PrintAndLog ( " hf mf sim u 11223344556677" );
1440 PrintAndLog ( " hf mf sim f uids.txt" );
1441 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1446 int CmdHF14AMfSim ( const char * Cmd
) {
1448 uint8_t uid
[ 7 ] = { 0 };
1449 uint8_t exitAfterNReads
= 0 ;
1452 bool setEmulatorMem
= false ;
1453 bool attackFromFile
= false ;
1455 char filename
[ FILE_PATH_SIZE
];
1456 memset ( filename
, 0x00 , sizeof ( filename
));
1461 bool errors
= false ;
1462 uint8_t cardsize
= '1' ;
1464 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1465 switch ( param_getchar ( Cmd
, cmdp
)) {
1467 cardsize
= param_getchar ( Cmd
+ 1 , cmdp
);
1473 default : cardsize
= '1' ;
1479 setEmulatorMem
= true ;
1481 flags
|= FLAG_INTERACTIVE
;
1482 flags
|= FLAG_NR_AR_ATTACK
;
1487 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
, sizeof ( filename
));
1489 PrintAndLog ( "error no filename found" );
1492 attackFromFile
= true ;
1494 flags
|= FLAG_INTERACTIVE
;
1495 flags
|= FLAG_NR_AR_ATTACK
;
1500 return usage_hf14_mfsim ();
1503 flags
|= FLAG_INTERACTIVE
;
1508 exitAfterNReads
= param_get8 ( Cmd
, cmdp
+ 1 );
1513 flags
|= FLAG_RANDOM_NONCE
;
1518 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1520 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1521 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1522 default : return usage_hf14_mfsim ();
1528 flags
|= FLAG_NR_AR_ATTACK
;
1532 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1539 if ( errors
) return usage_hf14_mfsim ();
1542 if ( attackFromFile
) {
1545 f
= fopen ( filename
, "r" );
1547 PrintAndLog ( "File %s not found or locked" , filename
);
1550 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1551 while (! feof ( f
) && ! ukbhit ()){
1552 memset ( buf
, 0 , sizeof ( buf
));
1553 memset ( uid
, 0 , sizeof ( uid
));
1555 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1556 if ( count
> 0 ) break ;
1558 PrintAndLog ( "File reading error." );
1562 if (! strlen ( buf
) && feof ( f
)) break ;
1564 uidlen
= strlen ( buf
)- 1 ;
1566 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1567 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1569 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1574 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1575 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1578 PrintAndLog ( "mf sim cardsize: %s, uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1579 cardsize
== '0' ? "Mini" :
1580 cardsize
== '2' ? "2K" :
1581 cardsize
== '4' ? "4K" : "1K" ,
1582 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1583 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ): "N/A" ,
1588 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, cardsize
}};
1589 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1590 clearCommandBuffer ();
1593 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1594 //We're waiting only 1.5 s at a time, otherwise we get the
1595 // annoying message about "Waiting for a response... "
1598 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1599 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1600 // We can skip the standard attack if we have RANDOM_NONCE set.
1601 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1602 if (( bool ) resp
. arg
[ 1 ]) {
1603 PrintAndLog ( "Device button pressed - quitting" );
1611 } else { //not from file
1613 PrintAndLog ( "mf sim cardsize: %s, uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1614 cardsize
== '0' ? "Mini" :
1615 cardsize
== '2' ? "2K" :
1616 cardsize
== '4' ? "4K" : "1K" ,
1617 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1618 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ): "N/A" ,
1623 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, cardsize
}};
1624 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1625 clearCommandBuffer ();
1628 if ( flags
& FLAG_INTERACTIVE
) {
1629 PrintAndLog ( "Press pm3-button to abort simulation" );
1630 while (! WaitForResponseTimeout ( CMD_ACK
, & resp
, 1500 )) {
1631 //We're waiting only 1.5 s at a time, otherwise we get the
1632 // annoying message about "Waiting for a response... "
1635 if ( flags
& FLAG_NR_AR_ATTACK
) {
1636 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1637 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1638 // We can skip the standard attack if we have RANDOM_NONCE set.
1639 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1647 int CmdHF14AMfDbg ( const char * Cmd
)
1649 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1651 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1654 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1655 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1656 PrintAndLog ( " 0 - no debug messages" );
1657 PrintAndLog ( " 1 - error messages" );
1658 PrintAndLog ( " 2 - plus information messages" );
1659 PrintAndLog ( " 3 - plus debug messages" );
1660 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1661 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1665 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1671 int CmdHF14AMfEGet ( const char * Cmd
)
1673 uint8_t blockNo
= 0 ;
1674 uint8_t data
[ 16 ] = { 0x00 };
1676 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1677 PrintAndLog ( "Usage: hf mf eget <block number>" );
1678 PrintAndLog ( " sample: hf mf eget 0 " );
1682 blockNo
= param_get8 ( Cmd
, 0 );
1685 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1686 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1688 PrintAndLog ( "Command execute timeout" );
1694 int CmdHF14AMfEClear ( const char * Cmd
)
1696 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1697 PrintAndLog ( "Usage: hf mf eclr" );
1698 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1702 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1708 int CmdHF14AMfESet ( const char * Cmd
)
1710 uint8_t memBlock
[ 16 ];
1711 uint8_t blockNo
= 0 ;
1713 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1715 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1716 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1717 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1721 blockNo
= param_get8 ( Cmd
, 0 );
1723 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1724 PrintAndLog ( "block data must include 32 HEX symbols" );
1729 return mfEmlSetMem ( memBlock
, blockNo
, 1 );
1733 int CmdHF14AMfELoad ( const char * Cmd
)
1736 char filename
[ FILE_PATH_SIZE
];
1737 char * fnameptr
= filename
;
1738 char buf
[ 64 ] = { 0x00 };
1739 uint8_t buf8
[ 64 ] = { 0x00 };
1740 int i
, len
, blockNum
, numBlocks
;
1741 int nameParamNo
= 1 ;
1743 char ctmp
= param_getchar ( Cmd
, 0 );
1745 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1746 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1747 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1748 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1750 PrintAndLog ( " sample: hf mf eload filename" );
1751 PrintAndLog ( " hf mf eload 4 filename" );
1756 case '0' : numBlocks
= 5 * 4 ; break ;
1758 case '\0' : numBlocks
= 16 * 4 ; break ;
1759 case '2' : numBlocks
= 32 * 4 ; break ;
1760 case '4' : numBlocks
= 256 ; break ;
1767 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1769 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1773 sprintf ( fnameptr
, ".eml" );
1776 f
= fopen ( filename
, "r" );
1778 PrintAndLog ( "File %s not found or locked" , filename
);
1784 memset ( buf
, 0 , sizeof ( buf
));
1786 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1788 if ( blockNum
>= numBlocks
) break ;
1790 PrintAndLog ( "File reading error." );
1795 if ( strlen ( buf
) < 32 ){
1796 if ( strlen ( buf
) && feof ( f
))
1798 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1803 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1804 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1807 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1808 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1815 if ( blockNum
>= numBlocks
) break ;
1820 if (( blockNum
!= numBlocks
)) {
1821 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1824 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1829 int CmdHF14AMfESave ( const char * Cmd
)
1832 char filename
[ FILE_PATH_SIZE
];
1833 char * fnameptr
= filename
;
1835 int i
, j
, len
, numBlocks
;
1836 int nameParamNo
= 1 ;
1838 memset ( filename
, 0 , sizeof ( filename
));
1839 memset ( buf
, 0 , sizeof ( buf
));
1841 char ctmp
= param_getchar ( Cmd
, 0 );
1843 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1844 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1845 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1846 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1848 PrintAndLog ( " sample: hf mf esave " );
1849 PrintAndLog ( " hf mf esave 4" );
1850 PrintAndLog ( " hf mf esave 4 filename" );
1855 case '0' : numBlocks
= 5 * 4 ; break ;
1857 case '\0' : numBlocks
= 16 * 4 ; break ;
1858 case '2' : numBlocks
= 32 * 4 ; break ;
1859 case '4' : numBlocks
= 256 ; break ;
1866 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1868 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1870 // user supplied filename?
1872 // get filename (UID from memory)
1873 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1874 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1875 len
= sprintf ( fnameptr
, "dump" );
1879 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1880 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1886 // add file extension
1887 sprintf ( fnameptr
, ".eml" );
1890 f
= fopen ( filename
, "w+" );
1893 PrintAndLog ( "Can't open file %s " , filename
);
1898 for ( i
= 0 ; i
< numBlocks
; i
++) {
1899 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1900 PrintAndLog ( "Cant get block: %d" , i
);
1903 for ( j
= 0 ; j
< 16 ; j
++)
1904 fprintf ( f
, "%02X" , buf
[ j
]);
1909 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1915 int CmdHF14AMfECFill ( const char * Cmd
)
1917 uint8_t keyType
= 0 ;
1918 uint8_t numSectors
= 16 ;
1920 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1921 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1922 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1924 PrintAndLog ( "samples: hf mf ecfill A" );
1925 PrintAndLog ( " hf mf ecfill A 4" );
1926 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1927 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1931 char ctmp
= param_getchar ( Cmd
, 0 );
1932 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1933 PrintAndLog ( "Key type must be A or B" );
1936 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1938 ctmp
= param_getchar ( Cmd
, 1 );
1940 case '0' : numSectors
= 5 ; break ;
1942 case '\0' : numSectors
= 16 ; break ;
1943 case '2' : numSectors
= 32 ; break ;
1944 case '4' : numSectors
= 40 ; break ;
1945 default : numSectors
= 16 ;
1948 printf ( "--params: numSectors: %d, keyType:%d \n " , numSectors
, keyType
);
1949 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1955 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1958 uint8_t numSectors
= 16 ;
1960 uint64_t keyA
, keyB
;
1961 bool createDumpFile
= false ;
1963 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1964 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1965 PrintAndLog ( "Usage: hf mf ekeyprn [card memory] [d]" );
1966 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1967 PrintAndLog ( " [d] : write keys to binary file dumpkeys.bin" );
1969 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1974 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1975 switch ( param_getchar ( Cmd
, cmdp
)) {
1976 case '0' : numSectors
= 5 ; break ;
1978 case '\0' : numSectors
= 16 ; break ;
1979 case '2' : numSectors
= 32 ; break ;
1980 case '4' : numSectors
= 40 ; break ;
1982 case 'D' : createDumpFile
= true ; break ;
1987 PrintAndLog ( "|---|----------------|----------------|" );
1988 PrintAndLog ( "|sec|key A |key B |" );
1989 PrintAndLog ( "|---|----------------|----------------|" );
1990 for ( i
= 0 ; i
< numSectors
; i
++) {
1991 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1992 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1995 keyA
= bytes_to_num ( data
, 6 );
1996 keyB
= bytes_to_num ( data
+ 10 , 6 );
1997 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1999 PrintAndLog ( "|---|----------------|----------------|" );
2002 if ( createDumpFile
) {
2004 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
2005 PrintAndLog ( "Could not create file dumpkeys.bin" );
2008 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
2009 for ( i
= 0 ; i
< numSectors
; i
++) {
2010 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
2011 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
2014 fwrite ( data
+ 6 , 1 , 6 , fkeys
);
2016 for ( i
= 0 ; i
< numSectors
; i
++) {
2017 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
2018 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
2021 fwrite ( data
+ 10 , 1 , 6 , fkeys
);
2030 int CmdHF14AMfCSetUID ( const char * Cmd
)
2032 uint8_t uid
[ 8 ] = { 0x00 };
2033 uint8_t oldUid
[ 8 ] = { 0x00 };
2034 uint8_t atqa
[ 2 ] = { 0x00 };
2035 uint8_t sak
[ 1 ] = { 0x00 };
2036 uint8_t atqaPresent
= 0 ;
2039 uint8_t needHelp
= 0 ;
2042 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
2043 PrintAndLog ( "UID must include 8 HEX symbols" );
2047 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
2051 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
2052 PrintAndLog ( "ATQA must include 4 HEX symbols" );
2056 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
2057 PrintAndLog ( "SAK must include 2 HEX symbols" );
2062 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
2064 switch ( param_getchar ( Cmd
, cmdp
))
2071 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
2078 if ( strlen ( Cmd
) < 1 || needHelp
) {
2080 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
2081 PrintAndLog ( "sample: hf mf csetuid 01020304" );
2082 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
2083 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
2087 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
2089 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
2092 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
2094 PrintAndLog ( "Can't set UID. Error=%d" , res
);
2098 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
2099 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
2103 int CmdHF14AMfCWipe ( const char * Cmd
)
2106 int numBlocks
= 16 * 4 ;
2107 bool wipeCard
= false ;
2108 bool fillCard
= false ;
2110 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2111 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [f]" );
2112 PrintAndLog ( "sample: hf mf cwipe 1 w f" );
2113 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
2114 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
2115 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
2119 gen
= mfCIdentify ();
2120 if (( gen
!= 1 ) && ( gen
!= 2 ))
2123 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2126 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2127 switch ( param_getchar ( Cmd
, cmdp
)) {
2142 if (! wipeCard
&& ! fillCard
)
2145 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2148 /* generation 1b magic card */
2150 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2152 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2154 /* generation 1a magic card by default */
2155 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2159 PrintAndLog ( "Can't wipe. error=%d" , res
);
2166 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2168 uint8_t memBlock
[ 16 ] = { 0x00 };
2169 uint8_t blockNo
= 0 ;
2170 bool wipeCard
= false ;
2173 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2174 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2175 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2176 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2177 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2181 gen
= mfCIdentify ();
2182 if (( gen
!= 1 ) && ( gen
!= 2 ))
2185 blockNo
= param_get8 ( Cmd
, 0 );
2187 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2188 PrintAndLog ( "block data must include 32 HEX symbols" );
2192 char ctmp
= param_getchar ( Cmd
, 2 );
2193 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2194 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2197 /* generation 1b magic card */
2198 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2200 /* generation 1a magic card by default */
2201 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2205 PrintAndLog ( "Can't write block. error=%d" , res
);
2212 int CmdHF14AMfCLoad ( const char * Cmd
)
2215 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2216 char * fnameptr
= filename
;
2217 char buf
[ 256 ] = { 0x00 };
2218 uint8_t buf8
[ 256 ] = { 0x00 };
2219 uint8_t fillFromEmulator
= 0 ;
2220 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2222 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2223 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2224 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2225 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2226 PrintAndLog ( " or: hf mf cload e [4]" );
2227 PrintAndLog ( "Sample: hf mf cload filename" );
2228 PrintAndLog ( " hf mf cload filname 4" );
2229 PrintAndLog ( " hf mf cload e" );
2230 PrintAndLog ( " hf mf cload e 4" );
2234 char ctmp
= param_getchar ( Cmd
, 0 );
2235 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2236 ctmp
= param_getchar ( Cmd
, 1 );
2237 if ( ctmp
== '4' ) numblock
= 256 ;
2239 gen
= mfCIdentify ();
2240 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2242 if ( fillFromEmulator
) {
2243 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2244 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2245 PrintAndLog ( "Cant get block: %d" , blockNum
);
2248 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2249 if ( blockNum
== 1 ) flags
= 0 ; // just write
2250 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2253 /* generation 1b magic card */
2254 flags
|= CSETBLOCK_MAGIC_1B
;
2255 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2256 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2262 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2264 len
= strlen ( filename
);
2265 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2267 //memcpy(filename, Cmd, len);
2270 sprintf ( fnameptr
, ".eml" );
2273 f
= fopen ( filename
, "r" );
2275 PrintAndLog ( "File not found or locked." );
2282 memset ( buf
, 0 , sizeof ( buf
));
2284 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2286 PrintAndLog ( "File reading error." );
2290 if ( strlen ( buf
) < 32 ) {
2291 if ( strlen ( buf
) && feof ( f
))
2293 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2297 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2298 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2300 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2301 if ( blockNum
== 1 ) flags
= 0 ; // just write
2302 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2305 /* generation 1b magic card */
2306 flags
|= CSETBLOCK_MAGIC_1B
;
2307 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2308 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2314 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2318 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2319 if ( blockNum
!= numblock
){
2320 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2323 PrintAndLog ( "Loaded from file: %s" , filename
);
2329 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2330 uint8_t memBlock
[ 16 ];
2331 uint8_t blockNo
= 0 ;
2333 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2335 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2336 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2337 PrintAndLog ( "sample: hf mf cgetblk 1" );
2338 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2342 gen
= mfCIdentify ();
2344 blockNo
= param_get8 ( Cmd
, 0 );
2346 PrintAndLog ( "--block number:%2d " , blockNo
);
2349 /* generation 1b magic card */
2350 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2352 /* generation 1a magic card by default */
2353 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2356 PrintAndLog ( "Can't read block. error=%d" , res
);
2360 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2362 if ( mfIsSectorTrailer ( blockNo
)) {
2363 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2364 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2365 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2366 int bln
= mfFirstBlockOfSector ( mfSectorNum ( blockNo
));
2367 int blinc
= ( mfNumBlocksPerSector ( mfSectorNum ( blockNo
)) > 4 ) ? 5 : 1 ;
2368 for ( int i
= 0 ; i
< 4 ; i
++) {
2369 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2372 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2378 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2379 uint8_t memBlock
[ 16 ] = { 0x00 };
2380 uint8_t sectorNo
= 0 ;
2381 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2383 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2384 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2385 PrintAndLog ( "sample: hf mf cgetsc 0" );
2386 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2390 sectorNo
= param_get8 ( Cmd
, 0 );
2392 if ( sectorNo
> 39 ) {
2393 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2397 PrintAndLog ( "--sector number:%d " , sectorNo
);
2399 gen
= mfCIdentify ();
2401 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2402 if ( sectorNo
< 32 ) {
2403 baseblock
= sectorNo
* 4 ;
2405 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2408 if ( sectorNo
> 31 ) sect_size
= 16 ;
2410 for ( i
= 0 ; i
< sect_size
; i
++) {
2411 if ( i
== 1 ) flags
= 0 ;
2412 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2415 /* generation 1b magic card */
2416 flags
|= CSETBLOCK_MAGIC_1B
;
2418 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2420 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2424 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2426 if ( mfIsSectorTrailer ( baseblock
+ i
)) {
2427 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2428 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2429 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2430 int bln
= baseblock
;
2431 int blinc
= ( mfNumBlocksPerSector ( sectorNo
) > 4 ) ? 5 : 1 ;
2432 for ( int i
= 0 ; i
< 4 ; i
++) {
2433 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2436 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2443 int CmdHF14AMfCSave ( const char * Cmd
) {
2446 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2447 char * fnameptr
= filename
;
2448 uint8_t fillFromEmulator
= 0 ;
2449 uint8_t buf
[ 256 ] = { 0x00 };
2450 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2452 // memset(filename, 0, sizeof(filename));
2453 // memset(buf, 0, sizeof(buf));
2455 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2456 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2457 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2458 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2459 PrintAndLog ( "Sample: hf mf csave " );
2460 PrintAndLog ( " hf mf csave filename" );
2461 PrintAndLog ( " hf mf csave e" );
2462 PrintAndLog ( " hf mf csave 4" );
2463 PrintAndLog ( " hf mf csave filename 4" );
2464 PrintAndLog ( " hf mf csave e 4" );
2468 char ctmp
= param_getchar ( Cmd
, 0 );
2469 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2470 if ( ctmp
== '4' ) numblock
= 256 ;
2471 ctmp
= param_getchar ( Cmd
, 1 );
2472 if ( ctmp
== '4' ) numblock
= 256 ;
2474 gen
= mfCIdentify ();
2475 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2477 if ( fillFromEmulator
) {
2478 // put into emulator
2479 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2480 for ( i
= 0 ; i
< numblock
; i
++) {
2481 if ( i
== 1 ) flags
= 0 ;
2482 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2485 /* generation 1b magic card */
2486 flags
|= CSETBLOCK_MAGIC_1B
;
2488 if ( mfCGetBlock ( i
, buf
, flags
)) {
2489 PrintAndLog ( "Cant get block: %d" , i
);
2493 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2494 PrintAndLog ( "Cant set emul block: %d" , i
);
2500 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2502 len
= strlen ( filename
);
2503 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2505 ctmp
= param_getchar ( Cmd
, 0 );
2506 if ( len
< 1 || ( ctmp
== '4' )) {
2509 flags
= CSETBLOCK_SINGLE_OPER
;
2511 /* generation 1b magic card */
2512 flags
|= CSETBLOCK_MAGIC_1B
;
2513 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2514 PrintAndLog ( "Cant get block: %d" , 0 );
2515 len
= sprintf ( fnameptr
, "dump" );
2519 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2520 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2523 //memcpy(filename, Cmd, len);
2527 sprintf ( fnameptr
, ".eml" );
2530 f
= fopen ( filename
, "w+" );
2533 PrintAndLog ( "File not found or locked." );
2538 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2539 for ( i
= 0 ; i
< numblock
; i
++) {
2540 if ( i
== 1 ) flags
= 0 ;
2541 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2544 /* generation 1b magic card */
2545 flags
|= CSETBLOCK_MAGIC_1B
;
2546 if ( mfCGetBlock ( i
, buf
, flags
)) {
2547 PrintAndLog ( "Cant get block: %d" , i
);
2550 for ( j
= 0 ; j
< 16 ; j
++)
2551 fprintf ( f
, "%02x" , buf
[ j
]);
2556 PrintAndLog ( "Saved to file: %s" , filename
);
2563 int CmdHF14AMfSniff ( const char * Cmd
){
2565 bool wantLogToFile
= 0 ;
2566 bool wantDecrypt
= 0 ;
2567 //bool wantSaveToEml = 0; TODO
2568 bool wantSaveToEmlFile
= 0 ;
2579 uint8_t atqa
[ 2 ] = { 0x00 };
2582 uint8_t * buf
= NULL
;
2583 uint16_t bufsize
= 0 ;
2584 uint8_t * bufPtr
= NULL
;
2587 char ctmp
= param_getchar ( Cmd
, 0 );
2588 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2589 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2590 PrintAndLog ( "You can specify:" );
2591 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2592 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2593 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2594 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2595 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2596 PrintAndLog ( " sample: hf mf sniff l d e" );
2600 for ( int i
= 0 ; i
< 4 ; i
++) {
2601 ctmp
= param_getchar ( Cmd
, i
);
2602 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2603 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2604 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2605 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2608 printf ( "------------------------------------------------------------------------- \n " );
2609 printf ( "Executing command. \n " );
2610 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2611 printf ( "Press the key on pc keyboard to abort the client. \n " );
2612 printf ( "------------------------------------------------------------------------- \n " );
2614 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2615 clearCommandBuffer ();
2624 printf ( " \n aborted via keyboard! \n " );
2629 if ( WaitForResponseTimeoutW ( CMD_ACK
, & resp
, 2000 , false )) {
2630 res
= resp
. arg
[ 0 ] & 0xff ;
2631 uint16_t traceLen
= resp
. arg
[ 1 ];
2634 if ( res
== 0 ) { // we are done
2638 if ( res
== 1 ) { // there is (more) data to be transferred
2639 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2640 if ( traceLen
> bufsize
|| buf
== NULL
) {
2642 if ( buf
== NULL
) { // not yet allocated
2643 p
= malloc ( traceLen
);
2644 } else { // need more memory
2645 p
= realloc ( buf
, traceLen
);
2648 PrintAndLog ( "Cannot allocate memory for trace" );
2656 memset ( buf
, 0x00 , traceLen
);
2658 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2663 if ( res
== 2 ) { // received all data, start displaying
2664 blockLen
= bufPtr
- buf
;
2667 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2668 while ( bufPtr
- buf
< blockLen
) {
2669 bufPtr
+= 6 ; // skip (void) timing information
2670 len
= *(( uint16_t *) bufPtr
);
2677 parlen
= ( len
- 1 ) / 8 + 1 ;
2679 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2680 memcpy ( uid
, bufPtr
+ 2 , 7 );
2681 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2682 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2684 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2685 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2689 if ( wantLogToFile
|| wantDecrypt
) {
2690 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2691 AddLogCurrentDT ( logHexFileName
);
2694 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2696 oddparitybuf ( bufPtr
, len
, parity
);
2697 PrintAndLog ( "%s(%d):%s [%s] c[%s]%c" ,
2698 isTag
? "TAG" : "RDR" ,
2700 sprint_hex ( bufPtr
, len
),
2701 printBitsPar ( bufPtr
+ len
, len
),
2702 printBitsPar ( parity
, len
),
2703 memcmp ( bufPtr
+ len
, parity
, len
/ 8 + 1 ) ? '!' : ' ' );
2705 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2707 mfTraceDecode ( bufPtr
, len
, bufPtr
[ len
], wantSaveToEmlFile
);
2711 bufPtr
+= parlen
; // ignore parity
2720 msleep ( 300 ); // wait for exiting arm side.
2721 PrintAndLog ( "Done." );
2725 //needs nt, ar, at, Data to decrypt
2726 int CmdDecryptTraceCmds ( const char * Cmd
){
2729 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2730 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2733 int CmdHF14AMfAuth4 ( const char * cmd
) {
2734 uint8_t keyn
[ 20 ] = { 0 };
2736 uint8_t key
[ 16 ] = { 0 };
2739 CLIParserInit ( "hf mf auth4" ,
2740 "Executes AES authentication command in ISO14443-4" ,
2741 "Usage: \n\t hf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication \n "
2742 " \t hf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication \n " );
2744 void * argtable
[] = {
2746 arg_str1 ( NULL
, NULL
, "<Key Num (HEX 2 bytes)>" , NULL
),
2747 arg_str1 ( NULL
, NULL
, "<Key Value (HEX 16 bytes)>" , NULL
),
2750 CLIExecWithReturn ( cmd
, argtable
, true );
2752 CLIGetHexWithReturn ( 1 , keyn
, & keynlen
);
2753 CLIGetHexWithReturn ( 2 , key
, & keylen
);
2757 PrintAndLog ( "ERROR: <Key Num> must be 2 bytes long instead of: %d" , keynlen
);
2762 PrintAndLog ( "ERROR: <Key Value> must be 16 bytes long instead of: %d" , keylen
);
2766 return MifareAuth4 ( NULL
, keyn
, key
, true , false , true );
2769 // https://www.nxp.com/docs/en/application-note/AN10787.pdf
2770 int CmdHF14AMfMAD ( const char * cmd
) {
2772 CLIParserInit ( "hf mf mad" ,
2773 "Checks and prints Mifare Application Directory (MAD)" ,
2774 "Usage: \n\t hf mf mad -> shows MAD if exists \n "
2775 " \t hf mf mad -a 03e1 -k ffffffffffff -b -> shows NDEF data if exists. read card with custom key and key B \n " );
2777 void * argtable
[] = {
2779 arg_lit0 ( "vV" , "verbose" , "show technical data" ),
2780 arg_str0 ( "aA" , "aid" , "print all sectors with aid" , NULL
),
2781 arg_str0 ( "kK" , "key" , "key for printing sectors" , NULL
),
2782 arg_lit0 ( "bB" , "keyb" , "use key B for access printing sectors (by default: key A)" ),
2785 CLIExecWithReturn ( cmd
, argtable
, true );
2786 bool verbose
= arg_get_lit ( 1 );
2787 uint8_t aid
[ 2 ] = { 0 };
2789 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2790 uint8_t key
[ 6 ] = { 0 };
2792 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2793 bool keyB
= arg_get_lit ( 4 );
2797 if ( aidlen
!= 2 && keylen
> 0 ) {
2798 PrintAndLogEx ( WARNING
, "do not need a key without aid." );
2801 uint8_t sector0
[ 16 * 4 ] = { 0 };
2802 uint8_t sector10
[ 16 * 4 ] = { 0 };
2803 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2804 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2809 for ( int i
= 0 ; i
< 4 ; i
++)
2810 PrintAndLogEx ( NORMAL
, "[%d] %s" , i
, sprint_hex (& sector0
[ i
* 16 ], 16 ));
2813 bool haveMAD2
= false ;
2814 MAD1DecodeAndPrint ( sector0
, verbose
, & haveMAD2
);
2817 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2818 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2822 MAD2DecodeAndPrint ( sector10
, verbose
);
2826 uint16_t aaid
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2827 PrintAndLogEx ( NORMAL
, " \n -------------- AID 0x%04x ---------------" , aaid
);
2829 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2831 if ( MADDecode ( sector0
, sector10
, mad
, & madlen
)) {
2832 PrintAndLogEx ( ERR
, "can't decode mad." );
2836 uint8_t akey
[ 6 ] = { 0 };
2837 memcpy ( akey
, g_mifare_ndef_key
, 6 );
2839 memcpy ( akey
, key
, 6 );
2842 for ( int i
= 0 ; i
< madlen
; i
++) {
2843 if ( aaid
== mad
[ i
]) {
2844 uint8_t vsector
[ 16 * 4 ] = { 0 };
2845 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, akey
, vsector
)) {
2846 PrintAndLogEx ( NORMAL
, "" );
2847 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2851 for ( int j
= 0 ; j
< ( verbose
? 4 : 3 ); j
++)
2852 PrintAndLogEx ( NORMAL
, " [%03d] %s" , ( i
+ 1 ) * 4 + j
, sprint_hex (& vsector
[ j
* 16 ], 16 ));
2860 int CmdHFMFNDEF ( const char * cmd
) {
2862 CLIParserInit ( "hf mf ndef" ,
2863 "Prints NFC Data Exchange Format (NDEF)" ,
2864 "Usage: \n\t hf mf ndef -> shows NDEF data \n "
2865 " \t hf mf ndef -a 03e1 -k ffffffffffff -b -> shows NDEF data with custom AID, key and with key B \n " );
2867 void * argtable
[] = {
2869 arg_litn ( "vV" , "verbose" , 0 , 2 , "show technical data" ),
2870 arg_str0 ( "aA" , "aid" , "replace default aid for NDEF" , NULL
),
2871 arg_str0 ( "kK" , "key" , "replace default key for NDEF" , NULL
),
2872 arg_lit0 ( "bB" , "keyb" , "use key B for access sectors (by default: key A)" ),
2875 CLIExecWithReturn ( cmd
, argtable
, true );
2877 bool verbose
= arg_get_lit ( 1 );
2878 bool verbose2
= arg_get_lit ( 1 ) > 1 ;
2879 uint8_t aid
[ 2 ] = { 0 };
2881 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2882 uint8_t key
[ 6 ] = { 0 };
2884 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2885 bool keyB
= arg_get_lit ( 4 );
2889 uint16_t ndefAID
= 0x03e1 ;
2891 ndefAID
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2893 uint8_t ndefkey
[ 6 ] = { 0 };
2894 memcpy ( ndefkey
, g_mifare_ndef_key
, 6 );
2896 memcpy ( ndefkey
, key
, 6 );
2899 uint8_t sector0
[ 16 * 4 ] = { 0 };
2900 uint8_t sector10
[ 16 * 4 ] = { 0 };
2901 uint8_t data
[ 4096 ] = { 0 };
2904 PrintAndLogEx ( NORMAL
, "" );
2906 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2907 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2911 bool haveMAD2
= false ;
2912 int res
= MADCheck ( sector0
, NULL
, verbose
, & haveMAD2
);
2914 PrintAndLogEx ( ERR
, "MAD error %d." , res
);
2919 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2920 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2925 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2927 if ( MADDecode ( sector0
, ( haveMAD2
? sector10
: NULL
), mad
, & madlen
)) {
2928 PrintAndLogEx ( ERR
, "can't decode mad." );
2932 printf ( "data reading:" );
2933 for ( int i
= 0 ; i
< madlen
; i
++) {
2934 if ( ndefAID
== mad
[ i
]) {
2935 uint8_t vsector
[ 16 * 4 ] = { 0 };
2936 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, ndefkey
, vsector
)) {
2937 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2941 memcpy (& data
[ datalen
], vsector
, 16 * 3 );
2950 PrintAndLogEx ( ERR
, "no NDEF data." );
2955 PrintAndLogEx ( NORMAL
, "NDEF data:" );
2956 dump_buffer ( data
, datalen
, stdout
, 1 );
2959 NDEFDecodeAndPrint ( data
, datalen
, verbose
);
2964 static command_t CommandTable
[] =
2966 { "help" , CmdHelp
, 1 , "This help" },
2967 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2968 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2969 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2970 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2971 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2972 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2973 { "auth4" , CmdHF14AMfAuth4
, 0 , "ISO14443-4 AES authentication" },
2974 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2975 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2976 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2977 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2978 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2979 { "sim" , CmdHF14AMfSim
, 0 , "Simulate MIFARE card" },
2980 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory" },
2981 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2982 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2983 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2984 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2985 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2986 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2987 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2988 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2989 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2990 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2991 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2992 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2993 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2994 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2995 { "mad" , CmdHF14AMfMAD
, 0 , "Checks and prints MAD" },
2996 { "ndef" , CmdHFMFNDEF
, 0 , "Prints NDEF records from card" },
2997 { NULL
, NULL
, 0 , NULL
}
3000 int CmdHFMF ( const char * Cmd
)
3002 ( void ) WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
3003 CmdsParse ( CommandTable
, Cmd
);
3007 int CmdHelp ( const char * Cmd
)
3009 CmdsHelp ( CommandTable
);