forked from can1357/ThePerfectInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleMapper.h
351 lines (289 loc) · 13 KB
/
SimpleMapper.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#include <Windows.h>
#include <fstream>
#include <vector>
#include <functional>
#pragma pack(push, 1)
struct TlsLockedHookController
{
BYTE IsFree;
BYTE NumThreadsWaiting;
BYTE EntryBytes;
};
#pragma pack(pop)
static std::vector<BYTE> Mp_ReadFile( const std::string& Path )
{
std::ifstream Stream( Path, std::ios::binary | std::ios::ate );
std::ifstream::pos_type Pos = Stream.tellg();
if ( Pos == ( std::ifstream::pos_type ) - 1 )
return {};
std::vector<BYTE> Data( Pos );
Stream.seekg( 0, std::ios::beg );
Stream.read( ( char* ) &Data[ 0 ], Pos );
return Data;
}
static void * Mp_RvaToPointer( BYTE* Image, DWORD Va )
{
PIMAGE_DOS_HEADER DosHeader = ( PIMAGE_DOS_HEADER ) Image;
PIMAGE_NT_HEADERS FileHeader = ( PIMAGE_NT_HEADERS ) ( ( uint64_t ) DosHeader + DosHeader->e_lfanew );
PIMAGE_SECTION_HEADER SectionHeader = ( PIMAGE_SECTION_HEADER )
( ( ( ULONG_PTR ) &FileHeader->OptionalHeader ) + FileHeader->FileHeader.SizeOfOptionalHeader );
for ( int i = 0; i < FileHeader->FileHeader.NumberOfSections; i++ )
{
char * Name = ( char* ) SectionHeader[ i ].Name;
DWORD RawData = SectionHeader[ i ].PointerToRawData;
DWORD VirtualAddress = SectionHeader[ i ].VirtualAddress;
DWORD RawSize = SectionHeader[ i ].SizeOfRawData;
DWORD VirtualSize = SectionHeader[ i ].Misc.VirtualSize;
if ( Va >= VirtualAddress &&
Va < ( VirtualAddress + VirtualSize ) )
{
return Image + Va - VirtualAddress + RawData;
}
}
return Image + Va;
}
static void Mp_PushBytes( std::vector<BYTE>& Target, const std::vector<BYTE>& Bytes )
{
int i = Target.size();
Target.resize( i + Bytes.size() );
memcpy( &Target[ i ], &Bytes[ 0 ], Bytes.size() );
}
static std::vector<BYTE> Mp_CreateImportShell( BYTE* Image, PVOID MappedAdr, bool LoadLib )
{
// no handle, no access to modules /shrug
// could prob read EProcess->Peb but cba sorry
std::vector<BYTE> Out =
{
0x48, 0x83, 0xEC, 0x38, // sub rsp,0x38
0x4C, 0x8D, 0x3D, 0xDD, 0xCC, 0xBB, 0x00, // lea r15, [rip+0xBBCCDD]
0x48, 0xB8, 0xAA, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, // mov rax, 0xAABBCCDDEEAA ; GetModuleHandleA // LoadLibraryA?
0x49, 0x89, 0xC5, // mov r13, rax
0x48, 0xB8, 0xAA, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, // mov rax, 0xAABBCCDDEEAA ; GetProcAddress
0x49, 0x89, 0xC4 // mov r11, rax
};
*( FARPROC* ) &Out[ 0xD ] = LoadLib ? GetProcAddress( GetModuleHandleA( "KERNEL32" ), "LoadLibraryA" ) : GetProcAddress( GetModuleHandleA( "KERNEL32" ), "GetModuleHandleA" ); // avoding __imp's
*( FARPROC* ) &Out[ 0x1A ] = GetProcAddress( GetModuleHandleA( "KERNEL32" ), "GetProcAddress" ); // avoding __imp's
std::vector<BYTE> DataContainer = {};
PIMAGE_DOS_HEADER DosHeader = ( PIMAGE_DOS_HEADER ) Image;
PIMAGE_NT_HEADERS FileHeader = ( PIMAGE_NT_HEADERS ) ( ( uint64_t ) DosHeader + DosHeader->e_lfanew );
PIMAGE_OPTIONAL_HEADER OptionalHeader = &FileHeader->OptionalHeader;
PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = ( PIMAGE_IMPORT_DESCRIPTOR ) Mp_RvaToPointer
(
Image,
FileHeader->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_IMPORT ].VirtualAddress
);
while ( ImportDescriptor && ImportDescriptor->Name && FileHeader->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_IMPORT ].Size )
{
PCHAR ModuleName = ( PCHAR ) Mp_RvaToPointer( Image, ImportDescriptor->Name );
IMAGE_THUNK_DATA * Thunk = NULL;
IMAGE_THUNK_DATA * Func = NULL;
uint32_t ModuleNameOffset = DataContainer.size();
do
DataContainer.push_back( *ModuleName );
while ( *ModuleName++ );
std::vector<BYTE> ModulePusher =
{
0x49, 0x8D, 0x8F, 0xBB, 0xAA, 0x00, 0x00, // lea rcx,[r15+0xaabb]
0x41, 0xFF, 0xD5, // call r13
0x48, 0x89, 0xC6 // mov rsi,rax
};
*( uint32_t* ) ( &ModulePusher[ 3 ] ) = ModuleNameOffset;
Mp_PushBytes( Out, ModulePusher );
if ( ImportDescriptor->OriginalFirstThunk )
{
Thunk = ( IMAGE_THUNK_DATA* ) Mp_RvaToPointer( Image, ImportDescriptor->OriginalFirstThunk);
Func = ( IMAGE_THUNK_DATA* ) ( ( PUCHAR ) MappedAdr + ImportDescriptor->FirstThunk );
}
else
{
Thunk = ( IMAGE_THUNK_DATA* ) Mp_RvaToPointer( Image, ImportDescriptor->FirstThunk);
Func = ( IMAGE_THUNK_DATA* ) ( ( PUCHAR ) MappedAdr + ImportDescriptor->FirstThunk );
}
for ( ; Thunk->u1.AddressOfData; Thunk++, Func++ )
{
assert( !( Thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG64 ) );
FARPROC FunctionAddress = NULL;
IMAGE_IMPORT_BY_NAME* ImageImportByName = ( IMAGE_IMPORT_BY_NAME* )
Mp_RvaToPointer( Image, *( DWORD* ) Thunk );
PCHAR ImportName = ( PCHAR ) ImageImportByName->Name;
ULONGLONG* Target = &Func->u1.Function;
uint32_t ImportNameOffset = DataContainer.size();
if ( !strcmpi( ImportName, "AddVectoredExceptionHandler" ) )
printf( "\n[+] WARNING: Vectored Exception Handling IS NOT SUPPORTED!\n\n" );
do
DataContainer.push_back( *ImportName );
while ( *ImportName++ );
uint32_t OffsetOffset = DataContainer.size();
DataContainer.resize( DataContainer.size() + 8 );
*( uint64_t* ) ( &DataContainer[ OffsetOffset ] ) = ( uint64_t ) Target;
std::vector<BYTE> ImportFixer =
{
0x48, 0x89, 0xF1, // mov rcx,rsi
0x49, 0x8D, 0x97, 0xBB, 0xAA, 0x00, 0x00, // lea rdx,[r9+0xaabb]
0x41, 0xFF, 0xD4, // call r12
0x49, 0x8B, 0x9F, 0xBB, 0xAA, 0x00, 0x00, // mov rbx,QWORD PTR [r9+0xaabb]
0x48, 0x89, 0x03 // mov QWORD PTR [rbx],rax
};
*( uint32_t* ) ( &ImportFixer[ 6 ] ) = ImportNameOffset;
*( uint32_t* ) ( &ImportFixer[ 16 ] ) = OffsetOffset;
Mp_PushBytes( Out, ImportFixer );
}
ImportDescriptor++;
}
Mp_PushBytes( Out, { 0x48, 0x83, 0xC4, 0x38 } ); // add rsp, 0x38
uint32_t JmpSize = Out.size();
Mp_PushBytes( Out, { 0xE9, 0x00, 0x00, 0x00, 0x00 } ); // jmp 0xAABBCCDD
*( uint32_t* ) ( &Out[ 7 ] ) = Out.size() - 0xB;
Mp_PushBytes( Out, DataContainer );
*( int32_t* ) ( &Out[ JmpSize + 1 ] ) = DataContainer.size();
return Out;
}
static void Mp_RelocateImage( BYTE* Image, BYTE* Target )
{
PIMAGE_DOS_HEADER DosHeader = ( PIMAGE_DOS_HEADER ) Image;
PIMAGE_NT_HEADERS FileHeader = ( PIMAGE_NT_HEADERS ) ( ( uint64_t ) DosHeader + DosHeader->e_lfanew );
PIMAGE_SECTION_HEADER SectionHeader = ( PIMAGE_SECTION_HEADER )
( ( ( ULONG_PTR ) &FileHeader->OptionalHeader ) + FileHeader->FileHeader.SizeOfOptionalHeader );
// Copy sections
memcpy( Target, Image, 0x1000 ); // Pe Header
for ( int i = 0; i < FileHeader->FileHeader.NumberOfSections; i++ )
{
char * Name = ( char* ) SectionHeader[ i ].Name;
uint64_t RawData = SectionHeader[ i ].PointerToRawData;
uint64_t VirtualAddress = SectionHeader[ i ].VirtualAddress;
uint64_t RawSize = SectionHeader[ i ].SizeOfRawData;
uint64_t VirtSize = SectionHeader[ i ].Misc.VirtualSize;
ZeroMemory( Target + VirtualAddress, VirtSize );
memcpy( Target + VirtualAddress, Image + RawData, RawSize );
if ( !strcmpi( Name, ".pdata" ) )
printf( "\n[+] WARNING: Structured Exception Handling IS NOT SUPPORTED!\n\n" );
if ( !strcmpi( Name, ".tls" ) )
printf( "\n[+] WARNING: Thread-local Storage IS NOT SUPPORTED!\n\n" );
}
// Reloc sections
if ( FileHeader->OptionalHeader.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC &&
FileHeader->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BASERELOC ].VirtualAddress != 0 )
{
PIMAGE_BASE_RELOCATION Reloc = ( PIMAGE_BASE_RELOCATION ) ( Target + FileHeader->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BASERELOC ].VirtualAddress );
DWORD RelocSize = FileHeader->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BASERELOC ].Size;
uint64_t Delta = (uint64_t)Target - FileHeader->OptionalHeader.ImageBase;
int c = 0;
while ( c < RelocSize )
{
size_t p = sizeof( IMAGE_BASE_RELOCATION );
LPWORD Chains = ( LPWORD ) ( ( PUCHAR ) Reloc + p );
while ( p < Reloc->SizeOfBlock )
{
uint64_t Base = ( uint64_t ) ( Target + Reloc->VirtualAddress );
switch ( *Chains >> 12 )
{
case IMAGE_REL_BASED_HIGHLOW:
*( uint32_t* ) ( Base + ( *Chains & 0xFFF ) ) += ( uint32_t ) Delta;
break;
case IMAGE_REL_BASED_DIR64:
*( uint64_t* ) ( Base + ( *Chains & 0xFFF ) ) += Delta;
break;
}
Chains++;
p += sizeof( WORD );
}
c += Reloc->SizeOfBlock;
Reloc = ( PIMAGE_BASE_RELOCATION ) ( ( PBYTE ) Reloc + Reloc->SizeOfBlock );
}
}
}
static TlsLockedHookController* Mp_MapDllAndCreateHookEntry( const std::string& Path, PVOID ValCheck, PVOID HookOut, bool LoadLib, const std::function<PVOID( SIZE_T )>& MemoryAllocator )
{
auto File = Mp_ReadFile( Path );
assert( File.size() );
PIMAGE_DOS_HEADER DosHeader = ( PIMAGE_DOS_HEADER ) File.data();
assert( DosHeader->e_magic == IMAGE_DOS_SIGNATURE );
PIMAGE_NT_HEADERS FileHeader = ( PIMAGE_NT_HEADERS ) ( ( uint64_t ) DosHeader + DosHeader->e_lfanew );
assert( FileHeader->Signature == IMAGE_NT_SIGNATURE );
PIMAGE_OPTIONAL_HEADER OptionalHeader = &FileHeader->OptionalHeader;
assert( OptionalHeader->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC );
assert( FileHeader->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64 );
std::vector<BYTE> Prologue =
{
0x00, 0x00, // data
0xF0, 0xFE, 0x05, 0xF8, 0xFF, 0xFF, 0xFF, // lock inc byte ptr [rip-n]
// wait_lock:
0x80, 0x3D, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, // cmp byte ptr [rip-m], 0x0
0xF3, 0x90, // pause
0x74, 0xF5, // je wait_lock
0x48, 0xB8, 0xAA, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, // mov rax, 0xAABBCCDDEEAA
// data_sync_lock:
0x0F, 0x0D, 0x08, // prefetchw [rax]
0x81, 0x38, 0xDD, 0xCC, 0xBB, 0xAA, // cmp dword ptr[rax], 0xAABBCCDD
0xF3, 0x90, // pause
0x75, 0xF3, // jne data_sync_lock
0xF0, 0xFE, 0x0D, 0xCF, 0xFF, 0xFF, 0xFF, // lock dec byte ptr [rip-n]
0x75, 0x41, // jnz continue_exec
0x53, // push stuff
0x51,
0x52,
0x56,
0x57,
0x55,
0x41, 0x50,
0x41, 0x51,
0x41, 0x52,
0x41, 0x53,
0x41, 0x54,
0x41, 0x55,
0x41, 0x56,
0x41, 0x57,
0x9C,
0x48, 0x89, 0xE5, // mov rbp, rsp
0x48, 0x83, 0xEC, 0x20, // sub rsp, 0x20
0x48, 0x83, 0xE4, 0xF0, // and rsp, 0xFFFFFFFFFFFFFFF0
0xE8, 0x26, 0x00, 0x00, 0x00, // call stub
0x48, 0x89, 0xEC, // mov rsp, rbp
0x9D, // pop stuff
0x41, 0x5F,
0x41, 0x5E,
0x41, 0x5D,
0x41, 0x5C,
0x41, 0x5B,
0x41, 0x5A,
0x41, 0x59,
0x41, 0x58,
0x5D,
0x5F,
0x5E,
0x5A,
0x59,
0x5B,
0x48, 0xB8, 0xAA, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x00, // mov rax, 0xAABBCCDDEEFFAA
0xFF, 0xE0 // jmp rax
// stub:
};
*( PVOID* ) &Prologue[ 0x77 ] = HookOut;
*( PVOID* ) &Prologue[ 0x16 ] = ValCheck;
*( DWORD* ) &Prologue[ 0x23 ] = *( DWORD* ) ValCheck;
std::vector<BYTE> JmpEntryPont =
{
0x48, 0xB8, 0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, 0x00, 0x00, // mov rax, 0xAABBCCDD
0x48, 0x89, 0xC1, // mov rcx, rax
0x48, 0xC7, 0xC2, 0x01, 0x00, 0x00, 0x00, // mov rdx, 1
0x4D, 0x31, 0xC0, // xor r8, r8
0x48, 0x05, 0xCD, 0xBB, 0xAA, 0x00, // add rax, 0xAABBCD
0xFF, 0xE0 // jmp rax
};
printf( "[+] Creating import shellcode...\n" );
uint32_t ShellSize = Mp_CreateImportShell( File.data(), nullptr, LoadLib ).size() + JmpEntryPont.size() + Prologue.size();
BYTE* Memory = ( BYTE* ) MemoryAllocator( OptionalHeader->SizeOfImage + ShellSize + 0xFFF );
uint64_t ImageMemory = ( ( uint64_t ) Memory + ShellSize + 0xFFF )&( ~0xFFF );
*( uint64_t* ) ( &JmpEntryPont[ 0x02 ] ) = ImageMemory;
*( uint32_t* ) ( &JmpEntryPont[ 0x19 ] ) = FileHeader->OptionalHeader.AddressOfEntryPoint;
auto Shell = Mp_CreateImportShell( File.data(), PVOID( ImageMemory ), LoadLib );
Mp_PushBytes( Shell, JmpEntryPont );
Mp_PushBytes( Prologue, Shell );
Shell = Prologue;
printf( "[+] Relocating image...\n" );
Mp_RelocateImage( File.data(), PBYTE( ImageMemory ) );
memcpy( Memory, Shell.data(), Shell.size() );
printf( "[+] Image mapping done!\n" );
return ( TlsLockedHookController * ) Memory;
}