forked from AmrThabet/x86Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmem.cpp
280 lines (258 loc) · 9.25 KB
/
vmem.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
*
* Copyright (C) 2010-2011 Amr Thabet <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to Amr Thabet
*
*/
#include "x86emu.h"
#define MAXIMUM_STATIC_SIZE 100
#define VMEM_START_VALLOC 0x00870000
#define VMEM_START_DLL 0x77000000
#define VMEM_START_STACK 0x00126000
VirtualMemory::VirtualMemory()
{
// it's a dynamic array to vMem* so we will initialize it
vAllocCommittedPages = VMEM_START_VALLOC;
DLLCommittedPages = VMEM_START_DLL;
StackCommittedPages = VMEM_START_STACK;
vmem = (vMem **) malloc(MAXIMUM_STATIC_SIZE * 4);
memset((void *) vmem, 0, 4);
vmem_length = 0;
cmem = (cMem **) malloc(4);
memset((void *) cmem, 0, 4);
cmem_length = 0;
last_accessed = new Log(0);
last_modified = new Log(0);
}
//This function create a random address for DLL Imagebase, VirtualProtect or Stack
DWORD VirtualMemory::create_memory_address(int Type)
{
DWORD* pCommitedPages = NULL;
if (Type == VMEM_TYPE_STACK)
{
pCommitedPages = &StackCommittedPages;
}
else if (Type == VMEM_TYPE_DLL)
{
pCommitedPages = &DLLCommittedPages;
}
else if (Type == VMEM_TYPE_ALLOC)
{
pCommitedPages = &vAllocCommittedPages;
}
if (pCommitedPages == NULL)return 0;
while(1)
{
if (read_virtual_mem(*pCommitedPages) != 0)
{
*pCommitedPages += 0x10000;
}
else
{
break;
}
}
return *pCommitedPages;
}
// DWORD VirtualMemory::add_pointer(DWORD rptr,DWORD vptr,DWORD size){
// add_pointer(rptr,vptr,size,MEM_READWRITE);
// };
void _cdecl VirtualMemory::add_pointer(DWORD rptr, DWORD vptr, DWORD size, int flags) {
if (vmem_length == 0) {
vmem[0] = (vMem *) malloc(MAXIMUM_STATIC_SIZE * sizeof(vMem));
vmem[0]->rmem = rptr;
vmem[0]->vmem = vptr;
vmem[0]->size = size;
vmem[0]->flags = flags;
vmem_length++;
} else {
if (vmem_length >= MAXIMUM_STATIC_SIZE) {
DWORD c = (DWORD) vmem;
vmem = (vMem **) realloc((void *) vmem, (vmem_length + 1) * 4);
memcpy((void *) c, vmem, (vmem_length) * 4);
vmem[vmem_length] = (vMem *) malloc(sizeof(vMem));
// if (vmem[vmem_length]==0)vmem[vmem_length]=(vMem*)alloc(sizeof(vMem));
memset(vmem[vmem_length], 0, 4);
} else {
// vmem=(vMem**)realloc((void*)vmem,(vmem_length+1)*4) ;
vmem[vmem_length] = (vMem*)((DWORD)vmem[vmem_length - 1] + (DWORD)sizeof(vMem));
}
vmem[vmem_length]->rmem = rptr;
vmem[vmem_length]->vmem = vptr;
vmem[vmem_length]->size = size;
vmem[vmem_length]->flags = flags; // */
vmem_length++;
}
}
DWORD VirtualMemory::get_virtual_pointer(DWORD ptr) {
for (int i = this->vmem_length - 1; i >= 0; i--) {
if ((ptr >= vmem[i]->rmem) && (ptr < (vmem[i]->rmem + vmem[i]->size)) && (vmem[i]->size != 0)) {
ptr -= vmem[i]->rmem;
ptr += vmem[i]->vmem;
return ptr;
}
}
// throw(EXP_INVALIDPOINTER); //we don't need errors this time
return 0;
}
DWORD * VirtualMemory::read_virtual_mem(DWORD ptr) {
DWORD vptr = ptr;
for (int i = this->vmem_length - 1; i >= 0; i--) {
// cout << (int*)vptr << " "<<(int*)vmem[i]->vmem << "\n";
if ((ptr >= vmem[i]->vmem) && (ptr < (vmem[i]->vmem + vmem[i]->size)) && (vmem[i]->size != 0)) {
ptr -= vmem[i]->vmem;
ptr += vmem[i]->rmem;
last_accessed->addlog(vptr);
return (DWORD *) ptr;
}
}
return 0;
}
bool VirtualMemory::get_memory_flags(DWORD ptr) {
for (int i = 0; i < this->cmem_length; i++) {
if ((ptr >= cmem[i]->ptr) && (ptr < (cmem[i]->ptr + cmem[i]->size))) {
return true;
}
}
// cout << this->cmem_length <<"\n";
return false;
}
DWORD VirtualMemory::write_virtual_mem(DWORD ptr, DWORD size,unsigned char * buff) {
int vptr = ptr;
int entry = 0;
for (int i = this->vmem_length - 1; i >= 0; i--) {
if ((ptr >= vmem[i]->vmem) && (ptr < (vmem[i]->vmem + vmem[i]->size)) && (vmem[i]->size != 0)) {
ptr -= vmem[i]->vmem;
ptr += vmem[i]->rmem;
entry = i;
goto mem_found;
}
}
return EXP_INVALIDPOINTER;
mem_found:
if (vmem[entry]->flags == MEM_IMAGEBASE) {
if (!check_writeaccess(vptr, vmem[entry]->vmem)) {
return EXP_WRITEACCESS_DENIED;
}
}
if ((vmem[entry]->flags == MEM_READONLY) || (vmem[entry]->flags == MEM_DLLBASE)) {
return EXP_WRITEACCESS_DENIED;
}
memcpy((void *) ptr, buff, size);
last_modified->addlog(vptr);
set_memory_flags((DWORD) vptr, size);
return 0;
}
void VirtualMemory::set_memory_flags(DWORD ptr, int size) {
for (int i = 0; i < this->cmem_length; i++) {
if ((ptr >= cmem[i]->ptr) && (ptr < (cmem[i]->ptr + cmem[i]->size))) {
// so it's allready written
goto found_ptr;
} else if (ptr == (cmem[i]->ptr + cmem[i]->size)) { // here if it's the next DWORD or the next byte (for loop on decrypting something
cmem[i]->size += size;
goto found_ptr;
} else if ((ptr + size) == cmem[i]->ptr) { // the prev byte or DWORD (decrypting from the end to the top)
cmem[i]->ptr -= size;
cmem[i]->size += size;
goto found_ptr;
}
}
// if not found so add it
if (cmem_length == 0) {
cmem[0] = (cMem *) malloc(sizeof(cMem));
cmem[0]->ptr = ptr;
cmem[0]->size = size;
cmem_length++;
} else {
cmem = (cMem **) realloc((void *) cmem, (cmem_length + 1) * 4);
cmem[cmem_length] = (cMem *) malloc(sizeof(cMem));
cmem[cmem_length]->ptr = ptr;
cmem[cmem_length]->size = size;
cmem_length++;
}
found_ptr:;
}
bool VirtualMemory::check_writeaccess(DWORD ptr, DWORD imagebase) {
// cout << (int*)ptr << "\n"<< (int*)imagebase << "\n";
image_header * PEHeader;
DWORD FileHandler, PEHeader_ptr;
image_section_header * data;
FileHandler = (DWORD) read_virtual_mem(imagebase);
PEHeader_ptr = ((dos_header *) FileHandler)->e_lfanew + FileHandler;
PEHeader = (image_header *) PEHeader_ptr;
if (ptr < (imagebase + PEHeader->optional.section_alignment)) {
return false;
}
ptr -= imagebase;
image_section_header * sections = (image_section_header *) (PEHeader->header.size_of_optional_header + (DWORD) & PEHeader->optional);
if (PEHeader->header.number_of_sections != 0) {
for (int i = 0; i < PEHeader->header.number_of_sections - 1; i++) {
if ((ptr >= sections[i].virtual_address) && (ptr < (sections[i + 1].virtual_address))) {
if (sections[i].characteristics & IMAGE_SCN_MEM_WRITE) {
return true;
} else {
/*if(ptr == 0x33b0 ){
cout << "Imagebase : "<< (int*)imagebase << "\n";
cout << "Section : "<< i << "\n";
cout << "Characteristics : "<< (int*)sections[i].characteristics << "\n";
cout << "VirtualAddress : " << (int*)sections[i].virtual_address << "\n";
cout << "VirtualSize : " << (int*)sections[i+1].virtual_address << "\n";
};//*/
return false;
}
}
}
int n = PEHeader->header.number_of_sections - 1;
DWORD s = (DWORD) & sections[n];
s += sizeof(image_section_header) + 1;
image_section_header * f = (image_section_header *) s;
if ((ptr >= sections[n].virtual_address) && (ptr < (PEHeader->optional.size_of_image))) {
if (sections[n].characteristics & IMAGE_SCN_MEM_WRITE) { //
return true;
} else {
return false;
}
}
}
return false;
}
DWORD VirtualMemory::delete_pointer(DWORD ptr) {
for (int i = this->vmem_length - 1; i >= 0; i--) {
if ((ptr >= vmem[i]->vmem) && (ptr <= (vmem[i]->vmem + vmem[i]->size)) && (vmem[i]->size != 0)) {
vmem[i]->size = 0;
return 0;
}
}
return -1;
}
DWORD VirtualMemory::get_last_accessed(int index) {
return last_accessed->getlog(index);
}
DWORD VirtualMemory::get_last_modified(int index) {
return last_modified->getlog(index);
}
VirtualMemory::~VirtualMemory()
{
for (int i = 0; i < vmem_length; i++) {
#ifdef WIN32
if (!(vmem[i]->flags & MEM_VIRTUALPROTECT)) {
VirtualFree((void *) vmem[i]->rmem, vmem[i]->size, MEM_DECOMMIT);
}
#else
free((void *) vmem[i]->rmem);
#endif
}
}