Files

222 lines
9.4 KiB
C

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include "blizzard_egress.h"
/* Gitea #13 + owner 2026-11-27: neutralise every Blizzard host in the read-only data of WowB.exe and WowB_loader.dll
* and in heap copies (ASCII and UTF-16LE), and refuse browser launches for Blizzard addresses (see blizzard_egress.h).
* Only initialised, non-executable sections are scanned; each match is rewritten under a temporary writable protection
* and the original protection is restored. */
static DWORD last_error;
static int make_writable(void *at, SIZE_T length, DWORD *old)
{
static const DWORD modes[] = {PAGE_READWRITE, PAGE_WRITECOPY, PAGE_EXECUTE_READWRITE};
unsigned i;
for (i = 0; i < sizeof(modes) / sizeof(modes[0]); ++i) {
if (VirtualProtect(at, length, modes[i], old)) return 1;
last_error = GetLastError();
}
return 0;
}
static int neutralise_module(HMODULE module, int *failed_out)
{
const uint8_t *base = (const uint8_t *)module;
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)base;
const IMAGE_NT_HEADERS *nt;
const IMAGE_SECTION_HEADER *section;
int rewritten = 0, failed = 0;
*failed_out = 0;
WORD i;
if (!module || dos->e_magic != IMAGE_DOS_SIGNATURE) return 0;
nt = (const IMAGE_NT_HEADERS *)(base + dos->e_lfanew);
if (nt->Signature != IMAGE_NT_SIGNATURE) return 0;
section = IMAGE_FIRST_SECTION(nt);
for (i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++section) {
const DWORD c = section->Characteristics;
uint8_t *data = (uint8_t *)base + section->VirtualAddress;
size_t size = section->Misc.VirtualSize, stride;
if ((c & IMAGE_SCN_MEM_EXECUTE) || !(c & IMAGE_SCN_CNT_INITIALIZED_DATA)) continue;
for (stride = 1; stride <= 2; ++stride) {
size_t at = 0, length = 0;
while ((at = gw_egress_find_stride(data, size, at, &length, stride)) < size) {
DWORD old = 0, ignored = 0;
const SIZE_T bytes = length * stride;
if (length > 64) { at += bytes; continue; }
if (make_writable(data + at, bytes, &old)) {
gw_egress_write(data + at, length, stride);
VirtualProtect(data + at, bytes, old, &ignored);
++rewritten;
} else {
++failed;
}
at += bytes;
}
}
}
*failed_out = failed;
return rewritten;
}
/* Copies the client made before the Bridge started (the breaking-news URL is formatted at startup, before any of
* our code runs) live in committed private read-write memory: rewrite them in place, same length. */
uint32_t gw_blizzard_egress_scan_ms, gw_blizzard_egress_scan_mb; /* #119: startup log event 124 */
static int neutralise_private_memory(void)
{
MEMORY_BASIC_INFORMATION info;
uint8_t *address = 0;
int rewritten = 0;
unsigned long long scanned = 0;
const ULONGLONG started = GetTickCount64();
while (VirtualQuery(address, &info, sizeof(info)) == sizeof(info)) {
uint8_t *next = (uint8_t *)info.BaseAddress + info.RegionSize;
const DWORD p = info.Protect;
if (info.State == MEM_COMMIT && info.Type == MEM_PRIVATE && !(p & (PAGE_GUARD | PAGE_NOACCESS)) &&
(p & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE))) {
uint8_t *data = (uint8_t *)info.BaseAddress;
size_t size = info.RegionSize, stride;
scanned += size;
for (stride = 1; stride <= 2; ++stride) {
size_t at = 0, length = 0;
while ((at = gw_egress_find_stride(data, size, at, &length, stride)) < size) {
if (length <= 64) { gw_egress_write(data + at, length, stride); ++rewritten; }
at += length * stride;
}
}
}
if (next <= address) break;
address = next;
}
gw_blizzard_egress_scan_ms = (uint32_t)(GetTickCount64() - started);
gw_blizzard_egress_scan_mb = (uint32_t)(scanned >> 20);
return rewritten;
}
/* Browser guard: the client resolves shell32 at run time, so its import table cannot be redirected. The entry of
* each ShellExecute variant is overwritten with an absolute jump to a guard; the guard refuses Blizzard web
* addresses and otherwise restores the original bytes, calls through, and re-arms (launches are rare UI actions,
* serialised by a critical section). */
typedef struct {
uint8_t *entry;
uint8_t saved[12];
void *guard;
} gw_url_hook;
static gw_url_hook hook_w, hook_ex_w, hook_a, hook_ex_a;
static CRITICAL_SECTION hook_lock;
static volatile LONG refused_launches;
static void hook_write(gw_url_hook *hook, int armed)
{
DWORD old = 0, ignored = 0;
uint8_t jump[12] = {0x48, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xe0}; /* mov rax, imm64; jmp rax */
if (!hook->entry || !VirtualProtect(hook->entry, sizeof(jump), PAGE_EXECUTE_READWRITE, &old)) return;
memcpy(jump + 2, &hook->guard, 8);
memcpy(hook->entry, armed ? jump : hook->saved, sizeof(jump));
VirtualProtect(hook->entry, sizeof(jump), old, &ignored);
FlushInstructionCache(GetCurrentProcess(), hook->entry, sizeof(jump));
}
static int hook_install(gw_url_hook *hook, HMODULE shell, const char *name, void *guard)
{
hook->entry = (uint8_t *)(void *)GetProcAddress(shell, name);
if (!hook->entry) return 0;
memcpy(hook->saved, hook->entry, sizeof(hook->saved));
hook->guard = guard;
hook_write(hook, 1);
return 1;
}
/* Widen an ANSI target for the check (only the scheme and host matter). */
static void widen(const char *in, uint16_t *out, size_t room)
{
size_t i = 0;
if (!in) { out[0] = 0; return; }
for (; in[i] && i + 1 < room; ++i) out[i] = (uint8_t)in[i];
out[i] = 0;
}
static int refuse(void)
{
InterlockedIncrement(&refused_launches);
SetLastError(ERROR_ACCESS_DENIED);
return 1;
}
static HINSTANCE WINAPI guard_shell_execute_w(HWND w, LPCWSTR op, LPCWSTR file, LPCWSTR params, LPCWSTR dir, INT show)
{
HINSTANCE result;
if (gw_blizzard_url_blocked((const uint16_t *)file) && refuse()) return (HINSTANCE)(INT_PTR)SE_ERR_ACCESSDENIED;
EnterCriticalSection(&hook_lock);
hook_write(&hook_w, 0);
result = ((HINSTANCE (WINAPI *)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR, INT))(void *)hook_w.entry)(w, op, file, params, dir, show);
hook_write(&hook_w, 1);
LeaveCriticalSection(&hook_lock);
return result;
}
static BOOL WINAPI guard_shell_execute_ex_w(SHELLEXECUTEINFOW *info)
{
BOOL result;
if (info && gw_blizzard_url_blocked((const uint16_t *)info->lpFile) && refuse()) {
info->hInstApp = (HINSTANCE)(INT_PTR)SE_ERR_ACCESSDENIED;
return FALSE;
}
EnterCriticalSection(&hook_lock);
hook_write(&hook_ex_w, 0);
result = ((BOOL (WINAPI *)(SHELLEXECUTEINFOW *))(void *)hook_ex_w.entry)(info);
hook_write(&hook_ex_w, 1);
LeaveCriticalSection(&hook_lock);
return result;
}
static HINSTANCE WINAPI guard_shell_execute_a(HWND w, LPCSTR op, LPCSTR file, LPCSTR params, LPCSTR dir, INT show)
{
HINSTANCE result;
uint16_t wide[512];
widen(file, wide, 512);
if (gw_blizzard_url_blocked(wide) && refuse()) return (HINSTANCE)(INT_PTR)SE_ERR_ACCESSDENIED;
EnterCriticalSection(&hook_lock);
hook_write(&hook_a, 0);
result = ((HINSTANCE (WINAPI *)(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT))(void *)hook_a.entry)(w, op, file, params, dir, show);
hook_write(&hook_a, 1);
LeaveCriticalSection(&hook_lock);
return result;
}
static BOOL WINAPI guard_shell_execute_ex_a(SHELLEXECUTEINFOA *info)
{
BOOL result;
uint16_t wide[512];
widen(info ? info->lpFile : NULL, wide, 512);
if (info && gw_blizzard_url_blocked(wide) && refuse()) {
info->hInstApp = (HINSTANCE)(INT_PTR)SE_ERR_ACCESSDENIED;
return FALSE;
}
EnterCriticalSection(&hook_lock);
hook_write(&hook_ex_a, 0);
result = ((BOOL (WINAPI *)(SHELLEXECUTEINFOA *))(void *)hook_ex_a.entry)(info);
hook_write(&hook_ex_a, 1);
LeaveCriticalSection(&hook_lock);
return result;
}
static int install_browser_guard(void)
{
HMODULE shell = LoadLibraryW(L"shell32.dll");
int hooked = 0;
if (!shell) return 0;
InitializeCriticalSection(&hook_lock);
hooked += hook_install(&hook_w, shell, "ShellExecuteW", (void *)guard_shell_execute_w);
hooked += hook_install(&hook_ex_w, shell, "ShellExecuteExW", (void *)guard_shell_execute_ex_w);
hooked += hook_install(&hook_a, shell, "ShellExecuteA", (void *)guard_shell_execute_a);
hooked += hook_install(&hook_ex_a, shell, "ShellExecuteExA", (void *)guard_shell_execute_ex_a);
return hooked;
}
/* Packed for the startup log: image hosts rewritten (saturating at 255) | failed << 8 | browser hooks << 12 |
* (last VirtualProtect error & 0xff) << 16 | heap copies rewritten (saturating at 255) << 24. */
uint32_t gw_blizzard_egress_install(void)
{
int failed_exe = 0, failed_loader = 0;
int exe = neutralise_module(GetModuleHandleW(NULL), &failed_exe);
int loader = neutralise_module(GetModuleHandleW(L"WowB_loader.dll"), &failed_loader);
int heap = neutralise_private_memory();
int image = exe + loader, failed = failed_exe + failed_loader, hooks = install_browser_guard();
return (uint32_t)(image > 255 ? 255 : image) | (uint32_t)(failed > 15 ? 15 : failed) << 8 |
(uint32_t)hooks << 12 | (last_error & 0xffu) << 16 | (uint32_t)(heap > 255 ? 255 : heap) << 24;
}