]>
cvs.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 //-----------------------------------------------------------------------------
18 #include "proxmark3.h"
20 #include "cmdhfmfhard.h"
22 #include "util_posix.h"
25 #include "mifarehost.h"
29 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
31 static int CmdHelp ( const char * Cmd
);
33 int CmdHF14AMifare ( const char * Cmd
)
37 isOK
= mfDarkside (& key
);
39 case - 1 : PrintAndLog ( "Button pressed. Aborted." ); return 1 ;
40 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)." ); return 1 ;
41 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable)." ); return 1 ;
42 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
43 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour." ); return 1 ;
44 case - 5 : PrintAndLog ( "Aborted via keyboard." ); return 1 ;
45 default : PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , key
);
53 int CmdHF14AMfWrBl ( const char * Cmd
)
57 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
58 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
63 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
64 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
68 blockNo
= param_get8 ( Cmd
, 0 );
69 cmdp
= param_getchar ( Cmd
, 1 );
71 PrintAndLog ( "Key type must be A or B" );
74 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
75 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
76 PrintAndLog ( "Key must include 12 HEX symbols" );
79 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
80 PrintAndLog ( "Block data must include 32 HEX symbols" );
83 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
84 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
86 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
87 memcpy ( c
. d
. asBytes
, key
, 6 );
88 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
92 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
93 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
94 PrintAndLog ( "isOk:%02x" , isOK
);
96 PrintAndLog ( "Command execute timeout" );
102 int CmdHF14AMfRdBl ( const char * Cmd
)
106 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
112 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
113 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
117 blockNo
= param_get8 ( Cmd
, 0 );
118 cmdp
= param_getchar ( Cmd
, 1 );
120 PrintAndLog ( "Key type must be A or B" );
123 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
124 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
125 PrintAndLog ( "Key must include 12 HEX symbols" );
128 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
130 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
131 memcpy ( c
. d
. asBytes
, key
, 6 );
135 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
136 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
137 uint8_t * data
= resp
. d
. asBytes
;
140 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
142 PrintAndLog ( "isOk:%02x" , isOK
);
144 PrintAndLog ( "Command execute timeout" );
150 int CmdHF14AMfRdSc ( const char * Cmd
)
153 uint8_t sectorNo
= 0 ;
155 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
157 uint8_t * data
= NULL
;
161 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
162 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
166 sectorNo
= param_get8 ( Cmd
, 0 );
168 PrintAndLog ( "Sector number must be less than 40" );
171 cmdp
= param_getchar ( Cmd
, 1 );
172 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
173 PrintAndLog ( "Key type must be A or B" );
176 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
177 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
178 PrintAndLog ( "Key must include 12 HEX symbols" );
181 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
183 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
184 memcpy ( c
. d
. asBytes
, key
, 6 );
189 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
190 isOK
= resp
. arg
[ 0 ] & 0xff ;
191 data
= resp
. d
. asBytes
;
193 PrintAndLog ( "isOk:%02x" , isOK
);
195 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
196 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
198 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
201 PrintAndLog ( "Command execute timeout" );
207 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
212 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
216 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
225 static int ParamCardSizeSectors ( const char c
) {
228 case '0' : numBlocks
= 5 ; break ;
229 case '2' : numBlocks
= 32 ; break ;
230 case '4' : numBlocks
= 40 ; break ;
231 default : numBlocks
= 16 ;
236 static int ParamCardSizeBlocks ( const char c
) {
237 int numBlocks
= 16 * 4 ;
239 case '0' : numBlocks
= 5 * 4 ; break ;
240 case '2' : numBlocks
= 32 * 4 ; break ;
241 case '4' : numBlocks
= 32 * 4 + 8 * 16 ; break ;
242 default : numBlocks
= 16 * 4 ;
247 int CmdHF14AMfDump ( const char * Cmd
)
249 uint8_t sectorNo
, blockNo
;
253 uint8_t rights
[ 40 ][ 4 ];
254 uint8_t carddata
[ 256 ][ 16 ];
255 uint8_t numSectors
= 16 ;
262 char cmdp
= param_getchar ( Cmd
, 0 );
263 numSectors
= ParamCardSizeSectors ( cmdp
);
265 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
266 PrintAndLog ( "Usage: hf mf dump [card memory]" );
267 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
269 PrintAndLog ( "Samples: hf mf dump" );
270 PrintAndLog ( " hf mf dump 4" );
274 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
275 PrintAndLog ( "Could not find file dumpkeys.bin" );
279 // Read keys A from file
280 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
281 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fin
);
282 if ( bytes_read
!= 6 ) {
283 PrintAndLog ( "File reading error." );
289 // Read keys B from file
290 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
291 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fin
);
292 if ( bytes_read
!= 6 ) {
293 PrintAndLog ( "File reading error." );
301 PrintAndLog ( "|-----------------------------------------|" );
302 PrintAndLog ( "|------ Reading sector access bits...-----|" );
303 PrintAndLog ( "|-----------------------------------------|" );
305 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
306 for ( tries
= 0 ; tries
< 3 ; tries
++) {
307 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
308 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
311 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
312 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
313 uint8_t * data
= resp
. d
. asBytes
;
315 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
316 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
317 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
318 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
320 } else if ( tries
== 2 ) { // on last try set defaults
321 PrintAndLog ( "Could not get access rights for sector %2d. Trying with defaults..." , sectorNo
);
322 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
323 rights
[ sectorNo
][ 3 ] = 0x01 ;
326 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
327 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
328 rights
[ sectorNo
][ 3 ] = 0x01 ;
333 PrintAndLog ( "|-----------------------------------------|" );
334 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
335 PrintAndLog ( "|-----------------------------------------|" );
338 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
339 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
340 bool received
= false ;
341 for ( tries
= 0 ; tries
< 3 ; tries
++) {
342 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
343 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
344 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
346 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
347 } else { // data block. Check if it can be read with key A or key B
348 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
349 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
350 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
351 memcpy ( c
. d
. asBytes
, keyB
[ sectorNo
], 6 );
353 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
354 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
356 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
358 } else { // key A would work
359 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
360 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
362 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
366 isOK
= resp
. arg
[ 0 ] & 0xff ;
372 isOK
= resp
. arg
[ 0 ] & 0xff ;
373 uint8_t * data
= resp
. d
. asBytes
;
374 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
375 data
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
376 data
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
377 data
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
378 data
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
379 data
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
380 data
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
381 data
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
382 data
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
383 data
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
384 data
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
385 data
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
386 data
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
389 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
390 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
392 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
398 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
405 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
406 PrintAndLog ( "Could not create file name dumpdata.bin" );
409 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
410 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
412 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
418 int CmdHF14AMfRestore ( const char * Cmd
)
420 uint8_t sectorNo
, blockNo
;
422 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
423 uint8_t bldata
[ 16 ] = { 0x00 };
431 char cmdp
= param_getchar ( Cmd
, 0 );
433 case '0' : numSectors
= 5 ; break ;
435 case '\0' : numSectors
= 16 ; break ;
436 case '2' : numSectors
= 32 ; break ;
437 case '4' : numSectors
= 40 ; break ;
438 default : numSectors
= 16 ;
441 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
442 PrintAndLog ( "Usage: hf mf restore [card memory]" );
443 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
445 PrintAndLog ( "Samples: hf mf restore" );
446 PrintAndLog ( " hf mf restore 4" );
450 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
451 PrintAndLog ( "Could not find file dumpkeys.bin" );
455 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
456 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
457 if ( bytes_read
!= 6 ) {
458 PrintAndLog ( "File reading error (dumpkeys.bin)." );
464 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
465 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
466 if ( bytes_read
!= 6 ) {
467 PrintAndLog ( "File reading error (dumpkeys.bin)." );
475 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
476 PrintAndLog ( "Could not find file dumpdata.bin" );
479 PrintAndLog ( "Restoring dumpdata.bin to card" );
481 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
482 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
483 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
484 memcpy ( c
. d
. asBytes
, key
, 6 );
486 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
487 if ( bytes_read
!= 16 ) {
488 PrintAndLog ( "File reading error (dumpdata.bin)." );
493 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
494 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
495 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
496 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
497 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
498 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
499 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
500 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
501 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
502 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
503 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
504 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
505 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
508 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
510 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
514 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
515 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
516 PrintAndLog ( "isOk:%02x" , isOK
);
518 PrintAndLog ( "Command execute timeout" );
527 //----------------------------------------------
529 //----------------------------------------------
531 static void parseParamTDS ( const char * Cmd
, const uint8_t indx
, bool * paramT
, bool * paramD
, uint8_t * timeout
) {
533 int len
= param_getlength ( Cmd
, indx
);
534 if ( len
> 0 && len
< 4 ){
535 param_getstr ( Cmd
, indx
, ctmp3
, sizeof ( ctmp3
));
537 * paramT
|= ( ctmp3
[ 0 ] == 't' || ctmp3
[ 0 ] == 'T' );
538 * paramD
|= ( ctmp3
[ 0 ] == 'd' || ctmp3
[ 0 ] == 'D' );
539 bool paramS1
= * paramT
|| * paramD
;
541 // slow and very slow
542 if ( ctmp3
[ 0 ] == 's' || ctmp3
[ 0 ] == 'S' || ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' ) {
543 * timeout
= 11 ; // slow
545 if (! paramS1
&& ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
546 * timeout
= 53 ; // very slow
548 if ( paramS1
&& ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
549 * timeout
= 53 ; // very slow
555 int CmdHF14AMfNested ( const char * Cmd
)
557 int i
, j
, res
, iterations
;
558 sector_t
* e_sector
= NULL
;
561 uint8_t trgBlockNo
= 0 ;
562 uint8_t trgKeyType
= 0 ;
563 uint8_t SectorsCnt
= 0 ;
564 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
565 uint8_t keyBlock
[ MifareDefaultKeysSize
* 6 ];
567 // timeout in units. (ms * 106)/10 or us*0.0106
568 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
570 bool autosearchKey
= false ;
572 bool transferToEml
= false ;
573 bool createDumpFile
= false ;
575 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
576 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
581 PrintAndLog ( "Usage:" );
582 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]" );
583 PrintAndLog ( " all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]" );
584 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
585 PrintAndLog ( " <target block number> <target key A/B> [t]" );
587 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
588 PrintAndLog ( "t - transfer keys to emulator memory" );
589 PrintAndLog ( "d - write keys to binary file dumpkeys.bin" );
590 PrintAndLog ( "s - Slow (1ms) check keys (required by some non standard cards)" );
591 PrintAndLog ( "ss - Very slow (5ms) check keys" );
593 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
594 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
595 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
596 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
597 PrintAndLog ( " sample5: hf mf nested 1 * t" );
598 PrintAndLog ( " sample6: hf mf nested 1 * ss" );
603 cmdp
= param_getchar ( Cmd
, 0 );
604 if ( cmdp
== 'o' || cmdp
== 'O' ) {
608 SectorsCnt
= ParamCardSizeSectors ( cmdp
);
611 // <block number>. number or autosearch key (*)
612 if ( param_getchar ( Cmd
, 1 ) == '*' ) {
613 autosearchKey
= true ;
615 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
617 PrintAndLog ( "--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us" ,
618 SectorsCnt
, transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
620 blockNo
= param_get8 ( Cmd
, 1 );
622 ctmp
= param_getchar ( Cmd
, 2 );
623 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
624 PrintAndLog ( "Key type must be A or B" );
628 if ( ctmp
!= 'A' && ctmp
!= 'a' )
631 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
632 PrintAndLog ( "Key must include 12 HEX symbols" );
636 // check if we can authenticate to sector
637 res
= mfCheckKeys ( blockNo
, keyType
, true , 1 , key
, & key64
);
639 PrintAndLog ( "Can't authenticate to block:%3d key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
645 trgBlockNo
= param_get8 ( Cmd
, 4 );
647 ctmp
= param_getchar ( Cmd
, 5 );
648 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
649 PrintAndLog ( "Target key type must be A or B" );
652 if ( ctmp
!= 'A' && ctmp
!= 'a' )
655 parseParamTDS ( Cmd
, 6 , & transferToEml
, & createDumpFile
, & btimeout14a
);
657 parseParamTDS ( Cmd
, 4 , & transferToEml
, & createDumpFile
, & btimeout14a
);
660 PrintAndLog ( "--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
661 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
665 if ( cmdp
== 'o' ) { // ------------------------------------ one sector working
666 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
667 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
670 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
671 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
672 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
673 default : PrintAndLog ( "Unknown Error. \n " );
677 key64
= bytes_to_num ( keyBlock
, 6 );
679 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
681 // transfer key to the emulator
683 uint8_t sectortrailer
;
684 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
685 sectortrailer
= ( trgBlockNo
& 0x03 ) + 3 ;
686 } else { // 16 block sector
687 sectortrailer
= ( trgBlockNo
& 0x0f ) + 15 ;
689 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
692 num_to_bytes ( key64
, 6 , keyBlock
);
694 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
695 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
696 PrintAndLog ( "Key transferred to emulator memory." );
699 PrintAndLog ( "No valid key found" );
702 else { // ------------------------------------ multiple sectors working
704 msclock1
= msclock ();
706 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
707 if ( e_sector
== NULL
) return 1 ;
709 //test current key and additional standard keys first
710 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< MifareDefaultKeysSize
; defaultKeyCounter
++){
711 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
714 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
715 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , MifareDefaultKeysSize
, keyBlock
, e_sector
);
717 // get known key from array
718 bool keyFound
= false ;
720 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
721 for ( j
= 0 ; j
< 2 ; j
++) {
722 if ( e_sector
[ i
]. foundKey
[ j
]) {
726 num_to_bytes ( e_sector
[ i
]. Key
[ j
], 6 , key
);
735 // Can't found a key....
737 PrintAndLog ( "Can't found any of the known keys." );
740 PrintAndLog ( "--auto key. block no:%3d, key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
745 PrintAndLog ( "nested..." );
746 bool calibrate
= true ;
747 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
748 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
749 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
750 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
751 PrintAndLog ( "-----------------------------------------------" );
752 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
755 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
756 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
757 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
758 default : PrintAndLog ( "Unknown Error. \n " );
768 key64
= bytes_to_num ( keyBlock
, 6 );
770 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
771 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
772 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
774 // try to check this key as a key to the other sectors
775 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , 1 , keyBlock
, e_sector
);
781 // print nested statistic
782 PrintAndLog ( " \n\n ----------------------------------------------- \n Nested statistic: \n Iterations count: %d" , iterations
);
783 PrintAndLog ( "Time in nested: %1.3f (%1.3f sec per key)" , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
786 PrintAndLog ( "|---|----------------|---|----------------|---|" );
787 PrintAndLog ( "|sec|key A |res|key B |res|" );
788 PrintAndLog ( "|---|----------------|---|----------------|---|" );
789 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
790 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
791 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
793 PrintAndLog ( "|---|----------------|---|----------------|---|" );
795 // transfer keys to the emulator memory
797 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
798 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
799 if ( e_sector
[ i
]. foundKey
[ 0 ])
800 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
801 if ( e_sector
[ i
]. foundKey
[ 1 ])
802 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
803 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
805 PrintAndLog ( "Keys transferred to emulator memory." );
809 if ( createDumpFile
) {
810 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
811 PrintAndLog ( "Could not create file dumpkeys.bin" );
815 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
816 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
817 if ( e_sector
[ i
]. foundKey
[ 0 ]){
818 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
819 fwrite ( tempkey
, 1 , 6 , fkeys
);
822 fwrite ( & standart
, 1 , 6 , fkeys
);
825 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
826 if ( e_sector
[ i
]. foundKey
[ 1 ]){
827 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
828 fwrite ( tempkey
, 1 , 6 , fkeys
);
831 fwrite ( & standart
, 1 , 6 , fkeys
);
843 int CmdHF14AMfNestedHard ( const char * Cmd
)
847 uint8_t trgBlockNo
= 0 ;
848 uint8_t trgKeyType
= 0 ;
849 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
850 uint8_t trgkey
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
853 ctmp
= param_getchar ( Cmd
, 0 );
855 if ( ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen ( Cmd
) < 20 ) {
856 PrintAndLog ( "Usage:" );
857 PrintAndLog ( " hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>" );
858 PrintAndLog ( " <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]" );
859 PrintAndLog ( " or hf mf hardnested r [known target key]" );
861 PrintAndLog ( "Options: " );
862 PrintAndLog ( " w: Acquire nonces and write them to binary file nonces.bin" );
863 PrintAndLog ( " s: Slower acquisition (required by some non standard cards)" );
864 PrintAndLog ( " r: Read nonces.bin and start attack" );
866 PrintAndLog ( " sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A" );
867 PrintAndLog ( " sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w" );
868 PrintAndLog ( " sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s" );
869 PrintAndLog ( " sample4: hf mf hardnested r" );
871 PrintAndLog ( "Add the known target key to check if it is present in the remaining key space:" );
872 PrintAndLog ( " sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF" );
876 bool know_target_key
= false ;
877 bool nonce_file_read
= false ;
878 bool nonce_file_write
= false ;
883 if ( ctmp
== 'R' || ctmp
== 'r' ) {
884 nonce_file_read
= true ;
885 if (! param_gethex ( Cmd
, 1 , trgkey
, 12 )) {
886 know_target_key
= true ;
888 } else if ( ctmp
== 'T' || ctmp
== 't' ) {
889 tests
= param_get32ex ( Cmd
, 1 , 100 , 10 );
890 if (! param_gethex ( Cmd
, 2 , trgkey
, 12 )) {
891 know_target_key
= true ;
894 blockNo
= param_get8 ( Cmd
, 0 );
895 ctmp
= param_getchar ( Cmd
, 1 );
896 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
897 PrintAndLog ( "Key type must be A or B" );
900 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
904 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
905 PrintAndLog ( "Key must include 12 HEX symbols" );
909 trgBlockNo
= param_get8 ( Cmd
, 3 );
910 ctmp
= param_getchar ( Cmd
, 4 );
911 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
912 PrintAndLog ( "Target key type must be A or B" );
915 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
921 if (! param_gethex ( Cmd
, 5 , trgkey
, 12 )) {
922 know_target_key
= true ;
926 while (( ctmp
= param_getchar ( Cmd
, i
))) {
927 if ( ctmp
== 's' || ctmp
== 'S' ) {
929 } else if ( ctmp
== 'w' || ctmp
== 'W' ) {
930 nonce_file_write
= true ;
932 PrintAndLog ( "Possible options are w and/or s" );
939 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 " ,
942 trgkey
[ 0 ], trgkey
[ 1 ], trgkey
[ 2 ], trgkey
[ 3 ], trgkey
[ 4 ], trgkey
[ 5 ],
943 know_target_key
? "" : " (not set)" ,
944 nonce_file_write
? "write" : nonce_file_read
? "read" : "none" ,
948 int16_t isOK
= mfnestedhard ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
952 case 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
953 case 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
963 int CmdHF14AMfChk ( const char * Cmd
)
966 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
967 PrintAndLog ( " * - all sectors" );
968 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
969 PrintAndLog ( "d - write keys to binary file \n " );
970 PrintAndLog ( "t - write keys to emulator memory" );
971 PrintAndLog ( "s - slow execute. timeout 1ms" );
972 PrintAndLog ( "ss- very slow execute. timeout 5ms" );
973 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
974 PrintAndLog ( " hf mf chk *1 ? t" );
975 PrintAndLog ( " hf mf chk *1 ? d" );
976 PrintAndLog ( " hf mf chk *1 ? s" );
977 PrintAndLog ( " hf mf chk *1 ? dss" );
982 char filename
[ FILE_PATH_SIZE
]={ 0 };
984 uint8_t * keyBlock
= NULL
, * p
;
985 uint16_t stKeyBlock
= 20 ;
990 char ctmp3
[ 3 ] = { 0x00 };
992 uint8_t SectorsCnt
= 0 ;
995 uint32_t timeout14a
= 0 ; // timeout in us
996 bool param3InUse
= false ;
998 int transferToEml
= 0 ;
999 int createDumpFile
= 0 ;
1001 sector_t
* e_sector
= NULL
;
1003 keyBlock
= calloc ( stKeyBlock
, 6 );
1004 if ( keyBlock
== NULL
) return 1 ;
1006 int defaultKeysSize
= MifareDefaultKeysSize
;
1007 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++){
1008 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
1011 if ( param_getchar ( Cmd
, 0 )== '*' ) {
1012 SectorsCnt
= ParamCardSizeSectors ( param_getchar ( Cmd
+ 1 , 0 ));
1015 blockNo
= param_get8 ( Cmd
, 0 );
1017 ctmp
= param_getchar ( Cmd
, 1 );
1029 PrintAndLog ( "Key type must be A , B or ?" );
1034 // transfer to emulator & create dump file
1035 ctmp
= param_getchar ( Cmd
, 2 );
1036 if ( ctmp
== 't' || ctmp
== 'T' ) transferToEml
= 1 ;
1037 if ( ctmp
== 'd' || ctmp
== 'D' ) createDumpFile
= 1 ;
1039 param3InUse
= transferToEml
| createDumpFile
;
1041 timeout14a
= 500 ; // fast by default
1042 // double parameters - ts, ds
1043 int clen
= param_getlength ( Cmd
, 2 );
1044 if ( clen
== 2 || clen
== 3 ){
1045 param_getstr ( Cmd
, 2 , ctmp3
, sizeof ( ctmp3
));
1049 if ( ctmp
== 's' || ctmp
== 'S' ) {
1050 timeout14a
= 1000 ; // slow
1051 if (! param3InUse
&& clen
== 2 && ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
1052 timeout14a
= 5000 ; // very slow
1054 if ( param3InUse
&& clen
== 3 && ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
1055 timeout14a
= 5000 ; // very slow
1060 for ( i
= param3InUse
; param_getchar ( Cmd
, 2 + i
); i
++) {
1061 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
1062 if ( stKeyBlock
- keycnt
< 2 ) {
1063 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1065 PrintAndLog ( "Cannot allocate memory for Keys" );
1071 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1072 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1073 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1076 // May be a dic file
1077 if ( param_getstr ( Cmd
, 2 + i
, filename
, sizeof ( filename
)) >= FILE_PATH_SIZE
) {
1078 PrintAndLog ( "File name too long" );
1083 if ( ( f
= fopen ( filename
, "r" )) ) {
1084 while ( fgets ( buf
, sizeof ( buf
), f
) ){
1085 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
1088 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
1090 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
1092 if (! isxdigit ( buf
[ 0 ])){
1093 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
1099 if ( stKeyBlock
- keycnt
< 2 ) {
1100 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1102 PrintAndLog ( "Cannot allocate memory for defKeys" );
1109 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
1110 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
1111 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
1113 memset ( buf
, 0 , sizeof ( buf
));
1117 PrintAndLog ( "File: %s: not found or locked." , filename
);
1125 // fill with default keys
1127 PrintAndLog ( "No key specified, trying default keys" );
1128 for (; keycnt
< defaultKeysSize
; keycnt
++)
1129 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1130 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1131 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1134 // initialize storage for found keys
1135 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
1136 if ( e_sector
== NULL
) return 1 ;
1137 for ( uint8_t keyAB
= 0 ; keyAB
< 2 ; keyAB
++) {
1138 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1139 e_sector
[ sectorNo
]. Key
[ keyAB
] = 0xffffffffffff ;
1140 e_sector
[ sectorNo
]. foundKey
[ keyAB
] = 0 ;
1145 bool foundAKey
= false ;
1146 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1148 PrintAndLog ( "To cancel this operation press the button on the proxmark..." );
1150 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1152 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1153 res
= mfCheckKeysSec ( SectorsCnt
, keyType
, timeout14a
* 1.06 / 100 , true , size
, & keyBlock
[ 6 * c
], e_sector
); // timeout is (ms * 106)/10 or us*0.0106
1164 PrintAndLog ( "Command execute timeout" );
1168 int keyAB
= keyType
;
1170 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1172 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1173 res
= mfCheckKeys ( blockNo
, keyAB
& 0x01 , true , size
, & keyBlock
[ 6 * c
], & key64
);
1177 PrintAndLog ( "Found valid key:[%d:%c]%012" PRIx64
, blockNo
, ( keyAB
& 0x01 )? 'B' : 'A' , key64
);
1181 PrintAndLog ( "Command execute timeout" );
1184 } while (-- keyAB
> 0 );
1191 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1192 PrintAndLog ( "|sec|key A |res|key B |res|" );
1193 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1194 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
1195 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
1196 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
1198 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1202 PrintAndLog ( "No valid keys found." );
1205 if ( transferToEml
) {
1207 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1208 if ( e_sector
[ sectorNo
]. foundKey
[ 0 ] || e_sector
[ sectorNo
]. foundKey
[ 1 ]) {
1209 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1210 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1211 if ( e_sector
[ sectorNo
]. foundKey
[ t
]) {
1212 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , block
+ t
* 10 );
1215 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1218 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1221 if ( createDumpFile
) {
1222 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1223 if ( fkeys
== NULL
) {
1224 PrintAndLog ( "Could not create file dumpkeys.bin" );
1230 for ( uint8_t t
= 0 ; t
< 2 ; t
++) {
1231 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1232 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , mkey
);
1233 fwrite ( mkey
, 1 , 6 , fkeys
);
1237 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1246 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1247 #define ATTACK_KEY_COUNT 7 // keep same as define in iso14443a.c -> Mifare1ksim()
1248 // cannot be more than 7 or it will overrun c.d.asBytes(512)
1254 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1255 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1257 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1258 memset ( stSector
, 0x00 , sizeof ( stSector
));
1259 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1260 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1262 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1263 if ( ar_resp
[ i
]. ar2
> 0 ) {
1264 //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);
1265 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1266 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 ));
1268 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1269 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1270 if ( ar_resp
[ i
]. keytype
== 0 ) {
1272 sector_trailer
[ ii
]. keyA
= key
;
1273 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1278 sector_trailer
[ ii
]. keyB
= key
;
1279 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1285 } else if ( mfkey32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1286 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1287 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1289 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1290 , keyType
? "B" : "A"
1295 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1296 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1299 sector_trailer
[ ii
]. keyA
= key
;
1300 stSector
[ ii
] = sectorNum
;
1305 sector_trailer
[ ii
]. keyB
= key
;
1306 stSector
[ ii
] = sectorNum
;
1316 //set emulator memory for keys
1317 if ( setEmulatorMem
) {
1318 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1320 uint8_t memBlock
[ 16 ];
1321 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1323 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1324 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 ));
1325 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1326 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1327 PrintAndLog ( "block data must include 32 HEX symbols" );
1331 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1332 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1333 clearCommandBuffer ();
1339 //un-comment to use as well moebius attack
1340 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1341 if (ar_resp[i].ar2 > 0) {
1342 if (tryMfk32_moebius(ar_resp[i], &key)) {
1343 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));
1349 int usage_hf14_mf1ksim ( void ) {
1350 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1351 PrintAndLog ( "options:" );
1352 PrintAndLog ( " h this help" );
1353 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1354 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1355 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1356 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1357 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1358 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1359 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1360 PrintAndLog ( "samples:" );
1361 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1362 PrintAndLog ( " hf mf sim u 11223344556677" );
1363 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1364 PrintAndLog ( " hf mf sim f uids.txt" );
1365 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1370 int CmdHF14AMf1kSim ( const char * Cmd
) {
1372 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1373 uint8_t exitAfterNReads
= 0 ;
1377 bool setEmulatorMem
= false ;
1378 bool attackFromFile
= false ;
1380 char filename
[ FILE_PATH_SIZE
];
1381 memset ( filename
, 0x00 , sizeof ( filename
));
1386 bool errors
= false ;
1388 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1389 switch ( param_getchar ( Cmd
, cmdp
)) {
1392 setEmulatorMem
= true ;
1394 flags
|= FLAG_INTERACTIVE
;
1395 flags
|= FLAG_NR_AR_ATTACK
;
1400 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
, sizeof ( filename
));
1402 PrintAndLog ( "error no filename found" );
1405 attackFromFile
= true ;
1407 flags
|= FLAG_INTERACTIVE
;
1408 flags
|= FLAG_NR_AR_ATTACK
;
1413 return usage_hf14_mf1ksim ();
1416 flags
|= FLAG_INTERACTIVE
;
1421 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1426 flags
|= FLAG_RANDOM_NONCE
;
1431 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1433 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1434 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1435 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1436 default : return usage_hf14_mf1ksim ();
1442 flags
|= FLAG_NR_AR_ATTACK
;
1446 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1453 if ( errors
) return usage_hf14_mf1ksim ();
1456 if ( attackFromFile
) {
1459 f
= fopen ( filename
, "r" );
1461 PrintAndLog ( "File %s not found or locked" , filename
);
1464 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1465 while (! feof ( f
) && ! ukbhit ()){
1466 memset ( buf
, 0 , sizeof ( buf
));
1467 memset ( uid
, 0 , sizeof ( uid
));
1469 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1470 if ( count
> 0 ) break ;
1472 PrintAndLog ( "File reading error." );
1476 if (! strlen ( buf
) && feof ( f
)) break ;
1478 uidlen
= strlen ( buf
)- 1 ;
1480 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1481 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1482 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1484 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1489 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1490 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1493 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1494 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1495 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1496 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1497 , exitAfterNReads
, flags
, flags
);
1499 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1500 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1501 clearCommandBuffer ();
1504 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1505 //We're waiting only 1.5 s at a time, otherwise we get the
1506 // annoying message about "Waiting for a response... "
1509 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1510 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1511 // We can skip the standard attack if we have RANDOM_NONCE set.
1512 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1513 if (( bool ) resp
. arg
[ 1 ]) {
1514 PrintAndLog ( "Device button pressed - quitting" );
1521 } else { //not from file
1523 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1524 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1525 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1526 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1527 , exitAfterNReads
, flags
, flags
);
1529 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1530 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1531 clearCommandBuffer ();
1534 if ( flags
& FLAG_INTERACTIVE
) {
1535 PrintAndLog ( "Press pm3-button to abort simulation" );
1536 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1537 //We're waiting only 1.5 s at a time, otherwise we get the
1538 // annoying message about "Waiting for a response... "
1541 if ( flags
& FLAG_NR_AR_ATTACK
) {
1542 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1543 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1544 // We can skip the standard attack if we have RANDOM_NONCE set.
1545 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1553 int CmdHF14AMfDbg ( const char * Cmd
)
1555 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1557 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1560 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1561 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1562 PrintAndLog ( " 0 - no debug messages" );
1563 PrintAndLog ( " 1 - error messages" );
1564 PrintAndLog ( " 2 - plus information messages" );
1565 PrintAndLog ( " 3 - plus debug messages" );
1566 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1567 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1571 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1577 int CmdHF14AMfEGet ( const char * Cmd
)
1579 uint8_t blockNo
= 0 ;
1580 uint8_t data
[ 16 ] = { 0x00 };
1582 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1583 PrintAndLog ( "Usage: hf mf eget <block number>" );
1584 PrintAndLog ( " sample: hf mf eget 0 " );
1588 blockNo
= param_get8 ( Cmd
, 0 );
1591 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1592 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1594 PrintAndLog ( "Command execute timeout" );
1600 int CmdHF14AMfEClear ( const char * Cmd
)
1602 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1603 PrintAndLog ( "Usage: hf mf eclr" );
1604 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1608 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1614 int CmdHF14AMfESet ( const char * Cmd
)
1616 uint8_t memBlock
[ 16 ];
1617 uint8_t blockNo
= 0 ;
1619 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1621 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1622 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1623 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1627 blockNo
= param_get8 ( Cmd
, 0 );
1629 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1630 PrintAndLog ( "block data must include 32 HEX symbols" );
1635 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, { blockNo
, 1 , 0 }};
1636 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1642 int CmdHF14AMfELoad ( const char * Cmd
)
1645 char filename
[ FILE_PATH_SIZE
];
1646 char * fnameptr
= filename
;
1647 char buf
[ 64 ] = { 0x00 };
1648 uint8_t buf8
[ 64 ] = { 0x00 };
1649 int i
, len
, blockNum
, numBlocks
;
1650 int nameParamNo
= 1 ;
1652 char ctmp
= param_getchar ( Cmd
, 0 );
1654 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1655 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1656 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1657 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1659 PrintAndLog ( " sample: hf mf eload filename" );
1660 PrintAndLog ( " hf mf eload 4 filename" );
1665 case '0' : numBlocks
= 5 * 4 ; break ;
1667 case '\0' : numBlocks
= 16 * 4 ; break ;
1668 case '2' : numBlocks
= 32 * 4 ; break ;
1669 case '4' : numBlocks
= 256 ; break ;
1676 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1678 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1682 sprintf ( fnameptr
, ".eml" );
1685 f
= fopen ( filename
, "r" );
1687 PrintAndLog ( "File %s not found or locked" , filename
);
1693 memset ( buf
, 0 , sizeof ( buf
));
1695 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1697 if ( blockNum
>= numBlocks
) break ;
1699 PrintAndLog ( "File reading error." );
1704 if ( strlen ( buf
) < 32 ){
1705 if ( strlen ( buf
) && feof ( f
))
1707 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1712 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1713 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1716 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1717 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1724 if ( blockNum
>= numBlocks
) break ;
1729 if (( blockNum
!= numBlocks
)) {
1730 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1733 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1738 int CmdHF14AMfESave ( const char * Cmd
)
1741 char filename
[ FILE_PATH_SIZE
];
1742 char * fnameptr
= filename
;
1744 int i
, j
, len
, numBlocks
;
1745 int nameParamNo
= 1 ;
1747 memset ( filename
, 0 , sizeof ( filename
));
1748 memset ( buf
, 0 , sizeof ( buf
));
1750 char ctmp
= param_getchar ( Cmd
, 0 );
1752 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1753 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1754 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1755 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1757 PrintAndLog ( " sample: hf mf esave " );
1758 PrintAndLog ( " hf mf esave 4" );
1759 PrintAndLog ( " hf mf esave 4 filename" );
1764 case '0' : numBlocks
= 5 * 4 ; break ;
1766 case '\0' : numBlocks
= 16 * 4 ; break ;
1767 case '2' : numBlocks
= 32 * 4 ; break ;
1768 case '4' : numBlocks
= 256 ; break ;
1775 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1777 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1779 // user supplied filename?
1781 // get filename (UID from memory)
1782 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1783 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1784 len
= sprintf ( fnameptr
, "dump" );
1788 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1789 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1795 // add file extension
1796 sprintf ( fnameptr
, ".eml" );
1799 f
= fopen ( filename
, "w+" );
1802 PrintAndLog ( "Can't open file %s " , filename
);
1807 for ( i
= 0 ; i
< numBlocks
; i
++) {
1808 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1809 PrintAndLog ( "Cant get block: %d" , i
);
1812 for ( j
= 0 ; j
< 16 ; j
++)
1813 fprintf ( f
, "%02X" , buf
[ j
]);
1818 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1824 int CmdHF14AMfECFill ( const char * Cmd
)
1826 uint8_t keyType
= 0 ;
1827 uint8_t numSectors
= 16 ;
1829 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1830 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1831 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1833 PrintAndLog ( "samples: hf mf ecfill A" );
1834 PrintAndLog ( " hf mf ecfill A 4" );
1835 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1836 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1840 char ctmp
= param_getchar ( Cmd
, 0 );
1841 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1842 PrintAndLog ( "Key type must be A or B" );
1845 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1847 ctmp
= param_getchar ( Cmd
, 1 );
1849 case '0' : numSectors
= 5 ; break ;
1851 case '\0' : numSectors
= 16 ; break ;
1852 case '2' : numSectors
= 32 ; break ;
1853 case '4' : numSectors
= 40 ; break ;
1854 default : numSectors
= 16 ;
1857 printf ( "--params: numSectors: %d, keyType:%d" , numSectors
, keyType
);
1858 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1863 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1868 uint64_t keyA
, keyB
;
1870 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1871 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1872 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1873 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1875 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1879 char cmdp
= param_getchar ( Cmd
, 0 );
1882 case '0' : numSectors
= 5 ; break ;
1884 case '\0' : numSectors
= 16 ; break ;
1885 case '2' : numSectors
= 32 ; break ;
1886 case '4' : numSectors
= 40 ; break ;
1887 default : numSectors
= 16 ;
1890 PrintAndLog ( "|---|----------------|----------------|" );
1891 PrintAndLog ( "|sec|key A |key B |" );
1892 PrintAndLog ( "|---|----------------|----------------|" );
1893 for ( i
= 0 ; i
< numSectors
; i
++) {
1894 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1895 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1898 keyA
= bytes_to_num ( data
, 6 );
1899 keyB
= bytes_to_num ( data
+ 10 , 6 );
1900 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1902 PrintAndLog ( "|---|----------------|----------------|" );
1907 int CmdHF14AMfCSetUID ( const char * Cmd
)
1909 uint8_t uid
[ 8 ] = { 0x00 };
1910 uint8_t oldUid
[ 8 ] = { 0x00 };
1911 uint8_t atqa
[ 2 ] = { 0x00 };
1912 uint8_t sak
[ 1 ] = { 0x00 };
1913 uint8_t atqaPresent
= 0 ;
1916 uint8_t needHelp
= 0 ;
1919 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
1920 PrintAndLog ( "UID must include 8 HEX symbols" );
1924 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
1928 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
1929 PrintAndLog ( "ATQA must include 4 HEX symbols" );
1933 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
1934 PrintAndLog ( "SAK must include 2 HEX symbols" );
1939 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
1941 switch ( param_getchar ( Cmd
, cmdp
))
1948 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1955 if ( strlen ( Cmd
) < 1 || needHelp
) {
1957 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
1958 PrintAndLog ( "sample: hf mf csetuid 01020304" );
1959 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
1960 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
1964 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
1966 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
1969 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
1971 PrintAndLog ( "Can't set UID. Error=%d" , res
);
1975 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
1976 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
1980 int CmdHF14AMfCWipe ( const char * Cmd
)
1983 int numBlocks
= 16 * 4 ;
1984 bool wipeCard
= false ;
1985 bool fillCard
= false ;
1987 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1988 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [p]" );
1989 PrintAndLog ( "sample: hf mf cwipe 1 w s" );
1990 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1991 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
1992 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
1996 gen
= mfCIdentify ();
1997 if (( gen
!= 1 ) && ( gen
!= 2 ))
2000 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2003 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2004 switch ( param_getchar ( Cmd
, cmdp
)) {
2019 if (! wipeCard
&& ! fillCard
)
2022 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2025 /* generation 1b magic card */
2027 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2029 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2031 /* generation 1a magic card by default */
2032 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2036 PrintAndLog ( "Can't wipe. error=%d" , res
);
2043 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2045 uint8_t memBlock
[ 16 ] = { 0x00 };
2046 uint8_t blockNo
= 0 ;
2047 bool wipeCard
= false ;
2050 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2051 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2052 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2053 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2054 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2058 gen
= mfCIdentify ();
2059 if (( gen
!= 1 ) && ( gen
!= 2 ))
2062 blockNo
= param_get8 ( Cmd
, 0 );
2064 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2065 PrintAndLog ( "block data must include 32 HEX symbols" );
2069 char ctmp
= param_getchar ( Cmd
, 2 );
2070 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2071 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2074 /* generation 1b magic card */
2075 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2077 /* generation 1a magic card by default */
2078 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2082 PrintAndLog ( "Can't write block. error=%d" , res
);
2089 int CmdHF14AMfCLoad ( const char * Cmd
)
2092 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2093 char * fnameptr
= filename
;
2094 char buf
[ 256 ] = { 0x00 };
2095 uint8_t buf8
[ 256 ] = { 0x00 };
2096 uint8_t fillFromEmulator
= 0 ;
2097 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2099 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2100 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2101 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2102 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2103 PrintAndLog ( " or: hf mf cload e [4]" );
2104 PrintAndLog ( "Sample: hf mf cload filename" );
2105 PrintAndLog ( " hf mf cload filname 4" );
2106 PrintAndLog ( " hf mf cload e" );
2107 PrintAndLog ( " hf mf cload e 4" );
2111 char ctmp
= param_getchar ( Cmd
, 0 );
2112 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2113 ctmp
= param_getchar ( Cmd
, 1 );
2114 if ( ctmp
== '4' ) numblock
= 256 ;
2116 gen
= mfCIdentify ();
2117 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2119 if ( fillFromEmulator
) {
2120 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2121 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2122 PrintAndLog ( "Cant get block: %d" , blockNum
);
2125 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2126 if ( blockNum
== 1 ) flags
= 0 ; // just write
2127 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2130 /* generation 1b magic card */
2131 flags
|= CSETBLOCK_MAGIC_1B
;
2132 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2133 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2139 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2141 len
= strlen ( filename
);
2142 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2144 //memcpy(filename, Cmd, len);
2147 sprintf ( fnameptr
, ".eml" );
2150 f
= fopen ( filename
, "r" );
2152 PrintAndLog ( "File not found or locked." );
2159 memset ( buf
, 0 , sizeof ( buf
));
2161 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2163 PrintAndLog ( "File reading error." );
2167 if ( strlen ( buf
) < 32 ) {
2168 if ( strlen ( buf
) && feof ( f
))
2170 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2174 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2175 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2177 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2178 if ( blockNum
== 1 ) flags
= 0 ; // just write
2179 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2182 /* generation 1b magic card */
2183 flags
|= CSETBLOCK_MAGIC_1B
;
2184 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2185 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2191 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2195 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2196 if ( blockNum
!= numblock
){
2197 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2200 PrintAndLog ( "Loaded from file: %s" , filename
);
2206 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2207 uint8_t memBlock
[ 16 ];
2208 uint8_t blockNo
= 0 ;
2210 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2212 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2213 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2214 PrintAndLog ( "sample: hf mf cgetblk 1" );
2215 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2219 gen
= mfCIdentify ();
2221 blockNo
= param_get8 ( Cmd
, 0 );
2223 PrintAndLog ( "--block number:%2d " , blockNo
);
2226 /* generation 1b magic card */
2227 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2229 /* generation 1a magic card by default */
2230 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2233 PrintAndLog ( "Can't read block. error=%d" , res
);
2237 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2241 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2242 uint8_t memBlock
[ 16 ] = { 0x00 };
2243 uint8_t sectorNo
= 0 ;
2244 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2246 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2247 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2248 PrintAndLog ( "sample: hf mf cgetsc 0" );
2249 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2253 sectorNo
= param_get8 ( Cmd
, 0 );
2255 if ( sectorNo
> 39 ) {
2256 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2260 PrintAndLog ( "--sector number:%d " , sectorNo
);
2262 gen
= mfCIdentify ();
2264 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2265 if ( sectorNo
< 32 ) {
2266 baseblock
= sectorNo
* 4 ;
2268 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2271 if ( sectorNo
> 31 ) sect_size
= 16 ;
2273 for ( i
= 0 ; i
< sect_size
; i
++) {
2274 if ( i
== 1 ) flags
= 0 ;
2275 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2278 /* generation 1b magic card */
2279 flags
|= CSETBLOCK_MAGIC_1B
;
2281 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2283 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2287 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2293 int CmdHF14AMfCSave ( const char * Cmd
) {
2296 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2297 char * fnameptr
= filename
;
2298 uint8_t fillFromEmulator
= 0 ;
2299 uint8_t buf
[ 256 ] = { 0x00 };
2300 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2302 // memset(filename, 0, sizeof(filename));
2303 // memset(buf, 0, sizeof(buf));
2305 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2306 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2307 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2308 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2309 PrintAndLog ( "Sample: hf mf csave " );
2310 PrintAndLog ( " hf mf csave filename" );
2311 PrintAndLog ( " hf mf csave e" );
2312 PrintAndLog ( " hf mf csave 4" );
2313 PrintAndLog ( " hf mf csave filename 4" );
2314 PrintAndLog ( " hf mf csave e 4" );
2318 char ctmp
= param_getchar ( Cmd
, 0 );
2319 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2320 if ( ctmp
== '4' ) numblock
= 256 ;
2321 ctmp
= param_getchar ( Cmd
, 1 );
2322 if ( ctmp
== '4' ) numblock
= 256 ;
2324 gen
= mfCIdentify ();
2325 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2327 if ( fillFromEmulator
) {
2328 // put into emulator
2329 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2330 for ( i
= 0 ; i
< numblock
; i
++) {
2331 if ( i
== 1 ) flags
= 0 ;
2332 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2335 /* generation 1b magic card */
2336 flags
|= CSETBLOCK_MAGIC_1B
;
2338 if ( mfCGetBlock ( i
, buf
, flags
)) {
2339 PrintAndLog ( "Cant get block: %d" , i
);
2343 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2344 PrintAndLog ( "Cant set emul block: %d" , i
);
2350 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2352 len
= strlen ( filename
);
2353 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2355 ctmp
= param_getchar ( Cmd
, 0 );
2356 if ( len
< 1 || ( ctmp
== '4' )) {
2359 flags
= CSETBLOCK_SINGLE_OPER
;
2361 /* generation 1b magic card */
2362 flags
|= CSETBLOCK_MAGIC_1B
;
2363 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2364 PrintAndLog ( "Cant get block: %d" , 0 );
2365 len
= sprintf ( fnameptr
, "dump" );
2369 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2370 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2373 //memcpy(filename, Cmd, len);
2377 sprintf ( fnameptr
, ".eml" );
2380 f
= fopen ( filename
, "w+" );
2383 PrintAndLog ( "File not found or locked." );
2388 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2389 for ( i
= 0 ; i
< numblock
; i
++) {
2390 if ( i
== 1 ) flags
= 0 ;
2391 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2394 /* generation 1b magic card */
2395 flags
|= CSETBLOCK_MAGIC_1B
;
2396 if ( mfCGetBlock ( i
, buf
, flags
)) {
2397 PrintAndLog ( "Cant get block: %d" , i
);
2400 for ( j
= 0 ; j
< 16 ; j
++)
2401 fprintf ( f
, "%02x" , buf
[ j
]);
2406 PrintAndLog ( "Saved to file: %s" , filename
);
2413 int CmdHF14AMfSniff ( const char * Cmd
){
2415 bool wantLogToFile
= 0 ;
2416 bool wantDecrypt
= 0 ;
2417 //bool wantSaveToEml = 0; TODO
2418 bool wantSaveToEmlFile
= 0 ;
2428 uint8_t atqa
[ 2 ] = { 0x00 };
2431 uint8_t * buf
= NULL
;
2432 uint16_t bufsize
= 0 ;
2433 uint8_t * bufPtr
= NULL
;
2435 char ctmp
= param_getchar ( Cmd
, 0 );
2436 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2437 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2438 PrintAndLog ( "You can specify:" );
2439 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2440 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2441 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2442 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2443 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2444 PrintAndLog ( " sample: hf mf sniff l d e" );
2448 for ( int i
= 0 ; i
< 4 ; i
++) {
2449 ctmp
= param_getchar ( Cmd
, i
);
2450 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2451 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2452 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2453 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2456 printf ( "------------------------------------------------------------------------- \n " );
2457 printf ( "Executing command. \n " );
2458 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2459 printf ( "Press the key on pc keyboard to abort the client. \n " );
2460 printf ( "------------------------------------------------------------------------- \n " );
2462 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2463 clearCommandBuffer ();
2472 printf ( " \n aborted via keyboard! \n " );
2477 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 2000 )) {
2478 res
= resp
. arg
[ 0 ] & 0xff ;
2479 uint16_t traceLen
= resp
. arg
[ 1 ];
2482 if ( res
== 0 ) { // we are done
2487 if ( res
== 1 ) { // there is (more) data to be transferred
2488 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2489 if ( traceLen
> bufsize
|| buf
== NULL
) {
2491 if ( buf
== NULL
) { // not yet allocated
2492 p
= malloc ( traceLen
);
2493 } else { // need more memory
2494 p
= realloc ( buf
, traceLen
);
2497 PrintAndLog ( "Cannot allocate memory for trace" );
2505 memset ( buf
, 0x00 , traceLen
);
2507 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2512 if ( res
== 2 ) { // received all data, start displaying
2513 blockLen
= bufPtr
- buf
;
2516 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2517 while ( bufPtr
- buf
< blockLen
) {
2518 bufPtr
+= 6 ; // skip (void) timing information
2519 len
= *(( uint16_t *) bufPtr
);
2527 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2528 memcpy ( uid
, bufPtr
+ 2 , 7 );
2529 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2530 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2532 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2533 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2537 if ( wantLogToFile
|| wantDecrypt
) {
2538 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2539 AddLogCurrentDT ( logHexFileName
);
2542 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2544 PrintAndLog ( "%s(%d):%s" , isTag
? "TAG" : "RDR" , num
, sprint_hex ( bufPtr
, len
));
2546 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2548 mfTraceDecode ( bufPtr
, len
, wantSaveToEmlFile
);
2552 bufPtr
+= (( len
- 1 )/ 8 + 1 ); // ignore parity
2563 //needs nt, ar, at, Data to decrypt
2564 int CmdDecryptTraceCmds ( const char * Cmd
){
2567 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2568 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2571 static command_t CommandTable
[] =
2573 { "help" , CmdHelp
, 1 , "This help" },
2574 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2575 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2576 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2577 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2578 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2579 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2580 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2581 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2582 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2583 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2584 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2585 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2586 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2587 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2588 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2589 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2590 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2591 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2592 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2593 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2594 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2595 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2596 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2597 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2598 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2599 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2600 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2601 { NULL
, NULL
, 0 , NULL
}
2604 int CmdHFMF ( const char * Cmd
)
2607 WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2609 CmdsParse ( CommandTable
, Cmd
);
2613 int CmdHelp ( const char * Cmd
)
2615 CmdsHelp ( CommandTable
);