summaryrefslogtreecommitdiff
path: root/easytool/easytool.c
blob: cb9fe3035210e0a92a224280e7a0b8dd24ade419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* A reverse-engineered implementation of the EasyCard data format */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/* System includes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>

/* libnfc includes */
#include <nfc/nfc-types.h>
#include <nfc/mifaretag.h>

#include "mifare_classic.h"

/* Easycard specific includes */
#include "easycard.h"

#define VERSION	"0.01"
#define COPYRIGHT 						\
	"EasyTool "VERSION"\n"					\
	"(C) 2010 by Harald Welte <laforge@gnumonks.org>\n"	\
	"This is FREE SOFTWARE with ABSOLUTELY NO VARRANTY\n\n"	\
	"Use of this software is only authorized for RESEARCH PURPOSE!\n\n" 

struct {
	int fd;
	unsigned long size;
	mifare_tag *mft;
} global;

static void dump_acc_bits(const struct acc_bits_parsed *abp)
{
	uint8_t block;

	for (block = 0; block < 4; block++) {
		printf("\tBlock %u: %x (%u %u %u)\n", block, abp->block[block],
			abp->block[block] & ABP_C1 ? 1 : 0,
			abp->block[block] & ABP_C2 ? 1 : 0,
			abp->block[block] & ABP_C3 ? 1 : 0);
	}
}

static void dump_mfcl(mifare_tag *mft)
{
	unsigned int sect;

	mifare_block_manufacturer *manuf = &mft->amb[0].mbm;

	printf("UID %x, ATQA %x\n", ntohl(*((uint32_t *) manuf->abtUID)),
		ntohs(*((uint16_t *) manuf->abtATQA)));

	for (sect = 0; sect < 16; sect++) {
		unsigned int block_base = sect * 4;
		uint8_t *access_bits = mft->amb[block_base+3].mbt.abtAccessBits;
		struct acc_bits_parsed abp;

		printf("Sector %02u (base: 0x%02x) Access bits: 0x%08x\n", sect,
			sect*4*16, ntohl(*((uint32_t *) access_bits)));
		mfcl_parse_acc_bits(&abp, access_bits);
		dump_acc_bits(&abp);
	}
}

static void dump_easycard(mifare_tag *mft)
{
	mifare_block_manufacturer *manuf = &mft->amb[0].mbm;
	struct mfcl_value_block *val =
			(struct mfcl_value_block *) mft->amb[8].mbd.abtData;
	struct easy_sect2blk0 *b2s0 =
			(struct easy_sect2blk0 *) mft->amb[1*4+0].mbd.abtData;
	struct easy_sect15blk2 *b15s2 =
			(struct easy_sect15blk2 *) mft->amb[15*4+2].mbd.abtData;
	uint32_t uid = *((uint32_t *) manuf->abtUID);

	/* dump the header */
	printf("EasyCard UID 0x%08x (%u)\n", ntohl(uid), uid);
	printf("Date of manufacture: %s\n",
		easy_asc_timestamp(b2s0->timestamp));
	printf("Current Balance: %5u NTD\n", val->value);
	printf("Sum of all purchases on day %u (of month): %u NTD\n",
		b15s2->day_of_month, b15s2->sum_of_day);
	printf("\nTransaction Log:\n");

	/* dump the transaction log */
	easy_dump_log(mft);
}

static void print_help(void)
{
}

int main(int argc, char **argv)
{
	struct stat st;

	printf(COPYRIGHT);

	if (argc < 2) {
		fprintf(stderr, "ERROR: You must specify the file name of "
			"a mifare dump file (.mfd)\n");
		print_help();
		exit(2);
	}

	global.fd = open(argv[1], O_RDONLY);
	if (global.fd < 0) {
		perror("Error opening the MFD file");
		exit(1);
	}
	if (fstat(global.fd, &st) < 0) {
		perror("Error stat()ing the MFD file");
		exit(1);
	}
	global.size = st.st_size;
	global.mft = mmap(NULL, global.size, PROT_READ, MAP_SHARED,
			  global.fd, 0);
	if (!global.mft) {
		perror("Error mmap()ing the MFD file");
		exit(1);
	}

	//dump_mfcl(global.mft);
	dump_easycard(global.mft);

	munmap(global.mft, global.size);
	close(global.fd);
	exit(0);
}
personal git repositories of Harald Welte. Your mileage may vary