48 #endif // HAVE_CONFIG_H
61 #define SAK_FLAG_ATS_SUPPORTED 0x20
63 #define MAX_FRAME_LEN 264
65 static uint8_t abtRx[MAX_FRAME_LEN];
67 static uint8_t abtRawUid[12];
68 static uint8_t abtAtqa[2];
69 static uint8_t abtSak;
70 static uint8_t abtAts[MAX_FRAME_LEN];
71 static uint8_t szAts = 0;
72 static size_t szCL = 1;
75 bool quiet_output =
false;
76 bool iso_ats_supported =
false;
79 uint8_t abtReqa[1] = { 0x26 };
80 uint8_t abtSelectAll[2] = { 0x93, 0x20 };
81 uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
82 uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 };
83 uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
84 #define CASCADE_BIT 0x04
87 uint8_t abtUnlock1[1] = { 0x40 };
88 uint8_t abtUnlock2[1] = { 0x43 };
89 uint8_t abtWipe[1] = { 0x41 };
90 uint8_t abtWrite[4] = { 0xa0, 0x00, 0x5f, 0xb1 };
91 uint8_t abtData[18] = { 0x01, 0x23, 0x45, 0x67, 0x00, 0x08, 0x04, 0x00, 0x46, 0x59, 0x25, 0x58, 0x49, 0x10, 0x23, 0x02, 0x23, 0xeb };
92 uint8_t abtBlank[18] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0xCC };
96 transmit_bits(
const uint8_t *pbtTx,
const size_t szTxBits)
100 printf(
"Sent bits: ");
101 print_hex_bits(pbtTx, szTxBits);
109 printf(
"Received bits: ");
110 print_hex_bits(abtRx, szRxBits);
118 transmit_bytes(
const uint8_t *pbtTx,
const size_t szTx)
122 printf(
"Sent bits: ");
123 print_hex(pbtTx, szTx);
132 printf(
"Received bits: ");
133 print_hex(abtRx, res);
140 print_usage(
char *argv[])
142 printf(
"Usage: %s [OPTIONS] [UID]\n", argv[0]);
143 printf(
"Options:\n");
144 printf(
"\t-h\tHelp. Print this message.\n");
145 printf(
"\t-f\tFormat. Delete all data (set to 0xFF) and reset ACLs to default.\n");
146 printf(
"\t-q\tQuiet mode. Suppress output of READER and CARD data (improves timing).\n");
147 printf(
"\n\tSpecify UID (4 HEX bytes) to set UID, or leave blank for default '01234567'.\n");
148 printf(
"\tThis utility can be used to recover cards that have been damaged by writing bad\n");
149 printf(
"\tdata (e.g. wrong BCC), thus making them non-selectable by most tools/readers.\n");
150 printf(
"\n\t*** Note: this utility only works with special Mifare 1K cards (Chinese clones).\n\n");
154 main(
int argc,
char *argv[])
159 char tmp[3] = { 0x00, 0x00, 0x00 };
163 for (arg = 1; arg < argc; arg++) {
164 if (0 == strcmp(argv[arg],
"-h")) {
167 }
else if (0 == strcmp(argv[arg],
"-f")) {
169 }
else if (0 == strcmp(argv[arg],
"-q")) {
171 }
else if (strlen(argv[arg]) == 8) {
172 for (i = 0 ; i < 4 ; ++i) {
173 memcpy(tmp, argv[arg] + i * 2, 2);
174 sscanf(tmp,
"%02x", &c);
175 abtData[i] = (char) c;
177 abtData[4] = abtData[0] ^ abtData[1] ^ abtData[2] ^ abtData[3];
178 iso14443a_crc_append(abtData, 16);
180 ERR(
"%s is not supported option.", argv[arg]);
188 if (context == NULL) {
189 ERR(
"Unable to init libnfc (malloc)");
197 ERR(
"Error opening NFC reader");
212 nfc_perror(pnd,
"nfc_device_set_property_bool");
219 nfc_perror(pnd,
"nfc_device_set_property_bool");
226 nfc_perror(pnd,
"nfc_device_set_property_bool");
235 if (!transmit_bits(abtReqa, 7)) {
236 printf(
"Error: No tag available\n");
241 memcpy(abtAtqa, abtRx, 2);
244 transmit_bytes(abtSelectAll, 2);
247 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
248 printf(
"WARNING: BCC check failed!\n");
252 memcpy(abtRawUid, abtRx, 4);
255 memcpy(abtSelectTag + 2, abtRx, 5);
256 iso14443a_crc_append(abtSelectTag, 7);
257 transmit_bytes(abtSelectTag, 9);
261 if (abtSak & CASCADE_BIT) {
264 if (abtRawUid[0] != 0x88) {
265 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
273 abtSelectAll[0] = 0x95;
276 transmit_bytes(abtSelectAll, 2);
279 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
280 printf(
"WARNING: BCC check failed!\n");
284 memcpy(abtRawUid + 4, abtRx, 4);
287 abtSelectTag[0] = 0x95;
288 memcpy(abtSelectTag + 2, abtRx, 5);
289 iso14443a_crc_append(abtSelectTag, 7);
290 transmit_bytes(abtSelectTag, 9);
294 if (abtSak & CASCADE_BIT) {
297 if (abtRawUid[0] != 0x88) {
298 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
306 abtSelectAll[0] = 0x97;
307 transmit_bytes(abtSelectAll, 2);
310 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
311 printf(
"WARNING: BCC check failed!\n");
315 memcpy(abtRawUid + 8, abtRx, 4);
318 abtSelectTag[0] = 0x97;
319 memcpy(abtSelectTag + 2, abtRx, 5);
320 iso14443a_crc_append(abtSelectTag, 7);
321 transmit_bytes(abtSelectTag, 9);
328 iso_ats_supported =
true;
331 printf(
"\nFound tag with\n UID: ");
334 printf(
"%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]);
337 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
338 printf(
"%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]);
341 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
342 printf(
"%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]);
343 printf(
"%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]);
347 printf(
"ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak);
350 print_hex(abtAts, szAts);
355 iso14443a_crc_append(abtHalt, 2);
356 transmit_bytes(abtHalt, 4);
357 transmit_bits(abtUnlock1, 7);
359 transmit_bytes(abtWipe, 1);
360 transmit_bytes(abtHalt, 4);
361 transmit_bits(abtUnlock1, 7);
363 transmit_bytes(abtUnlock2, 1);
364 transmit_bytes(abtWrite, 4);
365 transmit_bytes(abtData, 18);
367 for (i = 3 ; i < 64 ; i += 4) {
368 abtWrite[1] = (char) i;
369 iso14443a_crc_append(abtWrite, 2);
370 transmit_bytes(abtWrite, 4);
371 transmit_bytes(abtBlank, 18);