-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPatternScan.h
50 lines (43 loc) · 1.35 KB
/
PatternScan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Windows.h>
#include <vector>
// https://github.com/lguilhermee/Discord-DX11-Overlay-Hook/blob/master/Helper/Helper.cpp
uintptr_t PatternScan(uintptr_t moduleAdress, const char* signature)
{
static auto patternToByte = [](const char* pattern)
{
auto bytes = std::vector<int>{};
const auto start = const_cast<char*>(pattern);
const auto end = const_cast<char*>(pattern) + strlen(pattern);
for (auto current = start; current < end; ++current)
{
if (*current == '?')
{
++current;
bytes.push_back(-1);
}
else { bytes.push_back(strtoul(current, ¤t, 16)); }
}
return bytes;
};
const auto dosHeader = (PIMAGE_DOS_HEADER)moduleAdress;
const auto ntHeaders = (PIMAGE_NT_HEADERS)((std::uint8_t*)moduleAdress + dosHeader->e_lfanew);
const auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
auto patternBytes = patternToByte(signature);
const auto scanBytes = reinterpret_cast<std::uint8_t*>(moduleAdress);
const auto s = patternBytes.size();
const auto d = patternBytes.data();
for (auto i = 0ul; i < sizeOfImage - s; ++i)
{
bool found = true;
for (auto j = 0ul; j < s; ++j)
{
if (scanBytes[i + j] != d[j] && d[j] != -1)
{
found = false;
break;
}
}
if (found) { return reinterpret_cast<uintptr_t>(&scanBytes[i]); }
}
return NULL;
}