cfun/
c_inject.rs

1#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
2use std::{arch::global_asm, ffi::c_void};
3
4#[allow(dead_code)]
5type GetKernelAddrType = extern "C" fn() -> i64;
6
7#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
8extern "C" {
9    #[allow(dead_code)]
10    pub fn get_kernel_addr() -> i64;
11}
12
13#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
14global_asm! {
15    "get_kernel_addr:",
16    "xor rax, rax",
17    "mov rax, gs:[60h]",
18    "mov rax, [rax + 18h]",
19    "mov rax, [rax + 10h]",
20    "mov rax, [rax]",
21    "ret",
22}
23
24#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
25#[repr(C, packed)]
26#[allow(dead_code)]
27struct InjectParam {
28    get_kernel_addr_fun: GetKernelAddrType,
29    kernel: *const u16,
30    get_proc_address: *const u16,
31}
32
33#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
34#[repr(C, packed)]
35#[allow(non_snake_case)]
36struct UnicodeString {
37    Length: u16,
38    MaximumLength: u16,
39    Buffer: *const u16,
40}
41
42#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
43#[repr(C, packed)]
44#[allow(non_snake_case)]
45struct ListEntry {
46    Flink: *mut ListEntry,
47    Blink: *mut ListEntry,
48}
49
50#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
51#[repr(C, packed)]
52#[allow(non_snake_case)]
53struct LdrModule {
54    InLoadOrderModuleList: ListEntry,
55    InMemoryOrderModuleList: ListEntry,
56    InInitializationOrderModuleList: ListEntry,
57    BaseAddress: *mut c_void,
58    EntryPoint: *mut c_void,
59    SizeOfImage: u32,
60    FullDllName: UnicodeString,
61    BaseDllName: UnicodeString,
62    Flags: u32,
63    LoadCount: i16,
64    TlsIndex: i16,
65}
66#[allow(dead_code)]
67#[cfg(all(target_arch = "x86_64", target_os = "windows", feature = "inject"))]
68extern "C" fn inject_fun() {
69    let param = 0xFEFEFEFEFEFEFEFE as *const InjectParam;
70    let get_kernel_addr_fun = unsafe { (*param).get_kernel_addr_fun };
71    let kernel_addr = get_kernel_addr_fun();
72    let lib = kernel_addr as *const LdrModule;
73    unsafe {
74        let lib = lib.as_ref().unwrap();
75        while !lib.BaseDllName.Buffer.is_null() {}
76    };
77}