Repository navigation
Expand file tree
/
Copy pathAOB.cpp
More file actions
167 lines (145 loc) · 5.45 KB
/
Copy pathAOB.cpp
File metadata and controls
167 lines (145 loc) · 5.45 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "framework.h"
#include <psapi.h>
#include <vector>
#include <regex>
#include <string>
#include <iostream>
#include <algorithm>
#include "AOB.h"
namespace AOB {
bool bCompare(const BYTE* pData, const BYTE* bMask, const char* szMask)
{
for (; *szMask; ++szMask, ++pData, ++bMask)
if (*szMask == 'x' && *pData != *bMask)
return 0;
return (*szMask) == NULL;
}
DWORD FindPattern(DWORD dwAddress, DWORD dwLen, BYTE* bMask, char* szMask)
{
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))
{
return (DWORD)(dwAddress + i);
}
}
return 0;
}
std::vector<char> HexToBytes(const std::string& hex)
{
std::vector<char> bytes;
for (unsigned int i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2);
char byte = (char)strtol(byteString.c_str(), NULL, 16);
bytes.push_back(byte);
}
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)
{
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 = next;
}
if (address > runStart) return FindPattern((DWORD) runStart, (DWORD) (address - runStart), (BYTE*) content, mask);
return 0;
}
DWORD FindInRange(std::string ucp_aob_spec, DWORD min, DWORD max) {
std::string haystack = ucp_aob_spec;
std::regex target("([A-Fa-f0-9]{2})|([?]+)");
std::smatch sm;
std::string content("");
std::string mask("");
while (std::regex_search(haystack, sm, target))
{
if (sm[0] == "?") {
mask += " ";
content += "FF"; //Or 00? We just need dummy content here.
}
else {
mask += "x";
content += sm[0].str();
}
haystack = sm.suffix();
}
std::vector<char> 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?
// example: Find("57 E8 7B C0 10 ?")
DWORD Find(std::string ucp_aob_spec)
{
return FindInRange(ucp_aob_spec, 0x400000, 0x7FFFFFFF);
}
}