NtSetSystemEnvironmentValueEx
Writes or deletes a UEFI variable identified by (name, vendor GUID) — the canonical firmware-persistence surface.
Prototype
NTSTATUS NtSetSystemEnvironmentValueEx( PUNICODE_STRING VariableName, LPGUID VendorGuid, PVOID Value, ULONG ValueLength, ULONG Attributes );
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| VariableName | PUNICODE_STRING | in | Target UEFI variable name (e.g. L"BootOrder", L"dbx", a custom OEM key). |
| VendorGuid | LPGUID | in | Vendor namespace GUID. EFI_GLOBAL_VARIABLE for boot config; EFI_IMAGE_SECURITY_DATABASE_GUID for Secure Boot key DBs. |
| Value | PVOID | in | Pointer to the new value bytes. NULL with ValueLength=0 deletes the variable (when attributes allow). |
| ValueLength | ULONG | in | Size of the value buffer in bytes. Zero (with NULL Value) requests deletion. |
| Attributes | ULONG | in | EFI variable attributes: NON_VOLATILE, BOOTSERVICE_ACCESS, RUNTIME_ACCESS, AUTHENTICATED_WRITE_ACCESS, TIME_BASED_AUTH, APPEND_WRITE. |
Syscall IDs by Windows version
| Windows version | Syscall ID | Build |
|---|---|---|
| Win10 1507 | 0x18D | win10-1507 |
| Win10 1607 | 0x196 | win10-1607 |
| Win10 1703 | 0x19C | win10-1703 |
| Win10 1709 | 0x19F | win10-1709 |
| Win10 1803 | 0x1A1 | win10-1803 |
| Win10 1809 | 0x1A2 | win10-1809 |
| Win10 1903 | 0x1A3 | win10-1903 |
| Win10 1909 | 0x1A3 | win10-1909 |
| Win10 2004 | 0x1A9 | win10-2004 |
| Win10 20H2 | 0x1A9 | win10-20h2 |
| Win10 21H1 | 0x1A9 | win10-21h1 |
| Win10 21H2 | 0x1AB | win10-21h2 |
| Win10 22H2 | 0x1AB | win10-22h2 |
| Win11 21H2 | 0x1B4 | win11-21h2 |
| Win11 22H2 | 0x1B8 | win11-22h2 |
| Win11 23H2 | 0x1B8 | win11-23h2 |
| Win11 24H2 | 0x1BB | win11-24h2 |
| Server 2016 | 0x196 | winserver-2016 |
| Server 2019 | 0x1A2 | winserver-2019 |
| Server 2022 | 0x1B1 | winserver-2022 |
| Server 2025 | 0x1BB | winserver-2025 |
Kernel module
Related APIs
Syscall stub
4C 8B D1 mov r10, rcx B8 BB 01 00 00 mov eax, 0x1BB F6 04 25 08 03 FE 7F 01 test byte ptr [0x7FFE0308], 1 75 03 jne short +3 0F 05 syscall C3 ret CD 2E int 2Eh C3 ret
Undocumented notes
The high-value sibling of NtQuerySystemEnvironmentValueEx. This is the kernel path that backs `bcdedit /set` for many platform-level switches (test signing, debug mode, hypervisor launch type), writes Secure Boot policy variables (PK/KEK/db/dbx — most of which require authenticated writes the OS cannot mint on its own), and is the surface an attacker reaches for when planting **firmware-level persistence**: write an EFI variable that the OS loader or a vulnerable shim reads at boot. Requires SeSystemEnvironmentPrivilege. The firmware itself, not just the kernel, enforces signature checks on authenticated writes — modifications to PK/KEK/db without a valid auth payload are rejected at the SMM boundary.
Common malware usage
Centre of gravity for UEFI-persistence tradecraft. **LoJax** (Sednit / APT28, 2018) was the seminal example: a user-mode component armed RWEverything's RwDrv to write directly to SPI flash, but parallel implants in the wild use NtSetSystemEnvironmentValueEx to plant payload pointers or auxiliary state into NVRAM that the bootkit reads after rerouting the boot chain. **BlackLotus** (2022, CVE-2022-21894 "baton drop") writes attacker-chosen MOK/dbx entries and uses NVRAM to stage its second-stage bootkit configuration. **ESPecter**, **CosmicStrand**, and **MoonBounce** (APT41) all rely on the broader UEFI-variable surface either for configuration storage or for staging. Defensive note: writing dbx — i.e. *revoking* loader hashes — has been used both legitimately (KB5025885 dbx push) and offensively (DoS the user's own bootloader).
Detection opportunities
Microsoft-Windows-Kernel-General ETW emits firmware-write events with variable name, GUID, and result. Defenders should track three things: (1) which processes ever call this, (2) the privilege-enable transition on SeSystemEnvironmentPrivilege, and (3) any write to GUIDs matching EFI_GLOBAL_VARIABLE or EFI_IMAGE_SECURITY_DATABASE_GUID from non-trusted-installer / non-OEM binaries. Microsoft Defender for Endpoint and CrowdStrike Falcon both surface UEFI-variable writes in their device-control / boot-integrity dashboards. On Windows 11 24H2 and Server 2025, the OS additionally cross-checks against the System Guard Secure Launch state; mismatches surface in Microsoft-Windows-Kernel-Boot.
Direct syscall examples
asmx64 direct stub (Win11 24H2)
; Direct syscall stub for NtSetSystemEnvironmentValueEx (SSN 0x1BB, Win11 24H2)
NtSetSystemEnvironmentValueEx PROC
mov r10, rcx
mov eax, 1BBh
syscall
ret
NtSetSystemEnvironmentValueEx ENDPcLoJax-style NVRAM staging skeleton (NOT a working exploit — educational)
// Sketch only: a real bootkit ALSO needs a vulnerable / unsigned bootloader on the
// machine, a SecureBoot-disabled or signature-bypass state, and a SMM-friendly
// firmware. Modern attested boot, Secure Launch and KB5025885-class dbx pushes
// foreclose most of this on patched 2024+ systems.
#include <windows.h>
#define EFI_VARIABLE_NON_VOLATILE 0x00000001
#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x00000002
#define EFI_VARIABLE_RUNTIME_ACCESS 0x00000004
static const wchar_t* kVendorGuid =
L"{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}"; // EFI_GLOBAL_VARIABLE
BOOL plant_persistence_marker(const wchar_t* name, const void* blob, DWORD len) {
BOOLEAN old;
// Privilege is the first gate. Without admin + SeSystemEnvironmentPrivilege
// the syscall returns STATUS_PRIVILEGE_NOT_HELD immediately.
if (RtlAdjustPrivilege(SE_SYSTEM_ENVIRONMENT_PRIVILEGE, TRUE, FALSE, &old) < 0)
return FALSE;
return SetFirmwareEnvironmentVariableExW(
name, kVendorGuid, (PVOID)blob, len,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS);
}rustDelete a custom NVRAM marker
// Cargo: windows = { version = "0.59", features = ["Win32_System_Environment"] }
use windows::core::*;
use windows::Win32::System::Environment::*;
fn delete_marker(name: &str, guid: &str) -> windows::core::Result<()> {
let hn = HSTRING::from(name);
let hg = HSTRING::from(guid);
// ValueLength = 0 + Value = NULL + attributes = 0 => delete (per UEFI 2.x spec).
unsafe {
SetFirmwareEnvironmentVariableExW(
PCWSTR(hn.as_ptr()),
PCWSTR(hg.as_ptr()),
None,
0,
0,
)
}?;
Ok(())
}MITRE ATT&CK mappings
Last verified: 2026-05-20