NtQuerySystemInformation/WinAPI Hooking [требует правки]

355
30 июля 2017, 09:40

i'm trying to hook NtQuerySystemInformation for hiding simple process (nothing malicious) Just trying to figure out how hooking system works, and stuck for a few days on this.

This code compile as DLL, and using MinHook should be able to hide "calc.exe" from process list in Task Manager. It's don't and i really can't figure out why.

#include "Windows.h"
#include "Winternl.h"
#include "MinHook.h"
#pragma comment(lib, "ntdll.lib")
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
typedef struct _SYSTEM_PROCESS_INFO
{
    ULONG                   NextEntryOffset;
    ULONG                   NumberOfThreads;
    LARGE_INTEGER           Reserved[3];
    LARGE_INTEGER           CreateTime;
    LARGE_INTEGER           UserTime;
    LARGE_INTEGER           KernelTime;
    UNICODE_STRING          ImageName;
    ULONG                   BasePriority;
    HANDLE                  ProcessId;
    HANDLE                  InheritedFromProcessId;
}SYSTEM_PROCESS_INFO, *PSYSTEM_PROCESS_INFO;
typedef NTSTATUS LONG;
typedef NTSTATUS(NTAPI * PNtQuerySystemInformation)(
    SYSTEM_INFORMATION_CLASS,
    PVOID,
    ULONG,
    PULONG);
PNtQuerySystemInformation pOriginalNtQuerySystemInformation = (PNtQuerySystemInformation)GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation");

NTSTATUS NTAPI Detour_NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength)
{
    NTSTATUS status;
    PSYSTEM_PROCESS_INFO pCurrent, pNext;
    char *pname = NULL;
    status = pOriginalNtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
        if (SystemInformationClass == SystemProcessInformation && NT_SUCCESS(status))
        {

            pCurrent = (PSYSTEM_PROCESS_INFO)SystemInformation;
            PWSTR g_targetProc = L"calc.exe";
            pNext = (PSYSTEM_PROCESS_INFO)((LPBYTE)pCurrent + pCurrent->NextEntryOffset);
            while (pNext->NextEntryOffset != 0)
            {
                pname = (char *)GlobalAlloc(GMEM_ZEROINIT,
                    pCurrent->ImageName.Length + 2);
                WideCharToMultiByte(CP_ACP, 0,
                    pCurrent->ImageName.Buffer,
                    pCurrent->ImageName.Length + 1,
                    pname, pCurrent->ImageName.Length + 1,
                    NULL, NULL);

                if (!_strnicmp((char *)pname, "calc.exe", strlen("calc.exe")))
                {
                    pCurrent->NextEntryOffset += pNext->NextEntryOffset;
                }
                pCurrent = pNext;
                pNext = (PSYSTEM_PROCESS_INFO)((LPBYTE)pCurrent + pCurrent->NextEntryOffset);
                GlobalFree(pname);
            }
    }
    return status;
}

void SetHook()
{
    MH_Initialize();
    MH_CreateHookApi(L"ntdll.dll", "NtQuerySystemInformation", &Detour_NtQuerySystemInformation, reinterpret_cast<PVOID*>(&pOriginalNtQuerySystemInformation));
    MH_EnableHook(MH_ALL_HOOKS);
}
void Unhook()
{
    MH_DisableHook(MH_ALL_HOOKS);
    MH_Uninitialize();
}
READ ALSO
Friend callback функция-член класса C++

Friend callback функция-член класса C++

Не понимаю до конца как работает этот механизм

236
Как начать писать программу на С++?

Как начать писать программу на С++?

Я хочу написать небольшую консольную программу на С++, пара файлов с исходниками и тесты

305
Расчет суммы денег

Расчет суммы денег

Дано задание под номером 7Первые два года рассчитывает правильно, а вот на 10 год получается такой ответ = 10998

301
Ломаю голову, контроль пути

Ломаю голову, контроль пути

Имеется прорисовка пути маршрута автобуса выполненная при помощи массива точек (широта, долгота) которые затем через leaflet прорисовываются...

265