49 #endif // HAVE_CONFIG_H
62 #define SAK_FLAG_ATS_SUPPORTED 0x20
64 #define MAX_FRAME_LEN 264
66 static uint8_t abtRx[MAX_FRAME_LEN];
68 static uint8_t abtRawUid[12];
69 static uint8_t abtAtqa[2];
70 static uint8_t abtSak;
71 static uint8_t abtAts[MAX_FRAME_LEN];
72 static uint8_t szAts = 0;
73 static size_t szCL = 1;
76 bool quiet_output =
false;
77 bool iso_ats_supported =
false;
80 uint8_t abtReqa[1] = { 0x26 };
81 uint8_t abtSelectAll[2] = { 0x93, 0x20 };
82 uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
83 uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 };
84 uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
85 #define CASCADE_BIT 0x04
88 uint8_t abtUnlock1[1] = { 0x40 };
89 uint8_t abtUnlock2[1] = { 0x43 };
90 uint8_t abtWipe[1] = { 0x41 };
91 uint8_t abtWrite[4] = { 0xa0, 0x00, 0x5f, 0xb1 };
92 uint8_t abtData[18] = { 0x01, 0x23, 0x45, 0x67, 0x00, 0x08, 0x04, 0x00, 0x46, 0x59, 0x25, 0x58, 0x49, 0x10, 0x23, 0x02, 0x23, 0xeb };
93 uint8_t abtBlank[18] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0xCC };
97 transmit_bits(
const uint8_t *pbtTx,
const size_t szTxBits)
101 printf(
"Sent bits: ");
102 print_hex_bits(pbtTx, szTxBits);
110 printf(
"Received bits: ");
111 print_hex_bits(abtRx, szRxBits);
119 transmit_bytes(
const uint8_t *pbtTx,
const size_t szTx)
123 printf(
"Sent bits: ");
124 print_hex(pbtTx, szTx);
133 printf(
"Received bits: ");
134 print_hex(abtRx, res);
141 print_usage(
char *argv[])
143 printf(
"Usage: %s [OPTIONS] [UID]\n", argv[0]);
144 printf(
"Options:\n");
145 printf(
"\t-h\tHelp. Print this message.\n");
146 printf(
"\t-f\tFormat. Delete all data (set to 0xFF) and reset ACLs to default.\n");
147 printf(
"\t-q\tQuiet mode. Suppress output of READER and CARD data (improves timing).\n");
148 printf(
"\n\tSpecify UID (4 HEX bytes) to set UID, or leave blank for default '01234567'.\n");
149 printf(
"\tThis utility can be used to recover cards that have been damaged by writing bad\n");
150 printf(
"\tdata (e.g. wrong BCC), thus making them non-selectable by most tools/readers.\n");
151 printf(
"\n\t*** Note: this utility only works with special Mifare 1K cards (Chinese clones).\n\n");
155 main(
int argc,
char *argv[])
160 char tmp[3] = { 0x00, 0x00, 0x00 };
164 for (arg = 1; arg < argc; arg++) {
165 if (0 == strcmp(argv[arg],
"-h")) {
168 }
else if (0 == strcmp(argv[arg],
"-f")) {
170 }
else if (0 == strcmp(argv[arg],
"-q")) {
172 }
else if (strlen(argv[arg]) == 8) {
173 for (i = 0 ; i < 4 ; ++i) {
174 memcpy(tmp, argv[arg] + i * 2, 2);
175 sscanf(tmp,
"%02x", &c);
176 abtData[i] = (char) c;
178 abtData[4] = abtData[0] ^ abtData[1] ^ abtData[2] ^ abtData[3];
179 iso14443a_crc_append(abtData, 16);
181 ERR(
"%s is not supported option.", argv[arg]);
189 if (context == NULL) {
190 ERR(
"Unable to init libnfc (malloc)");
198 ERR(
"Error opening NFC reader");
213 nfc_perror(pnd,
"nfc_device_set_property_bool");
220 nfc_perror(pnd,
"nfc_device_set_property_bool");
227 nfc_perror(pnd,
"nfc_device_set_property_bool");
236 if (!transmit_bits(abtReqa, 7)) {
237 printf(
"Error: No tag available\n");
242 memcpy(abtAtqa, abtRx, 2);
245 transmit_bytes(abtSelectAll, 2);
248 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
249 printf(
"WARNING: BCC check failed!\n");
253 memcpy(abtRawUid, abtRx, 4);
256 memcpy(abtSelectTag + 2, abtRx, 5);
257 iso14443a_crc_append(abtSelectTag, 7);
258 transmit_bytes(abtSelectTag, 9);
262 if (abtSak & CASCADE_BIT) {
265 if (abtRawUid[0] != 0x88) {
266 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
274 abtSelectAll[0] = 0x95;
277 transmit_bytes(abtSelectAll, 2);
280 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
281 printf(
"WARNING: BCC check failed!\n");
285 memcpy(abtRawUid + 4, abtRx, 4);
288 abtSelectTag[0] = 0x95;
289 memcpy(abtSelectTag + 2, abtRx, 5);
290 iso14443a_crc_append(abtSelectTag, 7);
291 transmit_bytes(abtSelectTag, 9);
295 if (abtSak & CASCADE_BIT) {
298 if (abtRawUid[0] != 0x88) {
299 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
307 abtSelectAll[0] = 0x97;
308 transmit_bytes(abtSelectAll, 2);
311 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
312 printf(
"WARNING: BCC check failed!\n");
316 memcpy(abtRawUid + 8, abtRx, 4);
319 abtSelectTag[0] = 0x97;
320 memcpy(abtSelectTag + 2, abtRx, 5);
321 iso14443a_crc_append(abtSelectTag, 7);
322 transmit_bytes(abtSelectTag, 9);
329 iso_ats_supported =
true;
332 printf(
"\nFound tag with\n UID: ");
335 printf(
"%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]);
338 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
339 printf(
"%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]);
342 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
343 printf(
"%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]);
344 printf(
"%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]);
348 printf(
"ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak);
351 print_hex(abtAts, szAts);
356 iso14443a_crc_append(abtHalt, 2);
357 transmit_bytes(abtHalt, 4);
358 transmit_bits(abtUnlock1, 7);
360 transmit_bytes(abtWipe, 1);
361 transmit_bytes(abtHalt, 4);
362 transmit_bits(abtUnlock1, 7);
364 transmit_bytes(abtUnlock2, 1);
365 transmit_bytes(abtWrite, 4);
366 transmit_bytes(abtData, 18);
368 for (i = 3 ; i < 64 ; i += 4) {
369 abtWrite[1] = (char) i;
370 iso14443a_crc_append(abtWrite, 2);
371 transmit_bytes(abtWrite, 4);
372 transmit_bytes(abtBlank, 18);