Repository navigation
Expand file tree
/
Copy pathaslr.cpp
More file actions
266 lines (237 loc) · 7.76 KB
/
Copy pathaslr.cpp
File metadata and controls
266 lines (237 loc) · 7.76 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <Windows.h>
#include "util.h"
bool setting_aslr = true;
OSVERSIONINFOW VersionInformation;
#define WINDOWS_VISTA 6
typedef struct _thread_info
{
struct _thread_info *next;
void *handler;
}thread_info;
thread_info* module_header = NULL;
static int LastError;
int __stdcall init_thread_info(HANDLE hThread)
{
int ret; // eax@1
thread_info *current_thread; // esi@4
DWORD v3; // edi@5
ret = LastError;
if ( !LastError )
{
if ( hThread == GetCurrentThread() )
return 0;
current_thread = new thread_info();
if ( !current_thread ) // new failed
{
dword_1C898 = 0;
LastError = ERROR_NOT_ENOUGH_MEMORY; //8
return LastError;
}
if ( SuspendThread(hThread) == -1 )
{
delete current_thread;
dword_1C898 = 0;
LastError = GetLastError();
return LastError;
}
current_thread->next = module_header;// current_module->next = module_header
current_thread->handler = hThread; // current_module->handle = hThread
module_header = current_thread;
ret = 0;
}
return ret;
}
typedef struct _aslr_struct2{
}aslr_struct2;
typedef struct _aslr_struct
{
struct _aslr_struct *next;
void *param1;
void *param2;
void *new_add;
aslr_struct2 *aslr_struct2; //define later
int old_VP_flag;
}aslr_struct;
aslr_struct* struct_header;
volatile LONG CurrentThreadID;
LPVOID lpAddress;
void* dword_1C88C;
int dword_1C898;
unsigned int enable_execute()
{
unsigned int result;
DWORD flOldProtect;
if ( CurrentThreadID || InterlockedCompareExchange(&CurrentThreadID, GetCurrentThreadId(), 0) ){
result = ERROR_INVALID_OPERATION;//0x10DDu;
}else {
LPVOID address = lpAddress;
dword_1C88C = 0;
struct_header = NULL;
module_header = NULL;
LastError = 0;
dword_1C898 = 0;
while(address){
VirtualProtect(address, 0x10000u, PAGE_EXECUTE_READWRITE, &flOldProtect);
address = (LPVOID)*((DWORD *)address + 1);
}
}
return result;
}
int prepare_new_data2(void* a1, LPCVOID lpAddress, void* addr1, void* addr2, void* addr3)
{
if ( addr1 )
*addr1 = 0;
if (addr2)
*addr2 = 0;
if ( addr3)
*addr3 = 0;
if ( CurrentThreadID != GetCurrentThreadId() )
return ERROR_INVALID_OPERATION;//0x10DDu;
}
int prepare_new_data(void *a1, LPCVOID lpAddress)
{
return prepare_new_data2(a1, lpAddress, NULL, NULL, NULL);
}
void enable_aslr(HMODULE hd){
memset( &VersionInformation, 0, sizeof(VersionInformation) );
VersionInformation.dwOSVersionInfoSize = 284;
GetVersionExW((LPOSVERSIONINFOW)&VersionInformation);
if ( setting_aslr ){
getModuleHandleEx(hd);
if ( VersionInformation.dwMajorVersion >= WINDOWS_VISTA ){
enable_execute();
init_thread_info(GetCurrentThread());
prepare_new_data((int)&dword_1C408, lpAddress);
apply_aslr(NULL);
}
}
}
extern HANDLE refresh_code_inmemory();
int refresh_and_resume(DWORD flOldProtect)
{
int result = 0;
if ( CurrentThreadID == GetCurrentThreadId() ){
//release all struct_header
if ( struct_header ) {
aslr_struct* header = (aslr_struct *)struct_header;
do {
VirtualProtect(header->new_add, *((BYTE *)header->aslr_struct2 + 30), header->old_VP_flag, &flOldProtect);
if ( header->param1 ){
if ( header->aslr_struct2 ){
release_struct2(header->aslr_struct2);
header->aslr_struct2 = NULL;
}
}
aslr_struct *temp = header->next;
delete header;
header = temp;
}while(header);
}
struct_header = 0;
refresh_code_inmemory();
if ( module_header ){
thread_info* header = (thread_info *)module_header;
do {
ResumeThread(header->handler);
thread_info* temp = header->next;
delete header;
header = temp;
}while(header);
}
module_header = NULL;
CurrentThreadID = NULL;
result = 0;
} else {
result = ERROR_INVALID_OPERATION;//0x10DD;
}
return result;
}
int apply_aslr(void* a1)
{
DWORD flOldProtect;
if ( a1 )
*(DWORD*)a1 = dword_1C898;
if ( CurrentThreadID != GetCurrentThreadId() )
return 0x10DD;
if ( !LastError ) {
for (aslr_struct* i = (aslr_struct *)struct_header; i; i = i->next ){
aslr_struct2* struct2 = i->aslr_struct2;
if ( i->param1 ){
memcpy(i->new_add, (char *)struct2 + 32, *((BYTE *)struct2 + 54));
}else {
//TODO : not implement
}
}//end of for
if ( !module_header ){
Cleanup:
aslr_struct* header = struct_header;
if ( struct_header != NULL ){
do{
VirtualProtect(header->new_add, *((BYTE *)header->aslr_struct2 + 30), header->old_VP_flag, &flOldProtect);
FlushInstructionCache(GetCurrentProcess(), header->new_add, *((BYTE *)header->aslr_struct2 + 30));
if ( header->param1 != NULL ){
if ( header->aslr_struct2 ){
release_struct2(header->aslr_struct2);
header->aslr_struct2 = NULL;
}
}
aslr_struct* temp = header->next;
delete header;
header = temp;
}while( header );
}
struct_header = NULL;
refresh_code_inmemory();
if ( module_header != NULL ){
thread_info* header = (thread_info *)module_header;
do {
ResumeThread(header->handler);
thread_info* temp = header->next;
delete header;
header = temp;
}while(header);
}
module_header = NULL;
CurrentThreadID = NULL;
return LastError;
}
thread_info* header = (thread_info *)module_header;
CONTEXT context ;
while(1){
HANDLE handler = (HANDLE)header->handler;
context.ContextFlags = 0x10001;
if ( GetThreadContext(handler, &context) )
break;
header = header->next;
if ( !header )
goto Cleanup;
} //end of while
for ( aslr_struct* j = (aslr_struct*)struct_header; ; j = j->next ) //adjust eip
{
if ( !j ) {
header = header->next;
if ( !header )
goto Cleanup;
}
if ( j->param1 ){
int addr = (int)j->new_add;
if ( context.Eip >= (unsigned int)addr && context.Eip < j->aslr_struct2->module_size ){
context.Eip -= (DWORD)addr;
context.Eip += (DWORD)(j->new_add);
SetThreadContext((HANDLE)header->handler, &context);
continue;
} else {
int addr = (int)j->new_add;
if ( context.Eip >= (unsigned int)addr
&& context.Eip < (unsigned int)((char *)addr + *((BYTE *)j->aslr_struct2 + 30)) )
{
context.Eip -= (DWORD)addr; //old_base_addr
context.Eip += (DWORD)(j->aslr_struct2);
SetThreadContext((HANDLE)header->handler, &context);
continue;
}
}
}
}
}
}