]>
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 //-----------------------------------------------------------------------------
20 #include "cmdhfmfhard.h"
23 #include "util_posix.h"
26 #include "mifarehost.h"
29 #include "hardnested/hardnested_bf_core.h"
30 #include "cliparser/cliparser.h"
34 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
36 static int CmdHelp ( const char * Cmd
);
38 int CmdHF14AMifare ( const char * Cmd
)
42 isOK
= mfDarkside (& key
);
44 case - 1 : PrintAndLog ( "Button pressed. Aborted." ); return 1 ;
45 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)." ); return 1 ;
46 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable)." ); return 1 ;
47 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
48 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour." ); return 1 ;
49 case - 5 : PrintAndLog ( "Aborted via keyboard." ); return 1 ;
50 default : PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , key
);
58 int CmdHF14AMfWrBl ( const char * Cmd
)
62 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
63 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
68 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
69 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
73 blockNo
= param_get8 ( Cmd
, 0 );
74 cmdp
= param_getchar ( Cmd
, 1 );
76 PrintAndLog ( "Key type must be A or B" );
79 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
80 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
81 PrintAndLog ( "Key must include 12 HEX symbols" );
84 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
85 PrintAndLog ( "Block data must include 32 HEX symbols" );
88 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
89 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
91 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
92 memcpy ( c
. d
. asBytes
, key
, 6 );
93 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
97 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
98 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
99 PrintAndLog ( "isOk:%02x" , isOK
);
101 PrintAndLog ( "Command execute timeout" );
107 int CmdHF14AMfRdBl ( const char * Cmd
)
111 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
117 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
118 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
122 blockNo
= param_get8 ( Cmd
, 0 );
123 cmdp
= param_getchar ( Cmd
, 1 );
125 PrintAndLog ( "Key type must be A or B" );
128 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
129 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
130 PrintAndLog ( "Key must include 12 HEX symbols" );
133 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
135 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
136 memcpy ( c
. d
. asBytes
, key
, 6 );
140 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
141 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
142 uint8_t * data
= resp
. d
. asBytes
;
145 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
147 PrintAndLog ( "isOk:%02x" , isOK
);
149 PrintAndLog ( "Command execute timeout" );
155 int CmdHF14AMfRdSc ( const char * Cmd
)
158 uint8_t sectorNo
= 0 ;
160 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
162 uint8_t * data
= NULL
;
166 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
167 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
171 sectorNo
= param_get8 ( Cmd
, 0 );
173 PrintAndLog ( "Sector number must be less than 40" );
176 cmdp
= param_getchar ( Cmd
, 1 );
177 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
178 PrintAndLog ( "Key type must be A or B" );
181 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
182 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
183 PrintAndLog ( "Key must include 12 HEX symbols" );
186 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
188 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
189 memcpy ( c
. d
. asBytes
, key
, 6 );
194 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
195 isOK
= resp
. arg
[ 0 ] & 0xff ;
196 data
= resp
. d
. asBytes
;
198 PrintAndLog ( "isOk:%02x" , isOK
);
200 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
201 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
203 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
206 PrintAndLog ( "Command execute timeout" );
212 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
217 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
221 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
230 static int ParamCardSizeSectors ( const char c
) {
233 case '0' : numBlocks
= 5 ; break ;
234 case '2' : numBlocks
= 32 ; break ;
235 case '4' : numBlocks
= 40 ; break ;
236 default : numBlocks
= 16 ;
241 static int ParamCardSizeBlocks ( const char c
) {
242 int numBlocks
= 16 * 4 ;
244 case '0' : numBlocks
= 5 * 4 ; break ;
245 case '2' : numBlocks
= 32 * 4 ; break ;
246 case '4' : numBlocks
= 32 * 4 + 8 * 16 ; break ;
247 default : numBlocks
= 16 * 4 ;
252 int CmdHF14AMfDump ( const char * Cmd
)
254 uint8_t sectorNo
, blockNo
;
258 uint8_t rights
[ 40 ][ 4 ];
259 uint8_t carddata
[ 256 ][ 16 ];
260 uint8_t numSectors
= 16 ;
267 char cmdp
= param_getchar ( Cmd
, 0 );
268 numSectors
= ParamCardSizeSectors ( cmdp
);
270 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
271 PrintAndLog ( "Usage: hf mf dump [card memory]" );
272 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
274 PrintAndLog ( "Samples: hf mf dump" );
275 PrintAndLog ( " hf mf dump 4" );
279 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
280 PrintAndLog ( "Could not find file dumpkeys.bin" );
284 // Read keys A from file
285 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
286 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fin
);
287 if ( bytes_read
!= 6 ) {
288 PrintAndLog ( "File reading error." );
294 // Read keys B from file
295 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
296 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fin
);
297 if ( bytes_read
!= 6 ) {
298 PrintAndLog ( "File reading error." );
306 PrintAndLog ( "|-----------------------------------------|" );
307 PrintAndLog ( "|------ Reading sector access bits...-----|" );
308 PrintAndLog ( "|-----------------------------------------|" );
310 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
311 for ( tries
= 0 ; tries
< 3 ; tries
++) {
312 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
313 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
316 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
317 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
318 uint8_t * data
= resp
. d
. asBytes
;
320 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
321 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
322 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
323 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
325 } else if ( tries
== 2 ) { // on last try set defaults
326 PrintAndLog ( "Could not get 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 ;
331 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
332 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
333 rights
[ sectorNo
][ 3 ] = 0x01 ;
338 PrintAndLog ( "|-----------------------------------------|" );
339 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
340 PrintAndLog ( "|-----------------------------------------|" );
343 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
344 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
345 bool received
= false ;
346 for ( tries
= 0 ; tries
< 3 ; tries
++) {
347 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
348 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
349 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
351 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
352 } else { // data block. Check if it can be read with key A or key B
353 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
354 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
355 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
356 memcpy ( c
. d
. asBytes
, keyB
[ sectorNo
], 6 );
358 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
359 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
361 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
363 } else { // key A would work
364 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
365 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
367 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
371 isOK
= resp
. arg
[ 0 ] & 0xff ;
377 isOK
= resp
. arg
[ 0 ] & 0xff ;
378 uint8_t * data
= resp
. d
. asBytes
;
379 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
380 data
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
381 data
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
382 data
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
383 data
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
384 data
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
385 data
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
386 data
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
387 data
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
388 data
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
389 data
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
390 data
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
391 data
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
394 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
395 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
397 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
403 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
410 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
411 PrintAndLog ( "Could not create file name dumpdata.bin" );
414 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
415 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
417 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
423 int CmdHF14AMfRestore ( const char * Cmd
)
425 uint8_t sectorNo
, blockNo
;
427 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
428 uint8_t bldata
[ 16 ] = { 0x00 };
436 char cmdp
= param_getchar ( Cmd
, 0 );
438 case '0' : numSectors
= 5 ; break ;
440 case '\0' : numSectors
= 16 ; break ;
441 case '2' : numSectors
= 32 ; break ;
442 case '4' : numSectors
= 40 ; break ;
443 default : numSectors
= 16 ;
446 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
447 PrintAndLog ( "Usage: hf mf restore [card memory]" );
448 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
450 PrintAndLog ( "Samples: hf mf restore" );
451 PrintAndLog ( " hf mf restore 4" );
455 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
456 PrintAndLog ( "Could not find file dumpkeys.bin" );
460 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
461 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
462 if ( bytes_read
!= 6 ) {
463 PrintAndLog ( "File reading error (dumpkeys.bin)." );
469 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
470 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
471 if ( bytes_read
!= 6 ) {
472 PrintAndLog ( "File reading error (dumpkeys.bin)." );
480 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
481 PrintAndLog ( "Could not find file dumpdata.bin" );
484 PrintAndLog ( "Restoring dumpdata.bin to card" );
486 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
487 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
488 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
489 memcpy ( c
. d
. asBytes
, key
, 6 );
491 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
492 if ( bytes_read
!= 16 ) {
493 PrintAndLog ( "File reading error (dumpdata.bin)." );
498 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
499 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
500 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
501 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
502 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
503 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
504 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
505 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
506 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
507 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
508 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
509 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
510 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
513 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
515 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
519 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
520 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
521 PrintAndLog ( "isOk:%02x" , isOK
);
523 PrintAndLog ( "Command execute timeout" );
532 //----------------------------------------------
534 //----------------------------------------------
536 static void parseParamTDS ( const char * Cmd
, const uint8_t indx
, bool * paramT
, bool * paramD
, uint8_t * timeout
) {
538 int len
= param_getlength ( Cmd
, indx
);
539 if ( len
> 0 && len
< 4 ){
540 param_getstr ( Cmd
, indx
, ctmp3
, sizeof ( ctmp3
));
542 * paramT
|= ( ctmp3
[ 0 ] == 't' || ctmp3
[ 0 ] == 'T' );
543 * paramD
|= ( ctmp3
[ 0 ] == 'd' || ctmp3
[ 0 ] == 'D' );
544 bool paramS1
= * paramT
|| * paramD
;
546 // slow and very slow
547 if ( ctmp3
[ 0 ] == 's' || ctmp3
[ 0 ] == 'S' || ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' ) {
548 * timeout
= 11 ; // slow
550 if (! paramS1
&& ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
551 * timeout
= 53 ; // very slow
553 if ( paramS1
&& ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
554 * timeout
= 53 ; // very slow
560 int CmdHF14AMfNested ( const char * Cmd
)
562 int i
, j
, res
, iterations
;
563 sector_t
* e_sector
= NULL
;
566 uint8_t trgBlockNo
= 0 ;
567 uint8_t trgKeyType
= 0 ;
568 uint8_t SectorsCnt
= 0 ;
569 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
570 uint8_t keyBlock
[ MifareDefaultKeysSize
* 6 ];
572 // timeout in units. (ms * 106)/10 or us*0.0106
573 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
575 bool autosearchKey
= false ;
577 bool transferToEml
= false ;
578 bool createDumpFile
= false ;
580 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
581 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
586 PrintAndLog ( "Usage:" );
587 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]" );
588 PrintAndLog ( " all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]" );
589 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
590 PrintAndLog ( " <target block number> <target key A/B> [t]" );
592 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
593 PrintAndLog ( "t - transfer keys to emulator memory" );
594 PrintAndLog ( "d - write keys to binary file dumpkeys.bin" );
595 PrintAndLog ( "s - Slow (1ms) check keys (required by some non standard cards)" );
596 PrintAndLog ( "ss - Very slow (5ms) check keys" );
598 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
599 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
600 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
601 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
602 PrintAndLog ( " sample5: hf mf nested 1 * t" );
603 PrintAndLog ( " sample6: hf mf nested 1 * ss" );
608 cmdp
= param_getchar ( Cmd
, 0 );
609 if ( cmdp
== 'o' || cmdp
== 'O' ) {
613 SectorsCnt
= ParamCardSizeSectors ( cmdp
);
616 // <block number>. number or autosearch key (*)
617 if ( param_getchar ( Cmd
, 1 ) == '*' ) {
618 autosearchKey
= true ;
620 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
622 PrintAndLog ( "--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us" ,
623 SectorsCnt
, transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
625 blockNo
= param_get8 ( Cmd
, 1 );
627 ctmp
= param_getchar ( Cmd
, 2 );
628 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
629 PrintAndLog ( "Key type must be A or B" );
633 if ( ctmp
!= 'A' && ctmp
!= 'a' )
636 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
637 PrintAndLog ( "Key must include 12 HEX symbols" );
641 // check if we can authenticate to sector
642 res
= mfCheckKeys ( blockNo
, keyType
, true , 1 , key
, & key64
);
644 PrintAndLog ( "Can't authenticate to block:%3d key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
650 trgBlockNo
= param_get8 ( Cmd
, 4 );
652 ctmp
= param_getchar ( Cmd
, 5 );
653 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
654 PrintAndLog ( "Target key type must be A or B" );
657 if ( ctmp
!= 'A' && ctmp
!= 'a' )
660 parseParamTDS ( Cmd
, 6 , & transferToEml
, & createDumpFile
, & btimeout14a
);
662 parseParamTDS ( Cmd
, 4 , & transferToEml
, & createDumpFile
, & btimeout14a
);
665 PrintAndLog ( "--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
666 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
670 if ( cmdp
== 'o' ) { // ------------------------------------ one sector working
671 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
672 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
675 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
676 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
677 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
678 default : PrintAndLog ( "Unknown Error. \n " );
682 key64
= bytes_to_num ( keyBlock
, 6 );
684 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
686 // transfer key to the emulator
688 uint8_t sectortrailer
;
689 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
690 sectortrailer
= trgBlockNo
| 0x03 ;
691 } else { // 16 block sector
692 sectortrailer
= trgBlockNo
| 0x0f ;
694 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
697 num_to_bytes ( key64
, 6 , keyBlock
);
699 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
700 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
701 PrintAndLog ( "Key transferred to emulator memory." );
704 PrintAndLog ( "No valid key found" );
707 else { // ------------------------------------ multiple sectors working
709 msclock1
= msclock ();
711 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
712 if ( e_sector
== NULL
) return 1 ;
714 //test current key and additional standard keys first
715 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< MifareDefaultKeysSize
; defaultKeyCounter
++){
716 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
719 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
720 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , MifareDefaultKeysSize
, keyBlock
, e_sector
);
722 // get known key from array
723 bool keyFound
= false ;
725 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
726 for ( j
= 0 ; j
< 2 ; j
++) {
727 if ( e_sector
[ i
]. foundKey
[ j
]) {
731 num_to_bytes ( e_sector
[ i
]. Key
[ j
], 6 , key
);
739 // Can't found a key....
741 PrintAndLog ( "Can't found any of the known keys." );
745 PrintAndLog ( "--auto key. block no:%3d, key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
750 PrintAndLog ( "nested..." );
751 bool calibrate
= true ;
752 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
753 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
754 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
755 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
756 PrintAndLog ( "-----------------------------------------------" );
757 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
760 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
761 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
762 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
763 default : PrintAndLog ( "Unknown Error. \n " );
773 key64
= bytes_to_num ( keyBlock
, 6 );
775 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
776 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
777 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
779 // try to check this key as a key to the other sectors
780 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , 1 , keyBlock
, e_sector
);
786 // print nested statistic
787 PrintAndLog ( " \n\n ----------------------------------------------- \n Nested statistic: \n Iterations count: %d" , iterations
);
788 PrintAndLog ( "Time in nested: %1.3f (%1.3f sec per key)" , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
791 PrintAndLog ( "|---|----------------|---|----------------|---|" );
792 PrintAndLog ( "|sec|key A |res|key B |res|" );
793 PrintAndLog ( "|---|----------------|---|----------------|---|" );
794 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
795 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
796 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
798 PrintAndLog ( "|---|----------------|---|----------------|---|" );
800 // transfer keys to the emulator memory
802 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
803 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
804 if ( e_sector
[ i
]. foundKey
[ 0 ])
805 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
806 if ( e_sector
[ i
]. foundKey
[ 1 ])
807 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
808 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
810 PrintAndLog ( "Keys transferred to emulator memory." );
814 if ( createDumpFile
) {
815 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
816 PrintAndLog ( "Could not create file dumpkeys.bin" );
820 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
821 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
822 if ( e_sector
[ i
]. foundKey
[ 0 ]){
823 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
824 fwrite ( tempkey
, 1 , 6 , fkeys
);
827 fwrite ( & standart
, 1 , 6 , fkeys
);
830 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
831 if ( e_sector
[ i
]. foundKey
[ 1 ]){
832 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
833 fwrite ( tempkey
, 1 , 6 , fkeys
);
836 fwrite ( & standart
, 1 , 6 , fkeys
);
848 int CmdHF14AMfNestedHard ( const char * Cmd
)
852 uint8_t trgBlockNo
= 0 ;
853 uint8_t trgKeyType
= 0 ;
854 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
855 uint8_t trgkey
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
858 ctmp
= param_getchar ( Cmd
, 0 );
860 if ( ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen ( Cmd
) < 20 ) {
861 PrintAndLog ( "Usage:" );
862 PrintAndLog ( " hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>" );
863 PrintAndLog ( " <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]" );
864 PrintAndLog ( " or hf mf hardnested r [known target key]" );
866 PrintAndLog ( "Options: " );
867 PrintAndLog ( " w: Acquire nonces and write them to binary file nonces.bin" );
868 PrintAndLog ( " s: Slower acquisition (required by some non standard cards)" );
869 PrintAndLog ( " r: Read nonces.bin and start attack" );
870 PrintAndLog ( " iX: set type of SIMD instructions. Without this flag programs autodetect it." );
871 PrintAndLog ( " i5: AVX512" );
872 PrintAndLog ( " i2: AVX2" );
873 PrintAndLog ( " ia: AVX" );
874 PrintAndLog ( " is: SSE2" );
875 PrintAndLog ( " im: MMX" );
876 PrintAndLog ( " in: none (use CPU regular instruction set)" );
878 PrintAndLog ( " sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A" );
879 PrintAndLog ( " sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w" );
880 PrintAndLog ( " sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s" );
881 PrintAndLog ( " sample4: hf mf hardnested r" );
883 PrintAndLog ( "Add the known target key to check if it is present in the remaining key space:" );
884 PrintAndLog ( " sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF" );
888 bool know_target_key
= false ;
889 bool nonce_file_read
= false ;
890 bool nonce_file_write
= false ;
896 if ( ctmp
== 'R' || ctmp
== 'r' ) {
897 nonce_file_read
= true ;
899 if (! param_gethex ( Cmd
, 1 , trgkey
, 12 )) {
900 know_target_key
= true ;
903 } else if ( ctmp
== 'T' || ctmp
== 't' ) {
904 tests
= param_get32ex ( Cmd
, 1 , 100 , 10 );
906 if (! param_gethex ( Cmd
, 2 , trgkey
, 12 )) {
907 know_target_key
= true ;
911 blockNo
= param_get8 ( Cmd
, 0 );
912 ctmp
= param_getchar ( Cmd
, 1 );
913 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
914 PrintAndLog ( "Key type must be A or B" );
917 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
921 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
922 PrintAndLog ( "Key must include 12 HEX symbols" );
926 trgBlockNo
= param_get8 ( Cmd
, 3 );
927 ctmp
= param_getchar ( Cmd
, 4 );
928 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
929 PrintAndLog ( "Target key type must be A or B" );
932 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
938 if (! param_gethex ( Cmd
, 5 , trgkey
, 12 )) {
939 know_target_key
= true ;
944 while (( ctmp
= param_getchar ( Cmd
, i
))) {
945 if ( ctmp
== 's' || ctmp
== 'S' ) {
947 } else if ( ctmp
== 'w' || ctmp
== 'W' ) {
948 nonce_file_write
= true ;
949 } else if ( param_getlength ( Cmd
, i
) == 2 && ctmp
== 'i' ) {
952 PrintAndLog ( "Possible options are w , s and/or iX" );
959 SetSIMDInstr ( SIMD_AUTO
);
961 while (( ctmp
= param_getchar ( Cmd
, iindx
))) {
962 if ( param_getlength ( Cmd
, iindx
) == 2 && ctmp
== 'i' ) {
963 switch ( param_getchar_indx ( Cmd
, 1 , iindx
)) {
965 SetSIMDInstr ( SIMD_AVX512
);
968 SetSIMDInstr ( SIMD_AVX2
);
971 SetSIMDInstr ( SIMD_AVX
);
974 SetSIMDInstr ( SIMD_SSE2
);
977 SetSIMDInstr ( SIMD_MMX
);
980 SetSIMDInstr ( SIMD_NONE
);
983 PrintAndLog ( "Unknown SIMD type. %c" , param_getchar_indx ( Cmd
, 1 , iindx
));
991 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 " ,
994 trgkey
[ 0 ], trgkey
[ 1 ], trgkey
[ 2 ], trgkey
[ 3 ], trgkey
[ 4 ], trgkey
[ 5 ],
995 know_target_key
? "" : " (not set)" ,
996 nonce_file_write
? "write" : nonce_file_read
? "read" : "none" ,
1000 int16_t isOK
= mfnestedhard ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1004 case 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
1005 case 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
1015 int CmdHF14AMfChk ( const char * Cmd
)
1017 if ( strlen ( Cmd
)< 3 ) {
1018 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
1019 PrintAndLog ( " * - all sectors" );
1020 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
1021 PrintAndLog ( "d - write keys to binary file \n " );
1022 PrintAndLog ( "t - write keys to emulator memory" );
1023 PrintAndLog ( "s - slow execute. timeout 1ms" );
1024 PrintAndLog ( "ss - very slow execute. timeout 5ms" );
1025 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
1026 PrintAndLog ( " hf mf chk *1 ? t" );
1027 PrintAndLog ( " hf mf chk *1 ? d" );
1028 PrintAndLog ( " hf mf chk *1 ? s" );
1029 PrintAndLog ( " hf mf chk *1 ? dss" );
1034 char filename
[ FILE_PATH_SIZE
]={ 0 };
1036 uint8_t * keyBlock
= NULL
, * p
;
1037 uint16_t stKeyBlock
= 20 ;
1043 uint8_t blockNo
= 0 ;
1044 uint8_t SectorsCnt
= 0 ;
1045 uint8_t keyType
= 0 ;
1047 // timeout in units. (ms * 106)/10 or us*0.0106
1048 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
1049 bool param3InUse
= false ;
1051 bool transferToEml
= 0 ;
1052 bool createDumpFile
= 0 ;
1054 sector_t
* e_sector
= NULL
;
1056 keyBlock
= calloc ( stKeyBlock
, 6 );
1057 if ( keyBlock
== NULL
) return 1 ;
1059 int defaultKeysSize
= MifareDefaultKeysSize
;
1060 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++){
1061 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
1064 if ( param_getchar ( Cmd
, 0 )== '*' ) {
1065 SectorsCnt
= ParamCardSizeSectors ( param_getchar ( Cmd
+ 1 , 0 ));
1068 blockNo
= param_get8 ( Cmd
, 0 );
1070 ctmp
= param_getchar ( Cmd
, 1 );
1071 clen
= param_getlength ( Cmd
, 1 );
1084 PrintAndLog ( "Key type must be A , B or ?" );
1090 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
1092 param3InUse
= transferToEml
| createDumpFile
| ( btimeout14a
!= MF_CHKKEYS_DEFTIMEOUT
);
1094 PrintAndLog ( "--chk keys. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
1095 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
1097 for ( i
= param3InUse
; param_getchar ( Cmd
, 2 + i
); i
++) {
1098 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
1099 if ( stKeyBlock
- keycnt
< 2 ) {
1100 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1102 PrintAndLog ( "Cannot allocate memory for Keys" );
1108 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1109 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1110 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1113 // May be a dic file
1114 if ( param_getstr ( Cmd
, 2 + i
, filename
, sizeof ( filename
)) >= FILE_PATH_SIZE
) {
1115 PrintAndLog ( "File name too long" );
1120 if ( ( f
= fopen ( filename
, "r" )) ) {
1121 while ( fgets ( buf
, sizeof ( buf
), f
) ){
1122 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
1125 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
1127 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
1129 if (! isxdigit (( unsigned char ) buf
[ 0 ])){
1130 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
1136 if ( stKeyBlock
- keycnt
< 2 ) {
1137 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1139 PrintAndLog ( "Cannot allocate memory for defKeys" );
1146 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
1147 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
1148 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
1150 memset ( buf
, 0 , sizeof ( buf
));
1154 PrintAndLog ( "File: %s: not found or locked." , filename
);
1162 // fill with default keys
1164 PrintAndLog ( "No key specified, trying default keys" );
1165 for (; keycnt
< defaultKeysSize
; keycnt
++)
1166 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1167 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1168 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1171 // initialize storage for found keys
1172 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
1173 if ( e_sector
== NULL
) {
1177 for ( uint8_t keyAB
= 0 ; keyAB
< 2 ; keyAB
++) {
1178 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1179 e_sector
[ sectorNo
]. Key
[ keyAB
] = 0xffffffffffff ;
1180 e_sector
[ sectorNo
]. foundKey
[ keyAB
] = 0 ;
1185 bool foundAKey
= false ;
1186 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1188 PrintAndLog ( "To cancel this operation press the button on the proxmark..." );
1190 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1192 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1193 res
= mfCheckKeysSec ( SectorsCnt
, keyType
, btimeout14a
, true , size
, & keyBlock
[ 6 * c
], e_sector
); // timeout is (ms * 106)/10 or us*0.0106
1204 PrintAndLog ( "Command execute timeout" );
1208 int keyAB
= keyType
;
1210 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1212 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1213 res
= mfCheckKeys ( blockNo
, keyAB
& 0x01 , true , size
, & keyBlock
[ 6 * c
], & key64
);
1217 PrintAndLog ( "Found valid key:[%d:%c]%012" PRIx64
, blockNo
, ( keyAB
& 0x01 )? 'B' : 'A' , key64
);
1221 PrintAndLog ( "Command execute timeout" );
1224 } while (-- keyAB
> 0 );
1231 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1232 PrintAndLog ( "|sec|key A |res|key B |res|" );
1233 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1234 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
1235 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
1236 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
1238 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1242 PrintAndLog ( "No valid keys found." );
1245 if ( transferToEml
) {
1247 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1248 if ( e_sector
[ sectorNo
]. foundKey
[ 0 ] || e_sector
[ sectorNo
]. foundKey
[ 1 ]) {
1249 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1250 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1251 if ( e_sector
[ sectorNo
]. foundKey
[ t
]) {
1252 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , block
+ t
* 10 );
1255 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1258 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1261 if ( createDumpFile
) {
1262 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1263 if ( fkeys
== NULL
) {
1264 PrintAndLog ( "Could not create file dumpkeys.bin" );
1270 for ( uint8_t t
= 0 ; t
< 2 ; t
++) {
1271 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1272 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , mkey
);
1273 fwrite ( mkey
, 1 , 6 , fkeys
);
1277 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1286 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1287 #define ATTACK_KEY_COUNT 7 // keep same as define in iso14443a.c -> Mifare1ksim()
1288 // cannot be more than 7 or it will overrun c.d.asBytes(512)
1294 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1295 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1297 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1298 memset ( stSector
, 0x00 , sizeof ( stSector
));
1299 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1300 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1302 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1303 if ( ar_resp
[ i
]. ar2
> 0 ) {
1304 //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);
1305 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1306 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 ));
1308 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1309 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1310 if ( ar_resp
[ i
]. keytype
== 0 ) {
1312 sector_trailer
[ ii
]. keyA
= key
;
1313 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1318 sector_trailer
[ ii
]. keyB
= key
;
1319 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1325 } else if ( mfkey32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1326 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1327 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1329 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1330 , keyType
? "B" : "A"
1335 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1336 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1339 sector_trailer
[ ii
]. keyA
= key
;
1340 stSector
[ ii
] = sectorNum
;
1345 sector_trailer
[ ii
]. keyB
= key
;
1346 stSector
[ ii
] = sectorNum
;
1356 //set emulator memory for keys
1357 if ( setEmulatorMem
) {
1358 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1360 uint8_t memBlock
[ 16 ];
1361 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1363 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1364 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 ));
1365 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1366 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1367 PrintAndLog ( "block data must include 32 HEX symbols" );
1371 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1372 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1373 clearCommandBuffer ();
1379 //un-comment to use as well moebius attack
1380 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1381 if (ar_resp[i].ar2 > 0) {
1382 if (tryMfk32_moebius(ar_resp[i], &key)) {
1383 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));
1389 int usage_hf14_mf1ksim ( void ) {
1390 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1391 PrintAndLog ( "options:" );
1392 PrintAndLog ( " h this help" );
1393 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1394 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1395 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1396 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1397 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1398 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1399 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1400 PrintAndLog ( "samples:" );
1401 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1402 PrintAndLog ( " hf mf sim u 11223344556677" );
1403 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1404 PrintAndLog ( " hf mf sim f uids.txt" );
1405 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1410 int CmdHF14AMf1kSim ( const char * Cmd
) {
1412 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1413 uint8_t exitAfterNReads
= 0 ;
1417 bool setEmulatorMem
= false ;
1418 bool attackFromFile
= false ;
1420 char filename
[ FILE_PATH_SIZE
];
1421 memset ( filename
, 0x00 , sizeof ( filename
));
1426 bool errors
= false ;
1428 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1429 switch ( param_getchar ( Cmd
, cmdp
)) {
1432 setEmulatorMem
= true ;
1434 flags
|= FLAG_INTERACTIVE
;
1435 flags
|= FLAG_NR_AR_ATTACK
;
1440 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
, sizeof ( filename
));
1442 PrintAndLog ( "error no filename found" );
1445 attackFromFile
= true ;
1447 flags
|= FLAG_INTERACTIVE
;
1448 flags
|= FLAG_NR_AR_ATTACK
;
1453 return usage_hf14_mf1ksim ();
1456 flags
|= FLAG_INTERACTIVE
;
1461 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1466 flags
|= FLAG_RANDOM_NONCE
;
1471 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1473 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1474 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1475 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1476 default : return usage_hf14_mf1ksim ();
1482 flags
|= FLAG_NR_AR_ATTACK
;
1486 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1493 if ( errors
) return usage_hf14_mf1ksim ();
1496 if ( attackFromFile
) {
1499 f
= fopen ( filename
, "r" );
1501 PrintAndLog ( "File %s not found or locked" , filename
);
1504 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1505 while (! feof ( f
) && ! ukbhit ()){
1506 memset ( buf
, 0 , sizeof ( buf
));
1507 memset ( uid
, 0 , sizeof ( uid
));
1509 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1510 if ( count
> 0 ) break ;
1512 PrintAndLog ( "File reading error." );
1516 if (! strlen ( buf
) && feof ( f
)) break ;
1518 uidlen
= strlen ( buf
)- 1 ;
1520 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1521 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1522 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1524 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1529 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1530 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1533 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1534 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1535 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1536 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1537 , exitAfterNReads
, flags
, flags
);
1539 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1540 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1541 clearCommandBuffer ();
1544 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1545 //We're waiting only 1.5 s at a time, otherwise we get the
1546 // annoying message about "Waiting for a response... "
1549 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1550 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1551 // We can skip the standard attack if we have RANDOM_NONCE set.
1552 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1553 if (( bool ) resp
. arg
[ 1 ]) {
1554 PrintAndLog ( "Device button pressed - quitting" );
1561 } else { //not from file
1563 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1564 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1565 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1566 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1567 , exitAfterNReads
, flags
, flags
);
1569 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1570 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1571 clearCommandBuffer ();
1574 if ( flags
& FLAG_INTERACTIVE
) {
1575 PrintAndLog ( "Press pm3-button to abort simulation" );
1576 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1577 //We're waiting only 1.5 s at a time, otherwise we get the
1578 // annoying message about "Waiting for a response... "
1581 if ( flags
& FLAG_NR_AR_ATTACK
) {
1582 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1583 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1584 // We can skip the standard attack if we have RANDOM_NONCE set.
1585 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1593 int CmdHF14AMfDbg ( const char * Cmd
)
1595 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1597 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1600 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1601 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1602 PrintAndLog ( " 0 - no debug messages" );
1603 PrintAndLog ( " 1 - error messages" );
1604 PrintAndLog ( " 2 - plus information messages" );
1605 PrintAndLog ( " 3 - plus debug messages" );
1606 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1607 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1611 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1617 int CmdHF14AMfEGet ( const char * Cmd
)
1619 uint8_t blockNo
= 0 ;
1620 uint8_t data
[ 16 ] = { 0x00 };
1622 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1623 PrintAndLog ( "Usage: hf mf eget <block number>" );
1624 PrintAndLog ( " sample: hf mf eget 0 " );
1628 blockNo
= param_get8 ( Cmd
, 0 );
1631 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1632 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1634 PrintAndLog ( "Command execute timeout" );
1640 int CmdHF14AMfEClear ( const char * Cmd
)
1642 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1643 PrintAndLog ( "Usage: hf mf eclr" );
1644 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1648 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1654 int CmdHF14AMfESet ( const char * Cmd
)
1656 uint8_t memBlock
[ 16 ];
1657 uint8_t blockNo
= 0 ;
1659 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1661 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1662 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1663 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1667 blockNo
= param_get8 ( Cmd
, 0 );
1669 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1670 PrintAndLog ( "block data must include 32 HEX symbols" );
1675 return mfEmlSetMem ( memBlock
, blockNo
, 1 );
1679 int CmdHF14AMfELoad ( const char * Cmd
)
1682 char filename
[ FILE_PATH_SIZE
];
1683 char * fnameptr
= filename
;
1684 char buf
[ 64 ] = { 0x00 };
1685 uint8_t buf8
[ 64 ] = { 0x00 };
1686 int i
, len
, blockNum
, numBlocks
;
1687 int nameParamNo
= 1 ;
1689 char ctmp
= param_getchar ( Cmd
, 0 );
1691 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1692 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1693 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1694 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1696 PrintAndLog ( " sample: hf mf eload filename" );
1697 PrintAndLog ( " hf mf eload 4 filename" );
1702 case '0' : numBlocks
= 5 * 4 ; break ;
1704 case '\0' : numBlocks
= 16 * 4 ; break ;
1705 case '2' : numBlocks
= 32 * 4 ; break ;
1706 case '4' : numBlocks
= 256 ; break ;
1713 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1715 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1719 sprintf ( fnameptr
, ".eml" );
1722 f
= fopen ( filename
, "r" );
1724 PrintAndLog ( "File %s not found or locked" , filename
);
1730 memset ( buf
, 0 , sizeof ( buf
));
1732 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1734 if ( blockNum
>= numBlocks
) break ;
1736 PrintAndLog ( "File reading error." );
1741 if ( strlen ( buf
) < 32 ){
1742 if ( strlen ( buf
) && feof ( f
))
1744 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1749 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1750 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1753 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1754 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1761 if ( blockNum
>= numBlocks
) break ;
1766 if (( blockNum
!= numBlocks
)) {
1767 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1770 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1775 int CmdHF14AMfESave ( const char * Cmd
)
1778 char filename
[ FILE_PATH_SIZE
];
1779 char * fnameptr
= filename
;
1781 int i
, j
, len
, numBlocks
;
1782 int nameParamNo
= 1 ;
1784 memset ( filename
, 0 , sizeof ( filename
));
1785 memset ( buf
, 0 , sizeof ( buf
));
1787 char ctmp
= param_getchar ( Cmd
, 0 );
1789 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1790 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1791 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1792 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1794 PrintAndLog ( " sample: hf mf esave " );
1795 PrintAndLog ( " hf mf esave 4" );
1796 PrintAndLog ( " hf mf esave 4 filename" );
1801 case '0' : numBlocks
= 5 * 4 ; break ;
1803 case '\0' : numBlocks
= 16 * 4 ; break ;
1804 case '2' : numBlocks
= 32 * 4 ; break ;
1805 case '4' : numBlocks
= 256 ; break ;
1812 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1814 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1816 // user supplied filename?
1818 // get filename (UID from memory)
1819 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1820 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1821 len
= sprintf ( fnameptr
, "dump" );
1825 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1826 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1832 // add file extension
1833 sprintf ( fnameptr
, ".eml" );
1836 f
= fopen ( filename
, "w+" );
1839 PrintAndLog ( "Can't open file %s " , filename
);
1844 for ( i
= 0 ; i
< numBlocks
; i
++) {
1845 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1846 PrintAndLog ( "Cant get block: %d" , i
);
1849 for ( j
= 0 ; j
< 16 ; j
++)
1850 fprintf ( f
, "%02X" , buf
[ j
]);
1855 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1861 int CmdHF14AMfECFill ( const char * Cmd
)
1863 uint8_t keyType
= 0 ;
1864 uint8_t numSectors
= 16 ;
1866 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1867 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1868 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1870 PrintAndLog ( "samples: hf mf ecfill A" );
1871 PrintAndLog ( " hf mf ecfill A 4" );
1872 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1873 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1877 char ctmp
= param_getchar ( Cmd
, 0 );
1878 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1879 PrintAndLog ( "Key type must be A or B" );
1882 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1884 ctmp
= param_getchar ( Cmd
, 1 );
1886 case '0' : numSectors
= 5 ; break ;
1888 case '\0' : numSectors
= 16 ; break ;
1889 case '2' : numSectors
= 32 ; break ;
1890 case '4' : numSectors
= 40 ; break ;
1891 default : numSectors
= 16 ;
1894 printf ( "--params: numSectors: %d, keyType:%d \n " , numSectors
, keyType
);
1895 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1900 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1905 uint64_t keyA
, keyB
;
1907 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1908 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1909 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1910 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1912 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1916 char cmdp
= param_getchar ( Cmd
, 0 );
1919 case '0' : numSectors
= 5 ; break ;
1921 case '\0' : numSectors
= 16 ; break ;
1922 case '2' : numSectors
= 32 ; break ;
1923 case '4' : numSectors
= 40 ; break ;
1924 default : numSectors
= 16 ;
1927 PrintAndLog ( "|---|----------------|----------------|" );
1928 PrintAndLog ( "|sec|key A |key B |" );
1929 PrintAndLog ( "|---|----------------|----------------|" );
1930 for ( i
= 0 ; i
< numSectors
; i
++) {
1931 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1932 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1935 keyA
= bytes_to_num ( data
, 6 );
1936 keyB
= bytes_to_num ( data
+ 10 , 6 );
1937 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1939 PrintAndLog ( "|---|----------------|----------------|" );
1944 int CmdHF14AMfCSetUID ( const char * Cmd
)
1946 uint8_t uid
[ 8 ] = { 0x00 };
1947 uint8_t oldUid
[ 8 ] = { 0x00 };
1948 uint8_t atqa
[ 2 ] = { 0x00 };
1949 uint8_t sak
[ 1 ] = { 0x00 };
1950 uint8_t atqaPresent
= 0 ;
1953 uint8_t needHelp
= 0 ;
1956 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
1957 PrintAndLog ( "UID must include 8 HEX symbols" );
1961 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
1965 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
1966 PrintAndLog ( "ATQA must include 4 HEX symbols" );
1970 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
1971 PrintAndLog ( "SAK must include 2 HEX symbols" );
1976 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
1978 switch ( param_getchar ( Cmd
, cmdp
))
1985 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1992 if ( strlen ( Cmd
) < 1 || needHelp
) {
1994 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
1995 PrintAndLog ( "sample: hf mf csetuid 01020304" );
1996 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
1997 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
2001 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
2003 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
2006 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
2008 PrintAndLog ( "Can't set UID. Error=%d" , res
);
2012 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
2013 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
2017 int CmdHF14AMfCWipe ( const char * Cmd
)
2020 int numBlocks
= 16 * 4 ;
2021 bool wipeCard
= false ;
2022 bool fillCard
= false ;
2024 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2025 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [f]" );
2026 PrintAndLog ( "sample: hf mf cwipe 1 w f" );
2027 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
2028 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
2029 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
2033 gen
= mfCIdentify ();
2034 if (( gen
!= 1 ) && ( gen
!= 2 ))
2037 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2040 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2041 switch ( param_getchar ( Cmd
, cmdp
)) {
2056 if (! wipeCard
&& ! fillCard
)
2059 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2062 /* generation 1b magic card */
2064 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2066 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2068 /* generation 1a magic card by default */
2069 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2073 PrintAndLog ( "Can't wipe. error=%d" , res
);
2080 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2082 uint8_t memBlock
[ 16 ] = { 0x00 };
2083 uint8_t blockNo
= 0 ;
2084 bool wipeCard
= false ;
2087 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2088 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2089 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2090 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2091 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2095 gen
= mfCIdentify ();
2096 if (( gen
!= 1 ) && ( gen
!= 2 ))
2099 blockNo
= param_get8 ( Cmd
, 0 );
2101 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2102 PrintAndLog ( "block data must include 32 HEX symbols" );
2106 char ctmp
= param_getchar ( Cmd
, 2 );
2107 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2108 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2111 /* generation 1b magic card */
2112 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2114 /* generation 1a magic card by default */
2115 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2119 PrintAndLog ( "Can't write block. error=%d" , res
);
2126 int CmdHF14AMfCLoad ( const char * Cmd
)
2129 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2130 char * fnameptr
= filename
;
2131 char buf
[ 256 ] = { 0x00 };
2132 uint8_t buf8
[ 256 ] = { 0x00 };
2133 uint8_t fillFromEmulator
= 0 ;
2134 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2136 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2137 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2138 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2139 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2140 PrintAndLog ( " or: hf mf cload e [4]" );
2141 PrintAndLog ( "Sample: hf mf cload filename" );
2142 PrintAndLog ( " hf mf cload filname 4" );
2143 PrintAndLog ( " hf mf cload e" );
2144 PrintAndLog ( " hf mf cload e 4" );
2148 char ctmp
= param_getchar ( Cmd
, 0 );
2149 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2150 ctmp
= param_getchar ( Cmd
, 1 );
2151 if ( ctmp
== '4' ) numblock
= 256 ;
2153 gen
= mfCIdentify ();
2154 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2156 if ( fillFromEmulator
) {
2157 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2158 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2159 PrintAndLog ( "Cant get block: %d" , blockNum
);
2162 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2163 if ( blockNum
== 1 ) flags
= 0 ; // just write
2164 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2167 /* generation 1b magic card */
2168 flags
|= CSETBLOCK_MAGIC_1B
;
2169 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2170 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2176 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2178 len
= strlen ( filename
);
2179 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2181 //memcpy(filename, Cmd, len);
2184 sprintf ( fnameptr
, ".eml" );
2187 f
= fopen ( filename
, "r" );
2189 PrintAndLog ( "File not found or locked." );
2196 memset ( buf
, 0 , sizeof ( buf
));
2198 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2200 PrintAndLog ( "File reading error." );
2204 if ( strlen ( buf
) < 32 ) {
2205 if ( strlen ( buf
) && feof ( f
))
2207 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2211 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2212 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2214 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2215 if ( blockNum
== 1 ) flags
= 0 ; // just write
2216 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2219 /* generation 1b magic card */
2220 flags
|= CSETBLOCK_MAGIC_1B
;
2221 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2222 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2228 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2232 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2233 if ( blockNum
!= numblock
){
2234 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2237 PrintAndLog ( "Loaded from file: %s" , filename
);
2243 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2244 uint8_t memBlock
[ 16 ];
2245 uint8_t blockNo
= 0 ;
2247 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2249 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2250 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2251 PrintAndLog ( "sample: hf mf cgetblk 1" );
2252 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2256 gen
= mfCIdentify ();
2258 blockNo
= param_get8 ( Cmd
, 0 );
2260 PrintAndLog ( "--block number:%2d " , blockNo
);
2263 /* generation 1b magic card */
2264 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2266 /* generation 1a magic card by default */
2267 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2270 PrintAndLog ( "Can't read block. error=%d" , res
);
2274 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2278 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2279 uint8_t memBlock
[ 16 ] = { 0x00 };
2280 uint8_t sectorNo
= 0 ;
2281 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2283 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2284 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2285 PrintAndLog ( "sample: hf mf cgetsc 0" );
2286 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2290 sectorNo
= param_get8 ( Cmd
, 0 );
2292 if ( sectorNo
> 39 ) {
2293 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2297 PrintAndLog ( "--sector number:%d " , sectorNo
);
2299 gen
= mfCIdentify ();
2301 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2302 if ( sectorNo
< 32 ) {
2303 baseblock
= sectorNo
* 4 ;
2305 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2308 if ( sectorNo
> 31 ) sect_size
= 16 ;
2310 for ( i
= 0 ; i
< sect_size
; i
++) {
2311 if ( i
== 1 ) flags
= 0 ;
2312 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2315 /* generation 1b magic card */
2316 flags
|= CSETBLOCK_MAGIC_1B
;
2318 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2320 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2324 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2330 int CmdHF14AMfCSave ( const char * Cmd
) {
2333 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2334 char * fnameptr
= filename
;
2335 uint8_t fillFromEmulator
= 0 ;
2336 uint8_t buf
[ 256 ] = { 0x00 };
2337 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2339 // memset(filename, 0, sizeof(filename));
2340 // memset(buf, 0, sizeof(buf));
2342 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2343 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2344 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2345 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2346 PrintAndLog ( "Sample: hf mf csave " );
2347 PrintAndLog ( " hf mf csave filename" );
2348 PrintAndLog ( " hf mf csave e" );
2349 PrintAndLog ( " hf mf csave 4" );
2350 PrintAndLog ( " hf mf csave filename 4" );
2351 PrintAndLog ( " hf mf csave e 4" );
2355 char ctmp
= param_getchar ( Cmd
, 0 );
2356 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2357 if ( ctmp
== '4' ) numblock
= 256 ;
2358 ctmp
= param_getchar ( Cmd
, 1 );
2359 if ( ctmp
== '4' ) numblock
= 256 ;
2361 gen
= mfCIdentify ();
2362 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2364 if ( fillFromEmulator
) {
2365 // put into emulator
2366 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2367 for ( i
= 0 ; i
< numblock
; i
++) {
2368 if ( i
== 1 ) flags
= 0 ;
2369 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2372 /* generation 1b magic card */
2373 flags
|= CSETBLOCK_MAGIC_1B
;
2375 if ( mfCGetBlock ( i
, buf
, flags
)) {
2376 PrintAndLog ( "Cant get block: %d" , i
);
2380 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2381 PrintAndLog ( "Cant set emul block: %d" , i
);
2387 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2389 len
= strlen ( filename
);
2390 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2392 ctmp
= param_getchar ( Cmd
, 0 );
2393 if ( len
< 1 || ( ctmp
== '4' )) {
2396 flags
= CSETBLOCK_SINGLE_OPER
;
2398 /* generation 1b magic card */
2399 flags
|= CSETBLOCK_MAGIC_1B
;
2400 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2401 PrintAndLog ( "Cant get block: %d" , 0 );
2402 len
= sprintf ( fnameptr
, "dump" );
2406 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2407 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2410 //memcpy(filename, Cmd, len);
2414 sprintf ( fnameptr
, ".eml" );
2417 f
= fopen ( filename
, "w+" );
2420 PrintAndLog ( "File not found or locked." );
2425 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2426 for ( i
= 0 ; i
< numblock
; i
++) {
2427 if ( i
== 1 ) flags
= 0 ;
2428 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2431 /* generation 1b magic card */
2432 flags
|= CSETBLOCK_MAGIC_1B
;
2433 if ( mfCGetBlock ( i
, buf
, flags
)) {
2434 PrintAndLog ( "Cant get block: %d" , i
);
2437 for ( j
= 0 ; j
< 16 ; j
++)
2438 fprintf ( f
, "%02x" , buf
[ j
]);
2443 PrintAndLog ( "Saved to file: %s" , filename
);
2450 int CmdHF14AMfSniff ( const char * Cmd
){
2452 bool wantLogToFile
= 0 ;
2453 bool wantDecrypt
= 0 ;
2454 //bool wantSaveToEml = 0; TODO
2455 bool wantSaveToEmlFile
= 0 ;
2466 uint8_t atqa
[ 2 ] = { 0x00 };
2469 uint8_t * buf
= NULL
;
2470 uint16_t bufsize
= 0 ;
2471 uint8_t * bufPtr
= NULL
;
2474 char ctmp
= param_getchar ( Cmd
, 0 );
2475 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2476 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2477 PrintAndLog ( "You can specify:" );
2478 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2479 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2480 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2481 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2482 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2483 PrintAndLog ( " sample: hf mf sniff l d e" );
2487 for ( int i
= 0 ; i
< 4 ; i
++) {
2488 ctmp
= param_getchar ( Cmd
, i
);
2489 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2490 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2491 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2492 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2495 printf ( "------------------------------------------------------------------------- \n " );
2496 printf ( "Executing command. \n " );
2497 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2498 printf ( "Press the key on pc keyboard to abort the client. \n " );
2499 printf ( "------------------------------------------------------------------------- \n " );
2501 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2502 clearCommandBuffer ();
2511 printf ( " \n aborted via keyboard! \n " );
2516 if ( WaitForResponseTimeoutW ( CMD_ACK
, & resp
, 2000 , false )) {
2517 res
= resp
. arg
[ 0 ] & 0xff ;
2518 uint16_t traceLen
= resp
. arg
[ 1 ];
2521 if ( res
== 0 ) { // we are done
2525 if ( res
== 1 ) { // there is (more) data to be transferred
2526 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2527 if ( traceLen
> bufsize
|| buf
== NULL
) {
2529 if ( buf
== NULL
) { // not yet allocated
2530 p
= malloc ( traceLen
);
2531 } else { // need more memory
2532 p
= realloc ( buf
, traceLen
);
2535 PrintAndLog ( "Cannot allocate memory for trace" );
2543 memset ( buf
, 0x00 , traceLen
);
2545 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2550 if ( res
== 2 ) { // received all data, start displaying
2551 blockLen
= bufPtr
- buf
;
2554 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2555 while ( bufPtr
- buf
< blockLen
) {
2556 bufPtr
+= 6 ; // skip (void) timing information
2557 len
= *(( uint16_t *) bufPtr
);
2564 parlen
= ( len
- 1 ) / 8 + 1 ;
2566 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2567 memcpy ( uid
, bufPtr
+ 2 , 7 );
2568 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2569 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2571 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2572 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2576 if ( wantLogToFile
|| wantDecrypt
) {
2577 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2578 AddLogCurrentDT ( logHexFileName
);
2581 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2583 oddparitybuf ( bufPtr
, len
, parity
);
2584 PrintAndLog ( "%s(%d):%s [%s] c[%s]%c" ,
2585 isTag
? "TAG" : "RDR" ,
2587 sprint_hex ( bufPtr
, len
),
2588 printBitsPar ( bufPtr
+ len
, len
),
2589 printBitsPar ( parity
, len
),
2590 memcmp ( bufPtr
+ len
, parity
, len
/ 8 + 1 ) ? '!' : ' ' );
2592 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2594 mfTraceDecode ( bufPtr
, len
, bufPtr
[ len
], wantSaveToEmlFile
);
2598 bufPtr
+= parlen
; // ignore parity
2607 msleep ( 300 ); // wait for exiting arm side.
2608 PrintAndLog ( "Done." );
2612 //needs nt, ar, at, Data to decrypt
2613 int CmdDecryptTraceCmds ( const char * Cmd
){
2616 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2617 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2620 int CmdHF14AMfAuth4 ( const char * cmd
) {
2621 uint8_t keyn
[ 20 ] = { 0 };
2623 uint8_t key
[ 16 ] = { 0 };
2626 CLIParserInit ( "hf mf auth4" ,
2627 "Executes AES authentication command in ISO14443-4" ,
2628 "Usage: \n\t hf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication \n "
2629 " \t hf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication \n " );
2631 void * argtable
[] = {
2633 arg_str1 ( NULL
, NULL
, "<Key Num (HEX 2 bytes)>" , NULL
),
2634 arg_str1 ( NULL
, NULL
, "<Key Value (HEX 16 bytes)>" , NULL
),
2637 CLIExecWithReturn ( cmd
, argtable
, true );
2639 CLIGetHexWithReturn ( 1 , keyn
, & keynlen
);
2640 CLIGetHexWithReturn ( 2 , key
, & keylen
);
2644 PrintAndLog ( "ERROR: <Key Num> must be 2 bytes long instead of: %d" , keynlen
);
2649 PrintAndLog ( "ERROR: <Key Value> must be 16 bytes long instead of: %d" , keylen
);
2653 return MifareAuth4 ( NULL
, keyn
, key
, true , false , true );
2656 static command_t CommandTable
[] =
2658 { "help" , CmdHelp
, 1 , "This help" },
2659 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2660 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2661 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2662 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2663 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2664 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2665 { "auth4" , CmdHF14AMfAuth4
, 0 , "ISO14443-4 AES authentication" },
2666 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2667 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2668 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2669 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2670 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2671 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2672 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2673 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2674 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2675 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2676 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2677 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2678 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2679 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2680 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2681 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2682 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2683 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2684 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2685 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2686 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2687 { NULL
, NULL
, 0 , NULL
}
2690 int CmdHFMF ( const char * Cmd
)
2692 ( void ) WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2693 CmdsParse ( CommandTable
, Cmd
);
2697 int CmdHelp ( const char * Cmd
)
2699 CmdsHelp ( CommandTable
);