Skip to content
Ukine9999Public

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

plzsub

Please substitute. Small library for late-binding constants in injected hooks: bake unique non-canonical sentinels into your function body as MOV r64, imm64 immediates, then have plzsub scan the bytes and swap them for real addresses resolved at install time.

Why

You inject a hook - say a shadowed kernel handler. It needs to call KeSetTimerEx, dereference a per-injection page, read a struct field at a runtime-resolved offset. Nothing you can statically link from the injected code.

The usual workarounds are ugly: reserve globals and write them before install (breaks position independence and gives you a stable IOC), thread a context pointer through every call site (invasive), or emit trampolines (large). plzsub does the same job by rewriting immediates in place before the hook is installed.

How it works

SentinelFor(N) is a constexpr returning 0xC0DEBABE00000000 | N — a 64-bit constant with a fixed non-canonical prefix in the high 32 bits and the index in the low 32. Non-canonical means bit 63 is set but bits 62–48 are mixed, so the value can never match a legitimate address (kernel or user) and can never collide with real code the compiler emits. Guaranteed distinct per index for N < 2^32. Use it anywhere in the hook body:

volatile auto cmdPageSentinel = Plzsub::Domain::SentinelFor(1);
volatile auto keSetTimerSentinel = Plzsub::Domain::SentinelFor(2);
auto* cmdPage    = reinterpret_cast<CmdPage*>(cmdPageSentinel);
auto  keSetTimer = reinterpret_cast<PfnKeSetTimerEx>(keSetTimerSentinel);

The compiler emits 48/49 B8+r imm64 (REX.W + MOV r64, imm64, 10 bytes) with the sentinel as the immediate. Before install, copy the function bytes to a writable buffer, hand them to RemapContext with entries {name, sentinel, real_address}, and it scans for the opcode sequence, matches the imm against each entry's sentinel, rewrites it in place. VirtualProtect handles the RX→RWX→RX dance for code pages; FlushInstructionCache finishes.

The encoding is OR-based, not multiplication, on purpose. With a base * N scheme every N that wraps the base's high bits out of the 64-bit range collapses the sentinel to a small constant — for the natural 0xC00…00C * N base every N ≡ 0 (mod 4) produces sentinels like 0x30, 0x60, 0x90, which the compiler encodes as mov r32, imm32 (5 bytes) instead of MOV imm64 (10 bytes). Remap then finds no MOV imm64 to patch and silently no-ops. OR keeps the prefix intact for every index.

Requirements

  • Windows x86-64
  • C++20
  • The optimizer must actually emit a MOV imm64 — copy SentinelFor(N) to a volatile uintptr_t before casting, or add _ReadWriteBarrier()

Usage

#include "Plzsub/Application/RemapContext.h"
#include "Plzsub/Domain/SentinelIndex.h"
#include "Plzsub/Infrastructure/Logging/CallbackLogger.h"

std::vector<std::uint8_t> bytes(HOOK_SIZE);
std::memcpy(bytes.data(), reinterpret_cast<void*>(&MyHook), HOOK_SIZE);

volatile auto cmdPageSentinel = Plzsub::Domain::SentinelFor(1);
volatile auto keSetTimerSentinel = Plzsub::Domain::SentinelFor(2);

std::vector<Plzsub::Domain::RemapEntry> entries = {
    { "cmdPage",    cmdPageSentinel, resolvedCmdPage    },
    { "KeSetTimer", keSetTimerSentinel, resolvedKeSetTimer },
};

auto logger = Plzsub::Infrastructure::Logging::CallbackLogger(
    [](std::string_view line){ std::puts(std::string(line).c_str()); });

auto context = Plzsub::Application::RemapContext(
    logger, std::move(entries), bytes.data(), bytes.size());
int patched = context.Remap();

Layout

include/Plzsub/
  Domain/          - RemapEntry, SentinelFor
  Application/     - RemapContext (byte scan + patch)
    Ports/         - ILogger
  Infrastructure/  - NullLogger, CallbackLogger

Caveats

  • x86-64 only. AArch64 and x86-32 use different opcodes for the same trick and aren't handled here.
  • If the optimizer folds SentinelFor(N) before it becomes an immediate (turned into LEA, materialized from a smaller constant, etc.) the sentinel never lands in the bytes and Remap finds nothing. Use volatile locals; check the disassembly.
  • The default sentinel base is chosen so it won't collide with real code, but if you fork the constant to some other value that DOES occur in legit code, you'll silently corrupt bytes you didn't mean to. Playing with sharp knives.