Files
WoW-Hybrid-gateway/native/nohelper/original_connect_export_hook.c
2026-11-23 03:28:32 +02:00

286 lines
13 KiB
C

#include <windows.h>
#include <stdint.h>
#include <string.h>
#include "direct_near_relays.h"
#include "direct_two_site_transaction.h"
#include "original_connect_export_hook.h"
#include "direct_login_dr0_observer.h"
static const uint8_t expected_entry[32] = {
0x41,0x55,0x41,0x54,0x55,0x57,0x56,0x53,
0x48,0x83,0xec,0x68,0x49,0x89,0xcc,0x49,
0x89,0xd5,0x44,0x89,0xc3,0xf6,0x05,0x14,
0xad,0x00,0x00,0x08,0x0f,0x85,0xae,0x00
};
static const uintptr_t c49_return = (uintptr_t)0x142252cd5ULL;
static const uintptr_t c49_upstream_return = (uintptr_t)0x142253148ULL;
static const uintptr_t c49_next_upstream_return = (uintptr_t)0x14308180eULL;
static const uintptr_t c49_owner_orchestrator_return = (uintptr_t)0x14307c1caULL;
static struct GwDirectRelays relays;
static volatile LONG attempted, observed;
void *gw_export_connect_trampoline;
int (WINAPI *gw_export_wsa_get_error)(void);
void (WINAPI *gw_export_wsa_set_error)(int);
extern void gw_export_connect_thunk(void);
extern void gw_bridge_nohelper_log(uint32_t event, uint32_t result,
uint32_t error);
static int read_exact(const void *source, void *out, SIZE_T bytes)
{
SIZE_T got = 0;
return ReadProcessMemory(GetCurrentProcess(), source, out, bytes, &got) &&
got == bytes;
}
static int read_mapped_span(uintptr_t address, void *value, SIZE_T bytes)
{
MEMORY_BASIC_INFORMATION page = {0};
uintptr_t base;
if (!address || !bytes || address > UINTPTR_MAX - bytes ||
VirtualQuery((const void *)address, &page, sizeof(page)) !=
sizeof(page) || page.State != MEM_COMMIT ||
(page.Protect & (PAGE_GUARD | PAGE_NOACCESS)) != 0)
return 0;
base = (uintptr_t)page.BaseAddress;
if (address < base || page.RegionSize < bytes ||
address - base > page.RegionSize - bytes)
return 0;
return read_exact((const void *)address, value, bytes);
}
static int read_mapped_qword(uintptr_t address, uintptr_t *value)
{
return read_mapped_span(address, value, sizeof(*value));
}
/* Called before the original connect. The assembly thunk restores the saved
* WSA error, registers and flags, then tail-jumps to a replay of the original
* five pushes. No call is blocked, redirected or repeated. */
void gw_export_connect_observe(const void *address, int length,
uintptr_t return_pc, uintptr_t rdi,
uintptr_t entry_rsp)
{
uint8_t header[8];
FILETIME time;
HMODULE caller = NULL;
uintptr_t owner;
uintptr_t c49_caller = 0, callback_interface = 0;
uintptr_t callback_vtable = 0, callback_target = 0;
uintptr_t upstream_caller = 0, next_caller = 0, outer_caller = 0;
HMODULE upstream_module = NULL, next_module = NULL, outer_module = NULL;
uint32_t witness_status = 0, upstream_error = 0, next_error = 0;
uint32_t outer_error = 0;
if (!address || length < (int)sizeof(header) ||
InterlockedCompareExchange(&observed, 1, 1) != 0 ||
!read_exact(address, header, sizeof(header)) ||
header[0] != 2 || header[1] != 0 ||
header[2] != 0x04 || header[3] != 0x5f ||
header[4] != 127 || header[5] != 0 ||
header[6] != 0 || header[7] != 1 ||
InterlockedCompareExchange(&observed, 1, 0) != 0)
return;
GetSystemTimeAsFileTime(&time);
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)return_pc, &caller);
owner = return_pc == c49_return ? rdi : 0;
gw_bridge_nohelper_log(56, (uint32_t)return_pc,
(uint32_t)(return_pc >> 32));
gw_bridge_nohelper_log(57, (uint32_t)owner, (uint32_t)(owner >> 32));
gw_bridge_nohelper_log(58, time.dwLowDateTime, time.dwHighDateTime);
gw_bridge_nohelper_log(59, (uint32_t)(uintptr_t)caller,
(uint32_t)((uintptr_t)caller >> 32));
/* Saved original C49: five pushes, sub rsp,0x100, then the connect call.
* Its own caller return is exactly 0x130 above connect-entry RSP. The
* failure callback, if present, is [object+0xb0]->vtable[2]. These are
* pre-call identity observations, not evidence that the callback ran. */
if (owner && entry_rsp <= UINTPTR_MAX - 0x130 &&
read_mapped_qword(entry_rsp + 0x130, &c49_caller))
witness_status |= 1;
if (owner && owner <= UINTPTR_MAX - 0xb0 &&
read_mapped_qword(owner + 0xb0, &callback_interface)) {
witness_status |= 2;
if (callback_interface) witness_status |= 4;
}
if (callback_interface &&
read_mapped_qword(callback_interface, &callback_vtable))
witness_status |= 8;
if (callback_vtable && callback_vtable <= UINTPTR_MAX - 0x10 &&
read_mapped_qword(callback_vtable + 0x10, &callback_target))
witness_status |= 16;
gw_bridge_nohelper_log(62, witness_status, 0);
gw_bridge_nohelper_log(63, (uint32_t)c49_caller,
(uint32_t)(c49_caller >> 32));
gw_bridge_nohelper_log(64, (uint32_t)callback_interface,
(uint32_t)(callback_interface >> 32));
gw_bridge_nohelper_log(65, (uint32_t)callback_vtable,
(uint32_t)(callback_vtable >> 32));
gw_bridge_nohelper_log(66, (uint32_t)callback_target,
(uint32_t)(callback_target >> 32));
/* The exact 0x142253090 wrapper adds push RDI, sub rsp,0x50 and its
* call C49. Combined with the already verified C49 stack, its own
* caller return is at connect-entry RSP+0x190. Read one qword only. */
if (!owner || c49_caller != c49_upstream_return ||
entry_rsp > UINTPTR_MAX - 0x190)
upstream_error = 1;
else if (!read_mapped_qword(entry_rsp + 0x190, &upstream_caller))
upstream_error = 2;
else if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)upstream_caller, &upstream_module) ||
upstream_module != GetModuleHandleW(NULL))
upstream_error = 3;
gw_bridge_nohelper_log(71, upstream_error == 0, upstream_error);
gw_bridge_nohelper_log(72, upstream_error ? 0 : (uint32_t)upstream_caller,
upstream_error ? 0 : (uint32_t)(upstream_caller >> 32));
/* Saved CHAININFO parent 0x1430816e0 adds three pushes, sub rsp,0x50,
* and its call to 0x142253090. Together with the two proven inner
* frames, its own caller return is exactly at connect-entry RSP+0x200. */
if (upstream_error || upstream_caller != c49_next_upstream_return ||
entry_rsp > UINTPTR_MAX - 0x200)
next_error = 1;
else if (!read_mapped_qword(entry_rsp + 0x200, &next_caller))
next_error = 2;
else if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)next_caller, &next_module) ||
next_module != GetModuleHandleW(NULL))
next_error = 3;
gw_bridge_nohelper_log(75, next_error == 0, next_error);
gw_bridge_nohelper_log(76, next_error ? 0 : (uint32_t)next_caller,
next_error ? 0 : (uint32_t)(next_caller >> 32));
/* Saved owner 0x14307bca0 adds five pushes, sub rsp,0x160, and a
* vtable call to the transport dispatcher. The next original caller
* return is one qword at connect-entry RSP+0x390, only under the full
* three-return chain. No adjacent stack slot is treated as a frame. */
if (next_error || next_caller != c49_owner_orchestrator_return ||
entry_rsp > UINTPTR_MAX - 0x390)
outer_error = 1;
else if (!read_mapped_qword(entry_rsp + 0x390, &outer_caller))
outer_error = 2;
else if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)outer_caller, &outer_module) ||
outer_module != GetModuleHandleW(NULL))
outer_error = 3;
gw_bridge_nohelper_log(79, outer_error == 0, outer_error);
gw_bridge_nohelper_log(80, outer_error ? 0 : (uint32_t)outer_caller,
outer_error ? 0 : (uint32_t)(outer_caller >> 32));
#if GW_DIRECT_LOGIN_DR0_CANDIDATE && GW_DIRECT_LOGIN_DR0_DIAG
/* First exact original 1119 connect, before the original call resumes.
* The separate worker reads only the primary thread's debug registers. */
gw_direct_login_dr0_diag_at_c49((void *)owner);
#endif
}
static void absolute_jump(uint8_t *out, uintptr_t target)
{
unsigned i;
out[0] = 0xff; out[1] = 0x25;
out[2] = out[3] = out[4] = out[5] = 0;
for (i = 0; i < 8; ++i) out[6+i] = (uint8_t)(target >> (8*i));
}
int gw_connect_export_hook_install(void *owner, DWORD primary_thread)
{
HMODULE winsock, target_module = NULL, bridge_pin = NULL, winsock_pin = NULL;
FARPROC target;
MEMORY_BASIC_INFORMATION page = {0};
uint8_t entry[sizeof(expected_entry)], *trampoline;
uintptr_t address, sites[2], thunks[2], relay;
struct GwStartupPatch patch = {0};
DWORD old, ignored;
int64_t distance;
uint32_t encoded;
int result;
if (InterlockedCompareExchange(&attempted, 1, 0) != 0) return -1;
if (!owner || !primary_thread || GetCurrentThreadId() != primary_thread ||
*(void *volatile *)(uintptr_t)0x145e7a098ULL != owner ||
GetModuleHandleW(NULL) != (HMODULE)(uintptr_t)0x140000000ULL)
return -2;
winsock = GetModuleHandleA("ws2_32.dll");
if (!winsock) return -3;
target = GetProcAddress(winsock, "connect");
address = (uintptr_t)target;
if (!target || address < (uintptr_t)winsock ||
address - (uintptr_t)winsock != 0xc3f0ULL ||
!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)target, &target_module) ||
target_module != winsock) return -4;
if (VirtualQuery((const void *)target, &page, sizeof(page)) !=
sizeof(page) || page.State != MEM_COMMIT ||
page.Protect != PAGE_EXECUTE_READ || page.Type != MEM_IMAGE ||
page.AllocationBase != winsock ||
address < (uintptr_t)page.BaseAddress ||
page.RegionSize < sizeof(entry) ||
address - (uintptr_t)page.BaseAddress >
page.RegionSize - sizeof(entry) ||
!read_exact((const void *)target, entry, sizeof(entry)) ||
memcmp(entry, expected_entry, sizeof(entry)) != 0) return -5;
{
FARPROC get_error = GetProcAddress(winsock, "WSAGetLastError");
FARPROC set_error = GetProcAddress(winsock, "WSASetLastError");
_Static_assert(sizeof(get_error) == sizeof(gw_export_wsa_get_error),
"WSAGetLastError pointer size changed");
_Static_assert(sizeof(set_error) == sizeof(gw_export_wsa_set_error),
"WSASetLastError pointer size changed");
memcpy(&gw_export_wsa_get_error, &get_error, sizeof(get_error));
memcpy(&gw_export_wsa_set_error, &set_error, sizeof(set_error));
}
if (!gw_export_wsa_get_error || !gw_export_wsa_set_error) return -6;
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_PIN,
(LPCSTR)&gw_export_connect_thunk, &bridge_pin) ||
!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_PIN,
(LPCSTR)target, &winsock_pin)) return -7;
/* Reuse the existing near-relay allocator. Its second identical slot is
* unused; both are checked for rel32 reach before any export edit. */
sites[0] = sites[1] = address;
thunks[0] = thunks[1] = (uintptr_t)&gw_export_connect_thunk;
if (gw_direct_relays_prepare(sites, thunks, &relays)) return -8;
trampoline = (uint8_t *)relays.page + 32;
if (!VirtualProtect(relays.page, 64,
PAGE_READWRITE, &old)) {
gw_direct_relays_release(&relays);
return -9;
}
memcpy(trampoline, expected_entry, 5);
absolute_jump(trampoline + 5, address + 5);
if (!VirtualProtect(relays.page, 64, old, &ignored) ||
!FlushInstructionCache(GetCurrentProcess(), trampoline, 19)) {
gw_direct_relays_release(&relays);
return -10;
}
gw_export_connect_trampoline = trampoline;
MemoryBarrier();
relay = relays.relay[0];
distance = (int64_t)relay - (int64_t)(address + 5);
if (distance < INT32_MIN || distance > INT32_MAX) {
gw_direct_relays_release(&relays);
return -11;
}
patch.address = address;
patch.size = 5;
memcpy(patch.before, expected_entry, 5);
patch.after[0] = 0xe9;
encoded = (uint32_t)(int32_t)distance;
patch.after[1] = (uint8_t)encoded;
patch.after[2] = (uint8_t)(encoded >> 8);
patch.after[3] = (uint8_t)(encoded >> 16);
patch.after[4] = (uint8_t)(encoded >> 24);
result = gw_direct_apply_two_patches(&patch, 1);
if (result) {
gw_bridge_nohelper_log(61, (uint32_t)result,
gw_direct_admission.reason);
gw_direct_relays_release(&relays);
return -12;
}
return 0;
}