From 203a6943ca24fd24f245efc1426c8dd1daa96a89 Mon Sep 17 00:00:00 2001 From: Krarilotus <51748815+Krarilotus@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:21:28 +0100 Subject: [PATCH 1/2] Keep AOB scans inside their range and add scanForAOBInMainModule scanForAOB searches [min, max] inclusively: a match has to end at or before max, the last valid start is tested, and no read passes the end of a readable region. Adjacent readable regions are searched as one range. scanForAOBInMainModule(pattern) returns the first and the second match in the executable sections of the main module, so a caller can reject an ambiguous signature. The PE headers and section ranges are checked against the loaded image size before they are read. When the headers are invalid, or an executable section is empty, lies outside the image or cannot be read, it returns nil, nil, "incomplete", so an unread duplicate is never reported as unique. Version 1.6.0. --- AOB.cpp | 113 ++++++++++++++++++++++++++--------- AOB.h | 3 + CodeFunctions.cpp | 27 +++++++++ CodeFunctions.h | 1 + Directory.Build.props | 2 +- README.md | 6 +- RuntimePatchingSystem.cpp | 1 + RuntimePatchingSystem.nuspec | 2 +- tests/test-aob.lua | 23 +++++++ 9 files changed, 146 insertions(+), 32 deletions(-) create mode 100644 tests/test-aob.lua diff --git a/AOB.cpp b/AOB.cpp index 44afc15..a1d2335 100644 --- a/AOB.cpp +++ b/AOB.cpp @@ -1,9 +1,11 @@ #include "framework.h" +#include #include #include #include #include +#include #include "AOB.h" @@ -19,7 +21,9 @@ namespace AOB { } DWORD FindPattern(DWORD dwAddress, DWORD dwLen, BYTE* bMask, char* szMask) { - for (DWORD i = 0; i < (dwLen - strlen((char*)szMask)); i++) + const size_t length = strlen(szMask); + if (length == 0 || length > dwLen) return 0; + for (DWORD i = 0; i <= dwLen - length; i++) { if (bCompare((BYTE*)(dwAddress + i), bMask, szMask)) { @@ -42,38 +46,30 @@ namespace AOB { return bytes; } + static bool Readable(const MEMORY_BASIC_INFORMATION& mbi) + { + return mbi.State == MEM_COMMIT && mbi.Type == MEM_IMAGE && (mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD)) == 0; + } + + // Searches [min, max], both inclusive. Adjacent readable regions are searched as one range. DWORD Scan(char* content, char* mask, DWORD min, DWORD max) { - SYSTEM_INFO si; - GetSystemInfo(&si); - _MEMORY_BASIC_INFORMATION32 mbi; - DWORD address = min; - int remainder = 0; - - while (VirtualQuery((LPCVOID) address, ((MEMORY_BASIC_INFORMATION*)&mbi), sizeof(MEMORY_BASIC_INFORMATION)) != 0) { - if (mbi.State == MEM_COMMIT) { - if ((mbi.Type != MEM_MAPPED) && (mbi.Type != MEM_PRIVATE)) { - if ((mbi.Protect & PAGE_NOACCESS) == 0) { - // address = 0x401002 - // mbi.BaseAddress = 0x401000 - DWORD needle = FindPattern(address, mbi.RegionSize, (BYTE*)content, mask); - if (needle == 0) { - // address = 0x401000 - address = mbi.BaseAddress; - } - else { - return needle; - } - } + const unsigned long long end = (unsigned long long) max + 1; + unsigned long long address = min, runStart = min; + MEMORY_BASIC_INFORMATION mbi; + while (address < end) { + const bool queried = VirtualQuery((LPCVOID) (DWORD) address, &mbi, sizeof(mbi)) != 0; + const unsigned long long next = queried ? (std::min)((unsigned long long) (DWORD) mbi.BaseAddress + mbi.RegionSize, end) : end; + if (!queried || !Readable(mbi)) { + if (address > runStart) { + DWORD needle = FindPattern((DWORD) runStart, (DWORD) (address - runStart), (BYTE*) content, mask); + if (needle != 0) return needle; } + runStart = next; } - - // address = 0x59E000 - address += mbi.RegionSize; - if (address > max) { - return 0; - } + address = next; } + if (address > runStart) return FindPattern((DWORD) runStart, (DWORD) (address - runStart), (BYTE*) content, mask); return 0; } @@ -100,7 +96,66 @@ namespace AOB { haystack = sm.suffix(); } - return Scan((char*)(&AOB::HexToBytes(content)[0]), (char*)mask.c_str(), min, max); + std::vector bytes = HexToBytes(content); + if (bytes.empty()) return 0; + return Scan(bytes.data(), (char*)mask.c_str(), min, max); + } + + // Whether [offset, offset + size) lies inside an image of imageSize bytes. + static bool InImage(DWORD offset, DWORD size, DWORD imageSize) + { + return offset <= imageSize && size <= imageSize - offset; + } + + // Whether every byte of [address, address + size) is readable image memory. + static bool AllReadable(DWORD address, DWORD size) + { + const unsigned long long end = (unsigned long long) address + size; + MEMORY_BASIC_INFORMATION mbi; + for (unsigned long long a = address; a < end; a = (unsigned long long) (DWORD) mbi.BaseAddress + mbi.RegionSize) { + if (VirtualQuery((LPCVOID) (DWORD) a, &mbi, sizeof(mbi)) == 0 || !Readable(mbi)) return false; + } + return true; + } + + bool FindInMainModule(std::string ucp_aob_spec, DWORD& first, DWORD& second) + { + first = 0; + second = 0; + MODULEINFO info; + if (!GetModuleInformation(GetCurrentProcess(), GetModuleHandleW(NULL), &info, sizeof(info))) return false; + const DWORD base = (DWORD) info.lpBaseOfDll; + const DWORD size = info.SizeOfImage; + + if (!InImage(0, sizeof(IMAGE_DOS_HEADER), size) || !AllReadable(base, sizeof(IMAGE_DOS_HEADER))) return false; + const IMAGE_DOS_HEADER* dos = (const IMAGE_DOS_HEADER*) base; + const DWORD ntOffset = (DWORD) dos->e_lfanew; + if (dos->e_magic != IMAGE_DOS_SIGNATURE || !InImage(ntOffset, sizeof(IMAGE_NT_HEADERS), size) || !AllReadable(base + ntOffset, sizeof(IMAGE_NT_HEADERS))) return false; + const IMAGE_NT_HEADERS* nt = (const IMAGE_NT_HEADERS*) (base + ntOffset); + const DWORD tableOffset = ntOffset + FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + nt->FileHeader.SizeOfOptionalHeader; + const DWORD tableSize = nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER); + if (nt->Signature != IMAGE_NT_SIGNATURE || !InImage(tableOffset, tableSize, size) || !AllReadable(base + tableOffset, tableSize)) return false; + + const IMAGE_SECTION_HEADER* section = (const IMAGE_SECTION_HEADER*) (base + tableOffset); + for (WORD i = 0; i < nt->FileHeader.NumberOfSections; i++, section++) { + const DWORD offset = section->VirtualAddress; + const DWORD length = section->Misc.VirtualSize; + if ((section->Characteristics & IMAGE_SCN_MEM_EXECUTE) == 0) continue; + if (length == 0 || !InImage(offset, length, size) || !AllReadable(base + offset, length)) return false; + const DWORD last = base + offset + length - 1; + for (DWORD start = base + offset; start <= last;) { + const DWORD found = FindInRange(ucp_aob_spec, start, last); + if (found == 0) break; + if (first != 0) { + second = found; + return true; + } + first = found; + if (found == last) break; + start = found + 1; + } + } + return true; } // TODO: find all? diff --git a/AOB.h b/AOB.h index ac893e8..11fcea0 100644 --- a/AOB.h +++ b/AOB.h @@ -6,5 +6,8 @@ namespace AOB { //Consider: https://github.com/CvX/hadesmem DWORD FindPattern(DWORD dwAddress, DWORD dwLen, BYTE* bMask, char* szMask); DWORD Find(std::string ucp_aob); + // The first two matches in the executable sections of the main module (0 if none). + // Returns false when the headers or an executable section of the main module cannot be read. + bool FindInMainModule(std::string ucp_aob_spec, DWORD& first, DWORD& second); DWORD FindInRange(std::string ucp_aob_spec, DWORD min, DWORD max); } \ No newline at end of file diff --git a/CodeFunctions.cpp b/CodeFunctions.cpp index 0760900..f146cd9 100644 --- a/CodeFunctions.cpp +++ b/CodeFunctions.cpp @@ -1115,3 +1115,30 @@ int luaScanForAOB(lua_State* L) { return 1; } + +int luaScanForAOBInMainModule(lua_State* L) { + const char* query = luaL_checkstring(L, 1); + + if (strlen(query) < 2) { + return luaL_error(L, "first argument is too short, minimum length is 2"); + } + + if (!validateAOBQuery(query)) { + return luaL_error(L, "Invalid format, only spaces, 0-9, A-F, and ? are allowed"); + } + + DWORD first = 0; + DWORD second = 0; + + if (!AOB::FindInMainModule(query, first, second)) { + lua_pushnil(L); + lua_pushnil(L); + lua_pushstring(L, "incomplete"); + return 3; + } + + if (first == 0) lua_pushnil(L); else lua_pushinteger(L, first); + if (second == 0) lua_pushnil(L); else lua_pushinteger(L, second); + + return 2; +} diff --git a/CodeFunctions.h b/CodeFunctions.h index 5fa97a0..58e9015 100644 --- a/CodeFunctions.h +++ b/CodeFunctions.h @@ -21,6 +21,7 @@ int luaAllocateRWE(lua_State* L); int luaDeallocateRWE(lua_State* L); int luaScanForAOB(lua_State* L); +int luaScanForAOBInMainModule(lua_State* L); int luaDetourCode(lua_State* L); diff --git a/Directory.Build.props b/Directory.Build.props index 1002302..8853c89 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ RPS - 1.5.1 + 1.6.0 gynt gynt 2023 diff --git a/README.md b/README.md index 5772245..82fc71c 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,12 @@ detourCode(onDetour, 0xABCDEF, 7) scanForAOB(searchPattern[, min, max]) searchPattern scans the memory for this pattern min address to start searching - max address to stop searching + max last address of the search, a match has to end at or before it +scanForAOBInMainModule(searchPattern) + returns the first and the second match in the executable sections of the main module, nil for each one not found. + returns nil, nil, "incomplete" when the headers or an executable section of the main module cannot be read. + searchPattern: hexadecimal array with question marks for wildcards. example: "FF A1 E? B? ?? 00" ``` diff --git a/RuntimePatchingSystem.cpp b/RuntimePatchingSystem.cpp index cf7c7d4..33558d3 100644 --- a/RuntimePatchingSystem.cpp +++ b/RuntimePatchingSystem.cpp @@ -69,6 +69,7 @@ const struct luaL_Reg RPS_LIB[] = { {"registerString", registerString}, {"scanForAOB", luaScanForAOB}, + {"scanForAOBInMainModule", luaScanForAOBInMainModule}, {NULL, NULL} /* end of array */ }; diff --git a/RuntimePatchingSystem.nuspec b/RuntimePatchingSystem.nuspec index ebb08cf..21c4aa8 100644 --- a/RuntimePatchingSystem.nuspec +++ b/RuntimePatchingSystem.nuspec @@ -2,7 +2,7 @@ RPS - 1.5.2 + 1.6.0 Runtime Patching System gynt build\README.md diff --git a/tests/test-aob.lua b/tests/test-aob.lua new file mode 100644 index 0000000..332a104 --- /dev/null +++ b/tests/test-aob.lua @@ -0,0 +1,23 @@ +local lunatest = require("tests.lunatest.lunatest") +local rps = require("RPS") + +local test_aob = {} + +function test_aob.test_scanForAOB_inclusive_max() + local first = rps.scanForAOBInMainModule("? ? ?") + lunatest.assert_equal(first, rps.scanForAOB("? ? ?", first, first + 2)) + lunatest.assert_nil(rps.scanForAOB("? ? ?", first, first + 1)) +end + +function test_aob.test_scanForAOBInMainModule_second_match() + local first, second = rps.scanForAOBInMainModule("? ? ?") + lunatest.assert_equal(first + 1, second) +end + +function test_aob.test_scanForAOBInMainModule_not_found() + local first, second = rps.scanForAOBInMainModule(string.rep("37 9B E1 46 02 AF DC 85 ", 32)) + lunatest.assert_nil(first) + lunatest.assert_nil(second) +end + +return test_aob From 7ad87619041466f881e6c0310715da40832f3104 Mon Sep 17 00:00:00 2001 From: Krarilotus <51748815+Krarilotus@users.noreply.github.com> Date: Sat, 28 Nov 2026 01:06:21 +0100 Subject: [PATCH 2/2] Rename scanForAOBInMainModule to scanForAOBsInMainModule --- CodeFunctions.cpp | 2 +- CodeFunctions.h | 2 +- README.md | 2 +- RuntimePatchingSystem.cpp | 2 +- tests/test-aob.lua | 10 +++++----- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CodeFunctions.cpp b/CodeFunctions.cpp index f146cd9..ce8a6af 100644 --- a/CodeFunctions.cpp +++ b/CodeFunctions.cpp @@ -1116,7 +1116,7 @@ int luaScanForAOB(lua_State* L) { return 1; } -int luaScanForAOBInMainModule(lua_State* L) { +int luaScanForAOBsInMainModule(lua_State* L) { const char* query = luaL_checkstring(L, 1); if (strlen(query) < 2) { diff --git a/CodeFunctions.h b/CodeFunctions.h index 58e9015..d904e18 100644 --- a/CodeFunctions.h +++ b/CodeFunctions.h @@ -21,7 +21,7 @@ int luaAllocateRWE(lua_State* L); int luaDeallocateRWE(lua_State* L); int luaScanForAOB(lua_State* L); -int luaScanForAOBInMainModule(lua_State* L); +int luaScanForAOBsInMainModule(lua_State* L); int luaDetourCode(lua_State* L); diff --git a/README.md b/README.md index 82fc71c..296fcd7 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ scanForAOB(searchPattern[, min, max]) min address to start searching max last address of the search, a match has to end at or before it -scanForAOBInMainModule(searchPattern) +scanForAOBsInMainModule(searchPattern) returns the first and the second match in the executable sections of the main module, nil for each one not found. returns nil, nil, "incomplete" when the headers or an executable section of the main module cannot be read. diff --git a/RuntimePatchingSystem.cpp b/RuntimePatchingSystem.cpp index 33558d3..5a09583 100644 --- a/RuntimePatchingSystem.cpp +++ b/RuntimePatchingSystem.cpp @@ -69,7 +69,7 @@ const struct luaL_Reg RPS_LIB[] = { {"registerString", registerString}, {"scanForAOB", luaScanForAOB}, - {"scanForAOBInMainModule", luaScanForAOBInMainModule}, + {"scanForAOBsInMainModule", luaScanForAOBsInMainModule}, {NULL, NULL} /* end of array */ }; diff --git a/tests/test-aob.lua b/tests/test-aob.lua index 332a104..1e7a9b4 100644 --- a/tests/test-aob.lua +++ b/tests/test-aob.lua @@ -4,18 +4,18 @@ local rps = require("RPS") local test_aob = {} function test_aob.test_scanForAOB_inclusive_max() - local first = rps.scanForAOBInMainModule("? ? ?") + local first = rps.scanForAOBsInMainModule("? ? ?") lunatest.assert_equal(first, rps.scanForAOB("? ? ?", first, first + 2)) lunatest.assert_nil(rps.scanForAOB("? ? ?", first, first + 1)) end -function test_aob.test_scanForAOBInMainModule_second_match() - local first, second = rps.scanForAOBInMainModule("? ? ?") +function test_aob.test_scanForAOBsInMainModule_second_match() + local first, second = rps.scanForAOBsInMainModule("? ? ?") lunatest.assert_equal(first + 1, second) end -function test_aob.test_scanForAOBInMainModule_not_found() - local first, second = rps.scanForAOBInMainModule(string.rep("37 9B E1 46 02 AF DC 85 ", 32)) +function test_aob.test_scanForAOBsInMainModule_not_found() + local first, second = rps.scanForAOBsInMainModule(string.rep("37 9B E1 46 02 AF DC 85 ", 32)) lunatest.assert_nil(first) lunatest.assert_nil(second) end