dinvk 🦀
Dynamically invoke arbitrary code with Rust tricks, #[no_std] support, and compatibility for x64, x86, ARM64 and WoW64 (DInvoke)
This tool is a Rust version of DInvoke, originally written in C#, with additional features added.
Table of Contents
Features
- ✅ Dynamically invoke arbitrary code (x64, x86, Wow64, ARM64).
- ✅ Indirect Syscall (x64, x86, Wow64).
- ✅ Redirecting Syscall Invocation to Different DLLs.
- ✅ Tampered Syscalls Via Hardware BreakPoints (x64, x86, Wow64).
- ✅ PE headers parsing.
- ✅ Supports
#[no_std]environments (withalloc). - ✅ Retrieve exported API addresses via string, ordinal, and hashing.
- ✅ Retrieve module addresses via string and hashing.
- ✅ Supports multiple 32-bit hash algorithms for API Hashing using
GetModuleHandleandGetProcAddress: Jenkins3, Jenkins One-at-a-Time, DJB2, Murmur3, FNV-1a, SDBM, Lose, PJW, JS, and AP.
Getting started
Add dinvk to your project by updating your Cargo.toml:
Usage
dinvk provides several features for invoking code dynamically, performing indirect syscalls and manipulating exported modules and APIs. Below are detailed examples of how to use each feature.
Dynamically Invoke Arbitrary Code
Allows resolving and calling a function dynamically at runtime, avoiding static linking.
- This example demonstrates the dynamic invocation of arbitrary code using
dinvoke!, resolving function addresses at runtime without direct linking. In this case,HeapAllocis dynamically called to allocate memory. - Using this macro is beneficial if you want to avoid having APIs directly listed in the
Import Address Table (IAT)of your PE file.
use ;
const HEAP_ZERO_MEMORY: u32 = 8u32;
let kernel32 = GetModuleHandle;
let addr = dinvoke!;
println!;
Retrieving Module Addresses and Exported APIs
Retrieves the base address of a module and resolves exported APIs using different methods: by string, ordinal, or hash.
- In this example, the address of the
KERNEL32module is retrieved using both a string and a hash (Jenkins hash). - Then, the
LoadLibraryfunction address is resolved using the same methods, with an additional example using an ordinal number.
use ;
// Retrieving module address via string and hash
let kernel32 = GetModuleHandle;
let kernel32 = GetModuleHandle;
// Retrieving exported API address via string, ordinal and hash
let addr = GetProcAddress;
let addr = GetProcAddress;
let addr = GetProcAddress;
Indirect syscall
Executes syscalls indirectly, bypassing user-mode API hooks and security monitoring tools.
- Currently supporting x64, x86 and WoW64.
- It uses techniques such as Hells Gate, Halos Gate, and Tartarus Gate to dynamically locate the System Service Number (SSN) and invoke the syscall indirectly.
use ;
use ;
// Memory allocation using a syscall
let mut addr = ;
let mut size = as usize;
let status = syscall!?;
if !NT_SUCCESS
println!;
Redirecting Syscall Invocation to Different DLLs
By default, syscalls in Windows are invoked via ntdll.dll. However, on x86_64 architectures, other DLLs such as win32u.dll, vertdll.dll and iumdll.dll also contain syscall instructions, allowing you to avoid indirect calls via ntdll.dll. On x86, only win32u.dll has these instructions.
The code below demonstrates how to invoke NtAllocateVirtualMemory using different DLLs to execute the syscall:
use ;
use ;
// Alternatively, you can use Dll::Vertdll or Dll::Iumdll on x86_64
use_dll;
// Memory allocation using a syscall
let mut addr = ;
let mut size = as usize;
let status = syscall!?;
if !NT_SUCCESS
This method can be useful to avoid indirect invocations in ntdll.dll, diversifying the points of origin of the syscalls in the process.
Different Hash Methods for API Hashing
Supports various hashing algorithms for API resolution, improving stealth and flexibility.
- Currently, the library only supports 32-bit hashes for API lookup.
use *;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
Tampered Syscalls Via Hardware BreakPoints
Utilizes hardware breakpoints to manipulate syscall parameters before execution, bypassing security hooks.
- The library includes several API wrappers that leverage DInvoke and support hardware breakpoints to spoof syscall arguments dynamically.
- These breakpoints modify syscall parameters after security monitoring tools inspect them but before the syscall executes, effectively bypassing detection.
- Currently supporting x64, x86 and WoW64.
- You can find the full list of wrapped functions in the wrappers module.
use ;
use ;
// Enabling breakpoint hardware
set_use_breakpoint;
let handle = AddVectoredExceptionHandler;
// Allocating memory and using breakpoint hardware
let mut addr = null_mut;
let mut size = 1 << 12;
let status = NtAllocateVirtualMemory;
if !NT_SUCCESS
// Disabling breakpoint hardware
set_use_breakpoint;
RemoveVectoredExceptionHandler;
Support for no_std Environments
Enables #[no_std] compatibility for environments without the Rust standard library.
- To enable
#[no_std]support, define the required features in yourCargo.toml.
[]
= { = "<version>", = ["alloc", "panic"] }
- Running in
#[no_std]Mode.
use WinHeap;
use ;
static ALLOCATOR: WinHeap = WinHeap;
!
References
I want to express my gratitude to these projects that inspired me to create dinvk and contribute with some features:
License
This project is licensed under the MIT License. See the LICENSE file for details.