Expand description
§dinvk 🦀
Dynamically invoke arbitrary code in Rust with full support for #[no_std] and multiple architectures:
x64, x86, WoW64, ARM64.
This crate is a Rust reimplementation of DInvoke with extra features.
§Features
- Dynamic API resolution (
dinvoke!). - Indirect syscalls (Hells Gate / Halos Gate / Tartarus Gate).
- Syscall redirection to other DLLs (e.g.
win32u.dll,vertdll.dll). - PE parsing, proxy DLL loading.
- Multiple hashing algorithms for API resolution.
#[no_std]compatibility.
§Examples
§1. Dynamically Invoke Arbitrary Code
use dinvk::{
data::HeapAllocFn,
dinvoke, GetModuleHandle,
GetProcessHeap
};
const HEAP_ZERO_MEMORY: u32 = 8;
fn main() {
let kernel32 = GetModuleHandle("KERNEL32.DLL", None);
let addr = dinvoke!(
kernel32,
"HeapAlloc",
HeapAllocFn,
GetProcessHeap(),
HEAP_ZERO_MEMORY,
0x200
);
println!("[+] Address: {:?}", addr);
}§2. Indirect Syscall
use std::{ffi::c_void, ptr::null_mut};
use dinvk::{NtCurrentProcess, NT_SUCCESS, syscall};
use dinvk::data::NTSTATUS;
fn main() -> Result<(), NTSTATUS> {
let mut addr = null_mut::<c_void>();
let mut size = 0x1000;
let status = syscall!(
"NtAllocateVirtualMemory",
NtCurrentProcess(),
&mut addr,
0,
&mut size,
0x3000,
0x40
).ok_or(-1)?;
if !NT_SUCCESS(status) {
eprintln!("[-] NtAllocateVirtualMemory failed: {status:?}");
return Err(status);
}
println!("[+] Allocated at: {:?}", addr);
Ok(())
}§3. Hashing APIs
use dinvk::hash::*;
println!("jenkins: {}", jenkins("dinvk"));
println!("djb2: {}", djb2("dinvk"));
println!("fnv1a: {}", fnv1a("dinvk"));§4. Proxy DLL Loading
use dinvk::LdrProxy;
// Use RtlQueueWorkItem to indirectly load DLL
LdrProxy::new("xpsservices.dll").work();
// Or RtlCreateTimer
LdrProxy::new("xpsservices.dll").timer();
// Or RtlRegisterWait
LdrProxy::new("xpsservices.dll").register_wait();§More Information
For updates, usage guides, and examples, visit the repository.
Modules§
- data
- Structures and types used across the library.
- hash
- Runtime hash functions.
- ldr
- Module containing dynamic module loader proxy.
- pe
- PE Parsing
Macros§
- dinvoke
- Macro to dynamically invoke a function from a specified module.
- link
- Declares an external function from a dynamically linked library.
- println
- Prints output to the Windows console using
ConsoleWriter.
Structs§
- Console
Writer ConsoleWriteris a custom implementation ofcore::fmt::Writethat writes formatted strings directly to the Windows console.- LdrProxy
- A helper struct to interact with dynamic module loading with Windows APIs via Proxy.
Functions§
- AddVectored
Exception Handler - Wrapper for the
AddVectoredExceptionHandlerfunction fromKERNEL32.DLL. - GetCurrent
Process Id - Returns the process ID of the calling process from the TEB.
- GetCurrent
Thread Id - Returns the thread ID of the calling thread from the TEB.
- GetModule
Handle - Resolves the base address of a module loaded in memory by name or hash.
- GetProc
Address - Retrieves the address of an exported function from a loaded module.
- GetProcess
Heap - Returns the default heap handle for the current process from the PEB.
- GetStd
Handle - Wrapper for the
GetStdHandlefunction fromKERNEL32.DLL. - Heap
Alloc - Wrapper for the
HeapAllocfunction fromKERNEL32.DLL. - Heap
Create - Wrapper for the
HeapCreatefunction fromKERNEL32.DLL. - Heap
Free - Wrapper for the
HeapFreefunction fromKERNEL32.DLL. - Load
LibraryA - Wrapper for the
LoadLibraryAfunction fromKERNEL32.DLL. - NT_
SUCCESS - Evaluates to TRUE if the return value specified by
nt_statusis a success type (0 − 0x3FFFFFFF) or an informational type (0x40000000 − 0x7FFFFFFF). This function is taken from ntdef.h in the WDK. - NtAllocate
Virtual Memory - Wrapper for the
NtAllocateVirtualMemoryfunction fromNTDLL.DLL. - NtCreate
Thread Ex - Wrapper for the
NtCreateThreadExfunction fromNTDLL.DLL. - NtCurrent
Peb - Retrieves a pointer to the Process Environment Block (PEB) of the current process.
- NtCurrent
Process - Returns a pseudo-handle to the current process ((HANDLE)-1).
- NtCurrent
Teb - Retrieves a pointer to the Thread Environment Block (TEB) of the current thread.
- NtCurrent
Thread - Returns a pseudo-handle to the current thread ((HANDLE)-2).
- NtGet
Thread Context - Wrapper for the
NtGetThreadContextfunction fromNTDLL.DLL. - NtProtect
Virtual Memory - Wrapper for the
NtProtectVirtualMemoryfunction fromNTDLL.DLL. - NtSet
Thread Context - Wrapper for the
NtSetThreadContextfunction fromNTDLL.DLL. - NtWrite
Virtual Memory - Wrapper for the
NtWriteVirtualMemoryfunction fromNTDLL.DLL. - Remove
Vectored Exception Handler - Wrapper for the
RemoveVectoredExceptionHandlerfunction fromKERNEL32.DLL. - __
readx18 - Reads a
u64value from the x18 register at the specified offset. - get_
ntdll_ address - Retrieves the base address of the
ntdll.dllmodule. - resolve_
api_ set_ map - Resolves ApiSet contracts (e.g.,
api-ms-win-core-*) to the actual implementing DLLs. - ssn
- Resolves the System Service Number (SSN) for a given function name within a module.