buac
A minimal, robust, and zero-allocation Windows UAC (User Account Control) elevation library for Rust.
buac leverages the COM elevation moniker (ICMLuaUtil::ShellExec) combined with dynamic, scoped PEB (Process Environment Block) masquerading to silently elevate privileges or execute processes with Administrator rights on Windows without triggering UAC consent dialogs.
Features
- Zero Runtime Allocations for Masquerading: PEB strings and moniker GUID strings are stored in static
.rdata. - Automatic PEB Rollback: Utilizes RAII guards to restore the host process's original PEB structures (
ImagePathName,CommandLine,FullDllName,BaseDllName) immediately after elevation. - Fail-Fast & Strongly Typed Errors: No silent error suppression. Every step (COM initialization, PEB locking, moniker activation, execution) returns clear
Result<T, Error>with exact WindowsHRESULT/ Win32 error codes. - Windows Command-Line Escaping: Robustly handles command-line arguments containing spaces, embedded quotes, and backslashes according to Microsoft CRT /
CommandLineToArgvWstandards. - Full Architecture Support: Native support for both
x86_64andx86Windows targets. - Thread-Safe by Design: Internal raw-pointer guards automatically enforce
!Sendand!Sync.
Installation
Add buac to your Cargo.toml:
[]
= "0.1.2"
Usage
1. One-Line Self-Elevation
Automatically restarts the current process with elevated privileges if it is not already running as Administrator:
2. Mutex-Safe Elevation (Granular Lifecycle Control)
If your application uses single-instance mutexes, named pipes, or file locks, use spawn_elevated to perform cleanup before exiting:
3. Check Privilege Status
Check whether the current token is elevated:
if is_elevated? else
4. Execute an Arbitrary Process with Elevation
Execute any target binary as Administrator:
execute?;
How It Works
- PEB Masquerading: The library acquires the PEB lock via
ntdll!RtlAcquirePebLockand temporarily swapsProcessParametersandInLoadOrderModuleListentries to point toC:\Windows\explorer.exe. - COM Elevation Moniker: Activates the elevated
CMSTPLUACOM object ({3E5FC7F9-9A51-4367-9063-A120244FBEC7}) implementingICMLuaUtil. Windows AppInfo validates the caller against the masqueraded PEB and grants auto-elevation. - Execution: Invokes
ICMLuaUtil::ShellExecwith the target binary and parameters. - RAII Cleanup: Restores original PEB structures and releases COM interfaces, apartments, and kernel locks safely.
Disclaimer
This library is intended for educational, research, and legitimate administrative automation purposes. Use responsibly in accordance with applicable laws and security policies.
License
Licensed under the MIT License.