175 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
132 lines
5.3 KiB
C
132 lines
5.3 KiB
C
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "original_connect_export_probe.h"
|
|
|
|
enum { GW_EXPORT_BYTES = 32 };
|
|
static volatile LONG attempted;
|
|
|
|
static char *put_text(char *out, char *end, const char *value)
|
|
{
|
|
while (*value) {
|
|
if (out == end) return NULL;
|
|
*out++ = *value++;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
static char *put_hex(char *out, char *end, uint64_t value)
|
|
{
|
|
static const char digits[] = "0123456789abcdef";
|
|
int shift;
|
|
if (end - out < 18) return NULL;
|
|
*out++ = '0'; *out++ = 'x';
|
|
for (shift = 60; shift >= 0; shift -= 4)
|
|
*out++ = digits[(value >> shift) & 15u];
|
|
return out;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int gw_connect_export_probe_once(void *owner, DWORD primary_thread)
|
|
{
|
|
HMODULE winsock, target_module = NULL;
|
|
FARPROC target;
|
|
MEMORY_BASIC_INFORMATION page = {0};
|
|
uint8_t bytes[GW_EXPORT_BYTES] = {0};
|
|
char startup_path[1024], binary_path[1056], metadata_path[1056];
|
|
char winsock_path[512], target_path[512], metadata[1600];
|
|
DWORD path_size, winsock_size, target_size, written = 0;
|
|
HANDLE file;
|
|
char *cursor, *end;
|
|
int length;
|
|
uintptr_t address;
|
|
|
|
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");
|
|
if (!target) return -4;
|
|
address = (uintptr_t)target;
|
|
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
|
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
|
(LPCSTR)target, &target_module) || !target_module)
|
|
return -5;
|
|
winsock_size = GetModuleFileNameA(winsock, winsock_path,
|
|
sizeof(winsock_path));
|
|
target_size = GetModuleFileNameA(target_module, target_path,
|
|
sizeof(target_path));
|
|
if (!winsock_size || winsock_size >= sizeof(winsock_path) ||
|
|
!target_size || target_size >= sizeof(target_path)) return -6;
|
|
if (VirtualQuery((const void *)address, &page, sizeof(page)) !=
|
|
sizeof(page) || page.State != MEM_COMMIT ||
|
|
(page.Protect & (PAGE_GUARD | PAGE_NOACCESS)) ||
|
|
address < (uintptr_t)page.BaseAddress ||
|
|
page.RegionSize < sizeof(bytes) ||
|
|
address - (uintptr_t)page.BaseAddress >
|
|
page.RegionSize - sizeof(bytes)) return -7;
|
|
if (!read_exact((const void *)address, bytes, sizeof(bytes))) return -8;
|
|
path_size = GetEnvironmentVariableA("GW_NATIVE_STARTUP_LOG", startup_path,
|
|
sizeof(startup_path));
|
|
if (!path_size || path_size >= sizeof(startup_path) ||
|
|
path_size + sizeof(".connect-export.meta") >= sizeof(binary_path))
|
|
return -9;
|
|
memcpy(binary_path, startup_path, path_size);
|
|
memcpy(binary_path + path_size, ".connect-export.bin",
|
|
sizeof(".connect-export.bin"));
|
|
memcpy(metadata_path, startup_path, path_size);
|
|
memcpy(metadata_path + path_size, ".connect-export.meta",
|
|
sizeof(".connect-export.meta"));
|
|
cursor = metadata;
|
|
end = metadata + sizeof(metadata);
|
|
#define APP_TEXT(value) do { cursor = put_text(cursor, end, (value)); \
|
|
if (!cursor) return -10; } while (0)
|
|
#define APP_HEX(value) do { cursor = put_hex(cursor, end, (uint64_t)(value)); \
|
|
if (!cursor) return -10; } while (0)
|
|
APP_TEXT("abi=1 bytes=0x20 winsock_module="); APP_HEX((uintptr_t)winsock);
|
|
APP_TEXT(" target_module="); APP_HEX((uintptr_t)target_module);
|
|
APP_TEXT(" export="); APP_HEX(address);
|
|
APP_TEXT(" region_base="); APP_HEX((uintptr_t)page.BaseAddress);
|
|
APP_TEXT(" allocation_base="); APP_HEX((uintptr_t)page.AllocationBase);
|
|
APP_TEXT(" region_size="); APP_HEX(page.RegionSize);
|
|
APP_TEXT(" state="); APP_HEX(page.State);
|
|
APP_TEXT(" protect="); APP_HEX(page.Protect);
|
|
APP_TEXT(" type="); APP_HEX(page.Type);
|
|
APP_TEXT(" winsock_path="); APP_TEXT(winsock_path);
|
|
APP_TEXT(" target_path="); APP_TEXT(target_path);
|
|
APP_TEXT("\n");
|
|
#undef APP_HEX
|
|
#undef APP_TEXT
|
|
length = (int)(cursor - metadata);
|
|
file = CreateFileA(binary_path, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
|
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
if (file == INVALID_HANDLE_VALUE) return -11;
|
|
if (!WriteFile(file, bytes, sizeof(bytes), &written, NULL) ||
|
|
written != sizeof(bytes)) {
|
|
CloseHandle(file);
|
|
return -12;
|
|
}
|
|
CloseHandle(file);
|
|
SecureZeroMemory(bytes, sizeof(bytes));
|
|
file = CreateFileA(metadata_path, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
|
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
if (file == INVALID_HANDLE_VALUE) return -13;
|
|
written = 0;
|
|
if (!WriteFile(file, metadata, (DWORD)length, &written, NULL) ||
|
|
written != (DWORD)length) {
|
|
CloseHandle(file);
|
|
return -14;
|
|
}
|
|
CloseHandle(file);
|
|
return 0;
|
|
}
|