update procaddr hook

This commit is contained in:
Hay1tsme 2023-12-10 20:47:43 -05:00
parent ccdd07e262
commit 2ec0ee4794
10 changed files with 42 additions and 188 deletions

View File

@ -5,7 +5,7 @@
#include "cxbhook/led.h" #include "cxbhook/led.h"
#include "cxbhook/cxb-dll.h" #include "cxbhook/cxb-dll.h"
#include "hooklib/procaddr.h" #include "hook/procaddr.h"
#include "hook/table.h" #include "hook/table.h"
@ -50,7 +50,7 @@ static struct hook_symbol lamp_syms[] = {
HRESULT led_hook_init(struct led_config *cfg) HRESULT led_hook_init(struct led_config *cfg)
{ {
dprintf("LED: Init\n"); dprintf("LED: Init\n");
return proc_addr_table_push("CommLamp.dll", lamp_syms, _countof(lamp_syms)); return proc_addr_table_push(NULL, "CommLamp.dll", lamp_syms, _countof(lamp_syms));
} }
static int my_cCommLamp_Open(char *port) static int my_cCommLamp_Open(char *port)

View File

@ -6,7 +6,7 @@
#include "cxbhook/revio.h" #include "cxbhook/revio.h"
#include "cxbhook/cxb-dll.h" #include "cxbhook/cxb-dll.h"
#include "hooklib/procaddr.h" #include "hook/procaddr.h"
#include "hook/table.h" #include "hook/table.h"
@ -83,7 +83,7 @@ static struct hook_symbol revio_syms[] = {
HRESULT revio_hook_init(struct revio_config *cfg) HRESULT revio_hook_init(struct revio_config *cfg)
{ {
dprintf("Revio: Init\n"); dprintf("Revio: Init\n");
return proc_addr_table_push("CommIo.dll", revio_syms, _countof(revio_syms)); return proc_addr_table_push(NULL, "CommIo.dll", revio_syms, _countof(revio_syms));
} }
static int my_cCommIo_Open(char *port) static int my_cCommIo_Open(char *port)

View File

@ -23,8 +23,6 @@ hooklib_lib = static_library(
'fdshark.h', 'fdshark.h',
'path.c', 'path.c',
'path.h', 'path.h',
'procaddr.c',
'procaddr.h',
'reg.c', 'reg.c',
'reg.h', 'reg.h',
'setupapi.c', 'setupapi.c',

View File

@ -1,130 +0,0 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <libgen.h>
#include "hooklib/procaddr.h"
#include "hook/table.h"
#include "util/dprintf.h"
static struct proc_addr_table *proc_addr_hook_list;
static size_t proc_addr_hook_count;
static CRITICAL_SECTION proc_addr_hook_lock;
static bool proc_addr_hook_initted;
static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name);
static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name);
static void proc_addr_hook_init(void);
static const struct hook_symbol win32_hooks[] = {
{
.name = "GetProcAddress",
.patch = my_GetProcAddress,
.link = (void **) &next_GetProcAddress
}
};
HRESULT proc_addr_table_push(
const char *target,
struct hook_symbol *syms,
size_t nsyms
)
{
HRESULT hr;
struct proc_addr_table *new_item;
struct proc_addr_table *new_mem;
proc_addr_hook_init();
EnterCriticalSection(&proc_addr_hook_lock);
new_mem = realloc(
proc_addr_hook_list,
(proc_addr_hook_count + 1) * sizeof(struct proc_addr_table));
if (new_mem == NULL) {
hr = E_OUTOFMEMORY;
LeaveCriticalSection(&proc_addr_hook_lock);
return hr;
}
new_item = &new_mem[proc_addr_hook_count];
new_item->name = target;
new_item->nsyms = nsyms;
new_item->syms = syms;
proc_addr_hook_list = new_mem;
proc_addr_hook_count++;
hr = S_OK;
LeaveCriticalSection(&proc_addr_hook_lock);
return hr;
}
static void proc_addr_hook_init(void)
{
if (proc_addr_hook_initted) {
return;
}
dprintf("ProcAddr: Hook init\n");
proc_addr_hook_initted = true;
InitializeCriticalSection(&proc_addr_hook_lock);
proc_addr_insert_hooks(NULL);
}
void proc_addr_insert_hooks(HMODULE target)
{
hook_table_apply(
target,
"kernel32.dll",
win32_hooks,
_countof(win32_hooks));
}
FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name)
{
uintptr_t ordinal = (uintptr_t) name;
char mod_path[PATH_MAX];
char *mod_name;
const struct hook_symbol *sym;
FARPROC result = next_GetProcAddress(hModule, name);
GetModuleFileNameA(hModule, mod_path, PATH_MAX);
mod_name = basename(mod_path);
for (int i = 0; i < proc_addr_hook_count; i++) {
if (strcmp(proc_addr_hook_list[i].name, mod_name) == 0) {
for (int j = 0; j < proc_addr_hook_list[i].nsyms; j++) {
sym = &proc_addr_hook_list[i].syms[j];
if (ordinal > 0xFFFF) {
if (strcmp(sym->name, name) == 0) {
dprintf("ProcAddr: Hooking %s from %s\n", name, mod_name);
result = (FARPROC) sym->patch;
}
}
else {
if (sym->ordinal == ordinal) {
dprintf("ProcAddr: Hooking Ord %p from %s\n", (void *)ordinal, mod_name);
result = (FARPROC) sym->patch;
}
}
}
}
}
return result;
}

