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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_scan.h>
31 #include <librfid/rfid_reader.h>
32 #include <librfid/rfid_layer2.h>
33 #include <librfid/rfid_protocol.h>
35 #include <librfid/rfid_protocol_mifare_classic.h>
36 #include <librfid/rfid_protocol_mifare_ul.h>
38 #include <librfid/rfid_access_mifare_classic.h>
40 #include "librfid-tool.h"
42 static char *program_name;
44 static void help(void)
46 printf( " -h --help Print this help message\n"
47 " -r --read Read a mifare sector\n"
48 " -l --loop-read Loop reading a mifare sector\n"
49 " -w --write Write a mifare sector\n"
50 " -k --key Specify mifare access key (in hex tuples)\n");
53 static struct option mifare_opts[] = {
55 { "read", 1, 0, 'r' },
56 { "loop-read", 1, 0, 'l' },
57 { "write", 1 ,0, 'w' },
58 { "help", 0, 0, 'h' },
62 static int mifare_cl_auth(unsigned char *key, int page)
66 rc = mfcl_set_key(ph, key);
68 fprintf(stderr, "key format error\n");
71 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
73 fprintf(stderr, "mifare auth error\n");
76 printf("mifare auth succeeded!\n");
81 static void mifare_l3(void)
83 while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
85 printf("ISO14443-3A anticollision succeeded\n");
87 while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
89 printf("Mifare card available\n");
92 int main(int argc, char **argv)
94 int len, rc, c, option_index = 0;
95 unsigned int page,uid,uid_len;
96 char key[MIFARE_CL_KEY_LEN];
97 char buf[MIFARE_CL_PAGE_SIZE];
100 program_name = argv[0];
102 program_name = basename(argv[0]);
103 #endif/*__MINGW32__*/
105 memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON, MIFARE_CL_KEY_LEN);
107 printf("%s - (C) 2006 by Harald Welte\n"
108 "This program is Free Software and has "
109 "ABSOLUTELY NO WARRANTY\n\n", program_name);
111 printf("initializing librfid\n");
114 if (reader_init() < 0) {
115 fprintf(stderr, "error opening reader\n");
120 c = getopt_long(argc, argv, "k:r:l:w:", mifare_opts,
127 hexread(key, optarg, strlen(optarg));
128 printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
132 printf("read(key='%s',page=%u):",
133 hexdump(key, MIFARE_CL_KEY_LEN), page);
134 len = MIFARE_CL_PAGE_SIZE;
136 if (mifare_cl_auth(key, page) < 0)
141 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
142 printf("UID=%08X (len=%u)\n",uid,uid_len);
144 len=MIFARE_CL_PAGE_SIZE;
145 rc = rfid_protocol_read(ph, page, buf, &len);
148 fprintf(stderr, "error during read\n");
151 printf("len=%u data=%s\n", len, hexdump(buf, len));
153 if (page & 0x3 == 0x3) {
154 struct mfcl_access_sect s;
155 struct mfcl_access_exp_sect es;
157 u_int8_t recreated[4];
158 mfcl_parse_access(&s, buf+6);
159 printf("access b0:%u b1:%u b2:%u b3:%u\n",
160 s.block[0], s.block[1],
161 s.block[2], s.block[3]);
162 mfcl_access_to_exp(&es, &s);
163 for (b = 0; b < 3; b++)
164 printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
165 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
167 mfcl_compile_access(recreated, &s);
168 printf("recreated; %s\n", hexdump(recreated,4));
174 printf("read_loop(key='%s',page=%u):\n",
175 hexdump(key, MIFARE_CL_KEY_LEN), page);
178 if (mifare_cl_auth(key, page) < 0)
183 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
184 printf("UID=%08X (len=%u)\n",uid,uid_len);
186 len=MIFARE_CL_PAGE_SIZE;
187 rc = rfid_protocol_read(ph, page, buf, &len);
190 fprintf(stderr, "error during read\n");
193 printf("%s\n", hexdump(buf, len));
198 len = strlen(argv[optind]);
199 len = hexread(buf, argv[optind], len);
200 printf("write(key='%s',page=%u):",
201 hexdump(key, MIFARE_CL_KEY_LEN), page);
202 printf(" '%s'(%u):", hexdump(buf, len), len);
204 if (mifare_cl_auth(key, page) < 0)
206 rc = rfid_protocol_write(ph, page, buf, len);
209 fprintf(stderr, "error during write\n");
221 rfid_protocol_close(ph);
222 rfid_protocol_fini(ph);
224 rfid_layer2_close(l2h);
225 rfid_layer2_fini(l2h);
227 rfid_reader_close(rh);