Fibers Shellcode Execution
Theory
This technique executes shellcode by leveraging Windows Fibers for indirect execution flow control. Unlike traditional shellcode execution techniques that rely on CreateThread
or direct function pointers, this method utilizes ConvertThreadToFiber
, CreateFiber
, and SwitchToFiber
to execute shellcode in a fiber's context. This allows execution within an existing thread, making detection more challenging.
Windows fibers are manually scheduled execution units that run within the context of a thread. Unlike threads, fibers do not have their own kernel-managed execution state but instead share the thread's stack and register state. Fibers are useful in scenarios where a program needs finer control over execution switching.
Windows Fibers Overview
A fiber is a lightweight execution unit that must be explicitly scheduled by the application. The primary difference between a thread and a fiber is that threads are preemptively scheduled by the OS, whereas fibers must yield execution manually. The Windows API provides the following key functions for working with fibers:
ConvertThreadToFiber()
: Converts the calling thread into a fiber, enabling fiber-based execution.CreateFiber()
: Creates a new fiber with a specified stack size and entry function.SwitchToFiber()
: Switches execution to the specified fiber.DeleteFiber()
: Frees resources associated with a fiber when execution completes.
Since fibers execute within the thread that schedules them, all operations performed by a fiber appear as if they were performed by the thread itself. This includes memory access, thread-local storage (TLS), and API calls.
Execution Flow
Allocate Memory for Shellcode:
Memory is allocated within the process using
NtAllocateVirtualMemory
.Shellcode is written using
NtWriteVirtualMemory
.Memory protection is changed to executable using
NtProtectVirtualMemory
.
Convert Thread to Fiber:
The calling thread is converted into a fiber using
ConvertThreadToFiber()
. This enables fiber switching within the thread.
Create a Fiber for Shellcode Execution:
CreateFiber()
is used to create a new fiber pointing to the allocated shellcode.
Switch to Shellcode Fiber:
SwitchToFiber()
is called to transfer execution to the shellcode fiber.
Return Execution and Cleanup:
Once the shellcode executes, execution returns to the main fiber.
The fiber is deleted using
DeleteFiber()
.The thread is reverted back to its original state.
Practice
We maye use the following C++ code to execute the shellcode using fibers:
We can compile it from linux using following command
Resources
Last updated
Was this helpful?