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
use crate::windows_prelude::*;
#[macro_use]
mod util;
#[cfg(target_pointer_width = "32")]
#[rustfmt::skip]
const SHELLCODE: [u8; 84] = [
0xE8, 0x3F, 0x00, 0x00, 0x00, // call x86_getpc
0x83, 0xEE, 0x05, // sub esi, 5
0x8D, 0xBE, 0x64, 0x00, 0x00, 0x00, // lea edi, [esi + DllName]
0x57, // push edi
0xFF, 0x96, 0x54, 0x00, 0x00, 0x00, // call [esi + LoadLibraryW]
0x85, 0xC0, // test eax, eax
0x74, 0x1A, // jz error
0xE8, 0x2A, 0x00, 0x00, 0x00, // call wcslen
0x80, 0x3F, 0x00, // cmp byte ptr [edi], 0
0x74, 0x1C, // jz done_noinit
0x57, // push edi
0x50, // push eax
0xFF, 0x96, 0x58, 0x00, 0x00, 0x00, // call [esi + GetProcAddress]
0x85, 0xC0, // test eax, eax
0x74, 0x04, // jz error
0xFF, 0xD0, // call eax
0xEB, 0x0E, // jmp done
// error:
0xFF, 0x96, 0x5C, 0x00, 0x00, 0x00, // call [esi + GetLastError]
0x89, 0x86, 0x60, 0x00, 0x00, 0x00, // mov [esi + Error], eax
// done_noinit:
0x31, 0xC0, // xor eax, eax
// done:
0xC2, 0x04, 0x00, // ret 4
// x86_getpc:
0x8B, 0x34, 0x24, // mov esi, [esp]
0xC3, // ret
// wcslen:
0x66, 0x83, 0x3F, 0x00, // cmp word ptr [edi], 0
0x8D, 0x7F, 0x02, // lea edi, [edi + 2]
0x75, 0xF7, // jnz wcslen
0xC3, // ret
0xCC, 0xCC, // (padding)
];
#[cfg(target_pointer_width = "64")]
#[rustfmt::skip]
const SHELLCODE: [u8; 88] = [
0x48, 0x8D, 0x0D, 0x71, 0x00, 0x00, 0x00, // lea rcx, [rip + DllName]
0x51, // push rcx
0x48, 0x83, 0xEC, 0x20, // sub rsp, 32
0xFF, 0x15, 0x46, 0x00, 0x00, 0x00, // call [rip + LoadLibraryW]
0x48, 0x8B, 0x54, 0x24, 0x20, // mov rdx, [rsp + 32]
0x48, 0x85, 0xC0, // test rax, rax
0x74, 0x1C, // jz error
0x48, 0x89, 0xC1, // mov rcx, rax
0xE8, 0x29, 0x00, 0x00, 0x00, // call wcslen
0x80, 0x3A, 0x00, // cmp byte ptr [rdx], 0
0x74, 0x1B, // jz done_noinit
0xFF, 0x15, 0x31, 0x00, 0x00, 0x00, // call [rip + GetProcAddress]
0x48, 0x85, 0xC0, // test rax, rax
0x74, 0x04, // jz error
0xFF, 0xD0, // call rax
0xEB, 0x0F, // jmp done
// error:
0xFF, 0x15, 0x2A, 0x00, 0x00, 0x00, // call [rip + GetLastError]
0x89, 0x05, 0x2C, 0x00, 0x00, 0x00, // mov [rip + Error], rax
// done_noinit:
0x48, 0x31, 0xC0, // xor rax, rax
// done:
0x48, 0x83, 0xC4, 0x28, // add rsp, 40
0xC3, // ret
// wcslen:
0x66, 0x83, 0x3A, 0x00, // cmp word ptr [rdx], 0
0x48, 0x8D, 0x52, 0x02, // lea rdx, [rdx + 2]
0x75, 0xF6, // jnz wcslen
0xC3, // ret
0xCC, // (padding)
];
pub fn inject_dll(process: HANDLE, dll: &std::path::Path, function: &[u8]) -> Result<DWORD, DWORD> {
unsafe {
let pointers = util::get_fn_pointers();
// Convert the DLL path to an absolute path.
let dllstr: Vec<u16> = dll
.canonicalize()
.unwrap()
.into_os_string()
.encode_wide()
.collect();
let plen = std::mem::size_of::<util::Pointers>();
let dlllen = (dllstr.len() + 1) * 2;
let fnlen = function.len() + 1;
// the total amount of memory needed.
// the length of the shellcode,
// the length of the dll string in bytes
// the length of the symbol name in bytes
let total_size = SHELLCODE.len() + plen + dlllen + fnlen;
let alloc = util::alloc(process, total_size).cast::<std::ffi::c_void>();
bail_if_null!(alloc);
// TODO: yeet all of these into different functions
bail_if_zero!(WriteProcessMemory(
process,
alloc as _,
ptr!(SHELLCODE),
SHELLCODE.len(),
NULL as _
));
bail_if_zero!(WriteProcessMemory(
process,
alloc.add(SHELLCODE.len()) as _,
&pointers as *const _ as _,
plen,
NULL as _,
));
bail_if_zero!(WriteProcessMemory(
process,
alloc.add(SHELLCODE.len() + plen) as _,
ptr!(dllstr),
// We don't actually have a null terminator, we just use the fact all memory is zeroed
dlllen - 2,
NULL as _
));
// Turns out WriteProcessMemory errors if nSize is 0
if fnlen != 1 {
bail_if_zero!(WriteProcessMemory(
process,
alloc.add(SHELLCODE.len() + plen + dlllen) as _,
ptr!(function),
fnlen - 1,
NULL as _
));
}
// Run the thread!
let thread = CreateRemoteThread(
process,
NULL.cast(),
0,
std::mem::transmute(alloc),
NULL.cast(),
0,
NULL.cast(),
);
bail_if_null!(thread);
let result = WaitForSingleObject(thread, INFINITE);
bail_if_nonzero!(result);
// We can't just let the shellcode write to a pointer in our address space, I think
// So we let it write it to some memory, and read it back out.
let mut error: DWORD = 0;
bail_if_zero!(ReadProcessMemory(
process,
alloc.add(SHELLCODE.len() + plen - std::mem::size_of::<usize>()) as _,
&mut error as *mut _ as _,
4,
NULL as _
));
let mut exit: DWORD = 0;
GetExitCodeThread(thread, &mut exit as _);
// cleanup
// would also zero that memory we wrote, but in the case of a crash it might help
// also no good way to call ZeroMemory on another process. would have to just allocate zeroes.
VirtualFreeEx(process, alloc.cast(), total_size, MEM_RELEASE);
CloseHandle(thread);
if error == 0 {
Ok(exit)
} else {
Err(error)
}
}
}