]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
dbb6d092f5f49b2fabf9202b5550847cffd7af98
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"
28 #include "hardnested/hardnested_bf_core.h"
30 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
32 static int CmdHelp ( const char * Cmd
);
34 int CmdHF14AMifare ( const char * Cmd
)
38 isOK
= mfDarkside (& key
);
40 case - 1 : PrintAndLog ( "Button pressed. Aborted." ); return 1 ;
41 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)." ); return 1 ;
42 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable)." ); return 1 ;
43 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
44 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour." ); return 1 ;
45 case - 5 : PrintAndLog ( "Aborted via keyboard." ); return 1 ;
46 default : PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , key
);
54 int CmdHF14AMfWrBl ( const char * Cmd
)
58 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
59 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
64 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
65 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
69 blockNo
= param_get8 ( Cmd
, 0 );
70 cmdp
= param_getchar ( Cmd
, 1 );
72 PrintAndLog ( "Key type must be A or B" );
75 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
76 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
77 PrintAndLog ( "Key must include 12 HEX symbols" );
80 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
81 PrintAndLog ( "Block data must include 32 HEX symbols" );
84 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
85 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
87 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
88 memcpy ( c
. d
. asBytes
, key
, 6 );
89 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
93 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
94 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
95 PrintAndLog ( "isOk:%02x" , isOK
);
97 PrintAndLog ( "Command execute timeout" );
103 int CmdHF14AMfRdBl ( const char * Cmd
)
107 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
113 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
114 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
118 blockNo
= param_get8 ( Cmd
, 0 );
119 cmdp
= param_getchar ( Cmd
, 1 );
121 PrintAndLog ( "Key type must be A or B" );
124 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
125 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
126 PrintAndLog ( "Key must include 12 HEX symbols" );
129 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
131 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
132 memcpy ( c
. d
. asBytes
, key
, 6 );
136 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
137 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
138 uint8_t * data
= resp
. d
. asBytes
;
141 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
143 PrintAndLog ( "isOk:%02x" , isOK
);
145 PrintAndLog ( "Command execute timeout" );
151 int CmdHF14AMfRdSc ( const char * Cmd
)
154 uint8_t sectorNo
= 0 ;
156 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
158 uint8_t * data
= NULL
;
162 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
163 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
167 sectorNo
= param_get8 ( Cmd
, 0 );
169 PrintAndLog ( "Sector number must be less than 40" );
172 cmdp
= param_getchar ( Cmd
, 1 );
173 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
174 PrintAndLog ( "Key type must be A or B" );
177 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
178 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
179 PrintAndLog ( "Key must include 12 HEX symbols" );
182 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
184 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
185 memcpy ( c
. d
. asBytes
, key
, 6 );
190 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
191 isOK
= resp
. arg
[ 0 ] & 0xff ;
192 data
= resp
. d
. asBytes
;
194 PrintAndLog ( "isOk:%02x" , isOK
);
196 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
197 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
199 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
202 PrintAndLog ( "Command execute timeout" );
208 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
213 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
217 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
226 static int ParamCardSizeSectors ( const char c
) {
229 case '0' : numBlocks
= 5 ; break ;
230 case '2' : numBlocks
= 32 ; break ;
231 case '4' : numBlocks
= 40 ; break ;
232 default : numBlocks
= 16 ;
237 static int ParamCardSizeBlocks ( const char c
) {
238 int numBlocks
= 16 * 4 ;
240 case '0' : numBlocks
= 5 * 4 ; break ;
241 case '2' : numBlocks
= 32 * 4 ; break ;
242 case '4' : numBlocks
= 32 * 4 + 8 * 16 ; break ;
243 default : numBlocks
= 16 * 4 ;
248 int CmdHF14AMfDump ( const char * Cmd
)
250 uint8_t sectorNo
, blockNo
;
254 uint8_t rights
[ 40 ][ 4 ];
255 uint8_t carddata
[ 256 ][ 16 ];
256 uint8_t numSectors
= 16 ;
263 char cmdp
= param_getchar ( Cmd
, 0 );
264 numSectors
= ParamCardSizeSectors ( cmdp
);
266 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
267 PrintAndLog ( "Usage: hf mf dump [card memory]" );
268 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
270 PrintAndLog ( "Samples: hf mf dump" );
271 PrintAndLog ( " hf mf dump 4" );
275 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
276 PrintAndLog ( "Could not find file dumpkeys.bin" );
280 // Read keys A from file
281 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
282 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fin
);
283 if ( bytes_read
!= 6 ) {
284 PrintAndLog ( "File reading error." );
290 // Read keys B from file
291 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
292 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fin
);
293 if ( bytes_read
!= 6 ) {
294 PrintAndLog ( "File reading error." );
302 PrintAndLog ( "|-----------------------------------------|" );
303 PrintAndLog ( "|------ Reading sector access bits...-----|" );
304 PrintAndLog ( "|-----------------------------------------|" );
306 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
307 for ( tries
= 0 ; tries
< 3 ; tries
++) {
308 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
309 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
312 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
313 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
314 uint8_t * data
= resp
. d
. asBytes
;
316 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
317 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
318 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
319 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
321 } else if ( tries
== 2 ) { // on last try set defaults
322 PrintAndLog ( "Could not get access rights for sector %2d. Trying with defaults..." , sectorNo
);
323 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
324 rights
[ sectorNo
][ 3 ] = 0x01 ;
327 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
328 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
329 rights
[ sectorNo
][ 3 ] = 0x01 ;
334 PrintAndLog ( "|-----------------------------------------|" );
335 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
336 PrintAndLog ( "|-----------------------------------------|" );
339 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
340 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
341 bool received
= false ;
342 for ( tries
= 0 ; tries
< 3 ; tries
++) {
343 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
344 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
345 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
347 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
348 } else { // data block. Check if it can be read with key A or key B
349 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
350 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
351 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
352 memcpy ( c
. d
. asBytes
, keyB
[ sectorNo
], 6 );
354 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
355 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
357 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
359 } else { // key A would work
360 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
361 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
363 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
367 isOK
= resp
. arg
[ 0 ] & 0xff ;
373 isOK
= resp
. arg
[ 0 ] & 0xff ;
374 uint8_t * data
= resp
. d
. asBytes
;
375 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
376 data
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
377 data
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
378 data
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
379 data
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
380 data
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
381 data
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
382 data
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
383 data
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
384 data
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
385 data
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
386 data
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
387 data
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
390 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
391 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
393 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
399 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
406 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
407 PrintAndLog ( "Could not create file name dumpdata.bin" );
410 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
411 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
413 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
419 int CmdHF14AMfRestore ( const char * Cmd
)
421 uint8_t sectorNo
, blockNo
;
423 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
424 uint8_t bldata
[ 16 ] = { 0x00 };
432 char cmdp
= param_getchar ( Cmd
, 0 );
434 case '0' : numSectors
= 5 ; break ;
436 case '\0' : numSectors
= 16 ; break ;
437 case '2' : numSectors
= 32 ; break ;
438 case '4' : numSectors
= 40 ; break ;
439 default : numSectors
= 16 ;
442 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
443 PrintAndLog ( "Usage: hf mf restore [card memory]" );
444 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
446 PrintAndLog ( "Samples: hf mf restore" );
447 PrintAndLog ( " hf mf restore 4" );
451 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
452 PrintAndLog ( "Could not find file dumpkeys.bin" );
456 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
457 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
458 if ( bytes_read
!= 6 ) {
459 PrintAndLog ( "File reading error (dumpkeys.bin)." );
465 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
466 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
467 if ( bytes_read
!= 6 ) {
468 PrintAndLog ( "File reading error (dumpkeys.bin)." );
476 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
477 PrintAndLog ( "Could not find file dumpdata.bin" );
480 PrintAndLog ( "Restoring dumpdata.bin to card" );
482 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
483 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
484 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
485 memcpy ( c
. d
. asBytes
, key
, 6 );
487 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
488 if ( bytes_read
!= 16 ) {
489 PrintAndLog ( "File reading error (dumpdata.bin)." );
494 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
495 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
496 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
497 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
498 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
499 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
500 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
501 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
502 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
503 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
504 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
505 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
506 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
509 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
511 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
515 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
516 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
517 PrintAndLog ( "isOk:%02x" , isOK
);
519 PrintAndLog ( "Command execute timeout" );
528 //----------------------------------------------
530 //----------------------------------------------
532 static void parseParamTDS ( const char * Cmd
, const uint8_t indx
, bool * paramT
, bool * paramD
, uint8_t * timeout
) {
534 int len
= param_getlength ( Cmd
, indx
);
535 if ( len
> 0 && len
< 4 ){
536 param_getstr ( Cmd
, indx
, ctmp3
, sizeof ( ctmp3
));
538 * paramT
|= ( ctmp3
[ 0 ] == 't' || ctmp3
[ 0 ] == 'T' );
539 * paramD
|= ( ctmp3
[ 0 ] == 'd' || ctmp3
[ 0 ] == 'D' );
540 bool paramS1
= * paramT
|| * paramD
;
542 // slow and very slow
543 if ( ctmp3
[ 0 ] == 's' || ctmp3
[ 0 ] == 'S' || ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' ) {
544 * timeout
= 11 ; // slow
546 if (! paramS1
&& ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
547 * timeout
= 53 ; // very slow
549 if ( paramS1
&& ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
550 * timeout
= 53 ; // very slow
556 int CmdHF14AMfNested ( const char * Cmd
)
558 int i
, j
, res
, iterations
;
559 sector_t
* e_sector
= NULL
;
562 uint8_t trgBlockNo
= 0 ;
563 uint8_t trgKeyType
= 0 ;
564 uint8_t SectorsCnt
= 0 ;
565 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
566 uint8_t keyBlock
[ MifareDefaultKeysSize
* 6 ];
568 // timeout in units. (ms * 106)/10 or us*0.0106
569 uint8_t btimeout14a
= MF_CHKKEYS_DEFTIMEOUT
; // fast by default
571 bool autosearchKey
= false ;
573 bool transferToEml
= false ;
574 bool createDumpFile
= false ;
576 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
577 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
582 PrintAndLog ( "Usage:" );
583 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]" );
584 PrintAndLog ( " all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]" );
585 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
586 PrintAndLog ( " <target block number> <target key A/B> [t]" );
588 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
589 PrintAndLog ( "t - transfer keys to emulator memory" );
590 PrintAndLog ( "d - write keys to binary file dumpkeys.bin" );
591 PrintAndLog ( "s - Slow (1ms) check keys (required by some non standard cards)" );
592 PrintAndLog ( "ss - Very slow (5ms) check keys" );
594 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
595 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
596 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
597 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
598 PrintAndLog ( " sample5: hf mf nested 1 * t" );
599 PrintAndLog ( " sample6: hf mf nested 1 * ss" );
604 cmdp
= param_getchar ( Cmd
, 0 );
605 if ( cmdp
== 'o' || cmdp
== 'O' ) {
609 SectorsCnt
= ParamCardSizeSectors ( cmdp
);
612 // <block number>. number or autosearch key (*)
613 if ( param_getchar ( Cmd
, 1 ) == '*' ) {
614 autosearchKey
= true ;
616 parseParamTDS ( Cmd
, 2 , & transferToEml
, & createDumpFile
, & btimeout14a
);
618 PrintAndLog ( "--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us" ,
619 SectorsCnt
, transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
621 blockNo
= param_get8 ( Cmd
, 1 );
623 ctmp
= param_getchar ( Cmd
, 2 );
624 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
625 PrintAndLog ( "Key type must be A or B" );
629 if ( ctmp
!= 'A' && ctmp
!= 'a' )
632 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
633 PrintAndLog ( "Key must include 12 HEX symbols" );
637 // check if we can authenticate to sector
638 res
= mfCheckKeys ( blockNo
, keyType
, true , 1 , key
, & key64
);
640 PrintAndLog ( "Can't authenticate to block:%3d key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
646 trgBlockNo
= param_get8 ( Cmd
, 4 );
648 ctmp
= param_getchar ( Cmd
, 5 );
649 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
650 PrintAndLog ( "Target key type must be A or B" );
653 if ( ctmp
!= 'A' && ctmp
!= 'a' )
656 parseParamTDS ( Cmd
, 6 , & transferToEml
, & createDumpFile
, & btimeout14a
);
658 parseParamTDS ( Cmd
, 4 , & transferToEml
, & createDumpFile
, & btimeout14a
);
661 PrintAndLog ( "--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us" ,
662 SectorsCnt
, blockNo
, keyType
? 'B' : 'A' , transferToEml
? 'y' : 'n' , createDumpFile
? 'y' : 'n' , (( int ) btimeout14a
* 10000 ) / 106 );
666 if ( cmdp
== 'o' ) { // ------------------------------------ one sector working
667 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
668 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
671 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
672 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
673 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
674 default : PrintAndLog ( "Unknown Error. \n " );
678 key64
= bytes_to_num ( keyBlock
, 6 );
680 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
682 // transfer key to the emulator
684 uint8_t sectortrailer
;
685 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
686 sectortrailer
= ( trgBlockNo
& ~ 0x03 ) + 3 ;
687 } else { // 16 block sector
688 sectortrailer
= ( trgBlockNo
& ~ 0x0f ) + 15 ;
690 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
693 num_to_bytes ( key64
, 6 , keyBlock
);
695 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
696 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
697 PrintAndLog ( "Key transferred to emulator memory." );
700 PrintAndLog ( "No valid key found" );
703 else { // ------------------------------------ multiple sectors working
705 msclock1
= msclock ();
707 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
708 if ( e_sector
== NULL
) return 1 ;
710 //test current key and additional standard keys first
711 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< MifareDefaultKeysSize
; defaultKeyCounter
++){
712 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
715 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
716 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , MifareDefaultKeysSize
, keyBlock
, e_sector
);
718 // get known key from array
719 bool keyFound
= false ;
721 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
722 for ( j
= 0 ; j
< 2 ; j
++) {
723 if ( e_sector
[ i
]. foundKey
[ j
]) {
727 num_to_bytes ( e_sector
[ i
]. Key
[ j
], 6 , key
);
736 // Can't found a key....
738 PrintAndLog ( "Can't found any of the known keys." );
741 PrintAndLog ( "--auto key. block no:%3d, key type:%c key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
746 PrintAndLog ( "nested..." );
747 bool calibrate
= true ;
748 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
749 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
750 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
751 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
752 PrintAndLog ( "-----------------------------------------------" );
753 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
756 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
757 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
758 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
759 default : PrintAndLog ( "Unknown Error. \n " );
769 key64
= bytes_to_num ( keyBlock
, 6 );
771 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
772 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
773 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
775 // try to check this key as a key to the other sectors
776 mfCheckKeysSec ( SectorsCnt
, 2 , btimeout14a
, true , 1 , keyBlock
, e_sector
);
782 // print nested statistic
783 PrintAndLog ( " \n\n ----------------------------------------------- \n Nested statistic: \n Iterations count: %d" , iterations
);
784 PrintAndLog ( "Time in nested: %1.3f (%1.3f sec per key)" , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
787 PrintAndLog ( "|---|----------------|---|----------------|---|" );
788 PrintAndLog ( "|sec|key A |res|key B |res|" );
789 PrintAndLog ( "|---|----------------|---|----------------|---|" );
790 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
791 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
792 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
794 PrintAndLog ( "|---|----------------|---|----------------|---|" );
796 // transfer keys to the emulator memory
798 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
799 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
800 if ( e_sector
[ i
]. foundKey
[ 0 ])
801 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
802 if ( e_sector
[ i
]. foundKey
[ 1 ])
803 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
804 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
806 PrintAndLog ( "Keys transferred to emulator memory." );
810 if ( createDumpFile
) {
811 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
812 PrintAndLog ( "Could not create file dumpkeys.bin" );
816 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
817 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
818 if ( e_sector
[ i
]. foundKey
[ 0 ]){
819 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
820 fwrite ( tempkey
, 1 , 6 , fkeys
);
823 fwrite ( & standart
, 1 , 6 , fkeys
);
826 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
827 if ( e_sector
[ i
]. foundKey
[ 1 ]){
828 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
829 fwrite ( tempkey
, 1 , 6 , fkeys
);
832 fwrite ( & standart
, 1 , 6 , fkeys
);
844 int CmdHF14AMfNestedHard ( const char * Cmd
)
848 uint8_t trgBlockNo
= 0 ;
849 uint8_t trgKeyType
= 0 ;
850 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
851 uint8_t trgkey
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
854 ctmp
= param_getchar ( Cmd
, 0 );
856 if ( ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen ( Cmd
) < 20 ) {
857 PrintAndLog ( "Usage:" );
858 PrintAndLog ( " hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>" );
859 PrintAndLog ( " <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]" );
860 PrintAndLog ( " or hf mf hardnested r [known target key]" );
862 PrintAndLog ( "Options: " );
863 PrintAndLog ( " w: Acquire nonces and write them to binary file nonces.bin" );
864 PrintAndLog ( " s: Slower acquisition (required by some non standard cards)" );
865 PrintAndLog ( " r: Read nonces.bin and start attack" );
866 PrintAndLog ( " iX: set type of SIMD instructions. Without this flag programs autodetect it." );
867 PrintAndLog ( " i5: AVX512" );
868 PrintAndLog ( " i2: AVX2" );
869 PrintAndLog ( " ia: AVX" );
870 PrintAndLog ( " is: SSE2" );
871 PrintAndLog ( " im: MMX" );
872 PrintAndLog ( " in: none (use CPU regular instruction set)" );
874 PrintAndLog ( " sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A" );
875 PrintAndLog ( " sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w" );
876 PrintAndLog ( " sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s" );
877 PrintAndLog ( " sample4: hf mf hardnested r" );
879 PrintAndLog ( "Add the known target key to check if it is present in the remaining key space:" );
880 PrintAndLog ( " sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF" );
884 bool know_target_key
= false ;
885 bool nonce_file_read
= false ;
886 bool nonce_file_write
= false ;
892 if ( ctmp
== 'R' || ctmp
== 'r' ) {
893 nonce_file_read
= true ;
895 if (! param_gethex ( Cmd
, 1 , trgkey
, 12 )) {
896 know_target_key
= true ;
899 } else if ( ctmp
== 'T' || ctmp
== 't' ) {
900 tests
= param_get32ex ( Cmd
, 1 , 100 , 10 );
902 if (! param_gethex ( Cmd
, 2 , trgkey
, 12 )) {
903 know_target_key
= true ;
907 blockNo
= param_get8 ( Cmd
, 0 );
908 ctmp
= param_getchar ( Cmd
, 1 );
909 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
910 PrintAndLog ( "Key type must be A or B" );
913 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
917 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
918 PrintAndLog ( "Key must include 12 HEX symbols" );
922 trgBlockNo
= param_get8 ( Cmd
, 3 );
923 ctmp
= param_getchar ( Cmd
, 4 );
924 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
925 PrintAndLog ( "Target key type must be A or B" );
928 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
934 if (! param_gethex ( Cmd
, 5 , trgkey
, 12 )) {
935 know_target_key
= true ;
940 while (( ctmp
= param_getchar ( Cmd
, i
))) {
941 if ( ctmp
== 's' || ctmp
== 'S' ) {
943 } else if ( ctmp
== 'w' || ctmp
== 'W' ) {
944 nonce_file_write
= true ;
945 } else if ( param_getlength ( Cmd
, i
) == 2 && ctmp
== 'i' ) {
948 PrintAndLog ( "Possible options are w , s and/or iX" );
955 SetSIMDInstr ( SIMD_AUTO
);
957 while (( ctmp
= param_getchar ( Cmd
, iindx
))) {
958 if ( param_getlength ( Cmd
, iindx
) == 2 && ctmp
== 'i' ) {
959 switch ( param_getchar_indx ( Cmd
, 1 , iindx
)) {
961 SetSIMDInstr ( SIMD_AVX512
);
964 SetSIMDInstr ( SIMD_AVX2
);
967 SetSIMDInstr ( SIMD_AVX
);
970 SetSIMDInstr ( SIMD_SSE2
);
973 SetSIMDInstr ( SIMD_MMX
);
976 SetSIMDInstr ( SIMD_NONE
);
979 PrintAndLog ( "Unknown SIMD type. %c" , param_getchar_indx ( Cmd
, 1 , iindx
));
987 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 " ,
990 trgkey
[ 0 ], trgkey
[ 1 ], trgkey
[ 2 ], trgkey
[ 3 ], trgkey
[ 4 ], trgkey
[ 5 ],
991 know_target_key
? "" : " (not set)" ,
992 nonce_file_write
? "write" : nonce_file_read
? "read" : "none" ,
996 int16_t isOK
= mfnestedhard ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
1000 case 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
1001 case 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
1011 int CmdHF14AMfChk ( const char * Cmd
)
1013 if ( strlen ( Cmd
)< 3 ) {
1014 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
1015 PrintAndLog ( " * - all sectors" );
1016 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
1017 PrintAndLog ( "d - write keys to binary file \n " );
1018 PrintAndLog ( "t - write keys to emulator memory" );
1019 PrintAndLog ( "s - slow execute. timeout 1ms" );
1020 PrintAndLog ( "ss- very slow execute. timeout 5ms" );
1021 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
1022 PrintAndLog ( " hf mf chk *1 ? t" );
1023 PrintAndLog ( " hf mf chk *1 ? d" );
1024 PrintAndLog ( " hf mf chk *1 ? s" );
1025 PrintAndLog ( " hf mf chk *1 ? dss" );
1030 char filename
[ FILE_PATH_SIZE
]={ 0 };
1032 uint8_t * keyBlock
= NULL
, * p
;
1033 uint16_t stKeyBlock
= 20 ;
1039 char ctmp3
[ 3 ] = { 0x00 };
1040 uint8_t blockNo
= 0 ;
1041 uint8_t SectorsCnt
= 0 ;
1042 uint8_t keyType
= 0 ;
1044 uint32_t timeout14a
= 0 ; // timeout in us
1045 bool param3InUse
= false ;
1047 int transferToEml
= 0 ;
1048 int createDumpFile
= 0 ;
1050 sector_t
* e_sector
= NULL
;
1052 keyBlock
= calloc ( stKeyBlock
, 6 );
1053 if ( keyBlock
== NULL
) return 1 ;
1055 int defaultKeysSize
= MifareDefaultKeysSize
;
1056 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++){
1057 num_to_bytes ( MifareDefaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
1060 if ( param_getchar ( Cmd
, 0 )== '*' ) {
1061 SectorsCnt
= ParamCardSizeSectors ( param_getchar ( Cmd
+ 1 , 0 ));
1064 blockNo
= param_get8 ( Cmd
, 0 );
1066 ctmp
= param_getchar ( Cmd
, 1 );
1067 clen
= param_getlength ( Cmd
, 1 );
1080 PrintAndLog ( "Key type must be A , B or ?" );
1086 // transfer to emulator & create dump file
1087 ctmp
= param_getchar ( Cmd
, 2 );
1088 clen
= param_getlength ( Cmd
, 2 );
1089 if ( clen
== 1 && ( ctmp
== 't' || ctmp
== 'T' )) transferToEml
= 1 ;
1090 if ( clen
== 1 && ( ctmp
== 'd' || ctmp
== 'D' )) createDumpFile
= 1 ;
1092 param3InUse
= transferToEml
| createDumpFile
;
1094 timeout14a
= 500 ; // fast by default
1095 // double parameters - ts, ds
1096 clen
= param_getlength ( Cmd
, 2 );
1097 if ( clen
== 2 || clen
== 3 ){
1098 param_getstr ( Cmd
, 2 , ctmp3
, sizeof ( ctmp3
));
1102 if ( ctmp
== 's' || ctmp
== 'S' ) {
1103 timeout14a
= 1000 ; // slow
1104 if (! param3InUse
&& clen
== 2 && ( ctmp3
[ 1 ] == 's' || ctmp3
[ 1 ] == 'S' )) {
1105 timeout14a
= 5000 ; // very slow
1107 if ( param3InUse
&& clen
== 3 && ( ctmp3
[ 2 ] == 's' || ctmp3
[ 2 ] == 'S' )) {
1108 timeout14a
= 5000 ; // very slow
1113 for ( i
= param3InUse
; param_getchar ( Cmd
, 2 + i
); i
++) {
1114 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
1115 if ( stKeyBlock
- keycnt
< 2 ) {
1116 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1118 PrintAndLog ( "Cannot allocate memory for Keys" );
1124 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1125 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1126 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1129 // May be a dic file
1130 if ( param_getstr ( Cmd
, 2 + i
, filename
, sizeof ( filename
)) >= FILE_PATH_SIZE
) {
1131 PrintAndLog ( "File name too long" );
1136 if ( ( f
= fopen ( filename
, "r" )) ) {
1137 while ( fgets ( buf
, sizeof ( buf
), f
) ){
1138 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
1141 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
1143 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
1145 if (! isxdigit (( unsigned char ) buf
[ 0 ])){
1146 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
1152 if ( stKeyBlock
- keycnt
< 2 ) {
1153 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1155 PrintAndLog ( "Cannot allocate memory for defKeys" );
1162 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
1163 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
1164 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
1166 memset ( buf
, 0 , sizeof ( buf
));
1170 PrintAndLog ( "File: %s: not found or locked." , filename
);
1178 // fill with default keys
1180 PrintAndLog ( "No key specified, trying default keys" );
1181 for (; keycnt
< defaultKeysSize
; keycnt
++)
1182 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1183 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1184 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1187 // initialize storage for found keys
1188 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
1189 if ( e_sector
== NULL
) return 1 ;
1190 for ( uint8_t keyAB
= 0 ; keyAB
< 2 ; keyAB
++) {
1191 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1192 e_sector
[ sectorNo
]. Key
[ keyAB
] = 0xffffffffffff ;
1193 e_sector
[ sectorNo
]. foundKey
[ keyAB
] = 0 ;
1198 bool foundAKey
= false ;
1199 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1201 PrintAndLog ( "To cancel this operation press the button on the proxmark..." );
1203 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1205 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1206 res
= mfCheckKeysSec ( SectorsCnt
, keyType
, timeout14a
* 1.06 / 100 , true , size
, & keyBlock
[ 6 * c
], e_sector
); // timeout is (ms * 106)/10 or us*0.0106
1217 PrintAndLog ( "Command execute timeout" );
1221 int keyAB
= keyType
;
1223 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1225 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1226 res
= mfCheckKeys ( blockNo
, keyAB
& 0x01 , true , size
, & keyBlock
[ 6 * c
], & key64
);
1230 PrintAndLog ( "Found valid key:[%d:%c]%012" PRIx64
, blockNo
, ( keyAB
& 0x01 )? 'B' : 'A' , key64
);
1234 PrintAndLog ( "Command execute timeout" );
1237 } while (-- keyAB
> 0 );
1244 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1245 PrintAndLog ( "|sec|key A |res|key B |res|" );
1246 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1247 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
1248 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
1249 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
1251 PrintAndLog ( "|---|----------------|---|----------------|---|" );
1255 PrintAndLog ( "No valid keys found." );
1258 if ( transferToEml
) {
1260 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1261 if ( e_sector
[ sectorNo
]. foundKey
[ 0 ] || e_sector
[ sectorNo
]. foundKey
[ 1 ]) {
1262 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1263 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1264 if ( e_sector
[ sectorNo
]. foundKey
[ t
]) {
1265 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , block
+ t
* 10 );
1268 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1271 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1274 if ( createDumpFile
) {
1275 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1276 if ( fkeys
== NULL
) {
1277 PrintAndLog ( "Could not create file dumpkeys.bin" );
1283 for ( uint8_t t
= 0 ; t
< 2 ; t
++) {
1284 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1285 num_to_bytes ( e_sector
[ sectorNo
]. Key
[ t
], 6 , mkey
);
1286 fwrite ( mkey
, 1 , 6 , fkeys
);
1290 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1299 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1300 #define ATTACK_KEY_COUNT 7 // keep same as define in iso14443a.c -> Mifare1ksim()
1301 // cannot be more than 7 or it will overrun c.d.asBytes(512)
1307 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1308 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1310 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1311 memset ( stSector
, 0x00 , sizeof ( stSector
));
1312 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1313 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1315 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1316 if ( ar_resp
[ i
]. ar2
> 0 ) {
1317 //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);
1318 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1319 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 ));
1321 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1322 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1323 if ( ar_resp
[ i
]. keytype
== 0 ) {
1325 sector_trailer
[ ii
]. keyA
= key
;
1326 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1331 sector_trailer
[ ii
]. keyB
= key
;
1332 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1338 } else if ( mfkey32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1339 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1340 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1342 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1343 , keyType
? "B" : "A"
1348 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1349 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1352 sector_trailer
[ ii
]. keyA
= key
;
1353 stSector
[ ii
] = sectorNum
;
1358 sector_trailer
[ ii
]. keyB
= key
;
1359 stSector
[ ii
] = sectorNum
;
1369 //set emulator memory for keys
1370 if ( setEmulatorMem
) {
1371 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1373 uint8_t memBlock
[ 16 ];
1374 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1376 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1377 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 ));
1378 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1379 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1380 PrintAndLog ( "block data must include 32 HEX symbols" );
1384 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1385 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1386 clearCommandBuffer ();
1392 //un-comment to use as well moebius attack
1393 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1394 if (ar_resp[i].ar2 > 0) {
1395 if (tryMfk32_moebius(ar_resp[i], &key)) {
1396 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));
1402 int usage_hf14_mf1ksim ( void ) {
1403 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1404 PrintAndLog ( "options:" );
1405 PrintAndLog ( " h this help" );
1406 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1407 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1408 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1409 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1410 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1411 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1412 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1413 PrintAndLog ( "samples:" );
1414 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1415 PrintAndLog ( " hf mf sim u 11223344556677" );
1416 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1417 PrintAndLog ( " hf mf sim f uids.txt" );
1418 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1423 int CmdHF14AMf1kSim ( const char * Cmd
) {
1425 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1426 uint8_t exitAfterNReads
= 0 ;
1430 bool setEmulatorMem
= false ;
1431 bool attackFromFile
= false ;
1433 char filename
[ FILE_PATH_SIZE
];
1434 memset ( filename
, 0x00 , sizeof ( filename
));
1439 bool errors
= false ;
1441 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1442 switch ( param_getchar ( Cmd
, cmdp
)) {
1445 setEmulatorMem
= true ;
1447 flags
|= FLAG_INTERACTIVE
;
1448 flags
|= FLAG_NR_AR_ATTACK
;
1453 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
, sizeof ( filename
));
1455 PrintAndLog ( "error no filename found" );
1458 attackFromFile
= true ;
1460 flags
|= FLAG_INTERACTIVE
;
1461 flags
|= FLAG_NR_AR_ATTACK
;
1466 return usage_hf14_mf1ksim ();
1469 flags
|= FLAG_INTERACTIVE
;
1474 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1479 flags
|= FLAG_RANDOM_NONCE
;
1484 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1486 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1487 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1488 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1489 default : return usage_hf14_mf1ksim ();
1495 flags
|= FLAG_NR_AR_ATTACK
;
1499 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1506 if ( errors
) return usage_hf14_mf1ksim ();
1509 if ( attackFromFile
) {
1512 f
= fopen ( filename
, "r" );
1514 PrintAndLog ( "File %s not found or locked" , filename
);
1517 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1518 while (! feof ( f
) && ! ukbhit ()){
1519 memset ( buf
, 0 , sizeof ( buf
));
1520 memset ( uid
, 0 , sizeof ( uid
));
1522 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1523 if ( count
> 0 ) break ;
1525 PrintAndLog ( "File reading error." );
1529 if (! strlen ( buf
) && feof ( f
)) break ;
1531 uidlen
= strlen ( buf
)- 1 ;
1533 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1534 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1535 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1537 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1542 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1543 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1546 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1547 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1548 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1549 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1550 , exitAfterNReads
, flags
, flags
);
1552 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1553 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1554 clearCommandBuffer ();
1557 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1558 //We're waiting only 1.5 s at a time, otherwise we get the
1559 // annoying message about "Waiting for a response... "
1562 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1563 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1564 // We can skip the standard attack if we have RANDOM_NONCE set.
1565 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1566 if (( bool ) resp
. arg
[ 1 ]) {
1567 PrintAndLog ( "Device button pressed - quitting" );
1574 } else { //not from file
1576 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1577 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1578 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1579 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1580 , exitAfterNReads
, flags
, flags
);
1582 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1583 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1584 clearCommandBuffer ();
1587 if ( flags
& FLAG_INTERACTIVE
) {
1588 PrintAndLog ( "Press pm3-button to abort simulation" );
1589 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1590 //We're waiting only 1.5 s at a time, otherwise we get the
1591 // annoying message about "Waiting for a response... "
1594 if ( flags
& FLAG_NR_AR_ATTACK
) {
1595 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1596 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1597 // We can skip the standard attack if we have RANDOM_NONCE set.
1598 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1606 int CmdHF14AMfDbg ( const char * Cmd
)
1608 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1610 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1613 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1614 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1615 PrintAndLog ( " 0 - no debug messages" );
1616 PrintAndLog ( " 1 - error messages" );
1617 PrintAndLog ( " 2 - plus information messages" );
1618 PrintAndLog ( " 3 - plus debug messages" );
1619 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1620 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1624 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1630 int CmdHF14AMfEGet ( const char * Cmd
)
1632 uint8_t blockNo
= 0 ;
1633 uint8_t data
[ 16 ] = { 0x00 };
1635 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1636 PrintAndLog ( "Usage: hf mf eget <block number>" );
1637 PrintAndLog ( " sample: hf mf eget 0 " );
1641 blockNo
= param_get8 ( Cmd
, 0 );
1644 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1645 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1647 PrintAndLog ( "Command execute timeout" );
1653 int CmdHF14AMfEClear ( const char * Cmd
)
1655 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1656 PrintAndLog ( "Usage: hf mf eclr" );
1657 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1661 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1667 int CmdHF14AMfESet ( const char * Cmd
)
1669 uint8_t memBlock
[ 16 ];
1670 uint8_t blockNo
= 0 ;
1672 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1674 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1675 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1676 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1680 blockNo
= param_get8 ( Cmd
, 0 );
1682 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1683 PrintAndLog ( "block data must include 32 HEX symbols" );
1688 return mfEmlSetMem ( memBlock
, blockNo
, 1 );
1692 int CmdHF14AMfELoad ( const char * Cmd
)
1695 char filename
[ FILE_PATH_SIZE
];
1696 char * fnameptr
= filename
;
1697 char buf
[ 64 ] = { 0x00 };
1698 uint8_t buf8
[ 64 ] = { 0x00 };
1699 int i
, len
, blockNum
, numBlocks
;
1700 int nameParamNo
= 1 ;
1702 char ctmp
= param_getchar ( Cmd
, 0 );
1704 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1705 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1706 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1707 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1709 PrintAndLog ( " sample: hf mf eload filename" );
1710 PrintAndLog ( " hf mf eload 4 filename" );
1715 case '0' : numBlocks
= 5 * 4 ; break ;
1717 case '\0' : numBlocks
= 16 * 4 ; break ;
1718 case '2' : numBlocks
= 32 * 4 ; break ;
1719 case '4' : numBlocks
= 256 ; break ;
1726 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1728 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1732 sprintf ( fnameptr
, ".eml" );
1735 f
= fopen ( filename
, "r" );
1737 PrintAndLog ( "File %s not found or locked" , filename
);
1743 memset ( buf
, 0 , sizeof ( buf
));
1745 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1747 if ( blockNum
>= numBlocks
) break ;
1749 PrintAndLog ( "File reading error." );
1754 if ( strlen ( buf
) < 32 ){
1755 if ( strlen ( buf
) && feof ( f
))
1757 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1762 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1763 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1766 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1767 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1774 if ( blockNum
>= numBlocks
) break ;
1779 if (( blockNum
!= numBlocks
)) {
1780 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1783 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1788 int CmdHF14AMfESave ( const char * Cmd
)
1791 char filename
[ FILE_PATH_SIZE
];
1792 char * fnameptr
= filename
;
1794 int i
, j
, len
, numBlocks
;
1795 int nameParamNo
= 1 ;
1797 memset ( filename
, 0 , sizeof ( filename
));
1798 memset ( buf
, 0 , sizeof ( buf
));
1800 char ctmp
= param_getchar ( Cmd
, 0 );
1802 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1803 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1804 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1805 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1807 PrintAndLog ( " sample: hf mf esave " );
1808 PrintAndLog ( " hf mf esave 4" );
1809 PrintAndLog ( " hf mf esave 4 filename" );
1814 case '0' : numBlocks
= 5 * 4 ; break ;
1816 case '\0' : numBlocks
= 16 * 4 ; break ;
1817 case '2' : numBlocks
= 32 * 4 ; break ;
1818 case '4' : numBlocks
= 256 ; break ;
1825 len
= param_getstr ( Cmd
, nameParamNo
, filename
, sizeof ( filename
));
1827 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1829 // user supplied filename?
1831 // get filename (UID from memory)
1832 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1833 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1834 len
= sprintf ( fnameptr
, "dump" );
1838 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1839 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1845 // add file extension
1846 sprintf ( fnameptr
, ".eml" );
1849 f
= fopen ( filename
, "w+" );
1852 PrintAndLog ( "Can't open file %s " , filename
);
1857 for ( i
= 0 ; i
< numBlocks
; i
++) {
1858 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1859 PrintAndLog ( "Cant get block: %d" , i
);
1862 for ( j
= 0 ; j
< 16 ; j
++)
1863 fprintf ( f
, "%02X" , buf
[ j
]);
1868 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1874 int CmdHF14AMfECFill ( const char * Cmd
)
1876 uint8_t keyType
= 0 ;
1877 uint8_t numSectors
= 16 ;
1879 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1880 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1881 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1883 PrintAndLog ( "samples: hf mf ecfill A" );
1884 PrintAndLog ( " hf mf ecfill A 4" );
1885 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1886 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1890 char ctmp
= param_getchar ( Cmd
, 0 );
1891 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1892 PrintAndLog ( "Key type must be A or B" );
1895 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1897 ctmp
= param_getchar ( Cmd
, 1 );
1899 case '0' : numSectors
= 5 ; break ;
1901 case '\0' : numSectors
= 16 ; break ;
1902 case '2' : numSectors
= 32 ; break ;
1903 case '4' : numSectors
= 40 ; break ;
1904 default : numSectors
= 16 ;
1907 printf ( "--params: numSectors: %d, keyType:%d \n " , numSectors
, keyType
);
1908 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1913 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1918 uint64_t keyA
, keyB
;
1920 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1921 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1922 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1923 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1925 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1929 char cmdp
= param_getchar ( Cmd
, 0 );
1932 case '0' : numSectors
= 5 ; break ;
1934 case '\0' : numSectors
= 16 ; break ;
1935 case '2' : numSectors
= 32 ; break ;
1936 case '4' : numSectors
= 40 ; break ;
1937 default : numSectors
= 16 ;
1940 PrintAndLog ( "|---|----------------|----------------|" );
1941 PrintAndLog ( "|sec|key A |key B |" );
1942 PrintAndLog ( "|---|----------------|----------------|" );
1943 for ( i
= 0 ; i
< numSectors
; i
++) {
1944 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1945 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1948 keyA
= bytes_to_num ( data
, 6 );
1949 keyB
= bytes_to_num ( data
+ 10 , 6 );
1950 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1952 PrintAndLog ( "|---|----------------|----------------|" );
1957 int CmdHF14AMfCSetUID ( const char * Cmd
)
1959 uint8_t uid
[ 8 ] = { 0x00 };
1960 uint8_t oldUid
[ 8 ] = { 0x00 };
1961 uint8_t atqa
[ 2 ] = { 0x00 };
1962 uint8_t sak
[ 1 ] = { 0x00 };
1963 uint8_t atqaPresent
= 0 ;
1966 uint8_t needHelp
= 0 ;
1969 if ( param_getchar ( Cmd
, 0 ) && param_gethex ( Cmd
, 0 , uid
, 8 )) {
1970 PrintAndLog ( "UID must include 8 HEX symbols" );
1974 if ( param_getlength ( Cmd
, 1 ) > 1 && param_getlength ( Cmd
, 2 ) > 1 ) {
1978 if ( param_gethex ( Cmd
, 1 , atqa
, 4 )) {
1979 PrintAndLog ( "ATQA must include 4 HEX symbols" );
1983 if ( param_gethex ( Cmd
, 2 , sak
, 2 )) {
1984 PrintAndLog ( "SAK must include 2 HEX symbols" );
1989 while ( param_getchar ( Cmd
, cmdp
) != 0x00 )
1991 switch ( param_getchar ( Cmd
, cmdp
))
1998 PrintAndLog ( "ERROR: Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
2005 if ( strlen ( Cmd
) < 1 || needHelp
) {
2007 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]" );
2008 PrintAndLog ( "sample: hf mf csetuid 01020304" );
2009 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08" );
2010 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
2014 PrintAndLog ( "uid:%s" , sprint_hex ( uid
, 4 ));
2016 PrintAndLog ( "--atqa:%s sak:%02x" , sprint_hex ( atqa
, 2 ), sak
[ 0 ]);
2019 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
);
2021 PrintAndLog ( "Can't set UID. Error=%d" , res
);
2025 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
2026 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
2030 int CmdHF14AMfCWipe ( const char * Cmd
)
2033 int numBlocks
= 16 * 4 ;
2034 bool wipeCard
= false ;
2035 bool fillCard
= false ;
2037 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2038 PrintAndLog ( "Usage: hf mf cwipe [card size] [w] [f]" );
2039 PrintAndLog ( "sample: hf mf cwipe 1 w f" );
2040 PrintAndLog ( "[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
2041 PrintAndLog ( "w - Wipe magic Chinese card (only works with gen:1a cards)" );
2042 PrintAndLog ( "f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)" );
2046 gen
= mfCIdentify ();
2047 if (( gen
!= 1 ) && ( gen
!= 2 ))
2050 numBlocks
= ParamCardSizeBlocks ( param_getchar ( Cmd
, 0 ));
2053 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ){
2054 switch ( param_getchar ( Cmd
, cmdp
)) {
2069 if (! wipeCard
&& ! fillCard
)
2072 PrintAndLog ( "--blocks count:%2d wipe:%c fill:%c" , numBlocks
, ( wipeCard
)? 'y' : 'n' , ( fillCard
)? 'y' : 'n' );
2075 /* generation 1b magic card */
2077 PrintAndLog ( "WARNING: can't wipe magic card 1b generation" );
2079 res
= mfCWipe ( numBlocks
, true , false , fillCard
);
2081 /* generation 1a magic card by default */
2082 res
= mfCWipe ( numBlocks
, false , wipeCard
, fillCard
);
2086 PrintAndLog ( "Can't wipe. error=%d" , res
);
2093 int CmdHF14AMfCSetBlk ( const char * Cmd
)
2095 uint8_t memBlock
[ 16 ] = { 0x00 };
2096 uint8_t blockNo
= 0 ;
2097 bool wipeCard
= false ;
2100 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2101 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
2102 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
2103 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
2104 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
2108 gen
= mfCIdentify ();
2109 if (( gen
!= 1 ) && ( gen
!= 2 ))
2112 blockNo
= param_get8 ( Cmd
, 0 );
2114 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
2115 PrintAndLog ( "block data must include 32 HEX symbols" );
2119 char ctmp
= param_getchar ( Cmd
, 2 );
2120 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
2121 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
2124 /* generation 1b magic card */
2125 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2127 /* generation 1a magic card by default */
2128 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
2132 PrintAndLog ( "Can't write block. error=%d" , res
);
2139 int CmdHF14AMfCLoad ( const char * Cmd
)
2142 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2143 char * fnameptr
= filename
;
2144 char buf
[ 256 ] = { 0x00 };
2145 uint8_t buf8
[ 256 ] = { 0x00 };
2146 uint8_t fillFromEmulator
= 0 ;
2147 int i
, len
, blockNum
, flags
= 0 , gen
= 0 , numblock
= 64 ;
2149 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
2150 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
2151 PrintAndLog ( "or from emulator memory (option `e`). 4K card: (option `4`)" );
2152 PrintAndLog ( "Usage: hf mf cload [file name w/o `.eml`][e][4]" );
2153 PrintAndLog ( " or: hf mf cload e [4]" );
2154 PrintAndLog ( "Sample: hf mf cload filename" );
2155 PrintAndLog ( " hf mf cload filname 4" );
2156 PrintAndLog ( " hf mf cload e" );
2157 PrintAndLog ( " hf mf cload e 4" );
2161 char ctmp
= param_getchar ( Cmd
, 0 );
2162 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2163 ctmp
= param_getchar ( Cmd
, 1 );
2164 if ( ctmp
== '4' ) numblock
= 256 ;
2166 gen
= mfCIdentify ();
2167 PrintAndLog ( "Loading magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2169 if ( fillFromEmulator
) {
2170 for ( blockNum
= 0 ; blockNum
< numblock
; blockNum
+= 1 ) {
2171 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
2172 PrintAndLog ( "Cant get block: %d" , blockNum
);
2175 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2176 if ( blockNum
== 1 ) flags
= 0 ; // just write
2177 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
2180 /* generation 1b magic card */
2181 flags
|= CSETBLOCK_MAGIC_1B
;
2182 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2183 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
2189 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2191 len
= strlen ( filename
);
2192 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2194 //memcpy(filename, Cmd, len);
2197 sprintf ( fnameptr
, ".eml" );
2200 f
= fopen ( filename
, "r" );
2202 PrintAndLog ( "File not found or locked." );
2209 memset ( buf
, 0 , sizeof ( buf
));
2211 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
2213 PrintAndLog ( "File reading error." );
2217 if ( strlen ( buf
) < 32 ) {
2218 if ( strlen ( buf
) && feof ( f
))
2220 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
2224 for ( i
= 0 ; i
< 32 ; i
+= 2 )
2225 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
2227 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
2228 if ( blockNum
== 1 ) flags
= 0 ; // just write
2229 if ( blockNum
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
2232 /* generation 1b magic card */
2233 flags
|= CSETBLOCK_MAGIC_1B
;
2234 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
2235 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
2241 if ( blockNum
>= numblock
) break ; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks
2245 //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2246 if ( blockNum
!= numblock
){
2247 PrintAndLog ( "File content error. There must be %d blocks" , numblock
);
2250 PrintAndLog ( "Loaded from file: %s" , filename
);
2256 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
2257 uint8_t memBlock
[ 16 ];
2258 uint8_t blockNo
= 0 ;
2260 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
2262 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2263 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
2264 PrintAndLog ( "sample: hf mf cgetblk 1" );
2265 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2269 gen
= mfCIdentify ();
2271 blockNo
= param_get8 ( Cmd
, 0 );
2273 PrintAndLog ( "--block number:%2d " , blockNo
);
2276 /* generation 1b magic card */
2277 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
| CSETBLOCK_MAGIC_1B
);
2279 /* generation 1a magic card by default */
2280 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2283 PrintAndLog ( "Can't read block. error=%d" , res
);
2287 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2291 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2292 uint8_t memBlock
[ 16 ] = { 0x00 };
2293 uint8_t sectorNo
= 0 ;
2294 int i
, res
, flags
, gen
= 0 , baseblock
= 0 , sect_size
= 4 ;
2296 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2297 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2298 PrintAndLog ( "sample: hf mf cgetsc 0" );
2299 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2303 sectorNo
= param_get8 ( Cmd
, 0 );
2305 if ( sectorNo
> 39 ) {
2306 PrintAndLog ( "Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k." );
2310 PrintAndLog ( "--sector number:%d " , sectorNo
);
2312 gen
= mfCIdentify ();
2314 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2315 if ( sectorNo
< 32 ) {
2316 baseblock
= sectorNo
* 4 ;
2318 baseblock
= 128 + 16 * ( sectorNo
- 32 );
2321 if ( sectorNo
> 31 ) sect_size
= 16 ;
2323 for ( i
= 0 ; i
< sect_size
; i
++) {
2324 if ( i
== 1 ) flags
= 0 ;
2325 if ( i
== sect_size
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2328 /* generation 1b magic card */
2329 flags
|= CSETBLOCK_MAGIC_1B
;
2331 res
= mfCGetBlock ( baseblock
+ i
, memBlock
, flags
);
2333 PrintAndLog ( "Can't read block. %d error=%d" , baseblock
+ i
, res
);
2337 PrintAndLog ( "block %3d data:%s" , baseblock
+ i
, sprint_hex ( memBlock
, 16 ));
2343 int CmdHF14AMfCSave ( const char * Cmd
) {
2346 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2347 char * fnameptr
= filename
;
2348 uint8_t fillFromEmulator
= 0 ;
2349 uint8_t buf
[ 256 ] = { 0x00 };
2350 int i
, j
, len
, flags
, gen
= 0 , numblock
= 64 ;
2352 // memset(filename, 0, sizeof(filename));
2353 // memset(buf, 0, sizeof(buf));
2355 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2356 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2357 PrintAndLog ( "or into emulator memory (option `e`). 4K card: (option `4`)" );
2358 PrintAndLog ( "Usage: hf mf csave [file name w/o `.eml`][e][4]" );
2359 PrintAndLog ( "Sample: hf mf csave " );
2360 PrintAndLog ( " hf mf csave filename" );
2361 PrintAndLog ( " hf mf csave e" );
2362 PrintAndLog ( " hf mf csave 4" );
2363 PrintAndLog ( " hf mf csave filename 4" );
2364 PrintAndLog ( " hf mf csave e 4" );
2368 char ctmp
= param_getchar ( Cmd
, 0 );
2369 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2370 if ( ctmp
== '4' ) numblock
= 256 ;
2371 ctmp
= param_getchar ( Cmd
, 1 );
2372 if ( ctmp
== '4' ) numblock
= 256 ;
2374 gen
= mfCIdentify ();
2375 PrintAndLog ( "Saving magic mifare %dK" , numblock
== 256 ? 4 : 1 );
2377 if ( fillFromEmulator
) {
2378 // put into emulator
2379 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2380 for ( i
= 0 ; i
< numblock
; i
++) {
2381 if ( i
== 1 ) flags
= 0 ;
2382 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2385 /* generation 1b magic card */
2386 flags
|= CSETBLOCK_MAGIC_1B
;
2388 if ( mfCGetBlock ( i
, buf
, flags
)) {
2389 PrintAndLog ( "Cant get block: %d" , i
);
2393 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2394 PrintAndLog ( "Cant set emul block: %d" , i
);
2400 param_getstr ( Cmd
, 0 , filename
, sizeof ( filename
));
2402 len
= strlen ( filename
);
2403 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2405 ctmp
= param_getchar ( Cmd
, 0 );
2406 if ( len
< 1 || ( ctmp
== '4' )) {
2409 flags
= CSETBLOCK_SINGLE_OPER
;
2411 /* generation 1b magic card */
2412 flags
|= CSETBLOCK_MAGIC_1B
;
2413 if ( mfCGetBlock ( 0 , buf
, flags
)) {
2414 PrintAndLog ( "Cant get block: %d" , 0 );
2415 len
= sprintf ( fnameptr
, "dump" );
2419 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2420 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2423 //memcpy(filename, Cmd, len);
2427 sprintf ( fnameptr
, ".eml" );
2430 f
= fopen ( filename
, "w+" );
2433 PrintAndLog ( "File not found or locked." );
2438 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2439 for ( i
= 0 ; i
< numblock
; i
++) {
2440 if ( i
== 1 ) flags
= 0 ;
2441 if ( i
== numblock
- 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2444 /* generation 1b magic card */
2445 flags
|= CSETBLOCK_MAGIC_1B
;
2446 if ( mfCGetBlock ( i
, buf
, flags
)) {
2447 PrintAndLog ( "Cant get block: %d" , i
);
2450 for ( j
= 0 ; j
< 16 ; j
++)
2451 fprintf ( f
, "%02x" , buf
[ j
]);
2456 PrintAndLog ( "Saved to file: %s" , filename
);
2463 int CmdHF14AMfSniff ( const char * Cmd
){
2465 bool wantLogToFile
= 0 ;
2466 bool wantDecrypt
= 0 ;
2467 //bool wantSaveToEml = 0; TODO
2468 bool wantSaveToEmlFile
= 0 ;
2478 uint8_t atqa
[ 2 ] = { 0x00 };
2481 uint8_t * buf
= NULL
;
2482 uint16_t bufsize
= 0 ;
2483 uint8_t * bufPtr
= NULL
;
2485 char ctmp
= param_getchar ( Cmd
, 0 );
2486 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2487 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2488 PrintAndLog ( "You can specify:" );
2489 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2490 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2491 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2492 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2493 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2494 PrintAndLog ( " sample: hf mf sniff l d e" );
2498 for ( int i
= 0 ; i
< 4 ; i
++) {
2499 ctmp
= param_getchar ( Cmd
, i
);
2500 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2501 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2502 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2503 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2506 printf ( "------------------------------------------------------------------------- \n " );
2507 printf ( "Executing command. \n " );
2508 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2509 printf ( "Press the key on pc keyboard to abort the client. \n " );
2510 printf ( "------------------------------------------------------------------------- \n " );
2512 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2513 clearCommandBuffer ();
2522 printf ( " \n aborted via keyboard! \n " );
2527 if ( WaitForResponseTimeoutW ( CMD_ACK
, & resp
, 2000 , false )) {
2528 res
= resp
. arg
[ 0 ] & 0xff ;
2529 uint16_t traceLen
= resp
. arg
[ 1 ];
2532 if ( res
== 0 ) { // we are done
2536 if ( res
== 1 ) { // there is (more) data to be transferred
2537 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2538 if ( traceLen
> bufsize
|| buf
== NULL
) {
2540 if ( buf
== NULL
) { // not yet allocated
2541 p
= malloc ( traceLen
);
2542 } else { // need more memory
2543 p
= realloc ( buf
, traceLen
);
2546 PrintAndLog ( "Cannot allocate memory for trace" );
2554 memset ( buf
, 0x00 , traceLen
);
2556 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2561 if ( res
== 2 ) { // received all data, start displaying
2562 blockLen
= bufPtr
- buf
;
2565 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2566 while ( bufPtr
- buf
< blockLen
) {
2567 bufPtr
+= 6 ; // skip (void) timing information
2568 len
= *(( uint16_t *) bufPtr
);
2576 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2577 memcpy ( uid
, bufPtr
+ 2 , 7 );
2578 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2579 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2581 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2582 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2586 if ( wantLogToFile
|| wantDecrypt
) {
2587 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2588 AddLogCurrentDT ( logHexFileName
);
2591 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2593 PrintAndLog ( "%s(%d):%s" , isTag
? "TAG" : "RDR" , num
, sprint_hex ( bufPtr
, len
));
2595 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2597 mfTraceDecode ( bufPtr
, len
, wantSaveToEmlFile
);
2601 bufPtr
+= (( len
- 1 )/ 8 + 1 ); // ignore parity
2610 msleep ( 300 ); // wait for exiting arm side.
2611 PrintAndLog ( "Done." );
2615 //needs nt, ar, at, Data to decrypt
2616 int CmdDecryptTraceCmds ( const char * Cmd
){
2619 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2620 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2623 static command_t CommandTable
[] =
2625 { "help" , CmdHelp
, 1 , "This help" },
2626 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2627 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2628 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2629 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2630 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2631 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2632 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2633 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2634 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2635 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2636 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2637 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2638 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2639 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2640 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2641 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2642 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2643 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2644 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2645 { "cwipe" , CmdHF14AMfCWipe
, 0 , "Wipe magic Chinese card" },
2646 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2647 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2648 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2649 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2650 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2651 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2652 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2653 { NULL
, NULL
, 0 , NULL
}
2656 int CmdHFMF ( const char * Cmd
)
2659 WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2661 CmdsParse ( CommandTable
, Cmd
);
2665 int CmdHelp ( const char * Cmd
)
2667 CmdsHelp ( CommandTable
);