1 /* mifare-tool - a small command-line tool for librfid mifare testing
3 * (C) 2006 by Harald Welte <laforge@gnumonks.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
32 #include <librfid/rfid.h>
33 #include <librfid/rfid_scan.h>
34 #include <librfid/rfid_reader.h>
35 #include <librfid/rfid_layer2.h>
36 #include <librfid/rfid_protocol.h>
38 #include <librfid/rfid_protocol_mifare_classic.h>
39 #include <librfid/rfid_protocol_mifare_ul.h>
41 #include <librfid/rfid_access_mifare_classic.h>
43 #include "librfid-tool.h"
45 static char *program_name;
47 static void help(void)
49 printf( " -h --help Print this help message\n"
50 " -r --read Read a mifare sector\n"
51 " -l --loop-read Loop reading a mifare sector\n"
52 " -w --write Write a mifare sector\n"
53 " -k --key Specify mifare access key (in hex tuples)\n"
54 " -b --brute-force n Brute Force read sector n\n");
57 static struct option mifare_opts[] = {
59 { "read", 1, 0, 'r' },
60 { "loop-read", 1, 0, 'l' },
61 { "write", 1 ,0, 'w' },
62 { "help", 0, 0, 'h' },
63 { "brute-force", 1, 0, 'b' },
67 static int mifare_cl_auth(unsigned char *key, int page)
71 rc = mfcl_set_key(ph, key);
73 fprintf(stderr, "key format error\n");
76 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
78 fprintf(stderr, "mifare auth error\n");
81 printf("mifare auth succeeded!\n");
86 static void mifare_l3(void)
88 while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
90 printf("ISO14443-3A anticollision succeeded\n");
92 while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
94 printf("Mifare card available\n");
97 int main(int argc, char **argv)
99 int len, rc, c, option_index = 0;
100 unsigned int page,uid,uid_len;
101 char key[MIFARE_CL_KEY_LEN];
102 char buf[MIFARE_CL_PAGE_SIZE];
105 program_name = argv[0];
107 program_name = basename(argv[0]);
108 #endif/*__MINGW32__*/
110 memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON, MIFARE_CL_KEY_LEN);
112 printf("%s - (C) 2006 by Harald Welte\n"
113 "This program is Free Software and has "
114 "ABSOLUTELY NO WARRANTY\n\n", program_name);
116 printf("initializing librfid\n");
119 if (reader_init() < 0) {
120 fprintf(stderr, "error opening reader\n");
125 c = getopt_long(argc, argv, "k:r:l:w:b:h", mifare_opts,
134 printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
135 len = MIFARE_CL_PAGE_SIZE;
137 for (i = 0; i <= 0xff; i++) {
138 key[MIFARE_CL_KEY_LEN-1]=i;
139 if (mifare_cl_auth(key, page) >= 0)
140 printf("KEY: %s\n",hexdump(key, MIFARE_CL_KEY_LEN));
145 hexread(key, optarg, strlen(optarg));
146 printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
150 printf("read(key='%s',page=%u):",
151 hexdump(key, MIFARE_CL_KEY_LEN), page);
152 len = MIFARE_CL_PAGE_SIZE;
154 if (mifare_cl_auth(key, page) < 0)
159 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
160 printf("UID=%08X (len=%u)\n",uid,uid_len);
162 len=MIFARE_CL_PAGE_SIZE;
163 rc = rfid_protocol_read(ph, page, buf, &len);
166 fprintf(stderr, "error during read\n");
169 printf("len=%u data=%s\n", len, hexdump(buf, len));
171 if (page & 0x3 == 0x3) {
172 struct mfcl_access_sect s;
173 struct mfcl_access_exp_sect es;
175 u_int8_t recreated[4];
176 mfcl_parse_access(&s, buf+6);
177 printf("access b0:%u b1:%u b2:%u b3:%u\n",
178 s.block[0], s.block[1],
179 s.block[2], s.block[3]);
180 mfcl_access_to_exp(&es, &s);
181 for (b = 0; b < 3; b++)
182 printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
183 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
185 mfcl_compile_access(recreated, &s);
186 printf("recreated; %s\n", hexdump(recreated,4));
192 printf("read_loop(key='%s',page=%u):\n",
193 hexdump(key, MIFARE_CL_KEY_LEN), page);
196 if (mifare_cl_auth(key, page) < 0)
201 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
202 printf("UID=%08X (len=%u)\n",uid,uid_len);
204 len=MIFARE_CL_PAGE_SIZE;
205 rc = rfid_protocol_read(ph, page, buf, &len);
208 fprintf(stderr, "error during read\n");
211 printf("%s\n", hexdump(buf, len));
216 len = strlen(argv[optind]);
217 len = hexread(buf, argv[optind], len);
218 printf("write(key='%s',page=%u):",
219 hexdump(key, MIFARE_CL_KEY_LEN), page);
220 printf(" '%s'(%u):", hexdump(buf, len), len);
222 if (mifare_cl_auth(key, page) < 0)
224 rc = rfid_protocol_write(ph, page, buf, len);
227 fprintf(stderr, "error during write\n");
239 rfid_protocol_close(ph);
240 rfid_protocol_fini(ph);
242 rfid_layer2_close(l2h);
243 rfid_layer2_fini(l2h);
245 rfid_reader_close(rh);