diff --git a/board/config.c b/board/config.c index 19d47bc..96c4146 100644 --- a/board/config.c +++ b/board/config.c @@ -75,6 +75,15 @@ void aime_config_load(struct aime_config *cfg, const wchar_t *filename) cfg->port_no = GetPrivateProfileIntW(L"aime", L"portNo", 0, filename); cfg->high_baudrate = GetPrivateProfileIntW(L"aime", L"highBaud", 1, filename); cfg->gen = GetPrivateProfileIntW(L"aime", L"gen", 0, filename); + cfg->proxy_flag = GetPrivateProfileIntW(L"aime", L"proxyFlag", 2, filename); + + GetPrivateProfileStringW( + L"aime", + L"authdataPath", + L"DEVICE\\authdata.bin", + cfg->authdata_path, + _countof(cfg->authdata_path), + filename); } void io4_config_load(struct io4_config *cfg, const wchar_t *filename) diff --git a/board/sg-nfc-cmd.h b/board/sg-nfc-cmd.h index 08923fd..e54e0a7 100644 --- a/board/sg-nfc-cmd.h +++ b/board/sg-nfc-cmd.h @@ -5,21 +5,21 @@ #pragma pack(push, 1) enum { - SG_NFC_CMD_GET_FW_VERSION = 0x30, - SG_NFC_CMD_GET_HW_VERSION = 0x32, - SG_NFC_CMD_RADIO_ON = 0x40, - SG_NFC_CMD_RADIO_OFF = 0x41, - SG_NFC_CMD_POLL = 0x42, - SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43, - SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x50, - SG_NFC_CMD_MIFARE_AUTHENTICATE_A = 0x51, - SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52, - SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x54, - SG_NFC_CMD_MIFARE_AUTHENTICATE_B = 0x55, - SG_NFC_CMD_TO_UPDATE_MODE = 0x60, - SG_NFC_CMD_SEND_HEX_DATA = 0x61, - SG_NFC_CMD_RESET = 0x62, - SG_NFC_CMD_FELICA_ENCAP = 0x71, + SG_NFC_CMD_GET_FW_VERSION = 0x30, + SG_NFC_CMD_GET_HW_VERSION = 0x32, + SG_NFC_CMD_RADIO_ON = 0x40, + SG_NFC_CMD_RADIO_OFF = 0x41, + SG_NFC_CMD_POLL = 0x42, + SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43, + SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x50, + SG_NFC_CMD_MIFARE_AUTHENTICATE_AIME = 0x51, + SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52, + SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x54, + SG_NFC_CMD_MIFARE_AUTHENTICATE_BANA = 0x55, + SG_NFC_CMD_TO_UPDATE_MODE = 0x60, + SG_NFC_CMD_SEND_HEX_DATA = 0x61, + SG_NFC_CMD_RESET = 0x62, + SG_NFC_CMD_FELICA_ENCAP = 0x71, }; struct sg_nfc_res_get_fw_version { diff --git a/board/sg-nfc.c b/board/sg-nfc.c index eac0984..a072111 100644 --- a/board/sg-nfc.c +++ b/board/sg-nfc.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include "util/dprintf.h" #include "util/dump.h" +#include "util/slurp.h" static HRESULT sg_nfc_dispatch( void *ctx, @@ -87,6 +89,8 @@ void sg_nfc_init( uint8_t addr, const struct sg_nfc_ops *ops, unsigned int gen, + unsigned int proxy_flag, + const wchar_t* authdata_path, void *ops_ctx) { assert(nfc != NULL); @@ -96,6 +100,8 @@ void sg_nfc_init( nfc->ops_ctx = ops_ctx; nfc->addr = addr; nfc->gen = gen; + nfc->proxy_flag = proxy_flag; + nfc->authdata_path = authdata_path; } #ifdef NDEBUG @@ -189,8 +195,8 @@ static HRESULT sg_nfc_dispatch( &req->felica_encap, &res->felica_encap); - case SG_NFC_CMD_MIFARE_AUTHENTICATE_A: - case SG_NFC_CMD_MIFARE_AUTHENTICATE_B: + case SG_NFC_CMD_MIFARE_AUTHENTICATE_AIME: + case SG_NFC_CMD_MIFARE_AUTHENTICATE_BANA: case SG_NFC_CMD_SEND_HEX_DATA: return sg_nfc_cmd_send_hex_data(nfc, &req->simple, &res->simple); @@ -382,18 +388,62 @@ static HRESULT sg_nfc_cmd_mifare_read_block( sg_nfc_dprintf(nfc, "Read uid %08x block %i\n", uid, req->payload.block_no); - if (req->payload.block_no > 3) { + if (req->payload.block_no > 14) { sg_nfc_dprintf(nfc, "MIFARE block number out of range\n"); return E_FAIL; + } else if (req->payload.block_no >= 5){ // emoney auth encrypted + + sg_res_init(&res->res, &req->req, sizeof(res->block)); + + char* auth; + long size = wslurp(nfc->authdata_path, &auth, false); + if (size < 0){ + sg_nfc_dprintf(nfc, "Failed to read %ls: %lx!\n", nfc->authdata_path, GetLastError()); + return E_FAIL; + } + + int offset = 0; + if (req->payload.block_no == 6){ + offset = 16; + } else if (req->payload.block_no == 8){ + offset = 32; + } else if (req->payload.block_no == 9){ + offset = 48; + } else if (req->payload.block_no == 10){ + offset = 64; + } else if (req->payload.block_no == 12){ + offset = 82; + } else if (req->payload.block_no == 13){ + offset = 98; + } else if (req->payload.block_no == 14){ + offset = 114; + } + + for (int i = 0; i < 16 && offset + i < size; i++){ + res->block[i] = auth[offset + i]; + } + + free(auth); + + } else if (req->payload.block_no == 4){ // emoney auth plain + + sg_res_init(&res->res, &req->req, sizeof(res->block)); + + res->block[0] = 0x54; // header + res->block[1] = 0x43; + res->block[2] = nfc->proxy_flag; // 2 or 3 depending on game (useProxy in env.json) + res->block[3] = 0x01; // unknown flag + + } else { // read all other blocks normally + + sg_res_init(&res->res, &req->req, sizeof(res->block)); + + memcpy( res->block, + nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes, + sizeof(res->block)); } - sg_res_init(&res->res, &req->req, sizeof(res->block)); - - memcpy( res->block, - nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes, - sizeof(res->block)); - return S_OK; } diff --git a/board/sg-nfc.h b/board/sg-nfc.h index 3c8eb49..092b3e3 100644 --- a/board/sg-nfc.h +++ b/board/sg-nfc.h @@ -23,8 +23,10 @@ struct sg_nfc { void *ops_ctx; uint8_t addr; unsigned int gen; + unsigned int proxy_flag; struct felica felica; struct mifare mifare; + const wchar_t* authdata_path; }; void sg_nfc_init( @@ -32,6 +34,8 @@ void sg_nfc_init( uint8_t addr, const struct sg_nfc_ops *ops, unsigned int gen, + unsigned int proxy_flag, + const wchar_t* authdata_path, void *ops_ctx); void sg_nfc_transact( diff --git a/board/sg-reader.c b/board/sg-reader.c index bcc9701..2dc3c3d 100644 --- a/board/sg-reader.c +++ b/board/sg-reader.c @@ -81,7 +81,7 @@ HRESULT sg_reader_hook_init( return E_INVALIDARG; } - sg_nfc_init(&sg_reader_nfc, 0x00, &sg_reader_nfc_ops, gen, NULL); + sg_nfc_init(&sg_reader_nfc, 0x00, &sg_reader_nfc_ops, gen, cfg->proxy_flag, cfg->authdata_path, NULL); sg_led_init(&sg_reader_led, 0x08, &sg_reader_led_ops, gen, NULL); InitializeCriticalSection(&sg_reader_lock); diff --git a/board/sg-reader.h b/board/sg-reader.h index 1824e28..64de7e9 100644 --- a/board/sg-reader.h +++ b/board/sg-reader.h @@ -12,6 +12,8 @@ struct aime_config { unsigned int port_no; bool high_baudrate; unsigned int gen; + unsigned int proxy_flag; + wchar_t authdata_path[MAX_PATH]; }; HRESULT sg_reader_hook_init( diff --git a/doc/config/common.md b/doc/config/common.md index 8ef48eb..1485feb 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -101,6 +101,18 @@ emulates an IC card in its proximity. A variety of different IC cards can be emulated; the exact choice of card that is emulated depends on the presence or absence of the configured card ID files. +### `proxyFlag` + +Default: `2` + +The "proxy flag" of the emulated Thinca authentication card. This should be 2 if no proxy is used, and 3 if it is. Invalid values will break Thinca authentication card reading. This information can be obtained by checking for the presence of "use_proxy: true" `tfps-res-pro\env.json`. + +### `authdataPath` + +Default: `DEVICE\authdata.bin` + +Path to the binary file containing data for a Thinca authentication card (see `emoney.txt`) + ## `[vfd]` Controls emulation of the VFD GP1232A02A FUTABA assembly. @@ -636,3 +648,19 @@ Default: Empty string Configure the location of the "Option" data mount point. This mount point is optional (hence the name, probably) and contains directories which contain minor over-the-air content updates. + +## `[epay]` + +Configure Thinca Payment (E-Money) emulation and hooks. + +### `enable` + +Default: `1` + +Enables the Thinca emulation. This will allow you to enable E-Money on compatible servers. + +### `hook` + +Default: `0` + +Enables hooking of respective Thinca DLL functions to emulate the existence of E-Money. This cannot be used with a real E-Money server. \ No newline at end of file diff --git a/doc/emoney.md b/doc/emoney.md new file mode 100644 index 0000000..9ef063f --- /dev/null +++ b/doc/emoney.md @@ -0,0 +1,189 @@ +# E-Money Authentication Procedure +by Haruka Akechi + +### SETTING UP + +1) Obtain the 64 byte long authentication card encryption key and the 32 byte long static authentication card ID. amdaemon.exe holds the secrets. + +2) Get this java file, insert the ID and key, probably edit the passphrase and compile+run to generate authcard.bin: https://gist.github.com/akechi-haruka/a506184638e695a04eabe8cb53f62c36 + +3) Place authcard.bin in your DEVICE folder. + +4) Check tfps-res-pro\env.json for your game. If it contains a "use_proxy: true" statement, add "proxy_flag=3" under [aime] + +5) Replace the two URLs in tfps-res-pro\resource.xml to your servers'. This is to ensure the Host header will match the certificate's. + +6) Where amdaemon.exe is located, there should be a "ca.pem". Replace this file with either [this](https://curl.se/ca/cacert.pem) for the most common CA's (including Let's Encrypt), or whatever CA the server is using (your server will provide this). + +7) Run your game and enter the test menu, and navigate to E-Money Settings. + +8) Select "Terminal Authentication" + +9) Hold your key for scanning a card (default: RETURN) + +10) If your shop name shows up, everything was done succesfully. Otherwise, check the VFD. + +### TECHNICAL INFO + +For debugging anything e-money related, I highly recommend setting "emoney.log.level" to 4 in your game's amdaemon config.json. This should create a `\emoney_log\thincapayment.log`. + +When terminal authentication is started from the test menu, the game will check for an Aime reader of at least generation 3. If that is not fulfilled, the VFD will display "unsupported card reader" and abort. If the card reader is good, the VFD will prompt for a card to be touched, and the reader will start scanning for a MIFARE card, which from this point we call "Thinca Authentication Card". This card contains one unencrypted block (3) which contains: + +[0] = 0x54 // T + +[1] = 0x43 // C + +[2] = proxy_type + +[3] = 0x01 + +Afterwards, a number of encrypted blocks are read, namely the blocks 5,6,8,9,10,12,13 and 14. These blocks together form a 130 byte long binary blob that contains the authentication data. + +This data is encrypted as following: + +Given a fixed 0x40 byte long encryption key and a fixed 0x20 byte long static "card ID", both of which can be found in amdaemon: +XOR every byte of the encryption key with 0x1C. +Calculate a 0x20 bytes long HMAC-SHA-256 of the card ID with the XOR'ed encryption key as the key. +Calculate the needed IV by XORing the lower 0x10 bytes of the HMAC with the upper 0x10 bytes of the HMAC: + +``` +byte[] iv = new byte[16]; +for (int i = 0; i < 16; i++) { + iv[i] = (byte) (hmac[i + 16] ^ hmac[i]); +} +``` + +With this IV, and the HMAC as the key, finally encrypt the data with AES/CBC/PKCS5Padding. + +Now what is actually stored on such a card? This: + +``` ++---------------+---------------+-----------------+------------+----------+ +| Store Card ID | Merchant Code | Store Branch ID | Passphrase | NULL | ++---------------+---------------+-----------------+------------+----------+ +| 0x10 bytes | 0x14 bytes | 0xC bytes | 0x10 bytes | 0x1 byte | +| char* | char* | uint128_t | char* | NULL | ++---------------+---------------+-----------------+------------+----------+ +``` + +Only two things really matter here. The Store Branch ID must be non-zero, otherwise amdaemon will reject it, and the passphrase, which is the PFX key password for the certificate returned in the network response (see below). + +Technically with the Store Card ID you could bind different auth cards to different users, but for home usage, it really doesn't matter. + +That's the Thinca Authentication Card out of the way, so continue on to: + +### NETWORK + +First, a regular HTTP(S) connection will be made to the URL specified in tfps-res-pro\env.json, tasms.root_endpoint. + +Request Data: +`{"modelName":"ACA","serialNumber":"ACAE01A9999","merchantCode":"NOTSEGA","storeBranchNumber":11111,"storeCardId":"FAKESTORE"}` + +Note that the serialNumber here actually isn't the keychip ID, but rather the PCBID. The three other values are read from the Thinca Authentication Card. + +Response Headers: +`x-certificate-md5: ` + +Response Data: +``` +{ + "certificate": "", + "initSettings": { + "endpoints": { + "terminals": { + "uri": "https://localhost/emoney/terminals" + }, + "statuses": { + "uri": "https://localhost/emoney/statuses" + }, + "sales": { + "uri": "https://localhost/emoney/sales" + }, + "counters": { + "uri": "https://localhost/emoney/counters" + } + }, + "intervals": { + "checkSetting": 60, + "sendStatus": 60 + }, + "settigsType": "AmusementTerminalSettings", // sic + "status": "1", // a string + "terminalId": "536453645364536453645364536453", // must be exactly 30 characters + "version": "2024-01-01T01:01:01", // a timestamp + "availableElectronicMoney": [ + 1, + 2, + 3, + 5, + 6, + 8, + 9, + 91, // aimepay + 101 // "cash" ??? + ], + "cashAvailability": true, + "productCode": 1337 + } +} +``` + +Next up, we will connect to the TLAM service to get the URL for the TCAP service. Everything from this point on requires not only HTTPS, but also client certificate validation (which is the certificate returned from the previous request). Technically you don't need to validate it, but you must accept a client certificate or the client HTTPS library will not be happy. + +The client certificate itself must be signed with the same key than the server's HTTPS certificate. + +The client AND server certificate must have it's CA included in the "ca.pem" file in amdaemon's directory. You can freely replace this file with https://curl.se/ca/cacert.pem to allow Let's Encrypt and whatever else. + +At this point, "ThincaPayment::setClientCertificate(). ErrCode 203" means that the downloaded file couldn't be found, or the certificate password is wrong. + +[Warn ] TCAP communicate error 06514086 means the ca.pem has no entry for the given server CA. + +TLAM: +The TLAM url comes from tftp-res-pro\resource.xml, commonPrimaryUri. + +/initauth.jsp + +Request Data: + +Response Headers: +`Content-Type: application/x-tlam` +Response Data: +`SERV=https://localhost/emoney/tcap` + +TCAP: +Here be dragons. + +Is it this?? https://en.wikipedia.org/wiki/Transaction_Capabilities_Application_Part +Maybe thincatcapclient.dll holds the secrets? + +Request Data: +``` +02 05 01 00 ba 00 00 00 21 00 00 00 00 00 25 00 ....ยบ... !.....%. +9f 00 01 00 00 07 47 65 6e 65 72 69 63 06 43 4c ......Ge neric.CL +49 45 4e 54 00 02 00 00 07 47 65 6e 65 72 69 63 IENT.... .Generic +06 53 54 41 54 55 53 00 03 00 00 07 47 65 6e 65 .STATUS. ....Gene +72 69 63 06 4f 50 54 49 4f 4e 00 04 00 00 06 46 ric.OPTI ON.....F +65 6c 69 43 61 03 52 2f 57 00 05 00 00 07 47 65 eliCa.R/ W.....Ge +6e 65 72 69 63 09 52 2f 57 5f 45 56 45 4e 54 00 neric.R/ W_EVENT. +06 00 00 07 47 65 6e 65 72 69 63 0a 52 2f 57 5f ....Gene ric.R/W_ +53 54 41 54 55 53 00 07 00 00 07 47 65 6e 65 72 STATUS.. ...Gener +69 63 0a 52 2f 57 5f 4f 50 54 49 4f 4e 00 08 00 ic.R/W_O PTION... +00 07 47 65 6e 65 72 69 63 06 4e 46 43 5f 52 57 ..Generi c.NFC_RW +00 00 00 26 00 03 02 05 00 00 00 00 22 00 00 ...&.... ....".. +``` + +and +``` +02 05 03 00 17 00 00 00 21 00 11 34 34 20 30 30 ........ !..44 00 +20 30 31 20 32 31 20 30 30 20 30 30 01 21 0 0 00 +``` + +Response Headers: +``` +Content-Type: application/x-tcap +Transfer-Encoding: chunked +``` +Response Data: +???? + +To be continued ... \ No newline at end of file diff --git a/hooklib/dns.c b/hooklib/dns.c index 45b463e..9e06e5b 100644 --- a/hooklib/dns.c +++ b/hooklib/dns.c @@ -217,20 +217,24 @@ static void dns_hook_init(void) dns_hook_initted = true; InitializeCriticalSection(&dns_hook_lock); + dns_hook_apply_hooks(NULL); +} + +void dns_hook_apply_hooks(HMODULE mod){ hook_table_apply( - NULL, + mod, "dnsapi.dll", dns_hook_syms_dnsapi, _countof(dns_hook_syms_dnsapi)); hook_table_apply( - NULL, + mod, "ws2_32.dll", dns_hook_syms_ws2, _countof(dns_hook_syms_ws2)); hook_table_apply( - NULL, + mod, "winhttp.dll", dns_hook_syms_winhttp, _countof(dns_hook_syms_winhttp)); diff --git a/hooklib/dns.h b/hooklib/dns.h index 9279a37..b03f7dc 100644 --- a/hooklib/dns.h +++ b/hooklib/dns.h @@ -8,3 +8,4 @@ void port_hook_init(unsigned short _startup_port, unsigned short _billing_port, // if to_src is NULL, all lookups for from_src will fail HRESULT dns_hook_push(const wchar_t *from_src, const wchar_t *to_src); +void dns_hook_apply_hooks(HMODULE mod); diff --git a/hooklib/path.c b/hooklib/path.c index 82f97e2..1afb8a8 100644 --- a/hooklib/path.c +++ b/hooklib/path.c @@ -11,6 +11,8 @@ #include "hooklib/path.h" +#include + /* Helpers */ static void path_hook_init(void); @@ -533,6 +535,12 @@ static BOOL path_transform_w(wchar_t **out, const wchar_t *src) goto end; } +#if LOG_VFS + if (!wcsstr(src, L"AppUser")) { + dprintf("Path: %ls -> %ls\n", src, dest); + } +#endif + break; } diff --git a/meson.build b/meson.build index aada974..9eea85d 100644 --- a/meson.build +++ b/meson.build @@ -82,6 +82,9 @@ endif if get_option('log_all') or get_option('log_clock') add_project_arguments('-DLOG_CLOCK', language: 'c') endif +if get_option('log_all') or get_option('log_vfs') + add_project_arguments('-DLOG_VFS', language: 'c') +endif shlwapi_lib = cc.find_library('shlwapi') dinput8_lib = cc.find_library('dinput8') diff --git a/meson_options.txt b/meson_options.txt index de9aefc..b6989f4 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -69,7 +69,12 @@ option('log_mercury_slider', description : 'Enable debug logging for the WACCA Slider' ) option('log_clock', - type : 'boolean', - value : false, - description : 'Enable debug logging for clock APIs' + type : 'boolean', + value : false, + description : 'Enable debug logging for clock APIs' +) +option('log_vfs', + type : 'boolean', + value : false, + description : 'Enable debug logging for file system redirections' ) diff --git a/platform/config.c b/platform/config.c index 2904019..c9777da 100644 --- a/platform/config.c +++ b/platform/config.c @@ -360,4 +360,5 @@ void epay_config_load(struct epay_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"epay", L"enable", 1, filename); + cfg->hook = GetPrivateProfileIntW(L"epay", L"hook", 0, filename); } diff --git a/platform/epay.c b/platform/epay.c index 4a231c2..6193a2d 100644 --- a/platform/epay.c +++ b/platform/epay.c @@ -7,10 +7,13 @@ #include "hook/table.h" +#include "hooklib/dns.h" #include "hooklib/reg.h" #include "platform/epay.h" +#include + #include "util/dprintf.h" static HRESULT misc_read_thinca_adapter(void *bytes, uint32_t *nbytes); @@ -119,15 +122,8 @@ static const struct hook_symbol epay_syms[] = { } }; -HRESULT epay_hook_init(const struct epay_config *cfg) { - HRESULT hr; - assert(cfg != NULL); - - if (!cfg->enable) { - return S_FALSE; - } - - hr = reg_hook_push_key( +HRESULT epay_apply_registry_hooks(){ + HRESULT hr = reg_hook_push_key( HKEY_LOCAL_MACHINE, L"SOFTWARE\\TFPaymentService\\ThincaRwAdapter", epay_adapter_keys, @@ -162,34 +158,69 @@ HRESULT epay_hook_init(const struct epay_config *cfg) { L"SOFTWARE\\TFPaymentService\\ThincaTcapClient\\URL1", epay_tcap_url1_keys, _countof(epay_tcap_url1_keys)); - - hook_table_apply( - NULL, - "ThincaPayment.dll", - epay_syms, - _countof(epay_syms)); - - thinca_stub = (struct thinca_main *)malloc(sizeof(struct thinca_main)); - thinca_stub->impl1 = (struct thinca_impl *)malloc(sizeof(struct thinca_impl)); - thinca_stub->impl1->unk8 = thinca_unk8; - thinca_stub->impl1->initialize = thinca_initialize; - thinca_stub->impl1->dispose = thinca_dispose; - thinca_stub->impl1->setResource = thinca_set_resource; - thinca_stub->impl1->setThincaPaymentLog = thinca_set_pay_log; - thinca_stub->impl1->setThincaEventInterface = thinca_set_evt_handler; - thinca_stub->impl1->setIcasClientLog = thinca_set_client_log; - thinca_stub->impl1->setIcasClientConfig = thinca_set_client_cfg; - thinca_stub->impl1->setGoodsCode = thinca_set_goods_code; - thinca_stub->impl1->setTerminalSerial = thinca_set_serial; - thinca_stub->impl1->setClientCertificate = thinca_set_cert; - thinca_stub->impl1->checkDeal = thinca_check_deal; - thinca_stub->impl1->cancelRequest = thinca_cancel; - thinca_stub->impl1->selectButton = thinca_select; - thinca_stub->impl1->unk220 = thinca_unk; - thinca_stub->impl1->unk228 = thinca_unk; - - dprintf("Epay: Init.\n"); + return hr; +} + +HRESULT epay_hook_init(const struct epay_config *cfg) { + HRESULT hr; + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + hr = epay_apply_registry_hooks(); + if (FAILED(hr)){ + return hr; + } + + dprintf("EPay: Registry initialized\n"); + + // HACK:(?) the DLLs are loaded dynamically so we just preload it and apply DNS and VFS hooks to it + HMODULE thincahttpclient = LoadLibraryA("thincahttpclient.dll"); + if (thincahttpclient != NULL){ + dns_hook_apply_hooks(thincahttpclient); + path_hook_insert_hooks(thincahttpclient); + } + HMODULE thincapayment = LoadLibraryA("ThincaPayment.dll"); + if (thincapayment != NULL){ + path_hook_insert_hooks(thincapayment); + } + HMODULE thincatcapclient = LoadLibraryA("thincatcapclient.dll"); + if (thincatcapclient != NULL){ + path_hook_insert_hooks(thincatcapclient); + } + + if (cfg->hook) { + hook_table_apply( + NULL, + "ThincaPayment.dll", + epay_syms, + _countof(epay_syms)); + + thinca_stub = (struct thinca_main *) malloc(sizeof(struct thinca_main)); + thinca_stub->impl1 = (struct thinca_impl *) malloc(sizeof(struct thinca_impl)); + + thinca_stub->impl1->unk8 = thinca_unk8; + thinca_stub->impl1->initialize = thinca_initialize; + thinca_stub->impl1->dispose = thinca_dispose; + thinca_stub->impl1->setResource = thinca_set_resource; + thinca_stub->impl1->setThincaPaymentLog = thinca_set_pay_log; + thinca_stub->impl1->setThincaEventInterface = thinca_set_evt_handler; + thinca_stub->impl1->setIcasClientLog = thinca_set_client_log; + thinca_stub->impl1->setIcasClientConfig = thinca_set_client_cfg; + thinca_stub->impl1->setGoodsCode = thinca_set_goods_code; + thinca_stub->impl1->setTerminalSerial = thinca_set_serial; + thinca_stub->impl1->setClientCertificate = thinca_set_cert; + thinca_stub->impl1->checkDeal = thinca_check_deal; + thinca_stub->impl1->cancelRequest = thinca_cancel; + thinca_stub->impl1->selectButton = thinca_select; + thinca_stub->impl1->unk220 = thinca_unk; + thinca_stub->impl1->unk228 = thinca_unk; + + dprintf("Epay: Hooks initialized\n"); + } return hr; } diff --git a/platform/epay.h b/platform/epay.h index 6b5659b..70aa886 100644 --- a/platform/epay.h +++ b/platform/epay.h @@ -7,6 +7,7 @@ #pragma pack(push,1) struct epay_config { bool enable; + bool hook; }; /* The functions in these structs are how clients like amdaemon interface @@ -61,4 +62,5 @@ struct thinca_main { }; #pragma pack(pop) -HRESULT epay_hook_init(const struct epay_config *cfg); \ No newline at end of file +HRESULT epay_hook_init(const struct epay_config *cfg); +HRESULT epay_apply_registry_hooks(); \ No newline at end of file diff --git a/util/meson.build b/util/meson.build index 0274ad5..58782d3 100644 --- a/util/meson.build +++ b/util/meson.build @@ -21,6 +21,8 @@ util_lib = static_library( 'get_function_ordinal.h', 'lib.c', 'lib.h', + 'slurp.c', + 'slurp.h', 'str.c', 'str.h', 'env.c', diff --git a/util/slurp.c b/util/slurp.c new file mode 100644 index 0000000..1af73a4 --- /dev/null +++ b/util/slurp.c @@ -0,0 +1,67 @@ +#include +#include +#include +/* + * 'slurp' reads the file identified by 'path' into a character buffer + * pointed at by 'buf', optionally adding a terminating NUL if + * 'add_nul' is true. On success, the size of the file is returned; on + * failure, -1 is returned and ERRNO is set by the underlying system + * or library call that failed. + * + * WARNING: 'slurp' malloc()s memory to '*buf' which must be freed by + * the caller. + */ +long wslurp(const wchar_t* path, char **buf, bool add_nul) +{ + FILE *fp; + size_t fsz; + long off_end; + int rc; + + /* Open the file */ + fp = _wfopen(path, L"rb"); + if( NULL == fp ) { + return -1L; + } + + /* Seek to the end of the file */ + rc = fseek(fp, 0L, SEEK_END); + if( 0 != rc ) { + return -1L; + } + + /* Byte offset to the end of the file (size) */ + if( 0 > (off_end = ftell(fp)) ) { + return -1L; + } + fsz = (size_t)off_end; + + /* Allocate a buffer to hold the whole file */ + *buf = malloc( fsz+(int)add_nul ); + if( NULL == *buf ) { + return -1L; + } + + /* Rewind file pointer to start of file */ + rewind(fp); + + /* Slurp file into buffer */ + if( fsz != fread(*buf, 1, fsz, fp) ) { + free(*buf); + return -1L; + } + + /* Close the file */ + if( EOF == fclose(fp) ) { + free(*buf); + return -1L; + } + + if( add_nul ) { + /* Make sure the buffer is NUL-terminated, just in case */ + buf[fsz] = '\0'; + } + + /* Return the file size */ + return (long)fsz; +} \ No newline at end of file diff --git a/util/slurp.h b/util/slurp.h new file mode 100644 index 0000000..407a2d8 --- /dev/null +++ b/util/slurp.h @@ -0,0 +1,12 @@ +#include +/* + * 'slurp' reads the file identified by 'path' into a character buffer + * pointed at by 'buf', optionally adding a terminating NUL if + * 'add_nul' is true. On success, the size of the file is returned; on + * failure, -1 is returned and ERRNO is set by the underlying system + * or library call that failed. + * + * WARNING: 'slurp' malloc()s memory to '*buf' which must be freed by + * the caller. + */ +long wslurp(const wchar_t* path, char **buf, bool add_nul); \ No newline at end of file