]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2011,2012 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
18 #include "proxmark3.h"
20 #include "cmdhfmfhard.h"
24 #include "mifarehost.h"
28 #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up
31 static int CmdHelp ( const char * Cmd
);
33 int CmdHF14AMifare ( const char * Cmd
)
37 isOK
= mfDarkside (& key
);
39 case - 1 : PrintAndLog ( "Button pressed. Aborted." ); return 1 ;
40 case - 2 : PrintAndLog ( "Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)." ); return 1 ;
41 case - 3 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator is not predictable)." ); return 1 ;
42 case - 4 : PrintAndLog ( "Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown" );
43 PrintAndLog ( "generating polynomial with 16 effective bits only, but shows unexpected behaviour." ); return 1 ;
44 case - 5 : PrintAndLog ( "Aborted via keyboard." ); return 1 ;
45 default : PrintAndLog ( "Found valid key:%012" PRIx64
" \n " , key
);
53 int CmdHF14AMfWrBl ( const char * Cmd
)
57 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
58 uint8_t bldata
[ 16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
63 PrintAndLog ( "Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>" );
64 PrintAndLog ( " sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F" );
68 blockNo
= param_get8 ( Cmd
, 0 );
69 cmdp
= param_getchar ( Cmd
, 1 );
71 PrintAndLog ( "Key type must be A or B" );
74 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
75 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
76 PrintAndLog ( "Key must include 12 HEX symbols" );
79 if ( param_gethex ( Cmd
, 3 , bldata
, 32 )) {
80 PrintAndLog ( "Block data must include 32 HEX symbols" );
83 PrintAndLog ( "--block no:%d, key type:%c, key:%s" , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
84 PrintAndLog ( "--data: %s" , sprint_hex ( bldata
, 16 ));
86 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { blockNo
, keyType
, 0 }};
87 memcpy ( c
. d
. asBytes
, key
, 6 );
88 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
92 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
93 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
94 PrintAndLog ( "isOk:%02x" , isOK
);
96 PrintAndLog ( "Command execute timeout" );
102 int CmdHF14AMfRdBl ( const char * Cmd
)
106 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
112 PrintAndLog ( "Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>" );
113 PrintAndLog ( " sample: hf mf rdbl 0 A FFFFFFFFFFFF " );
117 blockNo
= param_get8 ( Cmd
, 0 );
118 cmdp
= param_getchar ( Cmd
, 1 );
120 PrintAndLog ( "Key type must be A or B" );
123 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
124 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
125 PrintAndLog ( "Key must include 12 HEX symbols" );
128 PrintAndLog ( "--block no:%d, key type:%c, key:%s " , blockNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
130 UsbCommand c
= { CMD_MIFARE_READBL
, { blockNo
, keyType
, 0 }};
131 memcpy ( c
. d
. asBytes
, key
, 6 );
135 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
136 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
137 uint8_t * data
= resp
. d
. asBytes
;
140 PrintAndLog ( "isOk:%02x data:%s" , isOK
, sprint_hex ( data
, 16 ));
142 PrintAndLog ( "isOk:%02x" , isOK
);
144 PrintAndLog ( "Command execute timeout" );
150 int CmdHF14AMfRdSc ( const char * Cmd
)
153 uint8_t sectorNo
= 0 ;
155 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
157 uint8_t * data
= NULL
;
161 PrintAndLog ( "Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>" );
162 PrintAndLog ( " sample: hf mf rdsc 0 A FFFFFFFFFFFF " );
166 sectorNo
= param_get8 ( Cmd
, 0 );
168 PrintAndLog ( "Sector number must be less than 40" );
171 cmdp
= param_getchar ( Cmd
, 1 );
172 if ( cmdp
!= 'a' && cmdp
!= 'A' && cmdp
!= 'b' && cmdp
!= 'B' ) {
173 PrintAndLog ( "Key type must be A or B" );
176 if ( cmdp
!= 'A' && cmdp
!= 'a' ) keyType
= 1 ;
177 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
178 PrintAndLog ( "Key must include 12 HEX symbols" );
181 PrintAndLog ( "--sector no:%d key type:%c key:%s " , sectorNo
, keyType
? 'B' : 'A' , sprint_hex ( key
, 6 ));
183 UsbCommand c
= { CMD_MIFARE_READSC
, { sectorNo
, keyType
, 0 }};
184 memcpy ( c
. d
. asBytes
, key
, 6 );
189 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
190 isOK
= resp
. arg
[ 0 ] & 0xff ;
191 data
= resp
. d
. asBytes
;
193 PrintAndLog ( "isOk:%02x" , isOK
);
195 for ( i
= 0 ; i
< ( sectorNo
< 32 ? 3 : 15 ); i
++) {
196 PrintAndLog ( "data : %s" , sprint_hex ( data
+ i
* 16 , 16 ));
198 PrintAndLog ( "trailer: %s" , sprint_hex ( data
+ ( sectorNo
< 32 ? 3 : 15 ) * 16 , 16 ));
201 PrintAndLog ( "Command execute timeout" );
207 uint8_t FirstBlockOfSector ( uint8_t sectorNo
)
212 return 32 * 4 + ( sectorNo
- 32 ) * 16 ;
216 uint8_t NumBlocksPerSector ( uint8_t sectorNo
)
225 int CmdHF14AMfDump ( const char * Cmd
)
227 uint8_t sectorNo
, blockNo
;
231 uint8_t rights
[ 40 ][ 4 ];
232 uint8_t carddata
[ 256 ][ 16 ];
233 uint8_t numSectors
= 16 ;
240 char cmdp
= param_getchar ( Cmd
, 0 );
242 case '0' : numSectors
= 5 ; break ;
244 case '\0' : numSectors
= 16 ; break ;
245 case '2' : numSectors
= 32 ; break ;
246 case '4' : numSectors
= 40 ; break ;
247 default : numSectors
= 16 ;
250 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
251 PrintAndLog ( "Usage: hf mf dump [card memory]" );
252 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
254 PrintAndLog ( "Samples: hf mf dump" );
255 PrintAndLog ( " hf mf dump 4" );
259 if (( fin
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
260 PrintAndLog ( "Could not find file dumpkeys.bin" );
264 // Read keys A from file
265 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
266 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fin
);
267 if ( bytes_read
!= 6 ) {
268 PrintAndLog ( "File reading error." );
274 // Read keys B from file
275 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
276 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fin
);
277 if ( bytes_read
!= 6 ) {
278 PrintAndLog ( "File reading error." );
286 PrintAndLog ( "|-----------------------------------------|" );
287 PrintAndLog ( "|------ Reading sector access bits...-----|" );
288 PrintAndLog ( "|-----------------------------------------|" );
290 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
291 for ( tries
= 0 ; tries
< 3 ; tries
++) {
292 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 0 , 0 }};
293 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
296 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
297 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
298 uint8_t * data
= resp
. d
. asBytes
;
300 rights
[ sectorNo
][ 0 ] = (( data
[ 7 ] & 0x10 )>> 2 ) | (( data
[ 8 ] & 0x1 )<< 1 ) | (( data
[ 8 ] & 0x10 )>> 4 ); // C1C2C3 for data area 0
301 rights
[ sectorNo
][ 1 ] = (( data
[ 7 ] & 0x20 )>> 3 ) | (( data
[ 8 ] & 0x2 )<< 0 ) | (( data
[ 8 ] & 0x20 )>> 5 ); // C1C2C3 for data area 1
302 rights
[ sectorNo
][ 2 ] = (( data
[ 7 ] & 0x40 )>> 4 ) | (( data
[ 8 ] & 0x4 )>> 1 ) | (( data
[ 8 ] & 0x40 )>> 6 ); // C1C2C3 for data area 2
303 rights
[ sectorNo
][ 3 ] = (( data
[ 7 ] & 0x80 )>> 5 ) | (( data
[ 8 ] & 0x8 )>> 2 ) | (( data
[ 8 ] & 0x80 )>> 7 ); // C1C2C3 for sector trailer
305 } else if ( tries
== 2 ) { // on last try set defaults
306 PrintAndLog ( "Could not get access rights for sector %2d. Trying with defaults..." , sectorNo
);
307 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
308 rights
[ sectorNo
][ 3 ] = 0x01 ;
311 PrintAndLog ( "Command execute timeout when trying to read access rights for sector %2d. Trying with defaults..." , sectorNo
);
312 rights
[ sectorNo
][ 0 ] = rights
[ sectorNo
][ 1 ] = rights
[ sectorNo
][ 2 ] = 0x00 ;
313 rights
[ sectorNo
][ 3 ] = 0x01 ;
318 PrintAndLog ( "|-----------------------------------------|" );
319 PrintAndLog ( "|----- Dumping all blocks to file... -----|" );
320 PrintAndLog ( "|-----------------------------------------|" );
323 for ( sectorNo
= 0 ; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
324 for ( blockNo
= 0 ; isOK
&& blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
325 bool received
= false ;
326 for ( tries
= 0 ; tries
< 3 ; tries
++) {
327 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. At least the Access Conditions can always be read with key A.
328 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 0 , 0 }};
329 memcpy ( c
. d
. asBytes
, keyA
[ sectorNo
], 6 );
331 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
332 } else { // data block. Check if it can be read with key A or key B
333 uint8_t data_area
= sectorNo
< 32 ? blockNo
: blockNo
/ 5 ;
334 if (( rights
[ sectorNo
][ data_area
] == 0x03 ) || ( rights
[ sectorNo
][ data_area
] == 0x05 )) { // only key B would work
335 UsbCommand c
= { CMD_MIFARE_READBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, 1 , 0 }};
336 memcpy ( c
. d
. asBytes
, keyB
[ sectorNo
], 6 );
338 received
= WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 );
339 } else if ( rights
[ sectorNo
][ data_area
] == 0x07 ) { // no key would work
341 PrintAndLog ( "Access rights do not allow reading of sector %2d block %3d" , sectorNo
, blockNo
);
343 } else { // key A would work
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 );
351 isOK
= resp
. arg
[ 0 ] & 0xff ;
357 isOK
= resp
. arg
[ 0 ] & 0xff ;
358 uint8_t * data
= resp
. d
. asBytes
;
359 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer. Fill in the keys.
360 data
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
361 data
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
362 data
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
363 data
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
364 data
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
365 data
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
366 data
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
367 data
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
368 data
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
369 data
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
370 data
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
371 data
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
374 memcpy ( carddata
[ FirstBlockOfSector ( sectorNo
) + blockNo
], data
, 16 );
375 PrintAndLog ( "Successfully read block %2d of sector %2d." , blockNo
, sectorNo
);
377 PrintAndLog ( "Could not read block %2d of sector %2d" , blockNo
, sectorNo
);
383 PrintAndLog ( "Command execute timeout when trying to read block %2d of sector %2d." , blockNo
, sectorNo
);
390 if (( fout
= fopen ( "dumpdata.bin" , "wb" )) == NULL
) {
391 PrintAndLog ( "Could not create file name dumpdata.bin" );
394 uint16_t numblocks
= FirstBlockOfSector ( numSectors
- 1 ) + NumBlocksPerSector ( numSectors
- 1 );
395 fwrite ( carddata
, 1 , 16 * numblocks
, fout
);
397 PrintAndLog ( "Dumped %d blocks (%d bytes) to file dumpdata.bin" , numblocks
, 16 * numblocks
);
403 int CmdHF14AMfRestore ( const char * Cmd
)
405 uint8_t sectorNo
, blockNo
;
407 uint8_t key
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
408 uint8_t bldata
[ 16 ] = { 0x00 };
416 char cmdp
= param_getchar ( Cmd
, 0 );
418 case '0' : numSectors
= 5 ; break ;
420 case '\0' : numSectors
= 16 ; break ;
421 case '2' : numSectors
= 32 ; break ;
422 case '4' : numSectors
= 40 ; break ;
423 default : numSectors
= 16 ;
426 if ( strlen ( Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H' ) {
427 PrintAndLog ( "Usage: hf mf restore [card memory]" );
428 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
430 PrintAndLog ( "Samples: hf mf restore" );
431 PrintAndLog ( " hf mf restore 4" );
435 if (( fkeys
= fopen ( "dumpkeys.bin" , "rb" )) == NULL
) {
436 PrintAndLog ( "Could not find file dumpkeys.bin" );
440 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
441 size_t bytes_read
= fread ( keyA
[ sectorNo
], 1 , 6 , fkeys
);
442 if ( bytes_read
!= 6 ) {
443 PrintAndLog ( "File reading error (dumpkeys.bin)." );
449 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
450 size_t bytes_read
= fread ( keyB
[ sectorNo
], 1 , 6 , fkeys
);
451 if ( bytes_read
!= 6 ) {
452 PrintAndLog ( "File reading error (dumpkeys.bin)." );
460 if (( fdump
= fopen ( "dumpdata.bin" , "rb" )) == NULL
) {
461 PrintAndLog ( "Could not find file dumpdata.bin" );
464 PrintAndLog ( "Restoring dumpdata.bin to card" );
466 for ( sectorNo
= 0 ; sectorNo
< numSectors
; sectorNo
++) {
467 for ( blockNo
= 0 ; blockNo
< NumBlocksPerSector ( sectorNo
); blockNo
++) {
468 UsbCommand c
= { CMD_MIFARE_WRITEBL
, { FirstBlockOfSector ( sectorNo
) + blockNo
, keyType
, 0 }};
469 memcpy ( c
. d
. asBytes
, key
, 6 );
471 size_t bytes_read
= fread ( bldata
, 1 , 16 , fdump
);
472 if ( bytes_read
!= 16 ) {
473 PrintAndLog ( "File reading error (dumpdata.bin)." );
478 if ( blockNo
== NumBlocksPerSector ( sectorNo
) - 1 ) { // sector trailer
479 bldata
[ 0 ] = ( keyA
[ sectorNo
][ 0 ]);
480 bldata
[ 1 ] = ( keyA
[ sectorNo
][ 1 ]);
481 bldata
[ 2 ] = ( keyA
[ sectorNo
][ 2 ]);
482 bldata
[ 3 ] = ( keyA
[ sectorNo
][ 3 ]);
483 bldata
[ 4 ] = ( keyA
[ sectorNo
][ 4 ]);
484 bldata
[ 5 ] = ( keyA
[ sectorNo
][ 5 ]);
485 bldata
[ 10 ] = ( keyB
[ sectorNo
][ 0 ]);
486 bldata
[ 11 ] = ( keyB
[ sectorNo
][ 1 ]);
487 bldata
[ 12 ] = ( keyB
[ sectorNo
][ 2 ]);
488 bldata
[ 13 ] = ( keyB
[ sectorNo
][ 3 ]);
489 bldata
[ 14 ] = ( keyB
[ sectorNo
][ 4 ]);
490 bldata
[ 15 ] = ( keyB
[ sectorNo
][ 5 ]);
493 PrintAndLog ( "Writing to block %3d: %s" , FirstBlockOfSector ( sectorNo
) + blockNo
, sprint_hex ( bldata
, 16 ));
495 memcpy ( c
. d
. asBytes
+ 10 , bldata
, 16 );
499 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
500 uint8_t isOK
= resp
. arg
[ 0 ] & 0xff ;
501 PrintAndLog ( "isOk:%02x" , isOK
);
503 PrintAndLog ( "Command execute timeout" );
519 int CmdHF14AMfNested ( const char * Cmd
)
521 int i
, j
, res
, iterations
;
522 sector_t
* e_sector
= NULL
;
525 uint8_t trgBlockNo
= 0 ;
526 uint8_t trgKeyType
= 0 ;
527 uint8_t SectorsCnt
= 0 ;
528 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
529 uint8_t keyBlock
[ 14 * 6 ];
531 bool transferToEml
= false ;
533 bool createDumpFile
= false ;
535 uint8_t standart
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
536 uint8_t tempkey
[ 6 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
541 PrintAndLog ( "Usage:" );
542 PrintAndLog ( " all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]" );
543 PrintAndLog ( " one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>" );
544 PrintAndLog ( " <target block number> <target key A/B> [t]" );
545 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
546 PrintAndLog ( "t - transfer keys into emulator memory" );
547 PrintAndLog ( "d - write keys to binary file" );
549 PrintAndLog ( " sample1: hf mf nested 1 0 A FFFFFFFFFFFF " );
550 PrintAndLog ( " sample2: hf mf nested 1 0 A FFFFFFFFFFFF t " );
551 PrintAndLog ( " sample3: hf mf nested 1 0 A FFFFFFFFFFFF d " );
552 PrintAndLog ( " sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A" );
556 cmdp
= param_getchar ( Cmd
, 0 );
557 blockNo
= param_get8 ( Cmd
, 1 );
558 ctmp
= param_getchar ( Cmd
, 2 );
560 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
561 PrintAndLog ( "Key type must be A or B" );
565 if ( ctmp
!= 'A' && ctmp
!= 'a' )
568 if ( param_gethex ( Cmd
, 3 , key
, 12 )) {
569 PrintAndLog ( "Key must include 12 HEX symbols" );
573 if ( cmdp
== 'o' || cmdp
== 'O' ) {
575 trgBlockNo
= param_get8 ( Cmd
, 4 );
576 ctmp
= param_getchar ( Cmd
, 5 );
577 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
578 PrintAndLog ( "Target key type must be A or B" );
581 if ( ctmp
!= 'A' && ctmp
!= 'a' )
586 case '0' : SectorsCnt
= 05 ; break ;
587 case '1' : SectorsCnt
= 16 ; break ;
588 case '2' : SectorsCnt
= 32 ; break ;
589 case '4' : SectorsCnt
= 40 ; break ;
590 default : SectorsCnt
= 16 ;
594 ctmp
= param_getchar ( Cmd
, 4 );
595 if ( ctmp
== 't' || ctmp
== 'T' ) transferToEml
= true ;
596 else if ( ctmp
== 'd' || ctmp
== 'D' ) createDumpFile
= true ;
598 ctmp
= param_getchar ( Cmd
, 6 );
599 transferToEml
|= ( ctmp
== 't' || ctmp
== 'T' );
600 transferToEml
|= ( ctmp
== 'd' || ctmp
== 'D' );
603 PrintAndLog ( "--target block no:%3d, target key type:%c " , trgBlockNo
, trgKeyType
? 'B' : 'A' );
604 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, keyBlock
, true );
607 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
608 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
609 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
610 default : PrintAndLog ( "Unknown Error. \n " );
614 key64
= bytes_to_num ( keyBlock
, 6 );
616 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
618 // transfer key to the emulator
620 uint8_t sectortrailer
;
621 if ( trgBlockNo
< 32 * 4 ) { // 4 block sector
622 sectortrailer
= ( trgBlockNo
& 0x03 ) + 3 ;
623 } else { // 16 block sector
624 sectortrailer
= ( trgBlockNo
& 0x0f ) + 15 ;
626 mfEmlGetMem ( keyBlock
, sectortrailer
, 1 );
629 num_to_bytes ( key64
, 6 , keyBlock
);
631 num_to_bytes ( key64
, 6 , & keyBlock
[ 10 ]);
632 mfEmlSetMem ( keyBlock
, sectortrailer
, 1 );
635 PrintAndLog ( "No valid key found" );
638 else { // ------------------------------------ multiple sectors working
640 msclock1
= msclock ();
642 e_sector
= calloc ( SectorsCnt
, sizeof ( sector_t
));
643 if ( e_sector
== NULL
) return 1 ;
645 //test current key and additional standard keys first
646 memcpy ( keyBlock
, key
, 6 );
647 num_to_bytes ( 0xffffffffffff , 6 , ( uint8_t *)( keyBlock
+ 1 * 6 ));
648 num_to_bytes ( 0x000000000000 , 6 , ( uint8_t *)( keyBlock
+ 2 * 6 ));
649 num_to_bytes ( 0xa0a1a2a3a4a5 , 6 , ( uint8_t *)( keyBlock
+ 3 * 6 ));
650 num_to_bytes ( 0xb0b1b2b3b4b5 , 6 , ( uint8_t *)( keyBlock
+ 4 * 6 ));
651 num_to_bytes ( 0xaabbccddeeff , 6 , ( uint8_t *)( keyBlock
+ 5 * 6 ));
652 num_to_bytes ( 0x4d3a99c351dd , 6 , ( uint8_t *)( keyBlock
+ 6 * 6 ));
653 num_to_bytes ( 0x1a982c7e459a , 6 , ( uint8_t *)( keyBlock
+ 7 * 6 ));
654 num_to_bytes ( 0xd3f7d3f7d3f7 , 6 , ( uint8_t *)( keyBlock
+ 8 * 6 ));
655 num_to_bytes ( 0x714c5c886e97 , 6 , ( uint8_t *)( keyBlock
+ 9 * 6 ));
656 num_to_bytes ( 0x587ee5f9350f , 6 , ( uint8_t *)( keyBlock
+ 10 * 6 ));
657 num_to_bytes ( 0xa0478cc39091 , 6 , ( uint8_t *)( keyBlock
+ 11 * 6 ));
658 num_to_bytes ( 0x533cb6c723f6 , 6 , ( uint8_t *)( keyBlock
+ 12 * 6 ));
659 num_to_bytes ( 0x8fd0a4f256e9 , 6 , ( uint8_t *)( keyBlock
+ 13 * 6 ));
661 PrintAndLog ( "Testing known keys. Sector count=%d" , SectorsCnt
);
662 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
663 for ( j
= 0 ; j
< 2 ; j
++) {
664 if ( e_sector
[ i
]. foundKey
[ j
]) continue ;
666 res
= mfCheckKeys ( FirstBlockOfSector ( i
), j
, true , 6 , keyBlock
, & key64
);
669 e_sector
[ i
]. Key
[ j
] = key64
;
670 e_sector
[ i
]. foundKey
[ j
] = 1 ;
677 PrintAndLog ( "nested..." );
678 bool calibrate
= true ;
679 for ( i
= 0 ; i
< NESTED_SECTOR_RETRY
; i
++) {
680 for ( uint8_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
681 for ( trgKeyType
= 0 ; trgKeyType
< 2 ; trgKeyType
++) {
682 if ( e_sector
[ sectorNo
]. foundKey
[ trgKeyType
]) continue ;
683 PrintAndLog ( "-----------------------------------------------" );
684 int16_t isOK
= mfnested ( blockNo
, keyType
, key
, FirstBlockOfSector ( sectorNo
), trgKeyType
, keyBlock
, calibrate
);
687 case - 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
688 case - 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
689 case - 3 : PrintAndLog ( "Tag isn't vulnerable to Nested Attack (random numbers are not predictable). \n " ); break ;
690 default : PrintAndLog ( "Unknown Error. \n " );
700 key64
= bytes_to_num ( keyBlock
, 6 );
702 PrintAndLog ( "Found valid key:%012" PRIx64
, key64
);
703 e_sector
[ sectorNo
]. foundKey
[ trgKeyType
] = 1 ;
704 e_sector
[ sectorNo
]. Key
[ trgKeyType
] = key64
;
710 printf ( "Time in nested: %1.3f (%1.3f sec per key) \n\n " , (( float )( msclock () - msclock1
))/ 1000.0 , (( float )( msclock () - msclock1
))/ iterations
/ 1000.0 );
712 PrintAndLog ( "----------------------------------------------- \n Iterations count: %d \n\n " , iterations
);
714 PrintAndLog ( "|---|----------------|---|----------------|---|" );
715 PrintAndLog ( "|sec|key A |res|key B |res|" );
716 PrintAndLog ( "|---|----------------|---|----------------|---|" );
717 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
718 PrintAndLog ( "|%03d| %012" PRIx64
" | %d | %012" PRIx64
" | %d |" , i
,
719 e_sector
[ i
]. Key
[ 0 ], e_sector
[ i
]. foundKey
[ 0 ], e_sector
[ i
]. Key
[ 1 ], e_sector
[ i
]. foundKey
[ 1 ]);
721 PrintAndLog ( "|---|----------------|---|----------------|---|" );
723 // transfer them to the emulator
725 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
726 mfEmlGetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
727 if ( e_sector
[ i
]. foundKey
[ 0 ])
728 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , keyBlock
);
729 if ( e_sector
[ i
]. foundKey
[ 1 ])
730 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , & keyBlock
[ 10 ]);
731 mfEmlSetMem ( keyBlock
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 );
736 if ( createDumpFile
) {
737 if (( fkeys
= fopen ( "dumpkeys.bin" , "wb" )) == NULL
) {
738 PrintAndLog ( "Could not create file dumpkeys.bin" );
742 PrintAndLog ( "Printing keys to binary file dumpkeys.bin..." );
743 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
744 if ( e_sector
[ i
]. foundKey
[ 0 ]){
745 num_to_bytes ( e_sector
[ i
]. Key
[ 0 ], 6 , tempkey
);
746 fwrite ( tempkey
, 1 , 6 , fkeys
);
749 fwrite ( & standart
, 1 , 6 , fkeys
);
752 for ( i
= 0 ; i
< SectorsCnt
; i
++) {
753 if ( e_sector
[ i
]. foundKey
[ 1 ]){
754 num_to_bytes ( e_sector
[ i
]. Key
[ 1 ], 6 , tempkey
);
755 fwrite ( tempkey
, 1 , 6 , fkeys
);
758 fwrite ( & standart
, 1 , 6 , fkeys
);
770 int CmdHF14AMfNestedHard ( const char * Cmd
)
774 uint8_t trgBlockNo
= 0 ;
775 uint8_t trgKeyType
= 0 ;
776 uint8_t key
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
777 uint8_t trgkey
[ 6 ] = { 0 , 0 , 0 , 0 , 0 , 0 };
780 ctmp
= param_getchar ( Cmd
, 0 );
782 if ( ctmp
!= 'R' && ctmp
!= 'r' && ctmp
!= 'T' && ctmp
!= 't' && strlen ( Cmd
) < 20 ) {
783 PrintAndLog ( "Usage:" );
784 PrintAndLog ( " hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>" );
785 PrintAndLog ( " <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]" );
786 PrintAndLog ( " or hf mf hardnested r [known target key]" );
788 PrintAndLog ( "Options: " );
789 PrintAndLog ( " w: Acquire nonces and write them to binary file nonces.bin" );
790 PrintAndLog ( " s: Slower acquisition (required by some non standard cards)" );
791 PrintAndLog ( " r: Read nonces.bin and start attack" );
793 PrintAndLog ( " sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A" );
794 PrintAndLog ( " sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w" );
795 PrintAndLog ( " sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s" );
796 PrintAndLog ( " sample4: hf mf hardnested r" );
798 PrintAndLog ( "Add the known target key to check if it is present in the remaining key space:" );
799 PrintAndLog ( " sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF" );
803 bool know_target_key
= false ;
804 bool nonce_file_read
= false ;
805 bool nonce_file_write
= false ;
810 if ( ctmp
== 'R' || ctmp
== 'r' ) {
811 nonce_file_read
= true ;
812 if (! param_gethex ( Cmd
, 1 , trgkey
, 12 )) {
813 know_target_key
= true ;
815 } else if ( ctmp
== 'T' || ctmp
== 't' ) {
816 tests
= param_get32ex ( Cmd
, 1 , 100 , 10 );
817 if (! param_gethex ( Cmd
, 2 , trgkey
, 12 )) {
818 know_target_key
= true ;
821 blockNo
= param_get8 ( Cmd
, 0 );
822 ctmp
= param_getchar ( Cmd
, 1 );
823 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
824 PrintAndLog ( "Key type must be A or B" );
827 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
831 if ( param_gethex ( Cmd
, 2 , key
, 12 )) {
832 PrintAndLog ( "Key must include 12 HEX symbols" );
836 trgBlockNo
= param_get8 ( Cmd
, 3 );
837 ctmp
= param_getchar ( Cmd
, 4 );
838 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
839 PrintAndLog ( "Target key type must be A or B" );
842 if ( ctmp
!= 'A' && ctmp
!= 'a' ) {
848 if (! param_gethex ( Cmd
, 5 , trgkey
, 12 )) {
849 know_target_key
= true ;
853 while (( ctmp
= param_getchar ( Cmd
, i
))) {
854 if ( ctmp
== 's' || ctmp
== 'S' ) {
856 } else if ( ctmp
== 'w' || ctmp
== 'W' ) {
857 nonce_file_write
= true ;
859 PrintAndLog ( "Possible options are w and/or s" );
866 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 " ,
869 trgkey
[ 0 ], trgkey
[ 1 ], trgkey
[ 2 ], trgkey
[ 3 ], trgkey
[ 4 ], trgkey
[ 5 ],
870 know_target_key
? "" : " (not set)" ,
871 nonce_file_write
? "write" : nonce_file_read
? "read" : "none" ,
875 int16_t isOK
= mfnestedhard ( blockNo
, keyType
, key
, trgBlockNo
, trgKeyType
, know_target_key
? trgkey
: NULL
, nonce_file_read
, nonce_file_write
, slow
, tests
);
879 case 1 : PrintAndLog ( "Error: No response from Proxmark. \n " ); break ;
880 case 2 : PrintAndLog ( "Button pressed. Aborted. \n " ); break ;
890 int CmdHF14AMfChk ( const char * Cmd
)
893 PrintAndLog ( "Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]" );
894 PrintAndLog ( " * - all sectors" );
895 PrintAndLog ( "card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K" );
896 PrintAndLog ( "d - write keys to binary file \n " );
897 PrintAndLog ( "t - write keys to emulator memory" );
898 PrintAndLog ( " sample: hf mf chk 0 A 1234567890ab keys.dic" );
899 PrintAndLog ( " hf mf chk *1 ? t" );
900 PrintAndLog ( " hf mf chk *1 ? d" );
905 char filename
[ FILE_PATH_SIZE
]={ 0 };
907 uint8_t * keyBlock
= NULL
, * p
;
908 uint8_t stKeyBlock
= 20 ;
914 uint8_t SectorsCnt
= 1 ;
918 int transferToEml
= 0 ;
919 int createDumpFile
= 0 ;
921 keyBlock
= calloc ( stKeyBlock
, 6 );
922 if ( keyBlock
== NULL
) return 1 ;
924 uint64_t defaultKeys
[] =
926 0xffffffffffff , // Default key (first key used by program if no user defined key)
927 0x000000000000 , // Blank key
928 0xa0a1a2a3a4a5 , // NFCForum MAD key
940 int defaultKeysSize
= sizeof ( defaultKeys
) / sizeof ( uint64_t );
942 for ( int defaultKeyCounter
= 0 ; defaultKeyCounter
< defaultKeysSize
; defaultKeyCounter
++)
944 num_to_bytes ( defaultKeys
[ defaultKeyCounter
], 6 , ( uint8_t *)( keyBlock
+ defaultKeyCounter
* 6 ));
947 if ( param_getchar ( Cmd
, 0 )== '*' ) {
949 switch ( param_getchar ( Cmd
+ 1 , 0 )) {
950 case '0' : SectorsCnt
= 5 ; break ;
951 case '1' : SectorsCnt
= 16 ; break ;
952 case '2' : SectorsCnt
= 32 ; break ;
953 case '4' : SectorsCnt
= 40 ; break ;
954 default : SectorsCnt
= 16 ;
958 blockNo
= param_get8 ( Cmd
, 0 );
960 ctmp
= param_getchar ( Cmd
, 1 );
972 PrintAndLog ( "Key type must be A , B or ?" );
976 ctmp
= param_getchar ( Cmd
, 2 );
977 if ( ctmp
== 't' || ctmp
== 'T' ) transferToEml
= 1 ;
978 else if ( ctmp
== 'd' || ctmp
== 'D' ) createDumpFile
= 1 ;
980 for ( i
= transferToEml
|| createDumpFile
; param_getchar ( Cmd
, 2 + i
); i
++) {
981 if (! param_gethex ( Cmd
, 2 + i
, keyBlock
+ 6 * keycnt
, 12 )) {
982 if ( stKeyBlock
- keycnt
< 2 ) {
983 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
985 PrintAndLog ( "Cannot allocate memory for Keys" );
991 PrintAndLog ( "chk key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
992 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
993 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
997 if ( param_getstr ( Cmd
, 2 + i
, filename
) >= FILE_PATH_SIZE
) {
998 PrintAndLog ( "File name too long" );
1003 if ( ( f
= fopen ( filename
, "r" )) ) {
1004 while ( fgets ( buf
, sizeof ( buf
), f
) ){
1005 if ( strlen ( buf
) < 12 || buf
[ 11 ] == ' \n ' )
1008 while ( fgetc ( f
) != ' \n ' && ! feof ( f
)) ; //goto next line
1010 if ( buf
[ 0 ]== '#' ) continue ; //The line start with # is comment, skip
1012 if (! isxdigit ( buf
[ 0 ])){
1013 PrintAndLog ( "File content error. '%s' must include 12 HEX symbols" , buf
);
1019 if ( stKeyBlock
- keycnt
< 2 ) {
1020 p
= realloc ( keyBlock
, 6 *( stKeyBlock
+= 10 ));
1022 PrintAndLog ( "Cannot allocate memory for defKeys" );
1029 memset ( keyBlock
+ 6 * keycnt
, 0 , 6 );
1030 num_to_bytes ( strtoll ( buf
, NULL
, 16 ), 6 , keyBlock
+ 6 * keycnt
);
1031 PrintAndLog ( "chk custom key[%2d] %012" PRIx64
, keycnt
, bytes_to_num ( keyBlock
+ 6 * keycnt
, 6 ));
1033 memset ( buf
, 0 , sizeof ( buf
));
1037 PrintAndLog ( "File: %s: not found or locked." , filename
);
1046 PrintAndLog ( "No key specified, trying default keys" );
1047 for (; keycnt
< defaultKeysSize
; keycnt
++)
1048 PrintAndLog ( "chk default key[%2d] %02x%02x%02x%02x%02x%02x" , keycnt
,
1049 ( keyBlock
+ 6 * keycnt
)[ 0 ],( keyBlock
+ 6 * keycnt
)[ 1 ], ( keyBlock
+ 6 * keycnt
)[ 2 ],
1050 ( keyBlock
+ 6 * keycnt
)[ 3 ], ( keyBlock
+ 6 * keycnt
)[ 4 ], ( keyBlock
+ 6 * keycnt
)[ 5 ], 6 );
1053 // initialize storage for found keys
1054 bool validKey
[ 2 ][ 40 ];
1055 uint8_t foundKey
[ 2 ][ 40 ][ 6 ];
1056 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1057 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1058 validKey
[ t
][ sectorNo
] = false ;
1059 for ( uint16_t i
= 0 ; i
< 6 ; i
++) {
1060 foundKey
[ t
][ sectorNo
][ i
] = 0xff ;
1065 for ( int t
= ! keyType
; t
< 2 ; keyType
== 2 ?( t
++):( t
= 2 ) ) {
1067 for ( int i
= 0 ; i
< SectorsCnt
; ++ i
) {
1068 PrintAndLog ( "--sector:%2d, block:%3d, key type:%C, key count:%2d " , i
, b
, t
? 'B' : 'A' , keycnt
);
1069 uint32_t max_keys
= keycnt
> USB_CMD_DATA_SIZE
/ 6 ? USB_CMD_DATA_SIZE
/ 6 : keycnt
;
1070 for ( uint32_t c
= 0 ; c
< keycnt
; c
+= max_keys
) {
1071 uint32_t size
= keycnt
- c
> max_keys
? max_keys
: keycnt
- c
;
1072 res
= mfCheckKeys ( b
, t
, true , size
, & keyBlock
[ 6 * c
], & key64
);
1075 PrintAndLog ( "Found valid key:[%012" PRIx64
"]" , key64
);
1076 num_to_bytes ( key64
, 6 , foundKey
[ t
][ i
]);
1077 validKey
[ t
][ i
] = true ;
1080 PrintAndLog ( "Command execute timeout" );
1083 b
< 127 ?( b
+= 4 ):( b
+= 16 );
1087 if ( transferToEml
) {
1089 for ( uint16_t sectorNo
= 0 ; sectorNo
< SectorsCnt
; sectorNo
++) {
1090 if ( validKey
[ 0 ][ sectorNo
] || validKey
[ 1 ][ sectorNo
]) {
1091 mfEmlGetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1092 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1093 if ( validKey
[ t
][ sectorNo
]) {
1094 memcpy ( block
+ t
* 10 , foundKey
[ t
][ sectorNo
], 6 );
1097 mfEmlSetMem ( block
, FirstBlockOfSector ( sectorNo
) + NumBlocksPerSector ( sectorNo
) - 1 , 1 );
1100 PrintAndLog ( "Found keys have been transferred to the emulator memory" );
1103 if ( createDumpFile
) {
1104 FILE * fkeys
= fopen ( "dumpkeys.bin" , "wb" );
1105 if ( fkeys
== NULL
) {
1106 PrintAndLog ( "Could not create file dumpkeys.bin" );
1110 for ( uint16_t t
= 0 ; t
< 2 ; t
++) {
1111 fwrite ( foundKey
[ t
], 1 , 6 * SectorsCnt
, fkeys
);
1114 PrintAndLog ( "Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys." );
1122 void readerAttack ( nonces_t ar_resp
[], bool setEmulatorMem
, bool doStandardAttack
) {
1123 #define ATTACK_KEY_COUNT 8 // keep same as define in iso14443a.c -> Mifare1ksim()
1129 st_t sector_trailer
[ ATTACK_KEY_COUNT
];
1130 memset ( sector_trailer
, 0x00 , sizeof ( sector_trailer
));
1132 uint8_t stSector
[ ATTACK_KEY_COUNT
];
1133 memset ( stSector
, 0x00 , sizeof ( stSector
));
1134 uint8_t key_cnt
[ ATTACK_KEY_COUNT
];
1135 memset ( key_cnt
, 0x00 , sizeof ( key_cnt
));
1137 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1138 if ( ar_resp
[ i
]. ar2
> 0 ) {
1139 //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);
1140 if ( doStandardAttack
&& mfkey32 ( ar_resp
[ i
], & key
)) {
1141 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 ));
1143 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1144 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== ar_resp
[ i
]. sector
) {
1145 if ( ar_resp
[ i
]. keytype
== 0 ) {
1147 sector_trailer
[ ii
]. keyA
= key
;
1148 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1153 sector_trailer
[ ii
]. keyB
= key
;
1154 stSector
[ ii
] = ar_resp
[ i
]. sector
;
1160 } else if ( mfkey32_moebius ( ar_resp
[ i
+ ATTACK_KEY_COUNT
], & key
)) {
1161 uint8_t sectorNum
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. sector
;
1162 uint8_t keyType
= ar_resp
[ i
+ ATTACK_KEY_COUNT
]. keytype
;
1164 PrintAndLog ( "M-Found Key%s for sector %02d: [%012" PRIx64
"]"
1165 , keyType
? "B" : "A"
1170 for ( uint8_t ii
= 0 ; ii
< ATTACK_KEY_COUNT
; ii
++) {
1171 if ( key_cnt
[ ii
]== 0 || stSector
[ ii
]== sectorNum
) {
1174 sector_trailer
[ ii
]. keyA
= key
;
1175 stSector
[ ii
] = sectorNum
;
1180 sector_trailer
[ ii
]. keyB
= key
;
1181 stSector
[ ii
] = sectorNum
;
1191 //set emulator memory for keys
1192 if ( setEmulatorMem
) {
1193 for ( uint8_t i
= 0 ; i
< ATTACK_KEY_COUNT
; i
++) {
1195 uint8_t memBlock
[ 16 ];
1196 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1198 memset ( cmd1
, 0x00 , sizeof ( cmd1
));
1199 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 ));
1200 PrintAndLog ( "Setting Emulator Memory Block %02d: [%s]" , stSector
[ i
]* 4 + 3 , cmd1
);
1201 if ( param_gethex ( cmd1
, 0 , memBlock
, 32 )) {
1202 PrintAndLog ( "block data must include 32 HEX symbols" );
1206 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, {( stSector
[ i
]* 4 + 3 ), 1 , 0 }};
1207 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1208 clearCommandBuffer ();
1214 //un-comment to use as well moebius attack
1215 for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {
1216 if (ar_resp[i].ar2 > 0) {
1217 if (tryMfk32_moebius(ar_resp[i], &key)) {
1218 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));
1224 int usage_hf14_mf1ksim ( void ) {
1225 PrintAndLog ( "Usage: hf mf sim h u <uid (8, 14, or 20 hex symbols)> n <numreads> i x" );
1226 PrintAndLog ( "options:" );
1227 PrintAndLog ( " h this help" );
1228 PrintAndLog ( " u (Optional) UID 4,7 or 10 bytes. If not specified, the UID 4B from emulator memory will be used" );
1229 PrintAndLog ( " n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite" );
1230 PrintAndLog ( " i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted" );
1231 PrintAndLog ( " x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)" );
1232 PrintAndLog ( " e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)" );
1233 PrintAndLog ( " f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)" );
1234 PrintAndLog ( " r (Optional) Generate random nonces instead of sequential nonces. Standard reader attack won't work with this option, only moebius attack works." );
1235 PrintAndLog ( "samples:" );
1236 PrintAndLog ( " hf mf sim u 0a0a0a0a" );
1237 PrintAndLog ( " hf mf sim u 11223344556677" );
1238 PrintAndLog ( " hf mf sim u 112233445566778899AA" );
1239 PrintAndLog ( " hf mf sim f uids.txt" );
1240 PrintAndLog ( " hf mf sim u 0a0a0a0a e" );
1245 int CmdHF14AMf1kSim ( const char * Cmd
) {
1247 uint8_t uid
[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1248 uint8_t exitAfterNReads
= 0 ;
1252 bool setEmulatorMem
= false ;
1253 bool attackFromFile
= false ;
1255 char filename
[ FILE_PATH_SIZE
];
1256 memset ( filename
, 0x00 , sizeof ( filename
));
1261 bool errors
= false ;
1263 while ( param_getchar ( Cmd
, cmdp
) != 0x00 ) {
1264 switch ( param_getchar ( Cmd
, cmdp
)) {
1267 setEmulatorMem
= true ;
1269 flags
|= FLAG_INTERACTIVE
;
1270 flags
|= FLAG_NR_AR_ATTACK
;
1275 len
= param_getstr ( Cmd
, cmdp
+ 1 , filename
);
1277 PrintAndLog ( "error no filename found" );
1280 attackFromFile
= true ;
1282 flags
|= FLAG_INTERACTIVE
;
1283 flags
|= FLAG_NR_AR_ATTACK
;
1288 return usage_hf14_mf1ksim ();
1291 flags
|= FLAG_INTERACTIVE
;
1296 exitAfterNReads
= param_get8 ( Cmd
, pnr
+ 1 );
1301 flags
|= FLAG_RANDOM_NONCE
;
1306 param_gethex_ex ( Cmd
, cmdp
+ 1 , uid
, & uidlen
);
1308 case 20 : flags
= FLAG_10B_UID_IN_DATA
; break ; //not complete
1309 case 14 : flags
= FLAG_7B_UID_IN_DATA
; break ;
1310 case 8 : flags
= FLAG_4B_UID_IN_DATA
; break ;
1311 default : return usage_hf14_mf1ksim ();
1317 flags
|= FLAG_NR_AR_ATTACK
;
1321 PrintAndLog ( "Unknown parameter '%c'" , param_getchar ( Cmd
, cmdp
));
1328 if ( errors
) return usage_hf14_mf1ksim ();
1331 if ( attackFromFile
) {
1334 f
= fopen ( filename
, "r" );
1336 PrintAndLog ( "File %s not found or locked" , filename
);
1339 PrintAndLog ( "Loading file and simulating. Press keyboard to abort" );
1340 while (! feof ( f
) && ! ukbhit ()){
1341 memset ( buf
, 0 , sizeof ( buf
));
1342 memset ( uid
, 0 , sizeof ( uid
));
1344 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1345 if ( count
> 0 ) break ;
1347 PrintAndLog ( "File reading error." );
1351 if (! strlen ( buf
) && feof ( f
)) break ;
1353 uidlen
= strlen ( buf
)- 1 ;
1355 case 20 : flags
|= FLAG_10B_UID_IN_DATA
; break ; //not complete
1356 case 14 : flags
|= FLAG_7B_UID_IN_DATA
; break ;
1357 case 8 : flags
|= FLAG_4B_UID_IN_DATA
; break ;
1359 PrintAndLog ( "uid in file wrong length at %d (length: %d) [%s]" , count
, uidlen
, buf
);
1364 for ( uint8_t i
= 0 ; i
< uidlen
; i
+= 2 ) {
1365 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& uid
[ i
/ 2 ]);
1368 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort" ,
1369 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1370 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1371 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1372 , exitAfterNReads
, flags
, flags
);
1374 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1375 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1376 clearCommandBuffer ();
1379 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1380 //We're waiting only 1.5 s at a time, otherwise we get the
1381 // annoying message about "Waiting for a response... "
1384 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1385 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1386 // We can skip the standard attack if we have RANDOM_NONCE set.
1387 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1388 if (( bool ) resp
. arg
[ 1 ]) {
1389 PrintAndLog ( "Device button pressed - quitting" );
1396 } else { //not from file
1398 PrintAndLog ( "mf 1k sim uid: %s, numreads:%d, flags:%d (0x%02x) " ,
1399 flags
& FLAG_4B_UID_IN_DATA
? sprint_hex ( uid
, 4 ):
1400 flags
& FLAG_7B_UID_IN_DATA
? sprint_hex ( uid
, 7 ):
1401 flags
& FLAG_10B_UID_IN_DATA
? sprint_hex ( uid
, 10 ): "N/A"
1402 , exitAfterNReads
, flags
, flags
);
1404 UsbCommand c
= { CMD_SIMULATE_MIFARE_CARD
, { flags
, exitAfterNReads
, 0 }};
1405 memcpy ( c
. d
. asBytes
, uid
, sizeof ( uid
));
1406 clearCommandBuffer ();
1409 if ( flags
& FLAG_INTERACTIVE
) {
1410 PrintAndLog ( "Press pm3-button to abort simulation" );
1411 while (! WaitForResponseTimeout ( CMD_ACK
,& resp
, 1500 )) {
1412 //We're waiting only 1.5 s at a time, otherwise we get the
1413 // annoying message about "Waiting for a response... "
1416 if ( flags
& FLAG_NR_AR_ATTACK
) {
1417 nonces_t ar_resp
[ ATTACK_KEY_COUNT
* 2 ];
1418 memcpy ( ar_resp
, resp
. d
. asBytes
, sizeof ( ar_resp
));
1419 // We can skip the standard attack if we have RANDOM_NONCE set.
1420 readerAttack ( ar_resp
, setEmulatorMem
, !( flags
& FLAG_RANDOM_NONCE
));
1428 int CmdHF14AMfDbg ( const char * Cmd
)
1430 int dbgMode
= param_get32ex ( Cmd
, 0 , 0 , 10 );
1432 PrintAndLog ( "Max debug mode parameter is 4 \n " );
1435 if ( strlen ( Cmd
) < 1 || ! param_getchar ( Cmd
, 0 ) || dbgMode
> 4 ) {
1436 PrintAndLog ( "Usage: hf mf dbg <debug level>" );
1437 PrintAndLog ( " 0 - no debug messages" );
1438 PrintAndLog ( " 1 - error messages" );
1439 PrintAndLog ( " 2 - plus information messages" );
1440 PrintAndLog ( " 3 - plus debug messages" );
1441 PrintAndLog ( " 4 - print even debug messages in timing critical functions" );
1442 PrintAndLog ( " Note: this option therefore may cause malfunction itself" );
1446 UsbCommand c
= { CMD_MIFARE_SET_DBGMODE
, { dbgMode
, 0 , 0 }};
1452 int CmdHF14AMfEGet ( const char * Cmd
)
1454 uint8_t blockNo
= 0 ;
1455 uint8_t data
[ 16 ] = { 0x00 };
1457 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1458 PrintAndLog ( "Usage: hf mf eget <block number>" );
1459 PrintAndLog ( " sample: hf mf eget 0 " );
1463 blockNo
= param_get8 ( Cmd
, 0 );
1466 if (! mfEmlGetMem ( data
, blockNo
, 1 )) {
1467 PrintAndLog ( "data[%3d]:%s" , blockNo
, sprint_hex ( data
, 16 ));
1469 PrintAndLog ( "Command execute timeout" );
1475 int CmdHF14AMfEClear ( const char * Cmd
)
1477 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1478 PrintAndLog ( "Usage: hf mf eclr" );
1479 PrintAndLog ( "It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n " );
1483 UsbCommand c
= { CMD_MIFARE_EML_MEMCLR
, { 0 , 0 , 0 }};
1489 int CmdHF14AMfESet ( const char * Cmd
)
1491 uint8_t memBlock
[ 16 ];
1492 uint8_t blockNo
= 0 ;
1494 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1496 if ( strlen ( Cmd
) < 3 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1497 PrintAndLog ( "Usage: hf mf eset <block number> <block data (32 hex symbols)>" );
1498 PrintAndLog ( " sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f " );
1502 blockNo
= param_get8 ( Cmd
, 0 );
1504 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1505 PrintAndLog ( "block data must include 32 HEX symbols" );
1510 UsbCommand c
= { CMD_MIFARE_EML_MEMSET
, { blockNo
, 1 , 0 }};
1511 memcpy ( c
. d
. asBytes
, memBlock
, 16 );
1517 int CmdHF14AMfELoad ( const char * Cmd
)
1520 char filename
[ FILE_PATH_SIZE
];
1521 char * fnameptr
= filename
;
1522 char buf
[ 64 ] = { 0x00 };
1523 uint8_t buf8
[ 64 ] = { 0x00 };
1524 int i
, len
, blockNum
, numBlocks
;
1525 int nameParamNo
= 1 ;
1527 char ctmp
= param_getchar ( Cmd
, 0 );
1529 if ( ctmp
== 'h' || ctmp
== 0x00 ) {
1530 PrintAndLog ( "It loads emul dump from the file `filename.eml`" );
1531 PrintAndLog ( "Usage: hf mf eload [card memory] <file name w/o `.eml`>" );
1532 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1534 PrintAndLog ( " sample: hf mf eload filename" );
1535 PrintAndLog ( " hf mf eload 4 filename" );
1540 case '0' : numBlocks
= 5 * 4 ; break ;
1542 case '\0' : numBlocks
= 16 * 4 ; break ;
1543 case '2' : numBlocks
= 32 * 4 ; break ;
1544 case '4' : numBlocks
= 256 ; break ;
1551 len
= param_getstr ( Cmd
, nameParamNo
, filename
);
1553 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1557 sprintf ( fnameptr
, ".eml" );
1560 f
= fopen ( filename
, "r" );
1562 PrintAndLog ( "File %s not found or locked" , filename
);
1568 memset ( buf
, 0 , sizeof ( buf
));
1570 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1572 if ( blockNum
>= numBlocks
) break ;
1574 PrintAndLog ( "File reading error." );
1579 if ( strlen ( buf
) < 32 ){
1580 if ( strlen ( buf
) && feof ( f
))
1582 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1587 for ( i
= 0 ; i
< 32 ; i
+= 2 ) {
1588 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1591 if ( mfEmlSetMem ( buf8
, blockNum
, 1 )) {
1592 PrintAndLog ( "Cant set emul block: %3d" , blockNum
);
1599 if ( blockNum
>= numBlocks
) break ;
1604 if (( blockNum
!= numBlocks
)) {
1605 PrintAndLog ( "File content error. Got %d must be %d blocks." , blockNum
, numBlocks
);
1608 PrintAndLog ( "Loaded %d blocks from file: %s" , blockNum
, filename
);
1613 int CmdHF14AMfESave ( const char * Cmd
)
1616 char filename
[ FILE_PATH_SIZE
];
1617 char * fnameptr
= filename
;
1619 int i
, j
, len
, numBlocks
;
1620 int nameParamNo
= 1 ;
1622 memset ( filename
, 0 , sizeof ( filename
));
1623 memset ( buf
, 0 , sizeof ( buf
));
1625 char ctmp
= param_getchar ( Cmd
, 0 );
1627 if ( ctmp
== 'h' || ctmp
== 'H' ) {
1628 PrintAndLog ( "It saves emul dump into the file `filename.eml` or `cardID.eml`" );
1629 PrintAndLog ( " Usage: hf mf esave [card memory] [file name w/o `.eml`]" );
1630 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1632 PrintAndLog ( " sample: hf mf esave " );
1633 PrintAndLog ( " hf mf esave 4" );
1634 PrintAndLog ( " hf mf esave 4 filename" );
1639 case '0' : numBlocks
= 5 * 4 ; break ;
1641 case '\0' : numBlocks
= 16 * 4 ; break ;
1642 case '2' : numBlocks
= 32 * 4 ; break ;
1643 case '4' : numBlocks
= 256 ; break ;
1650 len
= param_getstr ( Cmd
, nameParamNo
, filename
);
1652 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1654 // user supplied filename?
1656 // get filename (UID from memory)
1657 if ( mfEmlGetMem ( buf
, 0 , 1 )) {
1658 PrintAndLog ( "Can \' t get UID from block: %d" , 0 );
1659 len
= sprintf ( fnameptr
, "dump" );
1663 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
1664 sprintf ( fnameptr
, "%02X" , buf
[ j
]);
1670 // add file extension
1671 sprintf ( fnameptr
, ".eml" );
1674 f
= fopen ( filename
, "w+" );
1677 PrintAndLog ( "Can't open file %s " , filename
);
1682 for ( i
= 0 ; i
< numBlocks
; i
++) {
1683 if ( mfEmlGetMem ( buf
, i
, 1 )) {
1684 PrintAndLog ( "Cant get block: %d" , i
);
1687 for ( j
= 0 ; j
< 16 ; j
++)
1688 fprintf ( f
, "%02X" , buf
[ j
]);
1693 PrintAndLog ( "Saved %d blocks to file: %s" , numBlocks
, filename
);
1699 int CmdHF14AMfECFill ( const char * Cmd
)
1701 uint8_t keyType
= 0 ;
1702 uint8_t numSectors
= 16 ;
1704 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1705 PrintAndLog ( "Usage: hf mf ecfill <key A/B> [card memory]" );
1706 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1708 PrintAndLog ( "samples: hf mf ecfill A" );
1709 PrintAndLog ( " hf mf ecfill A 4" );
1710 PrintAndLog ( "Read card and transfer its data to emulator memory." );
1711 PrintAndLog ( "Keys must be laid in the emulator memory. \n " );
1715 char ctmp
= param_getchar ( Cmd
, 0 );
1716 if ( ctmp
!= 'a' && ctmp
!= 'A' && ctmp
!= 'b' && ctmp
!= 'B' ) {
1717 PrintAndLog ( "Key type must be A or B" );
1720 if ( ctmp
!= 'A' && ctmp
!= 'a' ) keyType
= 1 ;
1722 ctmp
= param_getchar ( Cmd
, 1 );
1724 case '0' : numSectors
= 5 ; break ;
1726 case '\0' : numSectors
= 16 ; break ;
1727 case '2' : numSectors
= 32 ; break ;
1728 case '4' : numSectors
= 40 ; break ;
1729 default : numSectors
= 16 ;
1732 printf ( "--params: numSectors: %d, keyType:%d" , numSectors
, keyType
);
1733 UsbCommand c
= { CMD_MIFARE_EML_CARDLOAD
, { numSectors
, keyType
, 0 }};
1739 int CmdHF14AMfEKeyPrn ( const char * Cmd
)
1744 uint64_t keyA
, keyB
;
1746 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
1747 PrintAndLog ( "It prints the keys loaded in the emulator memory" );
1748 PrintAndLog ( "Usage: hf mf ekeyprn [card memory]" );
1749 PrintAndLog ( " [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K" );
1751 PrintAndLog ( " sample: hf mf ekeyprn 1" );
1755 char cmdp
= param_getchar ( Cmd
, 0 );
1758 case '0' : numSectors
= 5 ; break ;
1760 case '\0' : numSectors
= 16 ; break ;
1761 case '2' : numSectors
= 32 ; break ;
1762 case '4' : numSectors
= 40 ; break ;
1763 default : numSectors
= 16 ;
1766 PrintAndLog ( "|---|----------------|----------------|" );
1767 PrintAndLog ( "|sec|key A |key B |" );
1768 PrintAndLog ( "|---|----------------|----------------|" );
1769 for ( i
= 0 ; i
< numSectors
; i
++) {
1770 if ( mfEmlGetMem ( data
, FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 , 1 )) {
1771 PrintAndLog ( "error get block %d" , FirstBlockOfSector ( i
) + NumBlocksPerSector ( i
) - 1 );
1774 keyA
= bytes_to_num ( data
, 6 );
1775 keyB
= bytes_to_num ( data
+ 10 , 6 );
1776 PrintAndLog ( "|%03d| %012" PRIx64
" | %012" PRIx64
" |" , i
, keyA
, keyB
);
1778 PrintAndLog ( "|---|----------------|----------------|" );
1784 int CmdHF14AMfCSetUID ( const char * Cmd
)
1786 uint8_t wipeCard
= 0 ;
1787 uint8_t uid
[ 8 ] = { 0x00 };
1788 uint8_t oldUid
[ 8 ] = { 0x00 };
1789 uint8_t atqa
[ 2 ] = { 0x00 };
1790 uint8_t sak
[ 1 ] = { 0x00 };
1791 uint8_t atqaPresent
= 1 ;
1796 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, argi
) == 'h' ) {
1797 PrintAndLog ( "Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]" );
1798 PrintAndLog ( "sample: hf mf csetuid 01020304" );
1799 PrintAndLog ( "sample: hf mf csetuid 01020304 0004 08 w" );
1800 PrintAndLog ( "Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)" );
1801 PrintAndLog ( "If you also want to wipe the card then add 'w' at the end of the command line." );
1805 if ( param_getchar ( Cmd
, argi
) && param_gethex ( Cmd
, argi
, uid
, 8 )) {
1806 PrintAndLog ( "UID must include 8 HEX symbols" );
1811 ctmp
= param_getchar ( Cmd
, argi
);
1812 if ( ctmp
== 'w' || ctmp
== 'W' ) {
1818 if ( param_getchar ( Cmd
, argi
)) {
1819 if ( param_gethex ( Cmd
, argi
, atqa
, 4 )) {
1820 PrintAndLog ( "ATQA must include 4 HEX symbols" );
1824 if (! param_getchar ( Cmd
, argi
) || param_gethex ( Cmd
, argi
, sak
, 2 )) {
1825 PrintAndLog ( "SAK must include 2 HEX symbols" );
1834 ctmp
= param_getchar ( Cmd
, argi
);
1835 if ( ctmp
== 'w' || ctmp
== 'W' ) {
1840 PrintAndLog ( "--wipe card:%s uid:%s" , ( wipeCard
)? "YES" : "NO" , sprint_hex ( uid
, 4 ));
1842 res
= mfCSetUID ( uid
, ( atqaPresent
)? atqa
: NULL
, ( atqaPresent
)? sak
: NULL
, oldUid
, wipeCard
);
1844 PrintAndLog ( "Can't set UID. error=%d" , res
);
1848 PrintAndLog ( "old UID:%s" , sprint_hex ( oldUid
, 4 ));
1849 PrintAndLog ( "new UID:%s" , sprint_hex ( uid
, 4 ));
1853 int CmdHF14AMfCSetBlk ( const char * Cmd
)
1855 uint8_t memBlock
[ 16 ] = { 0x00 };
1856 uint8_t blockNo
= 0 ;
1857 bool wipeCard
= false ;
1860 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1861 PrintAndLog ( "Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]" );
1862 PrintAndLog ( "sample: hf mf csetblk 1 01020304050607080910111213141516" );
1863 PrintAndLog ( "Set block data for magic Chinese card (only works with such cards)" );
1864 PrintAndLog ( "If you also want wipe the card then add 'w' at the end of the command line" );
1868 blockNo
= param_get8 ( Cmd
, 0 );
1870 if ( param_gethex ( Cmd
, 1 , memBlock
, 32 )) {
1871 PrintAndLog ( "block data must include 32 HEX symbols" );
1875 char ctmp
= param_getchar ( Cmd
, 2 );
1876 wipeCard
= ( ctmp
== 'w' || ctmp
== 'W' );
1877 PrintAndLog ( "--block number:%2d data:%s" , blockNo
, sprint_hex ( memBlock
, 16 ));
1879 res
= mfCSetBlock ( blockNo
, memBlock
, NULL
, wipeCard
, CSETBLOCK_SINGLE_OPER
);
1881 PrintAndLog ( "Can't write block. error=%d" , res
);
1888 int CmdHF14AMfCLoad ( const char * Cmd
)
1891 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
1892 char * fnameptr
= filename
;
1893 char buf
[ 64 ] = { 0x00 };
1894 uint8_t buf8
[ 64 ] = { 0x00 };
1895 uint8_t fillFromEmulator
= 0 ;
1896 int i
, len
, blockNum
, flags
= 0 ;
1898 if ( param_getchar ( Cmd
, 0 ) == 'h' || param_getchar ( Cmd
, 0 )== 0x00 ) {
1899 PrintAndLog ( "It loads magic Chinese card from the file `filename.eml`" );
1900 PrintAndLog ( "or from emulator memory (option `e`)" );
1901 PrintAndLog ( "Usage: hf mf cload <file name w/o `.eml`>" );
1902 PrintAndLog ( " or: hf mf cload e " );
1903 PrintAndLog ( " sample: hf mf cload filename" );
1907 char ctmp
= param_getchar ( Cmd
, 0 );
1908 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
1910 if ( fillFromEmulator
) {
1911 for ( blockNum
= 0 ; blockNum
< 16 * 4 ; blockNum
+= 1 ) {
1912 if ( mfEmlGetMem ( buf8
, blockNum
, 1 )) {
1913 PrintAndLog ( "Cant get block: %d" , blockNum
);
1916 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1917 if ( blockNum
== 1 ) flags
= 0 ; // just write
1918 if ( blockNum
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Magic Halt and switch off field.
1920 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
1921 PrintAndLog ( "Cant set magic card block: %d" , blockNum
);
1928 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
1930 memcpy ( filename
, Cmd
, len
);
1933 sprintf ( fnameptr
, ".eml" );
1936 f
= fopen ( filename
, "r" );
1938 PrintAndLog ( "File not found or locked." );
1945 memset ( buf
, 0 , sizeof ( buf
));
1947 if ( fgets ( buf
, sizeof ( buf
), f
) == NULL
) {
1949 PrintAndLog ( "File reading error." );
1953 if ( strlen ( buf
) < 32 ) {
1954 if ( strlen ( buf
) && feof ( f
))
1956 PrintAndLog ( "File content error. Block data must include 32 HEX symbols" );
1960 for ( i
= 0 ; i
< 32 ; i
+= 2 )
1961 sscanf (& buf
[ i
], "%02x" , ( unsigned int *)& buf8
[ i
/ 2 ]);
1963 if ( blockNum
== 0 ) flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
; // switch on field and send magic sequence
1964 if ( blockNum
== 1 ) flags
= 0 ; // just write
1965 if ( blockNum
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
; // Done. Switch off field.
1967 if ( mfCSetBlock ( blockNum
, buf8
, NULL
, 0 , flags
)) {
1968 PrintAndLog ( "Can't set magic card block: %d" , blockNum
);
1974 if ( blockNum
>= 16 * 4 ) break ; // magic card type - mifare 1K
1978 if ( blockNum
!= 16 * 4 && blockNum
!= 32 * 4 + 8 * 16 ){
1979 PrintAndLog ( "File content error. There must be 64 blocks" );
1982 PrintAndLog ( "Loaded from file: %s" , filename
);
1988 int CmdHF14AMfCGetBlk ( const char * Cmd
) {
1989 uint8_t memBlock
[ 16 ];
1990 uint8_t blockNo
= 0 ;
1992 memset ( memBlock
, 0x00 , sizeof ( memBlock
));
1994 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
1995 PrintAndLog ( "Usage: hf mf cgetblk <block number>" );
1996 PrintAndLog ( "sample: hf mf cgetblk 1" );
1997 PrintAndLog ( "Get block data from magic Chinese card (only works with such cards) \n " );
2001 blockNo
= param_get8 ( Cmd
, 0 );
2003 PrintAndLog ( "--block number:%2d " , blockNo
);
2005 res
= mfCGetBlock ( blockNo
, memBlock
, CSETBLOCK_SINGLE_OPER
);
2007 PrintAndLog ( "Can't read block. error=%d" , res
);
2011 PrintAndLog ( "block data:%s" , sprint_hex ( memBlock
, 16 ));
2016 int CmdHF14AMfCGetSc ( const char * Cmd
) {
2017 uint8_t memBlock
[ 16 ] = { 0x00 };
2018 uint8_t sectorNo
= 0 ;
2021 if ( strlen ( Cmd
) < 1 || param_getchar ( Cmd
, 0 ) == 'h' ) {
2022 PrintAndLog ( "Usage: hf mf cgetsc <sector number>" );
2023 PrintAndLog ( "sample: hf mf cgetsc 0" );
2024 PrintAndLog ( "Get sector data from magic Chinese card (only works with such cards) \n " );
2028 sectorNo
= param_get8 ( Cmd
, 0 );
2029 if ( sectorNo
> 15 ) {
2030 PrintAndLog ( "Sector number must be in [0..15] as in MIFARE classic." );
2034 PrintAndLog ( "--sector number:%d " , sectorNo
);
2036 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2037 for ( i
= 0 ; i
< 4 ; i
++) {
2038 if ( i
== 1 ) flags
= 0 ;
2039 if ( i
== 3 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2041 res
= mfCGetBlock ( sectorNo
* 4 + i
, memBlock
, flags
);
2043 PrintAndLog ( "Can't read block. %d error=%d" , sectorNo
* 4 + i
, res
);
2047 PrintAndLog ( "block %3d data:%s" , sectorNo
* 4 + i
, sprint_hex ( memBlock
, 16 ));
2053 int CmdHF14AMfCSave ( const char * Cmd
) {
2056 char filename
[ FILE_PATH_SIZE
] = { 0x00 };
2057 char * fnameptr
= filename
;
2058 uint8_t fillFromEmulator
= 0 ;
2059 uint8_t buf
[ 64 ] = { 0x00 };
2060 int i
, j
, len
, flags
;
2062 // memset(filename, 0, sizeof(filename));
2063 // memset(buf, 0, sizeof(buf));
2065 if ( param_getchar ( Cmd
, 0 ) == 'h' ) {
2066 PrintAndLog ( "It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`" );
2067 PrintAndLog ( "or into emulator memory (option `e`)" );
2068 PrintAndLog ( "Usage: hf mf esave [file name w/o `.eml`][e]" );
2069 PrintAndLog ( " sample: hf mf esave " );
2070 PrintAndLog ( " hf mf esave filename" );
2071 PrintAndLog ( " hf mf esave e \n " );
2075 char ctmp
= param_getchar ( Cmd
, 0 );
2076 if ( ctmp
== 'e' || ctmp
== 'E' ) fillFromEmulator
= 1 ;
2078 if ( fillFromEmulator
) {
2079 // put into emulator
2080 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2081 for ( i
= 0 ; i
< 16 * 4 ; i
++) {
2082 if ( i
== 1 ) flags
= 0 ;
2083 if ( i
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2085 if ( mfCGetBlock ( i
, buf
, flags
)) {
2086 PrintAndLog ( "Cant get block: %d" , i
);
2090 if ( mfEmlSetMem ( buf
, i
, 1 )) {
2091 PrintAndLog ( "Cant set emul block: %d" , i
);
2098 if ( len
> FILE_PATH_SIZE
- 5 ) len
= FILE_PATH_SIZE
- 5 ;
2102 if ( mfCGetBlock ( 0 , buf
, CSETBLOCK_SINGLE_OPER
)) {
2103 PrintAndLog ( "Cant get block: %d" , 0 );
2104 len
= sprintf ( fnameptr
, "dump" );
2108 for ( j
= 0 ; j
< 7 ; j
++, fnameptr
+= 2 )
2109 sprintf ( fnameptr
, "%02x" , buf
[ j
]);
2112 memcpy ( filename
, Cmd
, len
);
2116 sprintf ( fnameptr
, ".eml" );
2119 f
= fopen ( filename
, "w+" );
2122 PrintAndLog ( "File not found or locked." );
2127 flags
= CSETBLOCK_INIT_FIELD
+ CSETBLOCK_WUPC
;
2128 for ( i
= 0 ; i
< 16 * 4 ; i
++) {
2129 if ( i
== 1 ) flags
= 0 ;
2130 if ( i
== 16 * 4 - 1 ) flags
= CSETBLOCK_HALT
+ CSETBLOCK_RESET_FIELD
;
2132 if ( mfCGetBlock ( i
, buf
, flags
)) {
2133 PrintAndLog ( "Cant get block: %d" , i
);
2136 for ( j
= 0 ; j
< 16 ; j
++)
2137 fprintf ( f
, "%02x" , buf
[ j
]);
2142 PrintAndLog ( "Saved to file: %s" , filename
);
2149 int CmdHF14AMfSniff ( const char * Cmd
){
2151 bool wantLogToFile
= 0 ;
2152 bool wantDecrypt
= 0 ;
2153 //bool wantSaveToEml = 0; TODO
2154 bool wantSaveToEmlFile
= 0 ;
2164 uint8_t atqa
[ 2 ] = { 0x00 };
2167 uint8_t * buf
= NULL
;
2168 uint16_t bufsize
= 0 ;
2169 uint8_t * bufPtr
= NULL
;
2171 char ctmp
= param_getchar ( Cmd
, 0 );
2172 if ( ctmp
== 'h' || ctmp
== 'H' ) {
2173 PrintAndLog ( "It continuously gets data from the field and saves it to: log, emulator, emulator file." );
2174 PrintAndLog ( "You can specify:" );
2175 PrintAndLog ( " l - save encrypted sequence to logfile `uid.log`" );
2176 PrintAndLog ( " d - decrypt sequence and put it to log file `uid.log`" );
2177 PrintAndLog ( " n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory" );
2178 PrintAndLog ( " f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`" );
2179 PrintAndLog ( "Usage: hf mf sniff [l][d][e][f]" );
2180 PrintAndLog ( " sample: hf mf sniff l d e" );
2184 for ( int i
= 0 ; i
< 4 ; i
++) {
2185 ctmp
= param_getchar ( Cmd
, i
);
2186 if ( ctmp
== 'l' || ctmp
== 'L' ) wantLogToFile
= true ;
2187 if ( ctmp
== 'd' || ctmp
== 'D' ) wantDecrypt
= true ;
2188 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2189 if ( ctmp
== 'f' || ctmp
== 'F' ) wantSaveToEmlFile
= true ;
2192 printf ( "------------------------------------------------------------------------- \n " );
2193 printf ( "Executing command. \n " );
2194 printf ( "Press the key on the proxmark3 device to abort both proxmark3 and client. \n " );
2195 printf ( "Press the key on pc keyboard to abort the client. \n " );
2196 printf ( "------------------------------------------------------------------------- \n " );
2198 UsbCommand c
= { CMD_MIFARE_SNIFFER
, { 0 , 0 , 0 }};
2199 clearCommandBuffer ();
2208 printf ( " \n aborted via keyboard! \n " );
2213 if ( WaitForResponseTimeout ( CMD_ACK
,& resp
, 2000 )) {
2214 res
= resp
. arg
[ 0 ] & 0xff ;
2215 uint16_t traceLen
= resp
. arg
[ 1 ];
2218 if ( res
== 0 ) { // we are done
2223 if ( res
== 1 ) { // there is (more) data to be transferred
2224 if ( pckNum
== 0 ) { // first packet, (re)allocate necessary buffer
2225 if ( traceLen
> bufsize
|| buf
== NULL
) {
2227 if ( buf
== NULL
) { // not yet allocated
2228 p
= malloc ( traceLen
);
2229 } else { // need more memory
2230 p
= realloc ( buf
, traceLen
);
2233 PrintAndLog ( "Cannot allocate memory for trace" );
2241 memset ( buf
, 0x00 , traceLen
);
2243 memcpy ( bufPtr
, resp
. d
. asBytes
, len
);
2248 if ( res
== 2 ) { // received all data, start displaying
2249 blockLen
= bufPtr
- buf
;
2252 PrintAndLog ( "received trace len: %d packages: %d" , blockLen
, pckNum
);
2253 while ( bufPtr
- buf
< blockLen
) {
2254 bufPtr
+= 6 ; // skip (void) timing information
2255 len
= *(( uint16_t *) bufPtr
);
2263 if (( len
== 14 ) && ( bufPtr
[ 0 ] == 0xff ) && ( bufPtr
[ 1 ] == 0xff ) && ( bufPtr
[ 12 ] == 0xff ) && ( bufPtr
[ 13 ] == 0xff )) {
2264 memcpy ( uid
, bufPtr
+ 2 , 7 );
2265 memcpy ( atqa
, bufPtr
+ 2 + 7 , 2 );
2266 uid_len
= ( atqa
[ 0 ] & 0xC0 ) == 0x40 ? 7 : 4 ;
2268 PrintAndLog ( "tag select uid:%s atqa:0x%02x%02x sak:0x%02x" ,
2269 sprint_hex ( uid
+ ( 7 - uid_len
), uid_len
),
2273 if ( wantLogToFile
|| wantDecrypt
) {
2274 FillFileNameByUID ( logHexFileName
, uid
+ ( 7 - uid_len
), ".log" , uid_len
);
2275 AddLogCurrentDT ( logHexFileName
);
2278 mfTraceInit ( uid
, atqa
, sak
, wantSaveToEmlFile
);
2280 PrintAndLog ( "%s(%d):%s" , isTag
? "TAG" : "RDR" , num
, sprint_hex ( bufPtr
, len
));
2282 AddLogHex ( logHexFileName
, isTag
? "TAG: " : "RDR: " , bufPtr
, len
);
2284 mfTraceDecode ( bufPtr
, len
, wantSaveToEmlFile
);
2288 bufPtr
+= (( len
- 1 )/ 8 + 1 ); // ignore parity
2299 //needs nt, ar, at, Data to decrypt
2300 int CmdDecryptTraceCmds ( const char * Cmd
){
2303 param_gethex_ex ( Cmd
, 3 , data
,& len
);
2304 return tryDecryptWord ( param_get32ex ( Cmd
, 0 , 0 , 16 ), param_get32ex ( Cmd
, 1 , 0 , 16 ), param_get32ex ( Cmd
, 2 , 0 , 16 ), data
, len
/ 2 );
2307 static command_t CommandTable
[] =
2309 { "help" , CmdHelp
, 1 , "This help" },
2310 { "dbg" , CmdHF14AMfDbg
, 0 , "Set default debug mode" },
2311 { "rdbl" , CmdHF14AMfRdBl
, 0 , "Read MIFARE classic block" },
2312 { "rdsc" , CmdHF14AMfRdSc
, 0 , "Read MIFARE classic sector" },
2313 { "dump" , CmdHF14AMfDump
, 0 , "Dump MIFARE classic tag to binary file" },
2314 { "restore" , CmdHF14AMfRestore
, 0 , "Restore MIFARE classic binary file to BLANK tag" },
2315 { "wrbl" , CmdHF14AMfWrBl
, 0 , "Write MIFARE classic block" },
2316 { "chk" , CmdHF14AMfChk
, 0 , "Test block keys" },
2317 { "mifare" , CmdHF14AMifare
, 0 , "Read parity error messages." },
2318 { "hardnested" , CmdHF14AMfNestedHard
, 0 , "Nested attack for hardened Mifare cards" },
2319 { "nested" , CmdHF14AMfNested
, 0 , "Test nested authentication" },
2320 { "sniff" , CmdHF14AMfSniff
, 0 , "Sniff card-reader communication" },
2321 { "sim" , CmdHF14AMf1kSim
, 0 , "Simulate MIFARE card" },
2322 { "eclr" , CmdHF14AMfEClear
, 0 , "Clear simulator memory block" },
2323 { "eget" , CmdHF14AMfEGet
, 0 , "Get simulator memory block" },
2324 { "eset" , CmdHF14AMfESet
, 0 , "Set simulator memory block" },
2325 { "eload" , CmdHF14AMfELoad
, 0 , "Load from file emul dump" },
2326 { "esave" , CmdHF14AMfESave
, 0 , "Save to file emul dump" },
2327 { "ecfill" , CmdHF14AMfECFill
, 0 , "Fill simulator memory with help of keys from simulator" },
2328 { "ekeyprn" , CmdHF14AMfEKeyPrn
, 0 , "Print keys from simulator memory" },
2329 { "csetuid" , CmdHF14AMfCSetUID
, 0 , "Set UID for magic Chinese card" },
2330 { "csetblk" , CmdHF14AMfCSetBlk
, 0 , "Write block - Magic Chinese card" },
2331 { "cgetblk" , CmdHF14AMfCGetBlk
, 0 , "Read block - Magic Chinese card" },
2332 { "cgetsc" , CmdHF14AMfCGetSc
, 0 , "Read sector - Magic Chinese card" },
2333 { "cload" , CmdHF14AMfCLoad
, 0 , "Load dump into magic Chinese card" },
2334 { "csave" , CmdHF14AMfCSave
, 0 , "Save dump from magic Chinese card into file or emulator" },
2335 { "decrypt" , CmdDecryptTraceCmds
, 1 , "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace" },
2336 { NULL
, NULL
, 0 , NULL
}
2339 int CmdHFMF ( const char * Cmd
)
2342 WaitForResponseTimeout ( CMD_ACK
, NULL
, 100 );
2344 CmdsParse ( CommandTable
, Cmd
);
2348 int CmdHelp ( const char * Cmd
)
2350 CmdsHelp ( CommandTable
);