forked from luciouskami/shadowMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshadowMap.cpp
More file actions
202 lines (159 loc) · 4.1 KB
/
shadowMap.cpp
File metadata and controls
202 lines (159 loc) · 4.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <windows.h>
#include <vector>
#include <assert.h>
#include "shadowMap.h"
#define MAX_SECTION 28
static
PVOID g_mapAddress = NULL;
static
ULONG_PTR mapAddress[MAX_SECTION] = { 0 };
static
ULONG mapSize[MAX_SECTION] = { 0 };
static
ULONG_PTR realAddress[MAX_SECTION] = { 0 };
static
ULONG realSize[MAX_SECTION] = { 0 };
static
DWORD oldProtect[MAX_SECTION] = { 0 };
static
size_t section_count = 0;
/**
* VEH
*/
LONG WINAPI VEH(PEXCEPTION_POINTERS ExceptionInfo)
{
PVOID crashAddr = ExceptionInfo->ExceptionRecord->ExceptionAddress;
for (size_t i = 0; i < section_count; i++)
{
if (realAddress[i] <= (ULONG_PTR)crashAddr &&
(realAddress[i] + realSize[i]) >= (ULONG_PTR)crashAddr)
{
#ifdef _WIN64
ExceptionInfo->ContextRecord->Rip = \
(ULONG_PTR)crashAddr - realAddress[i] + mapAddress[i];
#else
ExceptionInfo->ContextRecord->Eip = \
(ULONG_PTR)crashAddr - realAddress[i] + mapAddress[i];
#endif
return EXCEPTION_CONTINUE_EXECUTION;
}
if (mapAddress[i] <= (ULONG_PTR)crashAddr &&
(mapAddress[i] + mapSize[i]) >= (ULONG_PTR)crashAddr)
{
#ifdef _WIN64
ExceptionInfo->ContextRecord->Rip = \
(ULONG_PTR)crashAddr - mapAddress[i] + realAddress[i];
ExceptionInfo->ExceptionRecord->ExceptionAddress = (PVOID) \
((ULONG_PTR)crashAddr - mapAddress[i] + realAddress[i]);
#else
ExceptionInfo->ContextRecord->Eip = \
(ULONG_PTR)crashAddr - mapAddress[i] + realAddress[i];
ExceptionInfo->ExceptionRecord->ExceptionAddress = (PVOID) \
((ULONG_PTR)crashAddr - mapAddress[i] + realAddress[i]);
#endif
return EXCEPTION_CONTINUE_EXECUTION;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
/**
* 安装hook
*/
BOOL WINAPI shadowMap_InstallHook(HMODULE hModule)
{
assert(hModule);
PIMAGE_DOS_HEADER doshead_ptr;
PIMAGE_NT_HEADERS nthead_ptr;
PIMAGE_SECTION_HEADER sec_ptr;
HANDLE hProcess;
DWORD protect;
doshead_ptr = (PIMAGE_DOS_HEADER)hModule;
if (doshead_ptr->e_magic != IMAGE_DOS_SIGNATURE)
{
return FALSE;
}
nthead_ptr = (PIMAGE_NT_HEADERS)((ULONG_PTR)hModule + doshead_ptr->e_lfanew);
if (nthead_ptr->Signature != IMAGE_NT_SIGNATURE)
{
return FALSE;
}
g_mapAddress = VirtualAlloc(NULL, nthead_ptr->OptionalHeader.SizeOfImage, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (g_mapAddress == NULL)
{
return FALSE;
}
//
//获取首个区段
//
sec_ptr = IMAGE_FIRST_SECTION(nthead_ptr);
//
//添加VEH异常
//
AddVectoredExceptionHandler(0, VEH);
section_count = 0;
for (int i = 0; i < nthead_ptr->FileHeader.NumberOfSections; i++)
{
if (sec_ptr->Characteristics & IMAGE_SCN_MEM_EXECUTE)
{
mapAddress[section_count] = (ULONG_PTR)g_mapAddress + sec_ptr->VirtualAddress;
mapSize[section_count] = sec_ptr->Misc.VirtualSize;
realAddress[section_count] = (ULONG_PTR)hModule + sec_ptr->VirtualAddress;
realSize[section_count] = sec_ptr->Misc.VirtualSize;
oldProtect[section_count] = sec_ptr->Characteristics;
memcpy((void*)mapAddress[section_count],
(void*)realAddress[section_count],
sec_ptr->Misc.VirtualSize);
section_count++;
}
sec_ptr++;
}
//
//绕过VMP3.X 内存保护
//
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if (hProcess != NULL)
{
for (size_t i = 0; i < section_count; i++)
{
protect = PAGE_READWRITE;
VirtualProtectEx(hProcess, (LPVOID)realAddress[i],
realSize[i],
protect,
&oldProtect[i]);
}
CloseHandle(hProcess);
}
else
{
for (size_t i = 0; i < section_count; i++)
{
protect = PAGE_READWRITE;
VirtualProtectEx((HANDLE)-1, (LPVOID)realAddress[i],
realSize[i],
protect,
&oldProtect[i]);
}
}
return TRUE;
}
/**
* 卸载Hook
*/
BOOL WINAPI shadowMap_UnloadHook()
{
return TRUE;
}
/**
* 读shadowMap内存
*/
BOOL WINAPI shadowMap_ReadMem(PVOID addr, UCHAR *buf, ULONG size)
{
return TRUE;
}
/**
* 写shadowMap内存
*/
BOOL WINAPI shadowMap_WriteMem(PVOID addr, UCHAR *buf, ULONG size)
{
return TRUE;
}