Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
231 lines (194 loc) · 6.4 KB
/
Copy pathutil.cpp
File metadata and controls
231 lines (194 loc) · 6.4 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
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
#include "util.h"
#include "kstructs.h"
#include <ntifs.h>
using namespace ssn_dispatch;
PCWSTR thread_to_process_name(PKTHREAD thread) {
PEPROCESS process = IoThreadToProcess(thread);
if (!process) {
return NULL;
}
return PsGetProcessImageFileName(process);
}
PVOID scan_module_pattern(pmodule_info module_inf, u8* pattern, size_t pattern_size) {
if (!module_inf || !module_inf->base || !pattern || pattern_size == 0) {
return NULL;
}
u8* base = (u8*)module_inf->base;
size_t scan_size = module_inf->size;
if (scan_size < pattern_size) {
return NULL;
}
size_t max_offset = scan_size - pattern_size;
for (size_t i = 0; i <= max_offset; i++) {
bool matched = true;
for (size_t j = 0; j < pattern_size; j++) {
if (pattern[j] != 0xFF && base[i + j] != pattern[j]) {
matched = false;
break;
}
}
if (matched) {
return (PVOID)(base + i);
}
}
return NULL;
}
void get_kernel_module_base(PCWSTR target, pmodule_info module_inf) {
UNICODE_STRING target_str;
RtlInitUnicodeString(&target_str, target);
const PLIST_ENTRY head = PsLoadedModuleList;
PLIST_ENTRY now_entry = head->Flink;
while(now_entry != head) {
PLdrDataTableEntry module_entry = CONTAINING_RECORD(now_entry, LdrDataTableEntry, InLoadOrderLinks);
if (RtlEqualUnicodeString(&module_entry->BaseDllName, &target_str, TRUE)) {
module_inf->base = module_entry->DllBase;
module_inf->size = module_entry->SizeOfImage;
return;
}
now_entry = now_entry->Flink;
}
return;
}
alt_syscall_manager::alt_syscall_manager() :
installed_slot(0),
ssn_count(0x200),
generic_path(TRUE),
PspServiceDescriptorGroupTable(NULL),
g_AltSyscall_mode(switch_mode::off),
row_to_insert{ 0 } {
u64 handler_offset;
size_t altsyscall_dispatch_table_size = sizeof(AltSyscallDispatchTable) + sizeof(u32) * ssn_count;
u8* key_insn_addr;
PVOID PsSyscallProviderDispatch;
u32 disp32;
u8 PsSyscallProviderDispatch_header_pattern[] = {
// nt!PsSyscallProviderDispatch
0x48, 0x89, 0x5c, 0x24, 0x08, // mov qword ptr [rsp+8], rbx
0x55, // push rbp
0x56, // push rsi
0x57, // push rdi
0x41, 0x56, // push r14
0x41, 0x57, // push r15
0x48, 0x83, 0xec, 0x30, // sub rsp, 30h
0x48, 0x83, 0x64, 0x24, 0x70, 0x00, // and qword ptr [rsp+70h], 0
0x48, 0x8b, 0xf1, // mov rsi, rcx
0x65, 0x48, 0x8b, 0x2c, 0x25, 0x88, 0x01, 0x00, 0x00, // mov rbp, qword ptr gs:[188h]
0xf6, 0x45, 0x03, 0x04 // test byte ptr [rbp+3], 4
};
get_kernel_module_base(L"AltSyscall_in_cpp.sys", &mydriver);
get_kernel_module_base(KERNEL_SYM, &kernel);
handler_addr = &alt_syscall_manager::syscall_handler;
handler_offset = (ULONG_PTR)handler_addr - (ULONG_PTR)mydriver.base;
ASSERT(handler_offset < MAXUINT32);
altsyscall_dispatch_table = (pAltSyscallDispatchTable)ExAllocatePool2(POOL_FLAG_NON_PAGED, altsyscall_dispatch_table_size, 'tdsA');
if(!altsyscall_dispatch_table) {
return;
}
altsyscall_dispatch_table->count = ssn_count;
for(u32 i = 0; i < ssn_count; i++) {
altsyscall_dispatch_table->descriptor[i].stack_args_num_in_qword = 0;
altsyscall_dispatch_table->descriptor[i].offset_and_path_flag = handler_offset | generic_path;
}
row_to_insert.driver_base = mydriver.base;
row_to_insert.ssn_dispatch_table = altsyscall_dispatch_table;
row_to_insert._reserved = NULL;
PsSyscallProviderDispatch = scan_module_pattern(&kernel, PsSyscallProviderDispatch_header_pattern, sizeof(PsSyscallProviderDispatch_header_pattern));
if(!PsSyscallProviderDispatch) {
return;
}
key_insn_addr = (u8*)((ULONG_PTR)PsSyscallProviderDispatch + 0x77); // lea rcx, PspServiceDescriptorGroupTable
disp32 = leunpackU32(
key_insn_addr[3],
key_insn_addr[4],
key_insn_addr[5],
key_insn_addr[6]
);
PspServiceDescriptorGroupTable = (pServiceDescriptorRow)(key_insn_addr + 7 + disp32);
PspServiceDescriptorGroupTable[installed_slot] = row_to_insert;
g_AltSyscall_mode = switch_mode::on;
configure_active_processes();
}
alt_syscall_manager::~alt_syscall_manager() {
PspServiceDescriptorGroupTable[installed_slot] = { 0 };
if (altsyscall_dispatch_table) {
ExFreePool(altsyscall_dispatch_table);
}
return;
}
void alt_syscall_manager::configure_active_processes() noexcept {
ULONG buffer_size = 0;
PVOID buffer = NULL;
UNICODE_STRING tmp_trg = { 0 };
RtlInitUnicodeString(&tmp_trg, L"victim.exe");
ZwQuerySystemInformation(SystemProcessInformation, NULL, 0, &buffer_size);
if (buffer_size == 0) {
return;
}
buffer = ExAllocatePool2(POOL_FLAG_PAGED, buffer_size, '0PMT');
if (!buffer) {
return;
}
if(NT_SUCCESS(ZwQuerySystemInformation(SystemProcessInformation, buffer, buffer_size, &buffer_size))) {
PSYSTEM_PROCESS_INFORMATION proc_info = (PSYSTEM_PROCESS_INFORMATION)buffer;
while (true) {
if( proc_info->ImageName.Buffer &&
RtlCompareUnicodeString(&proc_info->ImageName, &tmp_trg, TRUE) == 0) {
PEPROCESS process;
if (NT_SUCCESS(PsLookupProcessByProcessId(proc_info->UniqueProcessId, &process))) {
ObReferenceObject(process);
configure_process(process, g_AltSyscall_mode);
NPEPROCESS nprocess = (NPEPROCESS)process;
PLIST_ENTRY thread_list = nprocess->ThreadListHead.Flink;
while(thread_list != &nprocess->ThreadListHead) {
NPETHREAD thread = CONTAINING_RECORD(thread_list, NETHREAD, ThreadListEntry);
configure_thread((PKTHREAD)thread, g_AltSyscall_mode);
thread_list = thread_list->Flink;
}
ObDereferenceObject(process);
}
}
if (proc_info->NextEntryOffset == 0) {
break;
}
proc_info = (PSYSTEM_PROCESS_INFORMATION)((PUCHAR)proc_info + proc_info->NextEntryOffset);
}
}
if (buffer) {
ExFreePool(buffer);
}
return;
}
void alt_syscall_manager::configure_thread(PKTHREAD thread, switch_mode mode) noexcept {
_DISPATCHER_HEADER* dispatch_hdr = (_DISPATCHER_HEADER *)thread;
if (dispatch_hdr->Minimal) {
// skip pico thread
return;
}
switch (mode) {
case switch_mode::on:
dispatch_hdr->AltSyscall = 1;
return;
case switch_mode::off:
dispatch_hdr->AltSyscall = 0;
return;
}
}
void alt_syscall_manager::configure_process(PEPROCESS proc, switch_mode mode) noexcept {
NPEPROCESS process = (NPEPROCESS)proc;
switch(mode) {
case switch_mode::on:
process->SyscallProviderDispatchContext.Slot = installed_slot;
break;
case switch_mode::off:
process->SyscallProviderDispatchContext.Slot = 0;
break;
}
}
void alt_syscall_manager::syscall_handler(
PVOID original_Ntfunc_ptr,
u32 ssn,
PVOID args_base,
PVOID p3_home
) noexcept {
DbgBreakPoint();
}