Repository navigation
Expand file tree
/
Copy pathMemoryScan.cpp
More file actions
126 lines (116 loc) · 3.66 KB
/
Copy pathMemoryScan.cpp
File metadata and controls
126 lines (116 loc) · 3.66 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
#include "pch.h"
#include "MemoryScan.h"
using std::cout;
using std::hex;
using std::dec;
using std::endl;
void* IScanner::FindPattern(const char* module, const char* strPattern) {
int patternLen = 0;
int j = 0;
unsigned char* skipByteMask = new unsigned char[MAX_PATTERN_SIZE];
for (int k = 0; k < MAX_PATTERN_SIZE; k++)
skipByteMask[k] = 0;
unsigned char* pattern = Parse(patternLen, strPattern, skipByteMask);
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
MEMORY_BASIC_INFORMATION info;
MODULEENTRY32 moduleEntry;
GetModuleInfo(module, &moduleEntry);
void* pStart = moduleEntry.modBaseAddr;
void* pFinish = moduleEntry.modBaseAddr + moduleEntry.modBaseSize;
unsigned char* current = (unsigned char*)pStart;
DEBUG("current: ", current);
DEBUG("pStart: ", pStart);
DEBUG("pFinish: ", pFinish);
for (; current < pFinish && j < patternLen; current++) {
if (!VirtualQuery((LPCVOID)current, &info, sizeof(info)))
continue;
unsigned long long protectMask = PAGE_READONLY | PAGE_READWRITE | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE | PAGE_EXECUTE_READ;
if (info.State == MEM_COMMIT && info.Protect & protectMask && !(info.Protect & PAGE_GUARD)) {
unsigned long long finish = (unsigned long long)pFinish < (unsigned long long)info.BaseAddress + info.RegionSize ?
(unsigned long long)pFinish : (unsigned long long)info.BaseAddress + info.RegionSize;
current = (unsigned char*)info.BaseAddress;
unsigned char* rip = 0;
for (unsigned long long k = (unsigned long long)info.BaseAddress; k < finish && j < patternLen; k++, current++) {
if (skipByteMask[j] || pattern[j] == *current) {
if (j == 0)
rip = current;
j++;
}
else {
j = 0;
if (pattern[0] == *current) {
rip = current;
j = 1;
}
}
}
if (j == patternLen) {
current = rip;
break;
}
}
else
current += sysInfo.dwPageSize;
}
delete[] skipByteMask;
delete[] pattern;
if (j == patternLen)
return (void*)current;
return nullptr;
}
int IScanner::GetModuleInfo(const char* name, MODULEENTRY32* entry) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE, GetCurrentProcessId());
if (snapshot == INVALID_HANDLE_VALUE)
return 1;
entry->dwSize = sizeof(MODULEENTRY32);
if (!Module32First(snapshot, entry)) {
CloseHandle(snapshot);
return 1;
}
do {
if (!_stricmp(entry->szModule, name))
break;
} while (Module32Next(snapshot, entry));
CloseHandle(snapshot);
return 0;
}
unsigned char* IScanner::Parse(int& len, const char* strPattern, unsigned char* skipByteMask) {
int strPatternLen = strlen(strPattern);
unsigned char* pattern = new unsigned char[strPatternLen];
for (int i = 0; i < strPatternLen; i++)
pattern[i] = 0;
len = 0;
for (int i = 0; i < strPatternLen; i += 2) {
unsigned char code = 0;
if (strPattern[i] == SKIP_SYMBOL)
skipByteMask[len] = 1;
else
code = Parse(strPattern[i]) * 16 + Parse(strPattern[i + 1]);
i++;
pattern[len++] = code;
}
return pattern;
}
unsigned char IScanner::Parse(char byte) { // some magic values
if (byte >= '0' && byte <= '9')
return byte - '0';
else if (byte >= 'a' && byte <= 'f')
return byte - 'a' + 10;
else if (byte >= 'A' && byte <= 'F')
return byte - 'A' + 10;
return 0;
}
void ConsoleScanner::PrintMemory(const char* title, unsigned char* memPointer, int size) {
cout << title << ":" << endl;
for (int i = 0; i < size; i++)
cout << hex << (int)(*(memPointer + i)) << " ";
cout << endl;
cout << dec;
}
void FileScanner::PrintMemory(const char* title, unsigned char* memPointer, int size) {
fprintf(fptr, "%s:\n", title);
for (int i = 0; i < size; i++)
fprintf(fptr, "%x ", (int)(*(memPointer + i)));
fprintf(fptr, "\n", title);
}