View File

@ -1,20 +0,0 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include "hook/table.h"
struct proc_addr_table {
const char *name;
size_t nsyms;
struct hook_symbol *syms;
};
HRESULT proc_addr_table_push(
const char *target,
struct hook_symbol *syms,
size_t nsyms
);
void proc_addr_insert_hooks(HMODULE target);

View File

@ -7,7 +7,7 @@
#include "hook/table.h" #include "hook/table.h"
#include "hooklib/reg.h" #include "hooklib/reg.h"
#include "hooklib/procaddr.h" #include "hook/procaddr.h"
#include "util/dprintf.h" #include "util/dprintf.h"
#include "util/str.h" #include "util/str.h"
@ -313,6 +313,7 @@ static void reg_hook_init(void)
reg_hook_insert_hooks(NULL); reg_hook_insert_hooks(NULL);
proc_addr_table_push( proc_addr_table_push(
NULL,
"ADVAPI32.dll", "ADVAPI32.dll",
(struct hook_symbol *) reg_hook_syms, (struct hook_symbol *) reg_hook_syms,
_countof(reg_hook_syms)); _countof(reg_hook_syms));

View File

@ -18,7 +18,7 @@
#include "hooklib/spike.h" #include "hooklib/spike.h"
#include "hooklib/path.h" #include "hooklib/path.h"
#include "hooklib/reg.h" #include "hooklib/reg.h"
#include "hooklib/procaddr.h" #include "hook/procaddr.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "mai2hook/config.h" #include "mai2hook/config.h"

View File

