-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGeneralUtils.cpp
183 lines (146 loc) · 3.92 KB
/
GeneralUtils.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
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
#include <stdio.h>
#include <windows.h>
#include "WoWMIPS.h"
PEB *GetPeb()
{
PEB *pPEB = NULL;
_asm
{
push eax
mov eax, fs:[0x30]
mov pPEB, eax
pop eax
}
return pPEB;
}
LDR_DATA_TABLE_ENTRY *GetPebLdrDataTableEntry(BYTE *pModuleBase)
{
PEB *pPEB = NULL;
LDR_DATA_TABLE_ENTRY *pCurrEntry = NULL;
LIST_ENTRY *pListHead = NULL;
LIST_ENTRY *pCurrListEntry = NULL;
DWORD dwInMemoryOrderLinksOffset = 0;
// get PEB ptr
pPEB = GetPeb();
// get InMemoryOrderLinks offset in structure
dwInMemoryOrderLinksOffset = (DWORD)((BYTE*)&pCurrEntry->InMemoryOrderLinks - (BYTE*)pCurrEntry);
// loop over PEB_LDR module entries
pListHead = &pPEB->Ldr->InMemoryOrderModuleList;
pCurrListEntry = pListHead->Flink;
for(;;)
{
// check if this is the final entry
if(pCurrListEntry == pListHead)
{
break;
}
// get ptr to current module entry
pCurrEntry = (LDR_DATA_TABLE_ENTRY*)((BYTE*)pCurrListEntry - dwInMemoryOrderLinksOffset);
if(pCurrEntry->DllBase == (void*)pModuleBase)
{
return pCurrEntry;
}
// get next module entry in list
pCurrListEntry = pCurrListEntry->Flink;
}
return NULL;
}
DWORD HookFunction(BYTE *pFunctionAddr, BYTE *pHookFunctionAddr)
{
BYTE bJumpCode[] = { 0xE9, 0x00, 0x00, 0x00, 0x00 };
DWORD dwOrigProtect = 0;
// set relative 32-bit address
*(DWORD*)&bJumpCode[1] = (DWORD)pHookFunctionAddr - ((DWORD)pFunctionAddr + 5);
// adjust protection
if(VirtualProtect(pFunctionAddr, sizeof(bJumpCode), PAGE_EXECUTE_READWRITE, &dwOrigProtect) == 0)
{
return 1;
}
// copy hook
memcpy((void*)pFunctionAddr, (void*)bJumpCode, sizeof(bJumpCode));
// restore protection
if(VirtualProtect(pFunctionAddr, sizeof(bJumpCode), dwOrigProtect, &dwOrigProtect) == 0)
{
return 1;
}
return 0;
}
DWORD CheckAddressInModule(char *pModuleName, BYTE *pAddress)
{
HMODULE hModule = NULL;
MEMORY_BASIC_INFORMATION MemoryBasicInformation;
hModule = GetModuleHandle(pModuleName);
if(hModule == NULL)
{
return 1;
}
memset((void*)&MemoryBasicInformation, 0, sizeof(MemoryBasicInformation));
if(VirtualQuery((void*)pAddress, &MemoryBasicInformation, sizeof(MemoryBasicInformation)) == 0)
{
return 1;
}
if(MemoryBasicInformation.State != MEM_COMMIT)
{
return 1;
}
if(MemoryBasicInformation.AllocationBase != (void*)hModule)
{
return 1;
}
return 0;
}
DWORD ValidatePtrProtection(BYTE *pMemory, DWORD dwLength, DWORD dwValidFlags)
{
BYTE *pCurrPage = NULL;
MEMORY_BASIC_INFORMATION MemoryBasicInformation;
// round down to current page base
pCurrPage = (BYTE*)((DWORD)pMemory & 0xFFFFF000);
// check all pages within range
for(;;)
{
if(pCurrPage >= (BYTE*)(pMemory + dwLength))
{
break;
}
memset((void*)&MemoryBasicInformation, 0, sizeof(MemoryBasicInformation));
if(VirtualQuery((void*)pMemory, &MemoryBasicInformation, sizeof(MemoryBasicInformation)) == 0)
{
return 1;
}
if(MemoryBasicInformation.State != MEM_COMMIT)
{
return 1;
}
if((MemoryBasicInformation.Protect & dwValidFlags) == 0)
{
return 1;
}
// move to next page
pCurrPage += 0x1000;
}
return 0;
}
DWORD ValidatePtrRead(BYTE *pMemory, DWORD dwLength)
{
if(ValidatePtrProtection(pMemory, dwLength, PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY) != 0)
{
return 1;
}
return 0;
}
DWORD ValidatePtrWrite(BYTE *pMemory, DWORD dwLength)
{
if(ValidatePtrProtection(pMemory, dwLength, PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | PAGE_READWRITE | PAGE_WRITECOPY) != 0)
{
return 1;
}
return 0;
}
DWORD ValidatePtrExec(BYTE *pMemory, DWORD dwLength)
{
if(ValidatePtrProtection(pMemory, dwLength, PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) != 0)
{
return 1;
}
return 0;
}