-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpatterscan.cpp
More file actions
171 lines (142 loc) · 3.91 KB
/
patterscan.cpp
File metadata and controls
171 lines (142 loc) · 3.91 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
168
169
170
171
#include "patternscan.h"
//Split combo pattern into mask/pattern
void Pattern::Parse(char* combo, char* pattern, char* mask)
{
unsigned int patternLen = (strlen(combo) + 1) / 3;
for (unsigned int i = 0; i < strlen(combo); i++)
{
if (combo[i] == ' ')
{
continue;
}
else if (combo[i] == '?')
{
mask[(i + 1) / 3] = '?';
i += 2;
}
else
{
char byte = (char)strtol(&combo[i], 0, 16);
pattern[(i + 1) / 3] = byte;
mask[(i + 1) / 3] = 'x';
i += 2;
}
}
pattern[patternLen] = '\0';
mask[patternLen] = '\0';
}
//Internal Pattern Scan
char* Pattern::In::Scan(char* pattern, char* mask, char* begin, unsigned int size)
{
unsigned int patternLength = strlen(pattern);
for (unsigned int i = 0; i < size - patternLength; i++)
{
bool found = true;
for (unsigned int j = 0; j < patternLength; j++)
{
if (mask[j] != '?' && pattern[j] !=*(begin + i + j))
{
found = false;
break;
}
}
if (found)
{
return (begin + i);
}
}
return nullptr;
}
char* Pattern::In::Mod(char *combopattern, Module* module)
{
}
char* Pattern::In::AllMods(char* combopattern)
{
}
char* Pattern::In::Proc(char* combopattern)
{
}
//External Wrapper
char* Pattern::Ex::Scan(char* pattern, char* mask, char* begin, char* end, Process* process)
{
char* currentChunk = begin;
SIZE_T bytesRead;
while (currentChunk < end)
{
char buffer[4096]; //char * buffer[4096];?
DWORD oldprotect;
VirtualProtectEx(process->handle, currentChunk, sizeof(buffer), PROCESS_VM_READ, &oldprotect);
ReadProcessMemory(process->handle, currentChunk, &buffer, sizeof(buffer), &bytesRead);
VirtualProtectEx(process->handle, currentChunk, sizeof(buffer), oldprotect, NULL);
if (bytesRead == 0)
{
return nullptr;
}
char* internalAddress = In::Scan(pattern, mask, (char*)&buffer, bytesRead);
if (internalAddress != nullptr)
{
//calculate from internal to external
uintptr_t offsetFromBuffer = internalAddress - (char*)&buffer;
return (currentChunk + offsetFromBuffer);
}
else
{
//advance to next chunk
currentChunk = currentChunk + bytesRead;
}
}
return nullptr;
}
//Module wrapper for external pattern scan
char* Pattern::Ex::Mod(char* pattern, char* mask, Module* module)
{
return Scan(pattern, mask, (char*)module->modBaseAddr, (char*)module->modBaseAddr + module->modBaseSize, module->process);
}
//loops through all modules and scans them
char* Pattern::Ex::AllMods(char* pattern, char* mask, Process* process)
{
MODULEENTRY32 modEntry = { 0 };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process->th32ProcessID);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
modEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnapshot, &modEntry))
{
do
{
char* patternAddress = In::Scan(pattern, mask, (char*)modEntry.modBaseAddr, modEntry.dwSize);
if (patternAddress != nullptr)
{
CloseHandle(hSnapshot);
return patternAddress;
}
} while (Module32Next(hSnapshot, &modEntry));
}
CloseHandle(hSnapshot);
}
return nullptr;
}
//Combo External Module
char* Pattern::Ex::Mod(char* combopattern, Module* module)
{
unsigned int patternLen = ((strlen(combopattern) + 1) / 3) + 1;
char* pattern = new char[patternLen];
char* mask = new char[patternLen];
Parse(combopattern, pattern, mask);
char* match = Pattern::Ex::Mod(pattern, mask, module);
delete[] pattern;
delete[] mask;
return match;
}
//Combo External Process
char* Pattern::Ex::AllMods(char* combopattern, Process* process)
{
unsigned int patternLen = ((strlen(combopattern) + 1) / 3) + 1;
char* pattern = new char[patternLen];
char* mask = new char[patternLen];
Parse(combopattern, pattern, mask);
char* match = AllMods(pattern, mask, process);
delete[] pattern;
delete[] mask;
return match;
}