Files
praxisandClaude Fable 5.2 8e02dccb86 In-client stack: transport, session lifecycle and engine bridge
175 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

142 lines
6.0 KiB
C

#include <windows.h>
#include <stdint.h>
#include "original_connect_observer.h"
enum {
GW_CONNECT_SLOT = 0x1447c3530ULL,
GW_C49_RETURN = 0x142252cd5ULL
};
/* Written before the aligned slot CAS. The thunk tail-jumps through this
* immutable pointer, leaving the original connect return and WSA error intact. */
void *gw_connect_original;
extern void gw_connect_observer_thunk(void);
extern void gw_bridge_nohelper_log(uint32_t event, uint32_t result,
uint32_t error);
static volatile LONG observation_claimed;
/* 0 new, -1 installing, 1 installed, 2 opaque until C_Login capture,
* 3 terminal refusal. No frame polling or repeated credential generation. */
static volatile LONG install_state;
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;
}
/* Called before the original Winsock export. No post-connect API may disturb
* the original return value or WSAGetLastError. RDI is an object only for the
* exact recovered C49 return site; all other caller owners are unknown. */
void gw_connect_observe(const void *address, int length, uintptr_t return_pc,
uintptr_t rdi)
{
uint8_t header[8];
uintptr_t owner;
if (!address || length < (int)sizeof(header) ||
return_pc < (uintptr_t)0x140001000ULL ||
return_pc >= (uintptr_t)0x1447c145cULL ||
InterlockedCompareExchange(&observation_claimed, 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(&observation_claimed, 1, 0) != 0)
return;
owner = return_pc == GW_C49_RETURN ? rdi : 0;
gw_bridge_nohelper_log(47, (uint32_t)return_pc,
(uint32_t)(return_pc >> 32));
gw_bridge_nohelper_log(48, (uint32_t)owner, (uint32_t)(owner >> 32));
}
static int install_once(void *owner, DWORD primary_thread)
{
const uintptr_t slot_address = (uintptr_t)GW_CONNECT_SLOT;
void * volatile *slot = (void * volatile *)slot_address;
HMODULE main_module, winsock, pinned = NULL;
MEMORY_BASIC_INFORMATION page = {0};
void *expected = NULL, *preimage = NULL, *prior;
DWORD old_protect, ignored;
main_module = GetModuleHandleW(NULL);
if (!owner || !primary_thread || GetCurrentThreadId() != primary_thread ||
*(void *volatile *)(uintptr_t)0x145e7a098ULL != owner ||
main_module != (HMODULE)(uintptr_t)0x140000000ULL ||
slot_address & (sizeof(void *) - 1u)) return -2;
winsock = GetModuleHandleA("ws2_32.dll");
if (!winsock) return -3;
expected = (void *)GetProcAddress(winsock, "connect");
if (!expected) return -40;
if (VirtualQuery((const void *)slot_address, &page, sizeof(page)) !=
sizeof(page)) return -41;
if (page.State != MEM_COMMIT) return -42;
/* Wine materializes this IAT page as MEM_PRIVATE with an allocation base
* distinct from the WowB image. Its type/base are diagnostic metadata,
* while the exact slot VA, committed writable span and resolved target
* provide the bounded admission for this disposable observer. */
if (page.Protect != PAGE_READWRITE) {
gw_bridge_nohelper_log(49, page.Type, page.Protect);
return -45;
}
if (slot_address < (uintptr_t)page.BaseAddress ||
page.RegionSize < sizeof(void *) ||
slot_address - (uintptr_t)page.BaseAddress >
page.RegionSize - sizeof(void *)) return -46;
if (!read_exact((const void *)slot_address, &preimage,
sizeof(preimage))) return -47;
if (preimage != expected) {
uintptr_t live = (uintptr_t)preimage, resolved = (uintptr_t)expected;
gw_bridge_nohelper_log(49, (uint32_t)live,
(uint32_t)(live >> 32));
gw_bridge_nohelper_log(50, (uint32_t)resolved,
(uint32_t)(resolved >> 32));
return -48;
}
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN |
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCSTR)&gw_connect_observer_thunk, &pinned) ||
!pinned) return -5;
gw_connect_original = expected;
MemoryBarrier();
if (!VirtualProtect((void *)slot_address, sizeof(void *), PAGE_READWRITE,
&old_protect)) return -6;
prior = InterlockedCompareExchangePointer(slot,
(void *)&gw_connect_observer_thunk, expected);
if (prior != expected) {
VirtualProtect((void *)slot_address, sizeof(void *), old_protect,
&ignored);
return -7;
}
if (!VirtualProtect((void *)slot_address, sizeof(void *), old_protect,
&ignored)) {
/* The original export remains callable even if protection restoration
* fails. Attempt to roll the slot back, without ever unloading Bridge. */
InterlockedCompareExchangePointer(slot, expected,
(void *)&gw_connect_observer_thunk);
VirtualProtect((void *)slot_address, sizeof(void *), old_protect,
&ignored);
return -8;
}
return 0;
}
int gw_connect_observer_install(void *owner, DWORD primary_thread)
{
int result;
if (InterlockedCompareExchange(&install_state, -1, 0) != 0) return -1;
result = install_once(owner, primary_thread);
InterlockedExchange(&install_state, result == 0 ? 1 :
result == -48 ? 2 : 3);
return result;
}
int gw_connect_observer_after_capture(void *owner, DWORD primary_thread)
{
int result;
if (InterlockedCompareExchange(&install_state, -1, 2) != 2) return 1;
result = install_once(owner, primary_thread);
InterlockedExchange(&install_state, result == 0 ? 1 : 3);
return result;
}