forked from fengjixuchui/XHOOK-WoW-1.12.1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindPattern.cpp
108 lines (83 loc) · 2.94 KB
/
FindPattern.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "FindPattern.h"
inline bool Compare(const uint8_t* data, const uint8_t* pattern, const char* mask) {
for (; *mask; ++mask, ++data, ++pattern)
if (*mask == 'x' && *data != *pattern)
return false;
return (*mask) == 0;
}
uintptr_t FindPattern(const char* module, const char* pattern_string, const char* mask) {
MODULEINFO module_info = {};
GetModuleInformation(GetCurrentProcess(), GetModuleHandleA(module), &module_info, sizeof MODULEINFO);
uintptr_t module_start = uintptr_t(module_info.lpBaseOfDll);
const uint8_t* pattern = reinterpret_cast<const uint8_t*>(pattern_string);
for (size_t i = 0; i < module_info.SizeOfImage; i++)
if (Compare(reinterpret_cast<uint8_t*>(module_start + i), pattern, mask))
return module_start + i;
return 0;
}
/* ---------- UTILITIES MEMORY ---------- */
DWORD WaitOnModuleHandle(std::string moduleName)
{
DWORD ModuleHandle = NULL;
while (!ModuleHandle)
{
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
if (!ModuleHandle)
Sleep(50);
}
return ModuleHandle;
}
DWORD FindPatternMasks(std::string moduleName, BYTE* Mask, char* szMask)
{
DWORD Address = WaitOnModuleHandle(moduleName.c_str());
MODULEINFO ModInfo; GetModuleInformation(GetCurrentProcess(), (HMODULE)Address, &ModInfo, sizeof(MODULEINFO));
DWORD Length = ModInfo.SizeOfImage;
for (DWORD c = 0; c < Length; c += 1)
if (Compare((BYTE*)(Address + c), Mask, szMask))
return (DWORD)(Address + c);
// Utilities::Error("Failed to find pattern in " + moduleName + ". (" + std::string(szMask) + ", " + std::string((char*)Mask) + ")");
return 0;
}
uintptr_t FindPatternMask(std::string module, BYTE* mask, char* szMask)
{
auto temp = FindPatternMasks(module, mask, szMask);
if (!temp)
{
// Utilities::Error("Failed to find mask \"" + std::string(szMask) + "\" in module " + module + ".");
}
return temp;
}
//findpattern2
#define INRANGE(x,a,b) (x >= a && x <= b)
#define getBits( x ) (INRANGE((x&(~0x20)),'A','F') ? ((x&(~0x20)) - 'A' + 0xa) : (INRANGE(x,'0','9') ? x - '0' : 0))
#define getByte( x ) (getBits(x[0]) << 4 | getBits(x[1]))
DWORD FindPatternV2(std::string moduleName, std::string pattern)
{
const char* pat = pattern.c_str();
DWORD firstMatch = 0;
DWORD rangeStart = (DWORD)GetModuleHandleA(moduleName.c_str());
MODULEINFO miModInfo; GetModuleInformation(GetCurrentProcess(), (HMODULE)rangeStart, &miModInfo, sizeof(MODULEINFO));
DWORD rangeEnd = rangeStart + miModInfo.SizeOfImage;
for (DWORD pCur = rangeStart; pCur < rangeEnd; pCur++)
{
if (!*pat)
return firstMatch;
if (*(PBYTE)pat == '\?' || *(BYTE*)pCur == getByte(pat))
{
if (!firstMatch)
firstMatch = pCur;
if (!pat[2])
return firstMatch;
if (*(PWORD)pat == '\?\?' || *(PBYTE)pat != '\?')
pat += 3;
else
pat += 2; //one ?
}
else
{
pat = pattern.c_str();
firstMatch = 0;
}
}
return NULL;
}