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§
- breakpoint
- Hardware breakpoint management utilities (only for x86/x86_64 targets).
- 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
. - syscall
- Macro to perform a system call (syscall) by dynamically resolving its function name.
Structs§
- Console
Writer ConsoleWriter
is a custom implementation ofcore::fmt::Write
that writes formatted strings directly to the Windows console.- LdrProxy
- A helper struct to interact with dynamic module loading with Windows APIs via Proxy.
Enums§
- Dll
- Represents different dynamic link libraries (DLLs) that contain system call functions.
Functions§
- AddVectored
Exception Handler - Wrapper for the
AddVectoredExceptionHandler
function 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
GetStdHandle
function fromKERNEL32.DLL
. - Heap
Alloc - Wrapper for the
HeapAlloc
function fromKERNEL32.DLL
. - Heap
Create - Wrapper for the
HeapCreate
function fromKERNEL32.DLL
. - Heap
Free - Wrapper for the
HeapFree
function fromKERNEL32.DLL
. - Load
LibraryA - Wrapper for the
LoadLibraryA
function fromKERNEL32.DLL
. - NT_
SUCCESS - Evaluates to TRUE if the return value specified by
nt_status
is 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
NtAllocateVirtualMemory
function fromNTDLL.DLL
. - NtCreate
Thread Ex - Wrapper for the
NtCreateThreadEx
function 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
NtGetThreadContext
function fromNTDLL.DLL
. - NtProtect
Virtual Memory - Wrapper for the
NtProtectVirtualMemory
function fromNTDLL.DLL
. - NtSet
Thread Context - Wrapper for the
NtSetThreadContext
function fromNTDLL.DLL
. - NtWrite
Virtual Memory - Wrapper for the
NtWriteVirtualMemory
function fromNTDLL.DLL
. - Remove
Vectored Exception Handler - Wrapper for the
RemoveVectoredExceptionHandler
function fromKERNEL32.DLL
. - __
readgsqword - Reads a
u64
value from the GS segment at the specified offset. - get_
ntdll_ address - Retrieves the base address of the
ntdll.dll
module. - get_
syscall_ address - Retrieves the syscall address from a given function address.
- resolve_
api_ set_ map - Resolves ApiSet contracts (e.g.,
api-ms-win-core-*
) to the actual implementing DLLs. - shuffle
- Randomly shuffles the elements of a mutable slice in-place using a pseudo-random
number generator seeded by the CPU’s timestamp counter (
rdtsc
). - ssn
- Resolves the System Service Number (SSN) for a given function name within a module.