-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscall.zig
More file actions
318 lines (284 loc) · 10.6 KB
/
syscall.zig
File metadata and controls
318 lines (284 loc) · 10.6 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
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
const std = @import("std");
const win = std.os.windows;
const Coff = std.coff.Coff;
const IMAGE_EXPORT_DIRECTORY = extern struct {
Characteristics: u32,
TimeDateStamp: u32,
MajorVersion: u16,
MinorVersion: u16,
Name: u32,
Base: u32,
NumberOfFunctions: u32,
NumberOfNames: u32,
AddressOfFunctions: u32,
AddressOfNames: u32,
AddressOfNameOrdinals: u32,
};
pub var s_instance: SyscallFinder = undefined;
pub const SyscallFinder = struct {
allocator: std.mem.Allocator,
syscalls: std.AutoHashMap(u32, u32),
modules: []ModuleInfo,
pub fn init(allocator: std.mem.Allocator) !SyscallFinder {
return SyscallFinder{
.allocator = allocator,
.syscalls = .init(allocator),
.modules = try getAllModules(allocator)
};
}
pub fn getSyscallCodeByName(self: *SyscallFinder, comptime sys_name: []const u8) !u32 {
comptime {
if (!std.mem.startsWith(u8, sys_name, "Zw")
and !std.mem.startsWith(u8, sys_name, "Nt")) {
@compileError("The function must have the prefix 'Zw' or 'Nt'.");
}
}
var func_addr: ?usize = undefined;
const sys_name_hash = comptime hashU8(sys_name);
if (self.syscalls.get(sys_name_hash)) |value| {
return value;
}
for (self.modules) |module| {
func_addr = try getExportAddressByHash(sys_name_hash, module);
if (func_addr != null) break;
}
if (func_addr) |value| {
const code = try getSyscallcode(value);
try self.syscalls.put(sys_name_hash, code);
return code;
} else {
return error.SyscallNotFound;
}
}
fn getSyscallcode(func_addr: usize) !u32 {
var addr: [*]const u8 = @ptrFromInt(func_addr);
const code_pos = std.mem.indexOf(u8, addr[0..16], &[_]u8{ 0x4C, 0x8B, 0xD1, 0xB8 }).?;
addr += code_pos;
return std.mem.readInt(u32, addr[4..8], .little);
}
const ModuleInfo = struct {
base: [*]const u8,
len: usize,
};
fn getAllModules(allocator: std.mem.Allocator) ![]ModuleInfo {
var module_list = std.ArrayList(ModuleInfo).empty;
const head = &win.peb().Ldr.InLoadOrderModuleList;
var curr = head.Flink;
while (head != curr) : (curr = curr.Flink) {
const entry: *win.LDR_DATA_TABLE_ENTRY = @ptrCast(curr);
try module_list.append(allocator, ModuleInfo{
.base = @ptrCast(entry.DllBase),
.len = @intCast(entry.SizeOfImage)
});
}
return try module_list.toOwnedSlice(allocator);
}
fn getExportAddressByHash(syscall_hash: u32, info: ModuleInfo) !?usize {
var coff = try Coff.init(info.base[0..info.len], true);
const export_dir = coff.getDataDirectories()[@intFromEnum(std.coff.DirectoryEntry.EXPORT)];
if (export_dir.virtual_address == 0) return null;
const exports: *const IMAGE_EXPORT_DIRECTORY = @ptrCast(@alignCast(&coff.data[export_dir.virtual_address]));
const names_rva_ptr: [*]const u32 = @ptrCast(@alignCast(&coff.data[exports.AddressOfNames]));
const ordinals_ptr: [*]const u16 = @ptrCast(@alignCast(&coff.data[exports.AddressOfNameOrdinals]));
const functions_ptr: [*]const u32 = @ptrCast(@alignCast(&coff.data[exports.AddressOfFunctions]));
for (0..exports.NumberOfNames) |i| {
const name_rva = names_rva_ptr[i];
const current_name_ptr: [*:0]const u8 = @ptrCast(&coff.data[name_rva]);
const current_name = std.mem.span(current_name_ptr);
if (hashU8(current_name) == syscall_hash) {
const ordinal = ordinals_ptr[i];
const func_rva = functions_ptr[ordinal];
return @intFromPtr(info.base) + func_rva;
}
}
return null;
}
fn hashU8(buffer: []const u8) u32 {
return std.hash.Fnv1a_32.hash(buffer);
}
pub fn hashU16(name_u16: []const u16) u32 {
var hasher = std.hash.Fnv1a_32.init();
for (name_u16) |wc| {
const c = std.ascii.toLower(@as(u8, @truncate(wc)));
hasher.update(&[_]u8{c});
}
return hasher.final();
}
pub fn deinit(self: *SyscallFinder) void {
self.syscalls.deinit();
self.allocator.free(self.modules);
}
};
fn toUsize(val: anytype) usize {
const T = @TypeOf(val);
if (T == @TypeOf(null)) return 0;
switch (@typeInfo(T)) {
.bool => return if (val) 1 else 0,
.int, .comptime_int => return @bitCast(@as(isize, @intCast(val))),
.pointer => return @intFromPtr(val),
.optional => {
if (val) |v| return toUsize(v);
return 0;
},
else => @compileError("Unsupported argument type for syscall: " ++ @typeName(T)),
}
}
pub fn doSyscall(comptime name: []const u8, args: anytype) usize {
var abi_args: [10]usize = .{0} ** 10;
inline for (args, 0..) |arg, i| {
abi_args[i] = toUsize(arg);
}
const ssn = s_instance.getSyscallCodeByName(name) catch return 0;
return switch (args.len) {
0 => asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
: .{ .rcx = true, .r11 = true, .memory = true }
),
1 => asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
2 => asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
3 => asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
4 => asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
5 => asm volatile (
\\ sub $0x30, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ syscall
\\ add $0x30, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
6 => asm volatile (
\\ sub $0x38, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ mov %[a6], 0x30(%%rsp)
\\ syscall
\\ add $0x38, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
[a6] "r" (abi_args[5]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
7 => asm volatile (
\\ sub $0x40, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ mov %[a6], 0x30(%%rsp)
\\ mov %[a7], 0x38(%%rsp)
\\ syscall
\\ add $0x40, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
[a6] "r" (abi_args[5]),
[a7] "r" (abi_args[6]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
8 => asm volatile (
\\ sub $0x48, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ mov %[a6], 0x30(%%rsp)
\\ mov %[a7], 0x38(%%rsp)
\\ mov %[a8], 0x40(%%rsp)
\\ syscall
\\ add $0x48, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
[a6] "r" (abi_args[5]),
[a7] "r" (abi_args[6]),
[a8] "r" (abi_args[7]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
9 => asm volatile (
\\ sub $0x50, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ mov %[a6], 0x30(%%rsp)
\\ mov %[a7], 0x38(%%rsp)
\\ mov %[a8], 0x40(%%rsp)
\\ mov %[a9], 0x48(%%rsp)
\\ syscall
\\ add $0x50, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
[a6] "r" (abi_args[5]),
[a7] "r" (abi_args[6]),
[a8] "r" (abi_args[7]),
[a9] "r" (abi_args[8]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
10 => asm volatile (
\\ sub $0x58, %%rsp
\\ mov %[a5], 0x28(%%rsp)
\\ mov %[a6], 0x30(%%rsp)
\\ mov %[a7], 0x38(%%rsp)
\\ mov %[a8], 0x40(%%rsp)
\\ mov %[a9], 0x48(%%rsp)
\\ mov %[a10], 0x50(%%rsp)
\\ syscall
\\ add $0x58, %%rsp
: [ret] "={rax}" (-> usize),
: [ssn] "{rax}" (ssn),
[a1] "{r10}" (abi_args[0]),
[a2] "{rdx}" (abi_args[1]),
[a3] "{r8}" (abi_args[2]),
[a4] "{r9}" (abi_args[3]),
[a5] "r" (abi_args[4]),
[a6] "r" (abi_args[5]),
[a7] "r" (abi_args[6]),
[a8] "r" (abi_args[7]),
[a9] "r" (abi_args[8]),
[a10] "r" (abi_args[9]),
: .{ .rcx = true, .r11 = true, .memory = true }
),
else => @compileError("doSyscall supports a maximum of 10 arguments."),
};
}