@ -10,6 +10,7 @@
#include "mai2hook/mai2-dll.h" #include "mai2hook/mai2-dll.h"
#include "util/dprintf.h" #include "util/dprintf.h"
#include "util/dump.h"
const char CMD_START = '{'; const char CMD_START = '{';
const char CMD_END = '}'; const char CMD_END = '}';
@ -26,6 +27,7 @@ const char CMD_SENS_CHECK[2] = "th";
const char CMD_RESET[7] = "{RSET}"; // Reset board to default state const char CMD_RESET[7] = "{RSET}"; // Reset board to default state
const char CMD_STAT[7] = "{STAT}"; // Start sending touch state const char CMD_STAT[7] = "{STAT}"; // Start sending touch state
const char CMD_HALT[7] = "{HALT}"; // Stop sending touch state const char CMD_HALT[7] = "{HALT}"; // Stop sending touch state
const char RSP_ANY[6] = "(0000)"; // Stop sending touch state
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes); static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes); static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes);
@ -33,6 +35,7 @@ static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes);
static HRESULT touch_handle_irp(struct irp *irp); static HRESULT touch_handle_irp(struct irp *irp);
static HRESULT touch0_handle_irp_locked(struct irp *irp); static HRESULT touch0_handle_irp_locked(struct irp *irp);
static HRESULT touch1_handle_irp_locked(struct irp *irp); static HRESULT touch1_handle_irp_locked(struct irp *irp);
static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side);
bool touch0_auto = false; bool touch0_auto = false;
bool touch1_auto = false; bool touch1_auto = false;
@ -149,7 +152,7 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp)
//hr = mai2_dll.touch_init(); //hr = mai2_dll.touch_init();
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Mai2 touch: Backend error: %x\n", (int) hr); dprintf("Mai2 touch0: Backend error: %x\n", (int) hr);
return hr; return hr;
} }
@ -163,24 +166,16 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp)
for (;;) { for (;;) {
#if 0 #if 0
dprintf("TX0 Buffer:\n"); dprintf("touch0 Buffer:\n");
dump_iobuf(&touch0_uart.written); dump_iobuf(&touch0_uart.written);
#endif #endif
//hr = touch_frame_decode(&req, &touch0_uart.written, 0); hr = touch_cmd_dispatch((char*)touch0_uart.written.bytes, &touch0_uart.readable, 0);
if (hr != S_OK) {
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Mai2 touch: Deframe error: %x\n", (int) hr); dprintf("Mai2 touch0: Dispatch failed %08lX\n", hr);
}
return hr; return hr;
} }
//hr = touch_req_dispatch(&req); touch0_uart.written.pos = 0;
if (FAILED(hr)) {
dprintf("Mai2 touch: Processing error: %x\n", (int) hr);
}
return hr; return hr;
} }
@ -195,13 +190,13 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp)
//hr = mai2_dll.touch_init(); //hr = mai2_dll.touch_init();
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Mai2 touch: Backend error: %x\n", (int) hr); dprintf("Mai2 touch1: Backend error: %x\n", (int) hr);
return hr; return hr;
} }
} }
hr = uart_handle_irp(&touch0_uart, irp); hr = uart_handle_irp(&touch1_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) { if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr; return hr;
@ -209,25 +204,35 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp)
for (;;) { for (;;) {
#if 0 #if 0
dprintf("TX0 Buffer:\n"); dprintf("touch1 Buffer:\n");
dump_iobuf(&touch0_uart.written); dump_iobuf(&touch0_uart.written);
#endif #endif
//hr = touch_frame_decode(&req, &touch0_uart.written, 0);
if (hr != S_OK) { hr = touch_cmd_dispatch((char*)touch1_uart.written.bytes, &touch1_uart.readable, 1);
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Mai2 touch: Deframe error: %x\n", (int) hr); dprintf("Mai2 touch1: Dispatch failed %08lX\n", hr);
}
return hr; return hr;
} }
//hr = touch_req_dispatch(&req); touch1_uart.written.pos = 0;
if (FAILED(hr)) {
dprintf("Mai2 touch: Processing error: %x\n", (int) hr);
}
return hr; return hr;
} }
} }
static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side)
{
if (!strcmp(cmd, CMD_RESET)) {
dprintf("Mai2 touch%d: Reset\n", side);
return S_OK;
}
else if (!strcmp(cmd, CMD_HALT)) {
dprintf("Mai2 touch%d: Halt\n", side);
Sleep(1001); // ?
return S_OK;
}
dprintf("Mai2 touch%d: Unknow %s\n", side, cmd);
return S_OK;
}

View File

@ -8,7 +8,7 @@
#include "hooklib/dll.h" #include "hooklib/dll.h"
#include "hooklib/path.h" #include "hooklib/path.h"
#include "hooklib/reg.h" #include "hooklib/reg.h"
#include "hooklib/procaddr.h" #include "hook/procaddr.h"
#include "hooklib/serial.h" #include "hooklib/serial.h"
#include "util/dprintf.h" #include "util/dprintf.h"

View File

@ -1,4 +1,4 @@
[wrap-git] [wrap-git]
directory = capnhook directory = capnhook
url = https://github.com/Hay1tsme/capnhook url = https://github.com/Hay1tsme/capnhook
revision = 888d068d58e68cf702e0cee872959a71413a7b55 revision = dbdcd61b3a3043b08f86f959bd45df4967503a77