forked from ooooooooooossssps/ShellEmuX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangedmemory.cpp
More file actions
289 lines (267 loc) · 6.96 KB
/
changedmemory.cpp
File metadata and controls
289 lines (267 loc) · 6.96 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "changedmemory.h"
#include <iostream>
#include <string.h>
#include <beaengine/BeaEngine.h>
#include <vector>
#define EMULATION_CHM_THRESHOLD 3000
Data::Register ChangedMemory::convertRegister(int beareg)
{
switch (beareg)
{
case 0x1: return Data::EAX;
case 0x2: return Data::ECX;
case 0x4: return Data::EDX;
case 0x8: return Data::EBX;
case 0x10: return Data::ESP;
case 0x20: return Data::EBP;
case 0x40: return Data::ESI;
case 0x80: return Data::EDI;
default: return Data::NOREG;
}
}
Data::Register ChangedMemory::convertSegmentRegister(int beareg)
{
switch (beareg)
{
case 0x1: return Data::ES;
case 0x2: return Data::DS;
case 0x3: return Data::FS;
case 0x4: return Data::GS;
case 0x5: return Data::CS;
case 0x6: return Data::SS;
default: return Data::NOREG;
}
}
bool ChangedMemory::contains(IntPair new_p, IntPair cur_p)
{
return (new_p.first<=cur_p.first && new_p.second >=cur_p.second);
}
bool ChangedMemory::is_contained_by(IntPair new_p, IntPair cur_p)
{
return (new_p.first>=cur_p.first && new_p.second <=cur_p.second);
}
bool ChangedMemory::intersect_left(IntPair new_p, IntPair cur_p)
{
return (new_p.first<=cur_p.first-1 && new_p.second>=cur_p.first-1);
}
bool ChangedMemory::intersect_right(IntPair new_p, IntPair cur_p)
{
return (new_p.second>=cur_p.second+1 && new_p.first<=cur_p.second+1);
}
ChangedMemory::ChangedMemory(char* filename, int emulator_type)
{
reader=new Reader;
reader->load(filename);
switch (emulator_type)
{
case 1: emulator = new Emulator_LibEmu;
break;
/*
case 2: emulator = new Emulator_Qemu;
break;
default:;
*/
}
emulator -> bind(reader);
shellcode = NULL;
shellcode_size = NULL;
amount_shellcodes = 0;
}
ChangedMemory::ChangedMemory(unsigned char* data, int datasize, int emulator_type)
{
reader=new Reader;
reader->link(data, datasize);
switch (emulator_type)
{
case 1: emulator = new Emulator_LibEmu;
break;
/*
case 2: emulator = new Emulator_Qemu;
break;
default:;
*/
}
emulator -> bind(reader);
shellcode = NULL;
shellcode_size = NULL;
amount_shellcodes = 0;
}
ChangedMemory::~ChangedMemory()
{
delete reader;
delete emulator;
clear();
}
void ChangedMemory::getsizes(int* size)
{
for (int i = 0; i < amount_shellcodes; i++)
{
size[i]=shellcode_size[i];
}
}
void ChangedMemory::getmem(unsigned char** bytes)
{
for (int i = 0; i < amount_shellcodes; i++)
{
for (int j = 0; j < shellcode_size[i]; j++)
{
bytes[i][j] = shellcode[i][j];
}
}
}
void ChangedMemory::clear()
{
for (int i = 0; i < amount_shellcodes; i++)
{
delete [] shellcode[i];
}
if (shellcode != NULL)
delete [] shellcode;
if (shellcode_size != NULL)
delete [] shellcode_size;
shellcode = NULL;
shellcode_size = NULL;
amount_shellcodes = 0;
}
/*bool ChangedMemory::is_ok(vector<IntPair> intervals)
{
for (unsigned int i = 0; i < intervals.size(); i++)
{
if (intervals[i].first > intervals[i].second)
return false;
}
for (unsigned int i = 0; i < intervals.size() - 1; i++)
{
if (intervals[i+1].first <= intervals[i].second)
return false;
}
return true;
}*/
int ChangedMemory::compute(int entry_point)
{
clear();
emulator -> begin(entry_point);
char buf[10];
DISASM MyDisasm;
(void) memset (&MyDisasm, 0, sizeof(DISASM));
MyDisasm.EIP = (UIntPtr) buf;
unsigned int memory_changing;
unsigned int *max_addr = NULL, *min_addr = NULL;
IntPair p;
vector<IntPair> intervals, intervals_before;
for (int i = 0; i < EMULATION_CHM_THRESHOLD ; i++)
{
if (!emulator -> get_command(buf))
{
//cerr << "Execution error"<< endl;
break;
}
int num = emulator->get_register(Data::EIP);
if (!reader->is_valid(num)) {
//cout << " Reached end of the memory block, stopping instance." << endl;
break;
}
int len = Disasm(&MyDisasm);
if (len == UNKNOWN_OPCODE)
{
break;
}
//printf("%s\n", MyDisasm.CompleteInstr);
int num1 = 0, num2 = 0;
if (MyDisasm.Argument1.AccessMode == WRITE && MyDisasm.Argument1.ArgType == MEMORY_TYPE &&
MyDisasm.Argument1.ArgSize != 0)
{
if (convertRegister(MyDisasm.Argument1.Memory.BaseRegister)!=Data::NOREG)
num1=emulator->get_register(convertRegister(MyDisasm.Argument1.Memory.BaseRegister));
if (convertRegister(MyDisasm.Argument1.Memory.IndexRegister)!=Data::NOREG)
num2=emulator->get_register(convertRegister(MyDisasm.Argument1.Memory.IndexRegister));
memory_changing = num1+MyDisasm.Argument1.Memory.Scale*num2+MyDisasm.Argument1.Memory.Displacement;
if (min_addr == NULL)
{
min_addr = new unsigned int;
*min_addr = memory_changing;
}
if (max_addr == NULL)
{
max_addr = new unsigned int;
*max_addr = memory_changing+ MyDisasm.Argument1.ArgSize -1;
}
if (memory_changing < *min_addr)
*min_addr = memory_changing;
if (memory_changing+ MyDisasm.Argument1.ArgSize -1 > *max_addr)
*max_addr = memory_changing+ MyDisasm.Argument1.ArgSize -1;
p.first = memory_changing;
p.second = memory_changing + MyDisasm.Argument1.ArgSize -1;
unsigned int ind = 0;
bool inserted = false;
intervals_before = intervals;
for (ind = 0; ind < intervals.size(); ind++)
{
if (is_contained_by(p,intervals[ind]))
{
inserted = true;
}
if (contains(p,intervals[ind]))
{
intervals[ind] = p;
inserted = true;
}
if (intersect_left(p, intervals[ind]))
{
intervals[ind].first = p.first;
inserted = true;
}
if (intersect_right(p, intervals[ind]))
{
intervals[ind].second = p.second;
inserted = true;
}
}
if (inserted)
{
for (ind = 0; ind < intervals.size()-1; )
{
if (intervals[ind].second>=intervals[ind+1].first)
{
intervals[ind].second = intervals[ind+1].second;
intervals.erase(intervals.begin() + ind+1);
}
else
ind++;
}
}
else
{
for (ind = 0; ind <intervals.size() && intervals[ind].first < p.first; ind++);
intervals.insert( intervals.begin()+ind, p);
}
}
emulator -> step();
/*if (!emulator -> step())
{
cerr << "Execution error, skipping instruction" << endl;
emulator->jump(num + len);
}*/
}
shellcode = new unsigned char* [intervals.size()];
shellcode_size = new int [intervals.size()];
for (unsigned int i=0; i < intervals.size(); i++)
shellcode[i] = NULL;
amount_shellcodes = 0;
for (unsigned int i=0; i<intervals.size(); i++)
{
if (intervals[i].second < intervals[i].first + 20)
continue;
if (intervals[i].first - reader->entrance() >= reader->size())
continue;
if (intervals[i].second - reader->entrance() >= reader->size())
intervals[i].second = reader->size() - 1 + reader->entrance();
shellcode_size[amount_shellcodes] = intervals[i].second - intervals[i].first + 1;
shellcode[amount_shellcodes] = new unsigned char[shellcode_size[amount_shellcodes]];
emulator->get_memory((char *) shellcode[amount_shellcodes], intervals[i].first, shellcode_size[amount_shellcodes]);
amount_shellcodes++;
}
delete min_addr;
delete max_addr;
return amount_shellcodes;
}