-- Runtime port of the FL27 Strict Equal V1 release. local m = {} local PATCHES = { { 0x92da37, 0x92da47, "0f879b010000448b46184183f8157728418bd6660f1f44000083fa188bc2410f43c7486bc874443944", "418bd6660f1f440000", "e90b01000090909090" }, { 0x844fbf, 0x844fcf, "d50f29b42420010000e8730d210084c0740c488b4c2458e855382100eb0a410fbe4e", "740c", "9090" }, { 0x6df076, 0x6df086, "8bc2486bc874807c390d000f95c084c00f84a5000000428b9c3b0c4702000f28fe83fb157715", "0f84a5000000", "909090909090" }, } local function bytes(hex) return (hex:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)) end function m.preflight(base, name) if type(name) ~= "string" or name:lower() ~= "fl_2027.exe" then return nil, "requires the Normal FL27 executable" end if not base or memory.read(base, 2) ~= "MZ" then return nil, "invalid main image" end local header = memory.unpack("u32", memory.read(base + 0x3c, 4)) if header < 0x40 or header > 0x1000 then return nil, "invalid PE header" end local pe = base + header local image_size = memory.unpack("u32", memory.read(pe + 80, 4)) if memory.read(pe, 6) ~= "PE\x00\x00\x64\x86" or image_size < 0x92da61 or image_size >= 0x60000000 then return nil, "unsupported FL27 image" end local sites, active = {}, 0 for _, spec in ipairs(PATCHES) do local start, rva = spec[1], spec[2] local guard, original, replacement = bytes(spec[3]), bytes(spec[4]), bytes(spec[5]) local offset = rva - start local updated = guard:sub(1, offset) .. replacement .. guard:sub(offset + #original + 1) local actual = memory.read(base + start, #guard) if actual == updated then active = active + 1 elseif actual ~= guard then return nil, string.format("different instructions at RVA %x", rva) end sites[#sites + 1] = { address = base + rva, original = original, replacement = replacement } end if active > 0 and active < #sites then return nil, "partial existing patch" end return sites, active == #sites end function m.apply(base, name, write_code) local sites, state = m.preflight(base, name) if not sites then return false, state end if state then return true, "already active" end local changed = {} local failure for _, site in ipairs(sites) do if memory.read(site.address, #site.original) ~= site.original then failure = "instructions changed during activation" break end changed[#changed + 1] = site local ok = write_code(site, site.replacement) if not ok or memory.read(site.address, #site.replacement) ~= site.replacement then failure = "write or protection check failed" break end end if failure then local restored = true for index = #changed, 1, -1 do local site = changed[index] -- A previous attempt can fail when restoring permissions even if the bytes match. if site.protection or memory.read(site.address, #site.original) ~= site.original then local ok = write_code(site, site.original) restored = ok and memory.read(site.address, #site.original) == site.original and restored end end return false, failure .. "; rollback=" .. tostring(restored) end return true, "three Strict Equal V1 sites installed" end local function write_code(site, value) local address = ffi.cast("void*", site.address) local old = ffi.new("uint32_t[1]") if ffi.C.VirtualProtect(address, #value, 0x40, old) == 0 then return false end site.protection = site.protection or tonumber(old[0]) ffi.copy(address, value, #value) local flushed = ffi.C.FlushInstructionCache(ffi.C.GetCurrentProcess(), address, #value) ~= 0 local protected = ffi.C.VirtualProtect(address, #value, site.protection, old) ~= 0 return flushed and protected end function m.init() if not ffi then log("fl27-referee-strict-equal: SKIPPED LuaJIT extensions required"); return end ffi.cdef[[ uint8_t* GetModuleHandleA(const char* name); uint32_t GetModuleFileNameA(void* module, char* filename, uint32_t size); int VirtualProtect(void* address, size_t size, uint32_t protect, uint32_t* old); void* GetCurrentProcess(void); int FlushInstructionCache(void* process, const void* address, uint64_t size); ]] local module = ffi.C.GetModuleHandleA(nil) if module == nil then log("fl27-referee-strict-equal: SKIPPED main image unavailable"); return end local filename = ffi.new("char[1024]") local length = ffi.C.GetModuleFileNameA(module, filename, 1024) if length == 0 or length >= 1024 then log("fl27-referee-strict-equal: SKIPPED image name unavailable") return end local base = tonumber(ffi.cast("uintptr_t", module)) local name = ffi.string(filename, length):match("([^\\/]+)$") local ok, result = m.apply(base, name, write_code) log("fl27-referee-strict-equal: " .. (ok and "ACTIVE " or "SKIPPED ") .. result) end return m