1
0
mirror of synced 2024-11-27 23:10:49 +01:00
bananatools/board/bpreader.c

537 lines
16 KiB
C
Raw Normal View History

2023-01-03 05:35:53 +01:00
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
2023-03-16 06:45:39 +01:00
#include "hook/iobuf.h"
#include "hook/iohook.h"
#include "hooklib/uart.h"
#include "hooklib/fdshark.h"
#include "util/dprintf.h"
#include "util/dump.h"
2023-04-13 08:30:56 +02:00
#include "util/hexstr.h"
2023-03-16 06:45:39 +01:00
2023-09-25 09:16:26 +02:00
#include "iccard/aime.h"
#include "iccard/mifare.h"
2023-01-03 05:35:53 +01:00
#include "board/bpreader.h"
2023-03-27 10:03:23 +02:00
const uint8_t BPREADER_CMD_GO_NEXT[6] = { 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00 };
2023-07-22 05:44:59 +02:00
static bool flg = true;
2023-09-25 09:16:26 +02:00
static struct mifare card;
2023-03-16 06:45:39 +01:00
static HRESULT bp_handle_irp(struct irp *irp);
static HRESULT bp_handle_irp_locked(struct irp *irp);
2023-03-24 07:46:38 +01:00
static HRESULT crack_bpreader_request();
2023-04-13 08:30:56 +02:00
static size_t build_bpreader_response(
const uint8_t *data,
const size_t len_data,
struct bpreader_cmd_header *resp_header,
uint8_t *response,
const size_t resp_size);
static HRESULT bpreader_generic_cmd(uint32_t resp_code);
static HRESULT bpreader_poll_card_cmd();
static HRESULT bpreader_init_cmd();
static HRESULT bpreader_set_output_cmd();
2023-07-22 05:44:59 +02:00
static HRESULT bpreader_unk_08_cmd();
static HRESULT bpreader_unk_06_cmd();
static HRESULT bpreader_unk_0c_cmd();
2023-09-25 09:16:26 +02:00
static HRESULT bpreader_unk_44_cmd();
static HRESULT bpreader_unk_52_cmd();
2023-07-22 05:44:59 +02:00
static HRESULT bpreader_unk_54_cmd();
2023-09-25 09:16:26 +02:00
static HRESULT bpreader_send_mifare_cmd();
static HRESULT bpreader_empty_resp();
2023-03-16 06:45:39 +01:00
static struct bpreader_config *config;
static struct uart bp_uart;
static CRITICAL_SECTION bp_lock;
static uint8_t bp_written_bytes[520];
static uint8_t bp_readable_bytes[520];
static uint8_t last_cmd = 0;
2023-03-24 07:46:38 +01:00
static uint16_t read_ct = 0;
2023-03-16 06:45:39 +01:00
HRESULT bpreader_init(struct bpreader_config *cfg, uint16_t port)
{
config = cfg;
if (!config->enable) {
return S_OK;
}
if (cfg->port > 0) {
2023-03-16 06:45:39 +01:00
port = cfg->port;
}
2023-09-25 09:16:26 +02:00
uint8_t sec1[16] = { 0x00, 0x02, 0x4E, 0x42, 0x47, 0x49, 0x43, 0x36, 0x3A, 0xE7, 0xB5, 0x59, 0x45, 0x64, 0x0F, 0x3D };
2023-03-16 06:45:39 +01:00
uart_init(&bp_uart, port);
bp_uart.written.bytes = bp_written_bytes;
bp_uart.written.nbytes = sizeof(bp_written_bytes);
bp_uart.readable.bytes = bp_readable_bytes;
bp_uart.readable.nbytes = sizeof(bp_readable_bytes);
InitializeCriticalSection(&bp_lock);
2023-09-25 09:16:26 +02:00
HRESULT hr = aime_card_populate(&card, cfg->access_code_bytes, sizeof(cfg->access_code_bytes));
//memcpy_s(card.sectors[0].blocks[1].bytes, sizeof(card.sectors[0].blocks[1].bytes), sec1, sizeof(sec1));
if (FAILED(hr)) {
return hr;
}
2023-03-16 06:45:39 +01:00
dprintf("Reader: Init\n");
return iohook_push_handler(bp_handle_irp);
}
2023-01-03 05:35:53 +01:00
2023-03-16 06:45:39 +01:00
static HRESULT bp_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (uart_match_irp(&bp_uart, irp)) {
EnterCriticalSection(&bp_lock);
hr = bp_handle_irp_locked(irp);
LeaveCriticalSection(&bp_lock);
}
else {
return iohook_invoke_next(irp);
}
return hr;
2023-01-03 05:35:53 +01:00
}
2023-03-16 06:45:39 +01:00
static HRESULT bp_handle_irp_locked(struct irp *irp)
2023-01-03 05:35:53 +01:00
{
2023-03-16 06:45:39 +01:00
HRESULT hr;
if (irp->op == IRP_OP_OPEN) {
dprintf("Reader: Starting backend\n");
2023-03-16 06:45:39 +01:00
}
hr = uart_handle_irp(&bp_uart, irp);
if (FAILED(hr)) {
return hr;
}
2023-03-24 07:46:38 +01:00
2023-03-16 06:45:39 +01:00
#if 0
if (irp->op == IRP_OP_WRITE) {
dprintf("WRITE:\n");
dump_iobuf(&bp_uart.written);
}
#endif
2023-03-24 07:46:38 +01:00
2023-03-16 06:45:39 +01:00
if (irp->op == IRP_OP_WRITE) {
2023-03-24 07:46:38 +01:00
read_ct = 0;
2023-03-16 06:45:39 +01:00
if (bp_uart.written.bytes[0] == 0x55) {
dprintf("Reader: Hello\n");
2023-03-24 07:46:38 +01:00
bp_uart.written.pos = 0; // consume the written buffer
2023-03-16 06:45:39 +01:00
}
else if (bp_uart.written.bytes[3] == 0x00) {
2023-03-24 07:46:38 +01:00
bp_uart.written.pos = 0; // consume the written buffer
2023-03-16 06:45:39 +01:00
}
2023-03-24 07:46:38 +01:00
return hr;
2023-03-16 06:45:39 +01:00
}
2023-03-24 07:46:38 +01:00
else if (irp->op == IRP_OP_READ) {
2023-09-25 09:16:26 +02:00
if (!read_ct) {
hr = crack_bpreader_request();
2023-07-22 05:44:59 +02:00
} else {
2023-09-25 09:16:26 +02:00
// We need a delay here in order to properly
// "timeout" the DLL's read request.
Sleep(1);
}
2023-03-24 07:46:38 +01:00
read_ct++;
2023-03-16 06:45:39 +01:00
}
if (FAILED(hr)) {
2023-07-22 05:44:59 +02:00
dprintf("Reader: HR failed %lX\n", hr);
}
#if 0
if (irp->op == IRP_OP_READ) {
2023-07-22 05:44:59 +02:00
dprintf("READABLE:\n");
dump_iobuf(&bp_uart.readable);
}
#endif
2023-03-16 06:45:39 +01:00
2023-09-25 09:16:26 +02:00
bp_uart.written.pos = 0; // consume the written buffer
2023-07-22 05:44:59 +02:00
2023-03-16 06:45:39 +01:00
return hr;
2023-03-24 07:46:38 +01:00
}
static HRESULT crack_bpreader_request() {
struct bpreader_cmd_header header = { bp_uart.written.bytes[0], bp_uart.written.bytes[1], bp_uart.written.bytes[2],
bp_uart.written.bytes[3], bp_uart.written.bytes[4], bp_uart.written.bytes[5], bp_uart.written.bytes[6],
};
assert(header.padding0_00 == 0);
assert(header.padding1_00 == 0);
assert(header.padding2_ff == 0xFF);
assert(header.d_identifier == 0xD4);
2023-07-22 05:44:59 +02:00
last_cmd = header.cmd;
2023-03-24 07:46:38 +01:00
switch(header.cmd) {
case 0x06:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Cmd 0x06\n");
2023-07-22 05:44:59 +02:00
return bpreader_unk_06_cmd();
2023-03-24 07:46:38 +01:00
case 0x08:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Cmd 0x08\n");
2023-07-22 05:44:59 +02:00
return bpreader_unk_08_cmd();
2023-03-24 07:46:38 +01:00
case 0x12:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Cmd 0x12\n");
2023-07-22 05:44:59 +02:00
return bpreader_generic_cmd(0x13);
2023-03-24 07:46:38 +01:00
case 0x18:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Initialize\n");
2023-07-22 05:44:59 +02:00
return bpreader_init_cmd();
2023-03-24 07:46:38 +01:00
case 0x0C:
dprintf("Reader: Cmd 0x0C\n");
2023-07-22 05:44:59 +02:00
return bpreader_unk_0c_cmd();
2023-03-24 07:46:38 +01:00
case 0x0E:
dprintf("Reader: Set Output\n");
return bpreader_generic_cmd(0x0F);
2023-03-24 07:46:38 +01:00
case 0x32:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Cmd 0x32\n");
2023-07-22 05:44:59 +02:00
return bpreader_generic_cmd(0x33);
2023-03-24 07:46:38 +01:00
case 0x40:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Read Mifare\n");
return bpreader_send_mifare_cmd();
case 0x44:
dprintf("Reader: Cmd 0x44\n");
return bpreader_unk_44_cmd();
2023-03-24 07:46:38 +01:00
case 0x4A:
2023-09-25 09:16:26 +02:00
// dprintf("Reader: Poll Card\n");
return bpreader_poll_card_cmd();
2023-03-24 07:46:38 +01:00
case 0xA0:
dprintf("Reader: Read Felica\n");
break;
2023-07-22 05:44:59 +02:00
case 0x52:
2023-09-25 09:16:26 +02:00
//dprintf("Reader: Cmd 0x52\n");
return bpreader_unk_52_cmd();
2023-07-22 05:44:59 +02:00
case 0x54:
dprintf("Reader: Cmd 0x54\n");
return bpreader_unk_54_cmd();
2023-03-24 07:46:38 +01:00
default:
dprintf("Reader: Unknown command 0x%02x\n", header.cmd);
}
2023-07-22 05:44:59 +02:00
dump_iobuf(&bp_uart.written);
return (HRESULT)-1;
2023-03-27 10:03:23 +02:00
}
2023-04-13 08:30:56 +02:00
static size_t build_bpreader_response(
const uint8_t *data,
const size_t len_data,
struct bpreader_cmd_header *resp_header,
uint8_t *response,
const size_t resp_size)
{
// TODO: Account for escape characters
if (resp_size < 9) {
dprintf("Reader: build_bpreader_response resp_size less then 9\n");
return 0;
}
uint8_t full_resp_len = len_data + 2 + 7; // calculate full response length
uint8_t final_checksum = 0; // initialize checksum
resp_header->data_len = len_data + 2;
resp_header->header_checksum = 0xFF - ((0xFF + len_data + 2) & 0xFF);
memcpy_s(response, resp_size, resp_header, sizeof(*resp_header)); // Copy header
if (len_data > 0) {
2023-07-22 05:44:59 +02:00
for (int i = 0; i < len_data; i++) {
response[7+i] = data[i];
} // copy data if there's any
}
2023-04-13 08:30:56 +02:00
for (int i = 0; i < full_resp_len; i++) {
final_checksum = (final_checksum + response[i]) & 0xFF; // Calculate checksum
}
response[full_resp_len - 2] = 0xFF - final_checksum; // Assign checksum
return full_resp_len;
}
static HRESULT bpreader_generic_cmd(uint32_t resp_code)
{
//uint8_t buff[] = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, 0x19, 0x12, 0x00 };
uint8_t buff[9] = { 0x00 };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, resp_code };
size_t resp_len = 0;
resp_len = build_bpreader_response((uint8_t *){0}, 0, &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
//dump_iobuf(&bp_uart.readable);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_generic_cmd (0x%2X response code)!\n", resp_code);
return E_FAIL;
}
}
static HRESULT bpreader_init_cmd()
{
//uint8_t buff[] = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, 0x19, 0x12, 0x00 };
uint8_t buff[9] = { 0x00 };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, 0x19 };
size_t resp_len = 0;
resp_len = build_bpreader_response((uint8_t *){0}, 0, &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_init_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_set_output_cmd()
{
//uint8_t buff[] = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, 0x0F, 0x1C, 0x00 };
uint8_t buff[9] = { 0x00 };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD5, 0x0F };
size_t resp_len = 0;
resp_len = build_bpreader_response((uint8_t *){0}, 0, &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_set_output_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_poll_card_cmd()
2023-04-13 08:30:56 +02:00
{
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD5, 0x4B };
2023-07-22 05:44:59 +02:00
struct bpreader_poll_banapass_data data = { 0x00 };
2023-04-13 08:30:56 +02:00
uint8_t buff[32] = { 0x00 };
int data_len = 1;
size_t resp_len = 0;
2023-09-25 09:16:26 +02:00
if (GetAsyncKeyState(config->insert_card) & 0x8000) {
2023-04-13 08:30:56 +02:00
dprintf("Reader: Touch card %ls\n", config->access_code);
data.card_present = 0x01;
data.unknown1 = 0x01;
data.atqa[0] = 0x00;
data.atqa[1] = 0x04;
data.sak = 0x08;
data.unknown5 = 0x04;
2023-09-25 09:16:26 +02:00
memcpy_s(data.serial_number, sizeof(data.serial_number), config->access_code_bytes, 4);
2023-04-13 08:30:56 +02:00
data_len = sizeof(data);
2023-09-25 09:16:26 +02:00
resp_len = build_bpreader_response((uint8_t *)&data, data_len, &header, buff, sizeof(buff));
}
else {
data_len = 3;
uint8_t empty_buff[1] = {0};
resp_len = build_bpreader_response((uint8_t *)&empty_buff, data_len, &header, buff, sizeof(buff));
}
2023-04-13 08:30:56 +02:00
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_poll_card_cmd!\n");
2023-04-13 08:30:56 +02:00
return E_FAIL;
}
2023-07-22 05:44:59 +02:00
}
static HRESULT bpreader_unk_08_cmd()
{
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD5, 0x09 };
uint8_t buff[10] = { 0x00 };
uint8_t bfr_data[1] = { 0x00 };
size_t resp_len = 0;
resp_len = build_bpreader_response(bfr_data, sizeof(bfr_data), &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_set_output_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_unk_54_cmd()
{
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD5, 0x55 };
uint8_t buff[10] = { 0x00 };
uint8_t bfr_data[1] = { 0x00 };
size_t resp_len = 0;
2023-09-25 09:16:26 +02:00
resp_len = build_bpreader_response(bfr_data, 1, &header, buff, sizeof(buff));
2023-07-22 05:44:59 +02:00
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
2023-09-25 09:16:26 +02:00
dprintf("Reader: No data to return in bpreader_unk_54_cmd!\n");
2023-07-22 05:44:59 +02:00
return E_FAIL;
}
}
static HRESULT bpreader_unk_06_cmd()
{
//uint8_t hdr[] = { 0x00, 0x00, 0xFF, 0x0a, 0xf6, 0xd5, 0x07 }
//uint8_t data[] = { 0xff, 0x3f, 0x0e, 0xf1, 0xff, 0x3f, 0x0e, 0xf1, 0xaa, 0x00 };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x0a, 0xF6, 0xD5, 0x07 };
uint8_t buff_a[17] = { 0x00 };
uint8_t buff_b[20] = { 0x00 };
uint8_t bfr_data_a[8] = { 0xff, 0x3f, 0x0e, 0xf1, 0xff, 0x3f, 0x0e, 0xf1 };
uint8_t bfr_data_b[11] = { 0xDC, 0xF4, 0x3F, 0x11, 0x4D, 0x85, 0x61, 0xF1, 0x26, 0x6A, 0x87 };
size_t resp_len = 0;
uint8_t sample = bp_uart.written.bytes[8];
switch (sample) {
case 0x1c: resp_len = build_bpreader_response(bfr_data_a, sizeof(bfr_data_a), &header, buff_a, sizeof(buff_a)); break;
default: resp_len = build_bpreader_response(bfr_data_b, sizeof(bfr_data_b), &header, buff_b, sizeof(buff_b)); break;
}
if (resp_len > 0) {
switch (sample) {
case 0x1c: return iobuf_write(&bp_uart.readable, buff_a, sizeof(buff_a));
default: return iobuf_write(&bp_uart.readable, buff_b, sizeof(buff_b));
}
} else {
dprintf("Reader: No data to return in bpreader_set_output_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_unk_0c_cmd()
{
//uint8_t hdr[] = uint8_t buff[] = { 0x00, 0x00, 0xFF, 0x05, 0xFB, 0xD5, 0x0D };
//uint8_t data[] = { 0x00, 0x06, 0x00, 0x18, 0x00 };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x0a, 0xF6, 0xD5, 0x0D };
uint8_t buff[12] = { 0x00 };
uint8_t bfr_data[3] = { 0x00, 0x06, 0x00 };
size_t resp_len = 0;
resp_len = build_bpreader_response(bfr_data, sizeof(bfr_data), &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_set_output_cmd!\n");
return E_FAIL;
}
}
2023-09-25 09:16:26 +02:00
static HRESULT bpreader_send_mifare_cmd()
{
struct bpreader_read_mifare_req req = { bp_uart.written.bytes[7], bp_uart.written.bytes[8], bp_uart.written.bytes[9] };
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD5, 0x41 };
uint8_t data[17] = { 0x00 };
uint8_t buff[26] = { 0x00 };
size_t resp_len = 0;
switch (req.subcmd) {
case MIFARE_CMD_READ:
struct bpreader_read_mifare_resp_data resp;
if (req.block > 3) {
dprintf("Reader: MIFARE block number %d is out of range!\n", req.block);
return E_FAIL;
}
dprintf("Reader: Mifare Read Block 0x%02X\n", req.block);
resp.padding = 0;
memcpy_s(resp.block, sizeof(resp.block), card.sectors[0].blocks[req.block].bytes, sizeof(card.sectors[0].blocks[req.block].bytes));
header.data_len = 0x13;
resp_len = build_bpreader_response((uint8_t *)&resp, 0x11, &header, buff, sizeof(buff));
break;
case MIFARE_CMD_AUTH_KEY_A:
dprintf("Reader: Mifare Authentication with Key A\n");
return iobuf_write(&bp_uart.readable, BPREADER_CMD_GO_NEXT, sizeof(BPREADER_CMD_GO_NEXT));
break;
case MIFARE_CMD_AUTH_KEY_B:
dprintf("Reader: Mifare Authentication with Key B\n");
resp_len = build_bpreader_response(data, 1, &header, buff, sizeof(buff));
break;
default:
dprintf("Reader: Unknown Mifare Command 0x%02x\n", req.subcmd);
return E_FAIL;
}
if (resp_len > 0) {
return iobuf_write(&bp_uart.readable, buff, resp_len);
} else {
dprintf("Reader: No data to return in bpreader_send_mifare_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_unk_52_cmd()
{
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD5, 0x53 };
uint8_t buff[10] = { 0x00 };
uint8_t bfr_data[2] = { 0x01, 0x00 };
size_t resp_len = 0;
resp_len = build_bpreader_response(bfr_data, 2, &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_unk_52_cmd!\n");
return E_FAIL;
}
}
static HRESULT bpreader_unk_44_cmd()
{
struct bpreader_cmd_header header = { 0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD5, 0x45 };
uint8_t buff[10] = { 0x00 };
uint8_t bfr_data[2] = { 0x01, 0x00 };
size_t resp_len = 0;
resp_len = build_bpreader_response(bfr_data, 2, &header, buff, sizeof(buff));
if (resp_len > 0) {
HRESULT hr = iobuf_write(&bp_uart.readable, buff, resp_len);
return hr;
} else {
dprintf("Reader: No data to return in bpreader_unk_44_cmd!\n");
return E_FAIL;
}
}