This repository has been archived on 2026-11-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
my-first-apex-internal/shd/signature.cpp
T
2026-11-05 02:19:09 +01:00

92 lines
2.0 KiB
C++

#include <include.h>
DWORD_PTR shared::sig::FindPattern(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern)
{
PCWSTR pat = pattern;
DWORD_PTR firstMatch = 0x0, numSkippedBytes = 0x0;
for (DWORD_PTR pCur = baseAddress; pCur < baseAddress + length; pCur++)
{
numSkippedBytes += 1;
if (*(BYTE*)pat == '\?' || *(BYTE*)pCur == GetByte(pat)) {
if (!*pat) {
return firstMatch;
}
if (!firstMatch) {
firstMatch = pCur;
}
if (!pat[2]) {
return firstMatch;
}
if (*(WORD*)pat == '\?\?' || *(BYTE*)pat != '\?') {
pat += 3;
}
else {
pat += 2;
}
}
else {
pCur -= numSkippedBytes - 0x1;
numSkippedBytes = 0;
pat = pattern;
firstMatch = 0;
}
}
return 0;
}
uintptr_t shared::sig::find_offset(uintptr_t start_addr, const uint8_t* pattern_buf, size_t pattern_len, size_t offset, size_t opcode_len) noexcept
{
size_t max_steps = 32;
uintptr_t end_addr = (start_addr + max_steps * sizeof(uintptr_t));
hde64s instr[1];
for (auto current = (uint8_t*)start_addr; (uintptr_t)current < end_addr; current = (uint8_t*)((uintptr_t)current + instr[0].len))
{
if (!hde::disasm_buffer(reinterpret_cast<void*>(current), 32, instr, 1))
break;
if (stl::memeq(current, pattern_buf, pattern_len))
{
auto dst = ((uintptr_t)current + offset);
return *(u_long*)(dst + opcode_len);
}
}
return 0;
}
DWORD_PTR shared::sig::FindPatternSafe(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern)
{
if (!baseAddress || !length || !pattern)
return 0;
const auto buffer = stl::mem_alloc<void*>( length );
const auto buffer_base = mem::addr(buffer).Base();
if (!buffer || !buffer_base)
return 0;
memset(buffer, 0, length);
if (!util::read_memory(buffer, baseAddress, length))
return 0;
const auto result = FindPattern(buffer_base, length, pattern);
if (!result)
return 0;
const auto result_rva = mem::addr(result).Sub(buffer_base).Base();
const auto result_actual = mem::addr(baseAddress).Add(result_rva).Base();
return result_actual;
}