]>
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 }};
1954 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1959 uint64_t keyA
, keyB
;
1961 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1962 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1963 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1964 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1966 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1970 char cmdp
= param_getchar ( Cmd
, 0 );
1973 case '0' : numSectors
= 5 ; break ;
1975 case '\0' : numSectors
= 16 ; break ;
1976 case '2' : numSectors
= 32 ; break ;
1977 case '4' : numSectors
= 40 ; break ;
1978 default : numSectors
= 16 ;
1981 PrintAndLog ( "|---|----------------|----------------|" );
1982 PrintAndLog ( "|sec|key A |key B |" );
1983 PrintAndLog ( "|---|----------------|----------------|" );
1984 for ( i
= 0 ; i
< numSectors
; i
++) {
1985 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1986 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1989 keyA
= bytes_to_num ( data
, 6 );
1990 keyB
= bytes_to_num ( data
+ 10 , 6 );
1991 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1993 PrintAndLog ( "|---|----------------|----------------|" );
1998 int CmdHF14AMfCSetUID ( const char * Cmd
)
2000 uint8_t uid
[ 8 ] = { 0x00 };
2001 uint8_t oldUid
[ 8 ] = { 0x00 };
2002 uint8_t atqa
[ 2 ] = { 0x00 };
2003 uint8_t sak
[ 1 ] = { 0x00 };
2004 uint8_t atqaPresent
= 0 ;
2007 uint8_t needHelp
= 0 ;
2010 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
2011 PrintAndLog ( "UID must include 8 HEX symbols" );
2015 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
2019 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
2020 PrintAndLog ( "ATQA must include 4 HEX symbols" );
2024 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
2025 PrintAndLog ( "SAK must include 2 HEX symbols" );
2030 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
2032 switch ( param_getchar ( Cmd
, cmdp
))
2039 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
2046 if ( strlen ( Cmd
) < 1 || needHelp
) {
2048 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
2049 PrintAndLog ( "sample: hf mf csetuid 01020304" );
2050 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
2051 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
2055 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
2057 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
2060 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
2062 PrintAndLog ( "Can't set UID. Error=%d" , res
);
2066 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
2067 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
2071 int CmdHF14AMfCWipe ( const char * Cmd
)
2074 int numBlocks
= 16 * 4 ;
2075 bool wipeCard
= false ;
2076 bool fillCard
= false ;
2078 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2079 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [f]" );
2080 PrintAndLog ( "sample: hf mf cwipe 1 w f" );
2081 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
2082 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
2083 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
2087 gen
= mfCIdentify ();
2088 if (( gen
!= 1 ) && ( gen
!= 2 ))
2091 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2094 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2095 switch ( param_getchar ( Cmd
, cmdp
)) {
2110 if (! wipeCard
&& ! fillCard
)
2113 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2116 /* generation 1b magic card */
2118 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2120 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2122 /* generation 1a magic card by default */
2123 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2127 PrintAndLog ( "Can't wipe. error=%d" , res
);
2134 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2136 uint8_t memBlock
[ 16 ] = { 0x00 };
2137 uint8_t blockNo
= 0 ;
2138 bool wipeCard
= false ;
2141 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2142 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2143 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2144 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2145 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2149 gen
= mfCIdentify ();
2150 if (( gen
!= 1 ) && ( gen
!= 2 ))
2153 blockNo
= param_get8 ( Cmd
, 0 );
2155 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2156 PrintAndLog ( "block data must include 32 HEX symbols" );
2160 char ctmp
= param_getchar ( Cmd
, 2 );
2161 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2162 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2165 /* generation 1b magic card */
2166 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2168 /* generation 1a magic card by default */
2169 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2173 PrintAndLog ( "Can't write block. error=%d" , res
);
2180 int CmdHF14AMfCLoad ( const char * Cmd
)
2183 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2184 char * fnameptr
= filename
;
2185 char buf
[ 256 ] = { 0x00 };
2186 uint8_t buf8
[ 256 ] = { 0x00 };
2187 uint8_t fillFromEmulator
= 0 ;
2188 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2190 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2191 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2192 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2193 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2194 PrintAndLog ( " or: hf mf cload e [4]" );
2195 PrintAndLog ( "Sample: hf mf cload filename" );
2196 PrintAndLog ( " hf mf cload filname 4" );
2197 PrintAndLog ( " hf mf cload e" );
2198 PrintAndLog ( " hf mf cload e 4" );
2202 char ctmp
= param_getchar ( Cmd
, 0 );
2203 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2204 ctmp
= param_getchar ( Cmd
, 1 );
2205 if ( ctmp
== '4' ) numblock
= 256 ;
2207 gen
= mfCIdentify ();
2208 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2210 if ( fillFromEmulator
) {
2211 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2212 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2213 PrintAndLog ( "Cant get block: %d" , blockNum
);
2216 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2217 if ( blockNum
== 1 ) flags
= 0 ; // just write
2218 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2221 /* generation 1b magic card */
2222 flags
|= CSETBLOCK_MAGIC_1B
;
2223 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2224 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2230 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2232 len
= strlen ( filename
);
2233 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2235 //memcpy(filename, Cmd, len);
2238 sprintf ( fnameptr
, ".eml" );
2241 f
= fopen ( filename
, "r" );
2243 PrintAndLog ( "File not found or locked." );
2250 memset ( buf
, 0 , sizeof ( buf
));
2252 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2254 PrintAndLog ( "File reading error." );
2258 if ( strlen ( buf
) < 32 ) {
2259 if ( strlen ( buf
) && feof ( f
))
2261 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2265 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2266 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2268 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2269 if ( blockNum
== 1 ) flags
= 0 ; // just write
2270 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2273 /* generation 1b magic card */
2274 flags
|= CSETBLOCK_MAGIC_1B
;
2275 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2276 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2282 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2286 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2287 if ( blockNum
!= numblock
){
2288 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2291 PrintAndLog ( "Loaded from file: %s" , filename
);
2297 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2298 uint8_t memBlock
[ 16 ];
2299 uint8_t blockNo
= 0 ;
2301 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2303 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2304 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2305 PrintAndLog ( "sample: hf mf cgetblk 1" );
2306 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2310 gen
= mfCIdentify ();
2312 blockNo
= param_get8 ( Cmd
, 0 );
2314 PrintAndLog ( "--block number:%2d " , blockNo
);
2317 /* generation 1b magic card */
2318 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2320 /* generation 1a magic card by default */
2321 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2324 PrintAndLog ( "Can't read block. error=%d" , res
);
2328 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2330 if ( mfIsSectorTrailer ( blockNo
)) {
2331 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2332 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2333 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2334 int bln
= mfFirstBlockOfSector ( mfSectorNum ( blockNo
));
2335 int blinc
= ( mfNumBlocksPerSector ( mfSectorNum ( blockNo
)) > 4 ) ? 5 : 1 ;
2336 for ( int i
= 0 ; i
< 4 ; i
++) {
2337 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2340 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2346 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2347 uint8_t memBlock
[ 16 ] = { 0x00 };
2348 uint8_t sectorNo
= 0 ;
2349 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2351 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2352 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2353 PrintAndLog ( "sample: hf mf cgetsc 0" );
2354 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2358 sectorNo
= param_get8 ( Cmd
, 0 );
2360 if ( sectorNo
> 39 ) {
2361 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2365 PrintAndLog ( "--sector number:%d " , sectorNo
);
2367 gen
= mfCIdentify ();
2369 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2370 if ( sectorNo
< 32 ) {
2371 baseblock
= sectorNo
* 4 ;
2373 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2376 if ( sectorNo
> 31 ) sect_size
= 16 ;
2378 for ( i
= 0 ; i
< sect_size
; i
++) {
2379 if ( i
== 1 ) flags
= 0 ;
2380 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2383 /* generation 1b magic card */
2384 flags
|= CSETBLOCK_MAGIC_1B
;
2386 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2388 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2392 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2394 if ( mfIsSectorTrailer ( baseblock
+ i
)) {
2395 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2396 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2397 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2398 int bln
= baseblock
;
2399 int blinc
= ( mfNumBlocksPerSector ( sectorNo
) > 4 ) ? 5 : 1 ;
2400 for ( int i
= 0 ; i
< 4 ; i
++) {
2401 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2404 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2411 int CmdHF14AMfCSave ( const char * Cmd
) {
2414 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2415 char * fnameptr
= filename
;
2416 uint8_t fillFromEmulator
= 0 ;
2417 uint8_t buf
[ 256 ] = { 0x00 };
2418 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2420 // memset(filename, 0, sizeof(filename));
2421 // memset(buf, 0, sizeof(buf));
2423 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2424 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2425 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2426 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2427 PrintAndLog ( "Sample: hf mf csave " );
2428 PrintAndLog ( " hf mf csave filename" );
2429 PrintAndLog ( " hf mf csave e" );
2430 PrintAndLog ( " hf mf csave 4" );
2431 PrintAndLog ( " hf mf csave filename 4" );
2432 PrintAndLog ( " hf mf csave e 4" );
2436 char ctmp
= param_getchar ( Cmd
, 0 );
2437 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2438 if ( ctmp
== '4' ) numblock
= 256 ;
2439 ctmp
= param_getchar ( Cmd
, 1 );
2440 if ( ctmp
== '4' ) numblock
= 256 ;
2442 gen
= mfCIdentify ();
2443 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2445 if ( fillFromEmulator
) {
2446 // put into emulator
2447 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2448 for ( i
= 0 ; i
< numblock
; i
++) {
2449 if ( i
== 1 ) flags
= 0 ;
2450 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2453 /* generation 1b magic card */
2454 flags
|= CSETBLOCK_MAGIC_1B
;
2456 if ( mfCGetBlock ( i
, buf
, flags
)) {
2457 PrintAndLog ( "Cant get block: %d" , i
);
2461 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2462 PrintAndLog ( "Cant set emul block: %d" , i
);
2468 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2470 len
= strlen ( filename
);
2471 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2473 ctmp
= param_getchar ( Cmd
, 0 );
2474 if ( len
< 1 || ( ctmp
== '4' )) {
2477 flags
= CSETBLOCK_SINGLE_OPER
;
2479 /* generation 1b magic card */
2480 flags
|= CSETBLOCK_MAGIC_1B
;
2481 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2482 PrintAndLog ( "Cant get block: %d" , 0 );
2483 len
= sprintf ( fnameptr
, "dump" );
2487 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2488 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2491 //memcpy(filename, Cmd, len);
2495 sprintf ( fnameptr
, ".eml" );
2498 f
= fopen ( filename
, "w+" );
2501 PrintAndLog ( "File not found or locked." );
2506 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2507 for ( i
= 0 ; i
< numblock
; i
++) {
2508 if ( i
== 1 ) flags
= 0 ;
2509 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2512 /* generation 1b magic card */
2513 flags
|= CSETBLOCK_MAGIC_1B
;
2514 if ( mfCGetBlock ( i
, buf
, flags
)) {
2515 PrintAndLog ( "Cant get block: %d" , i
);
2518 for ( j
= 0 ; j
< 16 ; j
++)
2519 fprintf ( f
, "%02x" , buf
[ j
]);
2524 PrintAndLog ( "Saved to file: %s" , filename
);
2531 int CmdHF14AMfSniff ( const char * Cmd
){
2533 bool wantLogToFile
= 0 ;
2534 bool wantDecrypt
= 0 ;
2535 //bool wantSaveToEml = 0; TODO
2536 bool wantSaveToEmlFile
= 0 ;
2547 uint8_t atqa
[ 2 ] = { 0x00 };
2550 uint8_t * buf
= NULL
;
2551 uint16_t bufsize
= 0 ;
2552 uint8_t * bufPtr
= NULL
;
2555 char ctmp
= param_getchar ( Cmd
, 0 );
2556 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2557 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2558 PrintAndLog ( "You can specify:" );
2559 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2560 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2561 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2562 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2563 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2564 PrintAndLog ( " sample: hf mf sniff l d e" );
2568 for ( int i
= 0 ; i
< 4 ; i
++) {
2569 ctmp
= param_getchar ( Cmd
, i
);
2570 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2571 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2572 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2573 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2576 printf ( "------------------------------------------------------------------------- \n " );
2577 printf ( "Executing command. \n " );
2578 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2579 printf ( "Press the key on pc keyboard to abort the client. \n " );
2580 printf ( "------------------------------------------------------------------------- \n " );
2582 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2583 clearCommandBuffer ();
2592 printf ( " \n aborted via keyboard! \n " );
2597 if ( WaitForResponseTimeoutW ( CMD_ACK
, & resp
, 2000 , false )) {
2598 res
= resp
. arg
[ 0 ] & 0xff ;
2599 uint16_t traceLen
= resp
. arg
[ 1 ];
2602 if ( res
== 0 ) { // we are done
2606 if ( res
== 1 ) { // there is (more) data to be transferred
2607 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2608 if ( traceLen
> bufsize
|| buf
== NULL
) {
2610 if ( buf
== NULL
) { // not yet allocated
2611 p
= malloc ( traceLen
);
2612 } else { // need more memory
2613 p
= realloc ( buf
, traceLen
);
2616 PrintAndLog ( "Cannot allocate memory for trace" );
2624 memset ( buf
, 0x00 , traceLen
);
2626 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2631 if ( res
== 2 ) { // received all data, start displaying
2632 blockLen
= bufPtr
- buf
;
2635 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2636 while ( bufPtr
- buf
< blockLen
) {
2637 bufPtr
+= 6 ; // skip (void) timing information
2638 len
= *(( uint16_t *) bufPtr
);
2645 parlen
= ( len
- 1 ) / 8 + 1 ;
2647 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2648 memcpy ( uid
, bufPtr
+ 2 , 7 );
2649 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2650 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2652 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2653 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2657 if ( wantLogToFile
|| wantDecrypt
) {
2658 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2659 AddLogCurrentDT ( logHexFileName
);
2662 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2664 oddparitybuf ( bufPtr
, len
, parity
);
2665 PrintAndLog ( "%s(%d):%s [%s] c[%s]%c" ,
2666 isTag
? "TAG" : "RDR" ,
2668 sprint_hex ( bufPtr
, len
),
2669 printBitsPar ( bufPtr
+ len
, len
),
2670 printBitsPar ( parity
, len
),
2671 memcmp ( bufPtr
+ len
, parity
, len
/ 8 + 1 ) ? '!' : ' ' );
2673 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2675 mfTraceDecode ( bufPtr
, len
, bufPtr
[ len
], wantSaveToEmlFile
);
2679 bufPtr
+= parlen
; // ignore parity
2688 msleep ( 300 ); // wait for exiting arm side.
2689 PrintAndLog ( "Done." );
2693 //needs nt, ar, at, Data to decrypt
2694 int CmdDecryptTraceCmds ( const char * Cmd
){
2697 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2698 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2701 int CmdHF14AMfAuth4 ( const char * cmd
) {
2702 uint8_t keyn
[ 20 ] = { 0 };
2704 uint8_t key
[ 16 ] = { 0 };
2707 CLIParserInit ( "hf mf auth4" ,
2708 "Executes AES authentication command in ISO14443-4" ,
2709 "Usage: \n\t hf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication \n "
2710 " \t hf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication \n " );
2712 void * argtable
[] = {
2714 arg_str1 ( NULL
, NULL
, "<Key Num (HEX 2 bytes)>" , NULL
),
2715 arg_str1 ( NULL
, NULL
, "<Key Value (HEX 16 bytes)>" , NULL
),
2718 CLIExecWithReturn ( cmd
, argtable
, true );
2720 CLIGetHexWithReturn ( 1 , keyn
, & keynlen
);
2721 CLIGetHexWithReturn ( 2 , key
, & keylen
);
2725 PrintAndLog ( "ERROR: <Key Num> must be 2 bytes long instead of: %d" , keynlen
);
2730 PrintAndLog ( "ERROR: <Key Value> must be 16 bytes long instead of: %d" , keylen
);
2734 return MifareAuth4 ( NULL
, keyn
, key
, true , false , true );
2737 // https://www.nxp.com/docs/en/application-note/AN10787.pdf
2738 int CmdHF14AMfMAD ( const char * cmd
) {
2740 CLIParserInit ( "hf mf mad" ,
2741 "Checks and prints Mifare Application Directory (MAD)" ,
2742 "Usage: \n\t hf mf mad -> shows MAD if exists \n "
2743 " \t hf mf mad -a 03e1 -k ffffffffffff -b -> shows NDEF data if exists. read card with custom key and key B \n " );
2745 void * argtable
[] = {
2747 arg_lit0 ( "vV" , "verbose" , "show technical data" ),
2748 arg_str0 ( "aA" , "aid" , "print all sectors with aid" , NULL
),
2749 arg_str0 ( "kK" , "key" , "key for printing sectors" , NULL
),
2750 arg_lit0 ( "bB" , "keyb" , "use key B for access printing sectors (by default: key A)" ),
2753 CLIExecWithReturn ( cmd
, argtable
, true );
2754 bool verbose
= arg_get_lit ( 1 );
2755 uint8_t aid
[ 2 ] = { 0 };
2757 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2758 uint8_t key
[ 6 ] = { 0 };
2760 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2761 bool keyB
= arg_get_lit ( 4 );
2765 if ( aidlen
!= 2 && keylen
> 0 ) {
2766 PrintAndLogEx ( WARNING
, "do not need a key without aid." );
2769 uint8_t sector0
[ 16 * 4 ] = { 0 };
2770 uint8_t sector10
[ 16 * 4 ] = { 0 };
2771 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2772 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2777 for ( int i
= 0 ; i
< 4 ; i
++)
2778 PrintAndLogEx ( NORMAL
, "[%d] %s" , i
, sprint_hex (& sector0
[ i
* 16 ], 16 ));
2781 bool haveMAD2
= false ;
2782 MAD1DecodeAndPrint ( sector0
, verbose
, & haveMAD2
);
2785 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2786 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2790 MAD2DecodeAndPrint ( sector10
, verbose
);
2794 uint16_t aaid
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2795 PrintAndLogEx ( NORMAL
, " \n -------------- AID 0x%04x ---------------" , aaid
);
2797 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2799 if ( MADDecode ( sector0
, sector10
, mad
, & madlen
)) {
2800 PrintAndLogEx ( ERR
, "can't decode mad." );
2804 uint8_t akey
[ 6 ] = { 0 };
2805 memcpy ( akey
, g_mifare_ndef_key
, 6 );
2807 memcpy ( akey
, key
, 6 );
2810 for ( int i
= 0 ; i
< madlen
; i
++) {
2811 if ( aaid
== mad
[ i
]) {
2812 uint8_t vsector
[ 16 * 4 ] = { 0 };
2813 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, akey
, vsector
)) {
2814 PrintAndLogEx ( NORMAL
, "" );
2815 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2819 for ( int j
= 0 ; j
< ( verbose
? 4 : 3 ); j
++)
2820 PrintAndLogEx ( NORMAL
, " [%03d] %s" , ( i
+ 1 ) * 4 + j
, sprint_hex (& vsector
[ j
* 16 ], 16 ));
2828 int CmdHFMFNDEF ( const char * cmd
) {
2830 CLIParserInit ( "hf mf ndef" ,
2831 "Prints NFC Data Exchange Format (NDEF)" ,
2832 "Usage: \n\t hf mf ndef -> shows NDEF data \n "
2833 " \t hf mf ndef -a 03e1 -k ffffffffffff -b -> shows NDEF data with custom AID, key and with key B \n " );
2835 void * argtable
[] = {
2837 arg_litn ( "vV" , "verbose" , 0 , 2 , "show technical data" ),
2838 arg_str0 ( "aA" , "aid" , "replace default aid for NDEF" , NULL
),
2839 arg_str0 ( "kK" , "key" , "replace default key for NDEF" , NULL
),
2840 arg_lit0 ( "bB" , "keyb" , "use key B for access sectors (by default: key A)" ),
2843 CLIExecWithReturn ( cmd
, argtable
, true );
2845 bool verbose
= arg_get_lit ( 1 );
2846 bool verbose2
= arg_get_lit ( 1 ) > 1 ;
2847 uint8_t aid
[ 2 ] = { 0 };
2849 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2850 uint8_t key
[ 6 ] = { 0 };
2852 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2853 bool keyB
= arg_get_lit ( 4 );
2857 uint16_t ndefAID
= 0x03e1 ;
2859 ndefAID
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2861 uint8_t ndefkey
[ 6 ] = { 0 };
2862 memcpy ( ndefkey
, g_mifare_ndef_key
, 6 );
2864 memcpy ( ndefkey
, key
, 6 );
2867 uint8_t sector0
[ 16 * 4 ] = { 0 };
2868 uint8_t sector10
[ 16 * 4 ] = { 0 };
2869 uint8_t data
[ 4096 ] = { 0 };
2872 PrintAndLogEx ( NORMAL
, "" );
2874 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2875 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2879 bool haveMAD2
= false ;
2880 int res
= MADCheck ( sector0
, NULL
, verbose
, & haveMAD2
);
2882 PrintAndLogEx ( ERR
, "MAD error %d." , res
);
2887 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2888 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2893 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2895 if ( MADDecode ( sector0
, ( haveMAD2
? sector10
: NULL
), mad
, & madlen
)) {
2896 PrintAndLogEx ( ERR
, "can't decode mad." );
2900 printf ( "data reading:" );
2901 for ( int i
= 0 ; i
< madlen
; i
++) {
2902 if ( ndefAID
== mad
[ i
]) {
2903 uint8_t vsector
[ 16 * 4 ] = { 0 };
2904 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, ndefkey
, vsector
)) {
2905 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2909 memcpy (& data
[ datalen
], vsector
, 16 * 3 );
2918 PrintAndLogEx ( ERR
, "no NDEF data." );
2923 PrintAndLogEx ( NORMAL
, "NDEF data:" );
2924 dump_buffer ( data
, datalen
, stdout
, 1 );
2927 NDEFDecodeAndPrint ( data
, datalen
, verbose
);
2932 static command_t CommandTable
[] =
2934 { "help" , CmdHelp
, 1 , "This help" },
2935 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2936 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2937 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2938 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2939 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2940 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2941 { "auth4" , CmdHF14AMfAuth4
, 0 , "ISO14443-4 AES authentication" },
2942 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2943 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2944 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2945 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2946 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2947 { "sim" , CmdHF14AMfSim
, 0 , "Simulate MIFARE card" },
2948 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory" },
2949 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2950 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2951 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2952 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2953 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2954 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2955 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2956 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2957 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2958 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2959 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2960 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2961 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2962 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2963 { "mad" , CmdHF14AMfMAD
, 0 , "Checks and prints MAD" },
2964 { "ndef" , CmdHFMFNDEF
, 0 , "Prints NDEF records from card" },
2965 { NULL
, NULL
, 0 , NULL
}
2968 int CmdHFMF ( const char * Cmd
)
2970 ( void ) WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2971 CmdsParse ( CommandTable
, Cmd
);
2975 int CmdHelp ( const char * Cmd
)
2977 CmdsHelp ( CommandTable
);