add functions to alter easycard log records and mifare value blocks

This commit is contained in:
Harald Welte 2010-08-14 16:22:56 +08:00
parent 26064db907
commit aacd31e326
6 changed files with 135 additions and 57 deletions

View File

@ -6,7 +6,7 @@ all: easytool
clean:
@rm -f *.o easytool
easytool: easytool.o data.o utils.o mifare_classic.o
easytool: easytool.o easycard.o data.o utils.o mifare_classic.o
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c

77
easytool/easycard.c Normal file
View File

@ -0,0 +1,77 @@
/* 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 <errno.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"
time_t easy_timestamp2time(const uint8_t *easy_ts)
{
return (easy_ts[2] << 16 | easy_ts[1] << 8 | easy_ts[0]) << 8;
}
/* apply a telta (positive or negative) to a EasyCard log record */
int easy_update_log_rec(struct easy_log_rec *elr, int16_t delta)
{
int32_t sum = elr->amount + delta;
int32_t remaining = elr->remaining = delta;
if ((sum < 0 || sum > 0xffff) ||
(remaining < 0 || remaining > 0xffff))
return -ERANGE;
elr->amount = sum;
elr->remaining = remaining;
return 0;
}
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;
}

View File

@ -11,4 +11,37 @@
extern const struct value_string easy_tt_names[];
extern const struct value_string taipei_mrt_stn_id[];
/* Sector 0 of Block 2 seems to contain manufacturing timestamp */
struct easy_block2sec0 {
uint8_t unknown[6];
uint8_t timestamp[3];
uint8_t unknown2[7];
} __attribute__ ((packed));
/* Sector 2 of Block 15 */
struct easy_block15sec2 {
uint8_t unknown[11];
uint8_t day_of_month;
uint8_t unknown2; /* always 0x3d? */
uint16_t sum_of_day; /* sum of all shop purchases on a day */
uint8_t unknown3;
} __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));
time_t easy_timestamp2time(const uint8_t *easy_ts);
#endif /* EASYCARD_H */

View File

@ -54,62 +54,6 @@ struct {
mifare_tag *mft;
} global;
/* Sector 0 of Block 2 seems to contain manufacturing timestamp */
struct easy_block2sec0 {
uint8_t unknown[6];
uint8_t timestamp[3];
uint8_t unknown2[7];
} __attribute__ ((packed));
/* Sector 2 of Block 15 */
struct easy_block15sec2 {
uint8_t unknown[11];
uint8_t day_of_month;
uint8_t unknown2; /* always 0x3d? */
uint16_t sum_of_day; /* sum of all shop purchases on a day */
uint8_t unknown3;
} __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));
/* Mifare classic VALUE BLOCK */
struct mfcl_value_block {
uint32_t value; /* value in NTD */
uint32_t value_inv; /* bit-inverted copy of value in NTD */
uint32_t value_backup; /* backup copy of value in NTD */
uint8_t addr[4];
} __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",

View File

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "mifare_classic.h"
@ -27,3 +28,18 @@ void mfcl_parse_acc_bits(struct acc_bits_parsed *abp, uint8_t *acc_bits)
abp->block[block] |= ABP_C3;
}
}
/* apply a delta (positive or negative) to a Mifare Classic VALUE block */
int mfcl_update_value_block(struct mfcl_value_block *mvb, int32_t delta)
{
int64_t sum = mvb->value + delta;
if (sum > 0xffffffff || sum < 0)
return -ERANGE;
mvb->value = sum;
mvb->value_backup = mvb->value;
mvb->value_inv = ~sum;
return 0;
}

View File

@ -13,4 +13,12 @@ struct acc_bits_parsed {
void mfcl_parse_acc_bits(struct acc_bits_parsed *abp, uint8_t *acc_bits);
/* Mifare classic VALUE BLOCK */
struct mfcl_value_block {
uint32_t value; /* value in NTD */
uint32_t value_inv; /* bit-inverted copy of value in NTD */
uint32_t value_backup; /* backup copy of value in NTD */
uint8_t addr[4];
} __attribute__ ((packed));
#endif