summaryrefslogtreecommitdiff
path: root/easytool/easytool.c
blob: efb083f31203715383a85154a5606baa147c1a7c (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* 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.
 *
 */



#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>

#include <nfc/nfc-types.h>
#include <nfc/mifaretag.h>

#include "easycard.h"

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

#define ABP_C1	0x04
#define ABP_C2	0x02
#define ABP_C3	0x01

struct acc_bits_parsed {
	uint8_t block[4];
};

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 parse_acc_bits(struct acc_bits_parsed *abp, uint8_t *acc_bits)
{
	uint8_t c1, c2, c3;
	uint8_t block;

	memset(abp, 0, sizeof(*abp));

	c1 = acc_bits[1] >> 4;
	c2 = acc_bits[2] & 0xf;
	c3 = acc_bits[2] >> 4;

	printf("C1 = %x, C2 = %x, C3 = %x\n", c1, c2, c3);

	for (block = 0; block < 4; block++) {
		uint8_t testbit = 1 << block;
		if (c1 & testbit)
			abp->block[block] |= ABP_C1;
		if (c2 & testbit)
			abp->block[block] |= ABP_C2;
		if (c3 & testbit)
			abp->block[block] |= ABP_C3;
	}
}

struct easy_block2sec0 {
	uint8_t unknown[6];
	uint8_t timestamp[3];
	uint8_t unknown2[7];
} __attribute__ ((packed));

/* storage of a transaction log record on the transponder itself */
struct easy_log_rec {
	uint8_t trans_id;
	uint8_t unknown;
	uint8_t timestamp[3];	/* seconds since January 1st 1970 / 256 */
	uint8_t trans_type;
	uint16_t amount;	/* transaction amount / value */
	uint16_t remaining;	/* remaining value on card _after_ trans */
	uint8_t unknown2;
	uint8_t station_code;	/* MRT station code */
	uint16_t reader_code;	/* unique code of RFID reader */
	uint8_t unknown3[2];
} __attribute__ ((packed));

static time_t easy_timestamp2time(const uint8_t *easy_ts)
{
	return (easy_ts[2] << 16 | easy_ts[1] << 8 | easy_ts[0]) << 8;
}

static char tsbuf[64];
char *easy_asc_timestamp(const uint8_t *timestamp)
{
	time_t t_time = easy_timestamp2time(timestamp);
	struct tm *t_tm = gmtime(&t_time);
	memset(tsbuf, 0, sizeof(tsbuf));
	snprintf(tsbuf, sizeof(tsbuf), "%4u-%02u-%02u %02u:%02u",
		t_tm->tm_year+1900, t_tm->tm_mon+1, t_tm->tm_mday,
		t_tm->tm_hour, t_tm->tm_min);
	return tsbuf;
}

static void dump_easy_log(const struct easy_log_rec *elr)
{
	printf("%s | %02x | %10s | Paid %4u NTD | %4u NTD remaining\n",
		easy_asc_timestamp(elr->timestamp),
		elr->trans_id,
		get_value_string(easy_tt_names, elr->trans_type),
		elr->amount, elr->remaining);
	switch (elr->trans_type) {
	case EASY_TT_MRT_ENTER:
	case EASY_TT_MRT_EXIT:
		printf("\tMRT Station %s\n",
			get_value_string(taipei_mrt_stn_id,
					 elr->station_code));
		break;
	}
}

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)));
		parse_acc_bits(&abp, access_bits);
		dump_acc_bits(&abp);
	}
}

static void dump_easycard(mifare_tag *mft)
{
	unsigned int sect;
	mifare_block_manufacturer *manuf = &mft->amb[0].mbm;
	struct easy_block2sec0 *b2s0 = mft->amb[4].mbd.abtData;
	uint32_t uid = ntohl(*((uint32_t *) manuf->abtUID));

	/* dump the header */
	printf("EasyCard UID 0x%08x (%u)\n", uid, uid);
	printf("Date of manufacture: %s\n",
		easy_asc_timestamp(b2s0->timestamp));

	/* dump the transaction log */
	for (sect = 3; sect <=4; sect++) {
		unsigned int block_base = sect * 4;
		uint8_t *access_bits = mft->amb[block_base+3].mbt.abtAccessBits;
		unsigned int i;
		for (i = 0; i < 3; i++) {
			void *data = mft->amb[block_base+i].mbd.abtData;
			dump_easy_log(data);
		}
	}
}

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

	global.fd = open(argv[1], O_RDONLY);
	if (global.fd < 0) {
		perror("open");
		exit(1);
	}
	if (fstat(global.fd, &st) < 0) {
		perror("stat");
		exit(1);
	}
	global.size = st.st_size;
	global.mft = mmap(NULL, global.size, PROT_READ, MAP_SHARED,
			  global.fd, 0);
	if (!global.mft) {
		perror("mmap");
		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