]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1c006fbf6924d15c46009c5d048b447474a067f7
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' : numBlocks
= 5 ; break ;
260 case '2' : numBlocks
= 32 ; break ;
261 case '4' : numBlocks
= 40 ; break ;
262 default : numBlocks
= 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_mf1ksim ( void ) {
1425 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1426 PrintAndLog ( "options:" );
1427 PrintAndLog ( " h this help" );
1428 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1429 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1430 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1431 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1432 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1433 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1434 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1435 PrintAndLog ( "samples:" );
1436 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1437 PrintAndLog ( " hf mf sim u 11223344556677" );
1438 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1439 PrintAndLog ( " hf mf sim f uids.txt" );
1440 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1445 int CmdHF14AMf1kSim ( const char * Cmd
) {
1447 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1448 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 ;
1463 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1464 switch ( param_getchar ( Cmd
, cmdp
)) {
1467 setEmulatorMem
= true ;
1469 flags
|= FLAG_INTERACTIVE
;
1470 flags
|= FLAG_NR_AR_ATTACK
;
1475 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
, sizeof ( filename
));
1477 PrintAndLog ( "error no filename found" );
1480 attackFromFile
= true ;
1482 flags
|= FLAG_INTERACTIVE
;
1483 flags
|= FLAG_NR_AR_ATTACK
;
1488 return usage_hf14_mf1ksim ();
1491 flags
|= FLAG_INTERACTIVE
;
1496 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1501 flags
|= FLAG_RANDOM_NONCE
;
1506 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1508 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1509 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1510 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1511 default : return usage_hf14_mf1ksim ();
1517 flags
|= FLAG_NR_AR_ATTACK
;
1521 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1528 if ( errors
) return usage_hf14_mf1ksim ();
1531 if ( attackFromFile
) {
1534 f
= fopen ( filename
, "r" );
1536 PrintAndLog ( "File %s not found or locked" , filename
);
1539 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1540 while (! feof ( f
) && ! ukbhit ()){
1541 memset ( buf
, 0 , sizeof ( buf
));
1542 memset ( uid
, 0 , sizeof ( uid
));
1544 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1545 if ( count
> 0 ) break ;
1547 PrintAndLog ( "File reading error." );
1551 if (! strlen ( buf
) && feof ( f
)) break ;
1553 uidlen
= strlen ( buf
)- 1 ;
1555 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1556 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1557 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1559 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1564 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1565 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1568 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1569 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1570 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1571 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1572 , exitAfterNReads
, flags
, flags
);
1574 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1575 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1576 clearCommandBuffer ();
1579 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1580 //We're waiting only 1.5 s at a time, otherwise we get the
1581 // annoying message about "Waiting for a response... "
1584 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1585 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1586 // We can skip the standard attack if we have RANDOM_NONCE set.
1587 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1588 if (( bool ) resp
. arg
[ 1 ]) {
1589 PrintAndLog ( "Device button pressed - quitting" );
1596 } else { //not from file
1598 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1599 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1600 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1601 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1602 , exitAfterNReads
, flags
, flags
);
1604 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1605 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1606 clearCommandBuffer ();
1609 if ( flags
& FLAG_INTERACTIVE
) {
1610 PrintAndLog ( "Press pm3-button to abort simulation" );
1611 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1612 //We're waiting only 1.5 s at a time, otherwise we get the
1613 // annoying message about "Waiting for a response... "
1616 if ( flags
& FLAG_NR_AR_ATTACK
) {
1617 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1618 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1619 // We can skip the standard attack if we have RANDOM_NONCE set.
1620 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1628 int CmdHF14AMfDbg ( const char * Cmd
)
1630 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1632 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1635 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1636 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1637 PrintAndLog ( " 0 - no debug messages" );
1638 PrintAndLog ( " 1 - error messages" );
1639 PrintAndLog ( " 2 - plus information messages" );
1640 PrintAndLog ( " 3 - plus debug messages" );
1641 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1642 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1646 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1652 int CmdHF14AMfEGet ( const char * Cmd
)
1654 uint8_t blockNo
= 0 ;
1655 uint8_t data
[ 16 ] = { 0x00 };
1657 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1658 PrintAndLog ( "Usage: hf mf eget <block number>" );
1659 PrintAndLog ( " sample: hf mf eget 0 " );
1663 blockNo
= param_get8 ( Cmd
, 0 );
1666 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1667 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1669 PrintAndLog ( "Command execute timeout" );
1675 int CmdHF14AMfEClear ( const char * Cmd
)
1677 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1678 PrintAndLog ( "Usage: hf mf eclr" );
1679 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1683 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1689 int CmdHF14AMfESet ( const char * Cmd
)
1691 uint8_t memBlock
[ 16 ];
1692 uint8_t blockNo
= 0 ;
1694 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1696 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1697 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1698 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1702 blockNo
= param_get8 ( Cmd
, 0 );
1704 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1705 PrintAndLog ( "block data must include 32 HEX symbols" );
1710 return mfEmlSetMem ( memBlock
, blockNo
, 1 );
1714 int CmdHF14AMfELoad ( const char * Cmd
)
1717 char filename
[ FILE_PATH_SIZE
];
1718 char * fnameptr
= filename
;
1719 char buf
[ 64 ] = { 0x00 };
1720 uint8_t buf8
[ 64 ] = { 0x00 };
1721 int i
, len
, blockNum
, numBlocks
;
1722 int nameParamNo
= 1 ;
1724 char ctmp
= param_getchar ( Cmd
, 0 );
1726 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1727 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1728 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1729 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1731 PrintAndLog ( " sample: hf mf eload filename" );
1732 PrintAndLog ( " hf mf eload 4 filename" );
1737 case '0' : numBlocks
= 5 * 4 ; break ;
1739 case '\0' : numBlocks
= 16 * 4 ; break ;
1740 case '2' : numBlocks
= 32 * 4 ; break ;
1741 case '4' : numBlocks
= 256 ; break ;
1748 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1750 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1754 sprintf ( fnameptr
, ".eml" );
1757 f
= fopen ( filename
, "r" );
1759 PrintAndLog ( "File %s not found or locked" , filename
);
1765 memset ( buf
, 0 , sizeof ( buf
));
1767 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1769 if ( blockNum
>= numBlocks
) break ;
1771 PrintAndLog ( "File reading error." );
1776 if ( strlen ( buf
) < 32 ){
1777 if ( strlen ( buf
) && feof ( f
))
1779 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1784 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1785 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1788 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1789 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1796 if ( blockNum
>= numBlocks
) break ;
1801 if (( blockNum
!= numBlocks
)) {
1802 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1805 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1810 int CmdHF14AMfESave ( const char * Cmd
)
1813 char filename
[ FILE_PATH_SIZE
];
1814 char * fnameptr
= filename
;
1816 int i
, j
, len
, numBlocks
;
1817 int nameParamNo
= 1 ;
1819 memset ( filename
, 0 , sizeof ( filename
));
1820 memset ( buf
, 0 , sizeof ( buf
));
1822 char ctmp
= param_getchar ( Cmd
, 0 );
1824 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1825 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1826 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1827 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1829 PrintAndLog ( " sample: hf mf esave " );
1830 PrintAndLog ( " hf mf esave 4" );
1831 PrintAndLog ( " hf mf esave 4 filename" );
1836 case '0' : numBlocks
= 5 * 4 ; break ;
1838 case '\0' : numBlocks
= 16 * 4 ; break ;
1839 case '2' : numBlocks
= 32 * 4 ; break ;
1840 case '4' : numBlocks
= 256 ; break ;
1847 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1849 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1851 // user supplied filename?
1853 // get filename (UID from memory)
1854 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1855 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1856 len
= sprintf ( fnameptr
, "dump" );
1860 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1861 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1867 // add file extension
1868 sprintf ( fnameptr
, ".eml" );
1871 f
= fopen ( filename
, "w+" );
1874 PrintAndLog ( "Can't open file %s " , filename
);
1879 for ( i
= 0 ; i
< numBlocks
; i
++) {
1880 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1881 PrintAndLog ( "Cant get block: %d" , i
);
1884 for ( j
= 0 ; j
< 16 ; j
++)
1885 fprintf ( f
, "%02X" , buf
[ j
]);
1890 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1896 int CmdHF14AMfECFill ( const char * Cmd
)
1898 uint8_t keyType
= 0 ;
1899 uint8_t numSectors
= 16 ;
1901 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1902 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1903 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1905 PrintAndLog ( "samples: hf mf ecfill A" );
1906 PrintAndLog ( " hf mf ecfill A 4" );
1907 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1908 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1912 char ctmp
= param_getchar ( Cmd
, 0 );
1913 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1914 PrintAndLog ( "Key type must be A or B" );
1917 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1919 ctmp
= param_getchar ( Cmd
, 1 );
1921 case '0' : numSectors
= 5 ; break ;
1923 case '\0' : numSectors
= 16 ; break ;
1924 case '2' : numSectors
= 32 ; break ;
1925 case '4' : numSectors
= 40 ; break ;
1926 default : numSectors
= 16 ;
1929 printf ( "--params: numSectors: %d, keyType:%d \n " , numSectors
, keyType
);
1930 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1935 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1940 uint64_t keyA
, keyB
;
1942 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1943 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1944 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1945 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1947 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1951 char cmdp
= param_getchar ( Cmd
, 0 );
1954 case '0' : numSectors
= 5 ; break ;
1956 case '\0' : numSectors
= 16 ; break ;
1957 case '2' : numSectors
= 32 ; break ;
1958 case '4' : numSectors
= 40 ; break ;
1959 default : numSectors
= 16 ;
1962 PrintAndLog ( "|---|----------------|----------------|" );
1963 PrintAndLog ( "|sec|key A |key B |" );
1964 PrintAndLog ( "|---|----------------|----------------|" );
1965 for ( i
= 0 ; i
< numSectors
; i
++) {
1966 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1967 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1970 keyA
= bytes_to_num ( data
, 6 );
1971 keyB
= bytes_to_num ( data
+ 10 , 6 );
1972 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1974 PrintAndLog ( "|---|----------------|----------------|" );
1979 int CmdHF14AMfCSetUID ( const char * Cmd
)
1981 uint8_t uid
[ 8 ] = { 0x00 };
1982 uint8_t oldUid
[ 8 ] = { 0x00 };
1983 uint8_t atqa
[ 2 ] = { 0x00 };
1984 uint8_t sak
[ 1 ] = { 0x00 };
1985 uint8_t atqaPresent
= 0 ;
1988 uint8_t needHelp
= 0 ;
1991 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
1992 PrintAndLog ( "UID must include 8 HEX symbols" );
1996 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
2000 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
2001 PrintAndLog ( "ATQA must include 4 HEX symbols" );
2005 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
2006 PrintAndLog ( "SAK must include 2 HEX symbols" );
2011 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
2013 switch ( param_getchar ( Cmd
, cmdp
))
2020 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
2027 if ( strlen ( Cmd
) < 1 || needHelp
) {
2029 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
2030 PrintAndLog ( "sample: hf mf csetuid 01020304" );
2031 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
2032 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
2036 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
2038 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
2041 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
2043 PrintAndLog ( "Can't set UID. Error=%d" , res
);
2047 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
2048 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
2052 int CmdHF14AMfCWipe ( const char * Cmd
)
2055 int numBlocks
= 16 * 4 ;
2056 bool wipeCard
= false ;
2057 bool fillCard
= false ;
2059 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2060 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [f]" );
2061 PrintAndLog ( "sample: hf mf cwipe 1 w f" );
2062 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
2063 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
2064 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
2068 gen
= mfCIdentify ();
2069 if (( gen
!= 1 ) && ( gen
!= 2 ))
2072 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2075 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2076 switch ( param_getchar ( Cmd
, cmdp
)) {
2091 if (! wipeCard
&& ! fillCard
)
2094 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2097 /* generation 1b magic card */
2099 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2101 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2103 /* generation 1a magic card by default */
2104 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2108 PrintAndLog ( "Can't wipe. error=%d" , res
);
2115 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2117 uint8_t memBlock
[ 16 ] = { 0x00 };
2118 uint8_t blockNo
= 0 ;
2119 bool wipeCard
= false ;
2122 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2123 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2124 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2125 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2126 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2130 gen
= mfCIdentify ();
2131 if (( gen
!= 1 ) && ( gen
!= 2 ))
2134 blockNo
= param_get8 ( Cmd
, 0 );
2136 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2137 PrintAndLog ( "block data must include 32 HEX symbols" );
2141 char ctmp
= param_getchar ( Cmd
, 2 );
2142 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2143 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2146 /* generation 1b magic card */
2147 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2149 /* generation 1a magic card by default */
2150 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2154 PrintAndLog ( "Can't write block. error=%d" , res
);
2161 int CmdHF14AMfCLoad ( const char * Cmd
)
2164 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2165 char * fnameptr
= filename
;
2166 char buf
[ 256 ] = { 0x00 };
2167 uint8_t buf8
[ 256 ] = { 0x00 };
2168 uint8_t fillFromEmulator
= 0 ;
2169 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2171 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2172 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2173 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2174 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2175 PrintAndLog ( " or: hf mf cload e [4]" );
2176 PrintAndLog ( "Sample: hf mf cload filename" );
2177 PrintAndLog ( " hf mf cload filname 4" );
2178 PrintAndLog ( " hf mf cload e" );
2179 PrintAndLog ( " hf mf cload e 4" );
2183 char ctmp
= param_getchar ( Cmd
, 0 );
2184 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2185 ctmp
= param_getchar ( Cmd
, 1 );
2186 if ( ctmp
== '4' ) numblock
= 256 ;
2188 gen
= mfCIdentify ();
2189 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2191 if ( fillFromEmulator
) {
2192 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2193 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2194 PrintAndLog ( "Cant get block: %d" , blockNum
);
2197 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2198 if ( blockNum
== 1 ) flags
= 0 ; // just write
2199 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2202 /* generation 1b magic card */
2203 flags
|= CSETBLOCK_MAGIC_1B
;
2204 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2205 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2211 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2213 len
= strlen ( filename
);
2214 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2216 //memcpy(filename, Cmd, len);
2219 sprintf ( fnameptr
, ".eml" );
2222 f
= fopen ( filename
, "r" );
2224 PrintAndLog ( "File not found or locked." );
2231 memset ( buf
, 0 , sizeof ( buf
));
2233 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2235 PrintAndLog ( "File reading error." );
2239 if ( strlen ( buf
) < 32 ) {
2240 if ( strlen ( buf
) && feof ( f
))
2242 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2246 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2247 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2249 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2250 if ( blockNum
== 1 ) flags
= 0 ; // just write
2251 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2254 /* generation 1b magic card */
2255 flags
|= CSETBLOCK_MAGIC_1B
;
2256 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2257 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2263 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2267 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2268 if ( blockNum
!= numblock
){
2269 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2272 PrintAndLog ( "Loaded from file: %s" , filename
);
2278 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2279 uint8_t memBlock
[ 16 ];
2280 uint8_t blockNo
= 0 ;
2282 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2284 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2285 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2286 PrintAndLog ( "sample: hf mf cgetblk 1" );
2287 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2291 gen
= mfCIdentify ();
2293 blockNo
= param_get8 ( Cmd
, 0 );
2295 PrintAndLog ( "--block number:%2d " , blockNo
);
2298 /* generation 1b magic card */
2299 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2301 /* generation 1a magic card by default */
2302 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2305 PrintAndLog ( "Can't read block. error=%d" , res
);
2309 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2311 if ( mfIsSectorTrailer ( blockNo
)) {
2312 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2313 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2314 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2315 int bln
= mfFirstBlockOfSector ( mfSectorNum ( blockNo
));
2316 int blinc
= ( mfNumBlocksPerSector ( mfSectorNum ( blockNo
)) > 4 ) ? 5 : 1 ;
2317 for ( int i
= 0 ; i
< 4 ; i
++) {
2318 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2321 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2327 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2328 uint8_t memBlock
[ 16 ] = { 0x00 };
2329 uint8_t sectorNo
= 0 ;
2330 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2332 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2333 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2334 PrintAndLog ( "sample: hf mf cgetsc 0" );
2335 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2339 sectorNo
= param_get8 ( Cmd
, 0 );
2341 if ( sectorNo
> 39 ) {
2342 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2346 PrintAndLog ( "--sector number:%d " , sectorNo
);
2348 gen
= mfCIdentify ();
2350 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2351 if ( sectorNo
< 32 ) {
2352 baseblock
= sectorNo
* 4 ;
2354 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2357 if ( sectorNo
> 31 ) sect_size
= 16 ;
2359 for ( i
= 0 ; i
< sect_size
; i
++) {
2360 if ( i
== 1 ) flags
= 0 ;
2361 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2364 /* generation 1b magic card */
2365 flags
|= CSETBLOCK_MAGIC_1B
;
2367 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2369 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2373 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2375 if ( mfIsSectorTrailer ( baseblock
+ i
)) {
2376 PrintAndLogEx ( NORMAL
, "Trailer decoded:" );
2377 PrintAndLogEx ( NORMAL
, "Key A: %s" , sprint_hex_inrow ( memBlock
, 6 ));
2378 PrintAndLogEx ( NORMAL
, "Key B: %s" , sprint_hex_inrow (& memBlock
[ 10 ], 6 ));
2379 int bln
= baseblock
;
2380 int blinc
= ( mfNumBlocksPerSector ( sectorNo
) > 4 ) ? 5 : 1 ;
2381 for ( int i
= 0 ; i
< 4 ; i
++) {
2382 PrintAndLogEx ( NORMAL
, "Access block %d%s: %s" , bln
, (( blinc
> 1 ) && ( i
< 3 ) ? "+" : "" ) , mfGetAccessConditionsDesc ( i
, & memBlock
[ 6 ]));
2385 PrintAndLogEx ( NORMAL
, "UserData: %s" , sprint_hex_inrow (& memBlock
[ 9 ], 1 ));
2392 int CmdHF14AMfCSave ( const char * Cmd
) {
2395 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2396 char * fnameptr
= filename
;
2397 uint8_t fillFromEmulator
= 0 ;
2398 uint8_t buf
[ 256 ] = { 0x00 };
2399 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2401 // memset(filename, 0, sizeof(filename));
2402 // memset(buf, 0, sizeof(buf));
2404 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2405 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2406 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2407 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2408 PrintAndLog ( "Sample: hf mf csave " );
2409 PrintAndLog ( " hf mf csave filename" );
2410 PrintAndLog ( " hf mf csave e" );
2411 PrintAndLog ( " hf mf csave 4" );
2412 PrintAndLog ( " hf mf csave filename 4" );
2413 PrintAndLog ( " hf mf csave e 4" );
2417 char ctmp
= param_getchar ( Cmd
, 0 );
2418 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2419 if ( ctmp
== '4' ) numblock
= 256 ;
2420 ctmp
= param_getchar ( Cmd
, 1 );
2421 if ( ctmp
== '4' ) numblock
= 256 ;
2423 gen
= mfCIdentify ();
2424 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2426 if ( fillFromEmulator
) {
2427 // put into emulator
2428 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2429 for ( i
= 0 ; i
< numblock
; i
++) {
2430 if ( i
== 1 ) flags
= 0 ;
2431 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2434 /* generation 1b magic card */
2435 flags
|= CSETBLOCK_MAGIC_1B
;
2437 if ( mfCGetBlock ( i
, buf
, flags
)) {
2438 PrintAndLog ( "Cant get block: %d" , i
);
2442 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2443 PrintAndLog ( "Cant set emul block: %d" , i
);
2449 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2451 len
= strlen ( filename
);
2452 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2454 ctmp
= param_getchar ( Cmd
, 0 );
2455 if ( len
< 1 || ( ctmp
== '4' )) {
2458 flags
= CSETBLOCK_SINGLE_OPER
;
2460 /* generation 1b magic card */
2461 flags
|= CSETBLOCK_MAGIC_1B
;
2462 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2463 PrintAndLog ( "Cant get block: %d" , 0 );
2464 len
= sprintf ( fnameptr
, "dump" );
2468 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2469 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2472 //memcpy(filename, Cmd, len);
2476 sprintf ( fnameptr
, ".eml" );
2479 f
= fopen ( filename
, "w+" );
2482 PrintAndLog ( "File not found or locked." );
2487 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2488 for ( i
= 0 ; i
< numblock
; i
++) {
2489 if ( i
== 1 ) flags
= 0 ;
2490 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2493 /* generation 1b magic card */
2494 flags
|= CSETBLOCK_MAGIC_1B
;
2495 if ( mfCGetBlock ( i
, buf
, flags
)) {
2496 PrintAndLog ( "Cant get block: %d" , i
);
2499 for ( j
= 0 ; j
< 16 ; j
++)
2500 fprintf ( f
, "%02x" , buf
[ j
]);
2505 PrintAndLog ( "Saved to file: %s" , filename
);
2512 int CmdHF14AMfSniff ( const char * Cmd
){
2514 bool wantLogToFile
= 0 ;
2515 bool wantDecrypt
= 0 ;
2516 //bool wantSaveToEml = 0; TODO
2517 bool wantSaveToEmlFile
= 0 ;
2528 uint8_t atqa
[ 2 ] = { 0x00 };
2531 uint8_t * buf
= NULL
;
2532 uint16_t bufsize
= 0 ;
2533 uint8_t * bufPtr
= NULL
;
2536 char ctmp
= param_getchar ( Cmd
, 0 );
2537 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2538 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2539 PrintAndLog ( "You can specify:" );
2540 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2541 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2542 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2543 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2544 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2545 PrintAndLog ( " sample: hf mf sniff l d e" );
2549 for ( int i
= 0 ; i
< 4 ; i
++) {
2550 ctmp
= param_getchar ( Cmd
, i
);
2551 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2552 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2553 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2554 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2557 printf ( "------------------------------------------------------------------------- \n " );
2558 printf ( "Executing command. \n " );
2559 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2560 printf ( "Press the key on pc keyboard to abort the client. \n " );
2561 printf ( "------------------------------------------------------------------------- \n " );
2563 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2564 clearCommandBuffer ();
2573 printf ( " \n aborted via keyboard! \n " );
2578 if ( WaitForResponseTimeoutW ( CMD_ACK
, & resp
, 2000 , false )) {
2579 res
= resp
. arg
[ 0 ] & 0xff ;
2580 uint16_t traceLen
= resp
. arg
[ 1 ];
2583 if ( res
== 0 ) { // we are done
2587 if ( res
== 1 ) { // there is (more) data to be transferred
2588 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2589 if ( traceLen
> bufsize
|| buf
== NULL
) {
2591 if ( buf
== NULL
) { // not yet allocated
2592 p
= malloc ( traceLen
);
2593 } else { // need more memory
2594 p
= realloc ( buf
, traceLen
);
2597 PrintAndLog ( "Cannot allocate memory for trace" );
2605 memset ( buf
, 0x00 , traceLen
);
2607 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2612 if ( res
== 2 ) { // received all data, start displaying
2613 blockLen
= bufPtr
- buf
;
2616 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2617 while ( bufPtr
- buf
< blockLen
) {
2618 bufPtr
+= 6 ; // skip (void) timing information
2619 len
= *(( uint16_t *) bufPtr
);
2626 parlen
= ( len
- 1 ) / 8 + 1 ;
2628 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2629 memcpy ( uid
, bufPtr
+ 2 , 7 );
2630 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2631 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2633 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2634 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2638 if ( wantLogToFile
|| wantDecrypt
) {
2639 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2640 AddLogCurrentDT ( logHexFileName
);
2643 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2645 oddparitybuf ( bufPtr
, len
, parity
);
2646 PrintAndLog ( "%s(%d):%s [%s] c[%s]%c" ,
2647 isTag
? "TAG" : "RDR" ,
2649 sprint_hex ( bufPtr
, len
),
2650 printBitsPar ( bufPtr
+ len
, len
),
2651 printBitsPar ( parity
, len
),
2652 memcmp ( bufPtr
+ len
, parity
, len
/ 8 + 1 ) ? '!' : ' ' );
2654 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2656 mfTraceDecode ( bufPtr
, len
, bufPtr
[ len
], wantSaveToEmlFile
);
2660 bufPtr
+= parlen
; // ignore parity
2669 msleep ( 300 ); // wait for exiting arm side.
2670 PrintAndLog ( "Done." );
2674 //needs nt, ar, at, Data to decrypt
2675 int CmdDecryptTraceCmds ( const char * Cmd
){
2678 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2679 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2682 int CmdHF14AMfAuth4 ( const char * cmd
) {
2683 uint8_t keyn
[ 20 ] = { 0 };
2685 uint8_t key
[ 16 ] = { 0 };
2688 CLIParserInit ( "hf mf auth4" ,
2689 "Executes AES authentication command in ISO14443-4" ,
2690 "Usage: \n\t hf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication \n "
2691 " \t hf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication \n " );
2693 void * argtable
[] = {
2695 arg_str1 ( NULL
, NULL
, "<Key Num (HEX 2 bytes)>" , NULL
),
2696 arg_str1 ( NULL
, NULL
, "<Key Value (HEX 16 bytes)>" , NULL
),
2699 CLIExecWithReturn ( cmd
, argtable
, true );
2701 CLIGetHexWithReturn ( 1 , keyn
, & keynlen
);
2702 CLIGetHexWithReturn ( 2 , key
, & keylen
);
2706 PrintAndLog ( "ERROR: <Key Num> must be 2 bytes long instead of: %d" , keynlen
);
2711 PrintAndLog ( "ERROR: <Key Value> must be 16 bytes long instead of: %d" , keylen
);
2715 return MifareAuth4 ( NULL
, keyn
, key
, true , false , true );
2718 // https://www.nxp.com/docs/en/application-note/AN10787.pdf
2719 int CmdHF14AMfMAD ( const char * cmd
) {
2721 CLIParserInit ( "hf mf mad" ,
2722 "Checks and prints Mifare Application Directory (MAD)" ,
2723 "Usage: \n\t hf mf mad -> shows MAD if exists \n "
2724 " \t hf mf mad -a 03e1 -k ffffffffffff -b -> shows NDEF data if exists. read card with custom key and key B \n " );
2726 void * argtable
[] = {
2728 arg_lit0 ( "vV" , "verbose" , "show technical data" ),
2729 arg_str0 ( "aA" , "aid" , "print all sectors with aid" , NULL
),
2730 arg_str0 ( "kK" , "key" , "key for printing sectors" , NULL
),
2731 arg_lit0 ( "bB" , "keyb" , "use key B for access printing sectors (by default: key A)" ),
2734 CLIExecWithReturn ( cmd
, argtable
, true );
2735 bool verbose
= arg_get_lit ( 1 );
2736 uint8_t aid
[ 2 ] = { 0 };
2738 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2739 uint8_t key
[ 6 ] = { 0 };
2741 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2742 bool keyB
= arg_get_lit ( 4 );
2746 if ( aidlen
!= 2 && keylen
> 0 ) {
2747 PrintAndLogEx ( WARNING
, "do not need a key without aid." );
2750 uint8_t sector0
[ 16 * 4 ] = { 0 };
2751 uint8_t sector10
[ 16 * 4 ] = { 0 };
2752 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2753 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2758 for ( int i
= 0 ; i
< 4 ; i
++)
2759 PrintAndLogEx ( NORMAL
, "[%d] %s" , i
, sprint_hex (& sector0
[ i
* 16 ], 16 ));
2762 bool haveMAD2
= false ;
2763 MAD1DecodeAndPrint ( sector0
, verbose
, & haveMAD2
);
2766 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2767 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2771 MAD2DecodeAndPrint ( sector10
, verbose
);
2775 uint16_t aaid
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2776 PrintAndLogEx ( NORMAL
, " \n -------------- AID 0x%04x ---------------" , aaid
);
2778 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2780 if ( MADDecode ( sector0
, sector10
, mad
, & madlen
)) {
2781 PrintAndLogEx ( ERR
, "can't decode mad." );
2785 uint8_t akey
[ 6 ] = { 0 };
2786 memcpy ( akey
, g_mifare_ndef_key
, 6 );
2788 memcpy ( akey
, key
, 6 );
2791 for ( int i
= 0 ; i
< madlen
; i
++) {
2792 if ( aaid
== mad
[ i
]) {
2793 uint8_t vsector
[ 16 * 4 ] = { 0 };
2794 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, akey
, vsector
)) {
2795 PrintAndLogEx ( NORMAL
, "" );
2796 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2800 for ( int j
= 0 ; j
< ( verbose
? 4 : 3 ); j
++)
2801 PrintAndLogEx ( NORMAL
, " [%03d] %s" , ( i
+ 1 ) * 4 + j
, sprint_hex (& vsector
[ j
* 16 ], 16 ));
2809 int CmdHFMFNDEF ( const char * cmd
) {
2811 CLIParserInit ( "hf mf ndef" ,
2812 "Prints NFC Data Exchange Format (NDEF)" ,
2813 "Usage: \n\t hf mf ndef -> shows NDEF data \n "
2814 " \t hf mf ndef -a 03e1 -k ffffffffffff -b -> shows NDEF data with custom AID, key and with key B \n " );
2816 void * argtable
[] = {
2818 arg_litn ( "vV" , "verbose" , 0 , 2 , "show technical data" ),
2819 arg_str0 ( "aA" , "aid" , "replace default aid for NDEF" , NULL
),
2820 arg_str0 ( "kK" , "key" , "replace default key for NDEF" , NULL
),
2821 arg_lit0 ( "bB" , "keyb" , "use key B for access sectors (by default: key A)" ),
2824 CLIExecWithReturn ( cmd
, argtable
, true );
2826 bool verbose
= arg_get_lit ( 1 );
2827 bool verbose2
= arg_get_lit ( 1 ) > 1 ;
2828 uint8_t aid
[ 2 ] = { 0 };
2830 CLIGetHexWithReturn ( 2 , aid
, & aidlen
);
2831 uint8_t key
[ 6 ] = { 0 };
2833 CLIGetHexWithReturn ( 3 , key
, & keylen
);
2834 bool keyB
= arg_get_lit ( 4 );
2838 uint16_t ndefAID
= 0x03e1 ;
2840 ndefAID
= ( aid
[ 0 ] << 8 ) + aid
[ 1 ];
2842 uint8_t ndefkey
[ 6 ] = { 0 };
2843 memcpy ( ndefkey
, g_mifare_ndef_key
, 6 );
2845 memcpy ( ndefkey
, key
, 6 );
2848 uint8_t sector0
[ 16 * 4 ] = { 0 };
2849 uint8_t sector10
[ 16 * 4 ] = { 0 };
2850 uint8_t data
[ 4096 ] = { 0 };
2853 PrintAndLogEx ( NORMAL
, "" );
2855 if ( mfReadSector ( MF_MAD1_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector0
)) {
2856 PrintAndLogEx ( ERR
, "read sector 0 error. card don't have MAD or don't have MAD on default keys." );
2860 bool haveMAD2
= false ;
2861 int res
= MADCheck ( sector0
, NULL
, verbose
, & haveMAD2
);
2863 PrintAndLogEx ( ERR
, "MAD error %d." , res
);
2868 if ( mfReadSector ( MF_MAD2_SECTOR
, MF_KEY_A
, ( uint8_t *) g_mifare_mad_key
, sector10
)) {
2869 PrintAndLogEx ( ERR
, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys." );
2874 uint16_t mad
[ 7 + 8 + 8 + 8 + 8 ] = { 0 };
2876 if ( MADDecode ( sector0
, ( haveMAD2
? sector10
: NULL
), mad
, & madlen
)) {
2877 PrintAndLogEx ( ERR
, "can't decode mad." );
2881 printf ( "data reading:" );
2882 for ( int i
= 0 ; i
< madlen
; i
++) {
2883 if ( ndefAID
== mad
[ i
]) {
2884 uint8_t vsector
[ 16 * 4 ] = { 0 };
2885 if ( mfReadSector ( i
+ 1 , keyB
? MF_KEY_B
: MF_KEY_A
, ndefkey
, vsector
)) {
2886 PrintAndLogEx ( ERR
, "read sector %d error." , i
+ 1 );
2890 memcpy (& data
[ datalen
], vsector
, 16 * 3 );
2899 PrintAndLogEx ( ERR
, "no NDEF data." );
2904 PrintAndLogEx ( NORMAL
, "NDEF data:" );
2905 dump_buffer ( data
, datalen
, stdout
, 1 );
2908 NDEFDecodeAndPrint ( data
, datalen
, verbose
);
2913 static command_t CommandTable
[] =
2915 { "help" , CmdHelp
, 1 , "This help" },
2916 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2917 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2918 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2919 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2920 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2921 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2922 { "auth4" , CmdHF14AMfAuth4
, 0 , "ISO14443-4 AES authentication" },
2923 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2924 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2925 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2926 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2927 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2928 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2929 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2930 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2931 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2932 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2933 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2934 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2935 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2936 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2937 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2938 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2939 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2940 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2941 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2942 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2943 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2944 { "mad" , CmdHF14AMfMAD
, 0 , "Checks and prints MAD" },
2945 { "ndef" , CmdHFMFNDEF
, 0 , "Prints NDEF records from card" },
2946 { NULL
, NULL
, 0 , NULL
}
2949 int CmdHFMF ( const char * Cmd
)
2951 ( void ) WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2952 CmdsParse ( CommandTable
, Cmd
);
2956 int CmdHelp ( const char * Cmd
)
2958 CmdsHelp ( CommandTable
);