forked from AmrThabet/pySRDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.cpp
315 lines (281 loc) · 6.61 KB
/
emulator.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// emulator.cpp : For the disassembler and the emulator
//
#include "stdafx.h"
#include "pySRDF.h"
#include <Shlobj.h>
using namespace std;
Disasm::Disasm()
{
dis = new CPokasAsm();
memset(&temp_ins,0,sizeof(DISASM_INSTRUCTION));
}
DISASM_INS* Disasm::disasm (char bytes[])
{
DISASM_INS* ins = (DISASM_INS*)malloc(sizeof(DISASM_INS));
memset(ins,0,sizeof(DISASM_INS));
memset(&temp_ins,0,sizeof(DISASM_INSTRUCTION));
dis->Disassemble(bytes,&temp_ins);
DWORD len;
ins->ins = dis->Disassemble(bytes,len);
ins->cmd = (char*)temp_ins.opcode->c_str();
ins->opcode = temp_ins.hde.opcode;
ins->opcode2 = temp_ins.hde.opcode2;
ins->length = temp_ins.hde.len;
ins->dest = temp_ins.ndest;
ins->src = temp_ins.nsrc;
ins->category = temp_ins.category;
ins->flags = temp_ins.flags;
ins->other = temp_ins.other;
ins->modrm.length = temp_ins.modrm.length;
for (int i = 0;i < 3;i++)
{
ins->modrm.__items__[i] = temp_ins.modrm.items[i];
ins->modrm.__flags__[i] = temp_ins.modrm.flags[i];
}
return ins;
}
void Disasm::assemble(char* ins, char **s, int *slen)
{
DWORD len;
char* ins_bytes = dis->Assemble(ins,len);
*s = ins_bytes;
*slen = len;
}
Disasm::~Disasm()
{
delete dis;
}
//---------------------------------------------------------------------------------------
Emulator::Emulator(char *FileName)
{
char* DLLPath = (char*)malloc(MAX_PATH);
memset(DLLPath,0,MAX_PATH);
//throw(std::out_of_range("wrong filename"));
DWORD Length = SHGetSpecialFolderPath(0, DLLPath, CSIDL_SYSTEMX86, false);
cString SysPath = DLLPath;
SysPath += "\\";
LastError = -1;
try
{
emu = new CPokasEmu(FileName,SysPath.GetChar());
}
catch (int x)
{
LastError = 8;
set_err(GetLastError());
GetLastError();
return;
}
dis = new Disasm();
Imagebase = emu->GetImagebase();
RefreshRegisters();
MaxIterations = 1000000;
}
Emulator::Emulator(char *buff,int size)
{
char* DLLPath = (char*)malloc(MAX_PATH);
memset(DLLPath,0,MAX_PATH);
DWORD Length = GetSystemDirectoryA(DLLPath,MAX_PATH);
emu = new CPokasEmu(buff,size,PROCESS_SHELLCODE, DLLPath);
dis = new Disasm();
Imagebase = emu->GetImagebase();
RefreshRegisters();
LastError = -1;
MaxIterations = 1000000;
}
int Emulator::Run()
{
UpdateRegisters();
emu->MaxIteration = MaxIterations;
LastError = emu->Emulate();
RefreshRegisters();
return LastError;
}
int Emulator::Run(char* LogFile)
{
UpdateRegisters();
int LastError = emu->Emulate(LogFile);
RefreshRegisters();
return LastError;
}
int Emulator::Step()
{
UpdateRegisters();
LastError = emu->Step();
RefreshRegisters();
return LastError;
}
int Emulator::SetBp(char* Breakpoint)
{
try
{
return emu->SetBreakpoint(Breakpoint);
}
catch(int x)
{
set_err(emu->process->debugger->GetLastError().c_str());
return -1;
}
}
void Emulator::RemoveBp(int index)
{
return emu->DisableBreakpoint(index);
}
DWORD Emulator::GetRealAddr(DWORD vAddr)
{
return emu->GetRealAddr(vAddr);
}
void Emulator::ClearDirtyPages()
{
return emu->ClearDirtyPages();
}
int Emulator::Dump(char* OutputFile, int ImportFixType)
{
return emu->MakeDumpFile(OutputFile,ImportFixType);
}
DWORD Emulator::GetReg(int index)
{
return emu->GetReg(index);
}
void Emulator::SetReg(int index,DWORD value)
{
emu->SetReg(index,value);
RefreshRegisters();
}
DISASM_INS* Emulator::disasm(DWORD vAddr)
{
DWORD Addr = emu->GetRealAddr((DWORD)vAddr);
return dis->disasm((char*)Addr);
}
void Emulator::Read(DWORD vAddr,DWORD size,char **s, int *slen)
{
char* Addr = (char*)emu->GetRealAddr((DWORD)vAddr);
if (Addr)
{
*s = (char*)malloc(size);
memcpy(*s,Addr,size);
*slen = size;
}
}
void Emulator::Write(DWORD vAddr, char* buff, DWORD size)
{
char* Addr = (char*)emu->GetRealAddr((DWORD)vAddr);
if (Addr)
{
memcpy(Addr,buff,size);
}
}
array<DIRTYPAGES_STRUCT*> Emulator::GetDirtyPages()
{
array<DIRTYPAGES_STRUCT*> Pages;
Pages.init(sizeof(DIRTYPAGES_STRUCT));
for (int i = 0;i < emu->GetNumberOfDirtyPages();i++)
{
Pages.additem(emu->GetDirtyPage(i));
}
return Pages;
}
array<MEMORY_STRUCT*> Emulator::GetMemoryPage()
{
array<MEMORY_STRUCT*> Pages;
Pages.init(sizeof(MEMORY_STRUCT));
for (int i = 0;i < emu->GetNumberOfMemoryPages();i++)
{
Pages.additem(emu->GetMemoryPage(i));
}
return Pages;
}
MEMORY_STRUCT* Emulator::GetMemoryPageByVA(DWORD vAddr)
{
return emu->GetMemoryPageByVA(vAddr);
}
Emulator::~Emulator()
{
if (LastError != 8)
{
delete emu;
delete dis;
}
}
void Emulator::RefreshRegisters()
{
eax = emu->GetReg(0);
ecx = emu->GetReg(1);
edx = emu->GetReg(2);
ebx = emu->GetReg(3);
esp = emu->GetReg(4);
ebp = emu->GetReg(5);
esi = emu->GetReg(6);
edi = emu->GetReg(7);
eip = emu->GetEip();
EFlags = emu->GetEFLAGS();
}
void Emulator::UpdateRegisters()
{
emu->SetReg(0,eax);
emu->SetReg(1,ecx);
emu->SetReg(0,edx);
emu->SetReg(0,ebx);
emu->SetReg(0,esp);
emu->SetReg(0,ebp);
emu->SetReg(0,esi);
emu->SetReg(0,edi);
emu->SetEip(eip);
emu->SetEFLAGS(EFlags);
}
array<SEARCH_FOUND*> Emulator::Search(char* StringToSearch)
{
cString Signature = StringToSearch;
Signature.Replace(':',' ');
cYaraScanner* YaraScan = new cYaraScanner();
array<SEARCH_FOUND*> found;
found.init(sizeof(SEARCH_FOUND));
cString Rule = YaraScan->CreateRule("ProcessSearch",Signature);
int x = YaraScan->AddRule(Rule);
for (int i=0 ; i<(int)(emu->GetNumberOfMemoryPages()) ;i++)
{
MEMORY_STRUCT* MemMap = (MEMORY_STRUCT*)emu->GetMemoryPage(i);
unsigned char* Address = (unsigned char*)MemMap->RealAddr;
cList* Results = YaraScan->Scan(Address,MemMap->Size);
if (Results == NULL)continue;
_YARA_RESULT* Result = (_YARA_RESULT*)Results->GetItem(0);
if (Result == NULL)continue;
for (int l = 0; l < Result->Matches->GetNumberOfItems();l++)
{
SEARCH_FOUND item = {0};
MSTRING* Match = (MSTRING*)Result->Matches->GetItem(l);
//cout << "FOUND: " << (int*)(MemMap->VirtualAddr + Match->offset) << "\n";
item.Address = MemMap->VirtualAddr + Match->offset;
item.Allocationbase = MemMap->VirtualAddr;
found.additem(&item);
}
}
return found;
}
char* Emulator::GetLastError()
{
switch(LastError)
{
case -1:
return "The application didn't run";
case EXP_EXCEED_MAX_ITERATIONS:
return "The application exceed the maximum iterations. Run again";
case EXP_INVALIDPOINTER:
return "Invalid pointer";
case EXP_WRITEACCESS_DENIED:
return "Unable to write, access denied";
case EXP_INVALID_OPCODE:
return "Invalid instruction";
case EXP_DIVID_BY_ZERO:
return "Divid by Zero";
case EXP_INVALID_INSTRUCTION:
return "Invalid instruction";
case EXP_DIV_OVERFLOW:
return "Divid Overflow";
case EXP_BREAKPOINT:
return "Breakpoint reached";
case ERROR_FILENAME:
return "Wrong filename or access denied";
}
return "no error";
}