Walking Module List Using PEB (x86)

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
aka panic.rs
Kurucu
Katılım
18 Haz 2015
Mesajlar
3,379
Çözümler
50
Tepki puanı
13,156
Ödüller
22
Sosyal
10 HİZMET YILI
base source:
Bağlantıları görmek için lütfen Giriş Yap

C++:
#include <windows.h>
#include <subauth.h>
#include <stdio.h>
#include <locale>
// modified by leftspace

/* Windows structures */
typedef struct _PEB_LDR_DATA {
        BYTE       Reserved1[8];
        PVOID      Reserved2[3];
        LIST_ENTRY InMemoryOrderModuleList;
    } PEB_LDR_DATA, * PPEB_LDR_DATA;

    typedef struct _RTL_USER_PROCESS_PARAMETERS {
        UCHAR           Reserved1[16];
        PVOID           Reserved2[10];
        UNICODE_STRING  ImagePathName;
        UNICODE_STRING  CommandLine;
    } RTL_USER_PROCESS_PARAMETERS, * PRTL_USER_PROCESS_PARAMETERS;

    typedef struct _LDR_DATA_TABLE_ENTRY {
        LIST_ENTRY InLoadOrderLinks;
        LIST_ENTRY InMemoryOrderModuleList;
        LIST_ENTRY InInitializationOrderModuleList;
        PVOID DllBase;
        PVOID EntryPoint;
        ULONG SizeOfImage;
        UNICODE_STRING FullDllName;
        UNICODE_STRING BaseDllName;
        ULONG Flags;
        USHORT LoadCount;
        USHORT TlsIndex;
        union {
            LIST_ENTRY HashLinks;
            struct
            {
                PVOID SectionPointer;
                ULONG CheckSum;
            };
        };
        union {
            ULONG TimeDateStamp;
            PVOID LoadedImports;
        };
        PVOID EntryPointActivationContext;
        PVOID PatchInformation;
    } LDR_DATA_TABLE_ENTRY, * PLDR_DATA_TABLE_ENTRY;

    typedef struct _PEB {
        BYTE                          Reserved1[2];
        BYTE                          BeingDebugged;
        BYTE                          Reserved2[1];
        PVOID                         Reserved3[2];
        PPEB_LDR_DATA                 Ldr;
        PRTL_USER_PROCESS_PARAMETERS  ProcessParameters;
        BYTE                          Reserved4[104];
        PVOID                         Reserved5[52];
        PVOID                         PostProcessInitRoutine;
        BYTE                          Reserved6[128];
        PVOID                         Reserved7[1];
        ULONG                         SessionId;
    } PEB, * PPEB;

    // MODULE_ENTRY contains basic information about a module
    typedef struct _MODULE_ENTRY {
        UNICODE_STRING BaseName; // BaseName of the module
        UNICODE_STRING FullName; // FullName of the module
        ULONG SizeOfImage; // Size in bytes of the module
        PVOID BaseAddress; // Base address of the module
        PVOID EntryPoint; // Entrypoint of the module
    } MODULE_ENTRY, * PMODULE_ENTRY;

    // MODULE_INFORMATION_TABLE contains basic information about all the modules of a given process
    typedef struct _MODULE_INFORMATION_TABLE {
        ULONG Pid; // PID of the process
        ULONG ModuleCount; // Modules count for the above pointer
        MODULE_ENTRY* Modules; // Pointer to 0...* modules
    } MODULE_INFORMATION_TABLE, * PMODULE_INFORMATION_TABLE;


    PPEB
        GetCurrentPebProcess(
            void
        ) {


        PPEB pPeb = reinterpret_cast<PPEB>(__readfsdword(0x30));
        return pPeb;
    }


    PMODULE_INFORMATION_TABLE
        CreateModuleInformation(
            IN PPEB pPeb
        ) {
        ULONG Count = 0;
        ULONG CurCount = 0;
        PLIST_ENTRY pEntry = NULL;
        PLIST_ENTRY pHeadEntry = NULL;
        PPEB_LDR_DATA pLdrData = NULL;
        PMODULE_ENTRY CurModule = NULL;
        PLDR_DATA_TABLE_ENTRY pLdrEntry = NULL;
        PMODULE_INFORMATION_TABLE pModuleInformationTable = NULL;

        pLdrData = pPeb->Ldr;
        pHeadEntry = &pLdrData->InMemoryOrderModuleList;

        // Count user modules : iterate through the entire list
        pEntry = pHeadEntry->Flink;
        while (pEntry != pHeadEntry) {
            Count++;
            pEntry = pEntry->Flink;
        }

        // Allocate a MODULE_INFORMATION_TABLE
        if ((pModuleInformationTable = (PMODULE_INFORMATION_TABLE)malloc(sizeof(MODULE_INFORMATION_TABLE))) == NULL) {
            printf("Cannot allocate a MODULE_INFORMATION_TABLE.\n");
            return NULL;
        }

        // Allocate the correct amount of memory depending of the modules count
        if ((pModuleInformationTable->Modules = (PMODULE_ENTRY)malloc(Count * sizeof(MODULE_ENTRY))) == NULL) {
            printf("Cannot allocate a MODULE_INFORMATION_TABLE.\n");
            return NULL;
        }

        // Fill the basic information of MODULE_INFORMATION_TABLE
        pModuleInformationTable->ModuleCount = Count;

        // Fill all the modules information in the table
        pEntry = pHeadEntry->Flink;
        while (pEntry != pHeadEntry)
        {
            // Retrieve the current MODULE_ENTRY
            CurModule = &pModuleInformationTable->Modules[CurCount++];

            // Retrieve the current LDR_DATA_TABLE_ENTRY
            pLdrEntry = CONTAINING_RECORD(pEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderModuleList);

            // Fill the MODULE_ENTRY with the LDR_DATA_TABLE_ENTRY information
            RtlCopyMemory(&CurModule->BaseName, &pLdrEntry->BaseDllName, sizeof(CurModule->BaseName));
            RtlCopyMemory(&CurModule->FullName, &pLdrEntry->FullDllName, sizeof(CurModule->FullName));
            RtlCopyMemory(&CurModule->SizeOfImage, &pLdrEntry->SizeOfImage, sizeof(CurModule->SizeOfImage));
            RtlCopyMemory(&CurModule->BaseAddress, &pLdrEntry->DllBase, sizeof(CurModule->BaseAddress));
            RtlCopyMemory(&CurModule->EntryPoint, &pLdrEntry->EntryPoint, sizeof(CurModule->EntryPoint));

            // Iterate to the next entry
            pEntry = pEntry->Flink;
        }

        return pModuleInformationTable;
    }

    PMODULE_INFORMATION_TABLE
        QueryModuleInformationProcess(
            void
        ) {
        PPEB pPeb = NULL;
        PMODULE_INFORMATION_TABLE pModuleInformationTable = NULL;

        // Read the PEB from the current process
        if ((pPeb = GetCurrentPebProcess()) == NULL) {
            printf("GetPebCurrentProcess failed.\n");
            return NULL;
        }

        // Convert the PEB into a MODULE_INFORMATION_TABLE
        if ((pModuleInformationTable = CreateModuleInformation(pPeb)) == NULL) {
            printf("CreateModuleInformation failed.");
            return NULL;
        }

        return pModuleInformationTable;
    }

/*Extensions*/
std::string w2s(const std::wstring& var)
{
    static std::locale loc("");
    auto& facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc);
    return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).to_bytes(var);
}

std::wstring s2w(const std::string& var)
{
    static std::locale loc("");
    auto& facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc);
    return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).from_bytes(var);
}

MODULE_ENTRY* GetModuleEntryPEB(const char* Module)
{
    MODULE_INFORMATION_TABLE* moduleTable = QueryModuleInformationProcess();

    if (!moduleTable) {
        printf("Module table not found.\n");
        return NULL;
    }

    // Iterate through modules table
    size_t moduleIndex;
    for (moduleIndex = 0; moduleIndex < moduleTable->ModuleCount; moduleIndex++)
    {
        MODULE_ENTRY* moduleEntry = &moduleTable->Modules[moduleIndex];
        PVOID baseAddress = moduleEntry->BaseAddress;
        DWORD sizeOfModule = (DWORD)moduleEntry->SizeOfImage;
        if (moduleEntry->BaseName.Length == 0)
            continue;

        if (Module == NULL)
        {
#if DEBUGPEB
            printf("%-15S : 0x%08x -> 0x%08x (%S)\n", moduleEntry->BaseName.Buffer, baseAddress, (DWORD)baseAddress + sizeOfModule, moduleEntry->FullName.Buffer);
#endif
            return moduleEntry;
        }

        auto current = w2s(moduleEntry->BaseName.Buffer);


        if (current == std::string(Module))
        {
#if DEBUGPEB
            printf("%-15S : 0x%08x -> 0x%08x (%S)\n", moduleEntry->BaseName.Buffer, baseAddress, (DWORD)baseAddress + sizeOfModule, moduleEntry->FullName.Buffer);
#endif
            return moduleEntry;
        }

    }
    return NULL;
}
HMODULE GetModuleHandlePEB(const char* Module)
{
    if (auto Entry = GetModuleEntryPEB(Module); Entry)
        return (HMODULE)Entry->BaseAddress;
    return NULL;
}
SIZE_T GetModuleSizePEB(const char* Module)
{
    if (auto Entry = GetModuleEntryPEB(Module); Entry)
        return (SIZE_T)Entry->SizeOfImage;

    return NULL;
}
/*Extensions*/
 
Hack Is My Life
Süper Üye
Katılım
11 May 2019
Mesajlar
1,289
Çözümler
3
Tepki puanı
124
Ödüller
6
Yaş
36
7 HİZMET YILI
look nice :fastthink:
 
Samurai of the seas
Seçkin Üye
Katılım
1 Ağu 2018
Mesajlar
504
Çözümler
3
Tepki puanı
43
Ödüller
7
7 HİZMET YILI
Looks pretty well brother thanks alot
 
Süper Üye
Katılım
22 Şub 2019
Mesajlar
633
Çözümler
1
Tepki puanı
53
Ödüller
6
Yaş
31
7 HİZMET YILI
saol like atılacak bir paylaşım .)
 
Alone Wolf
Banlı Üye
Katılım
31 Mar 2019
Mesajlar
560
Çözümler
5
Tepki puanı
38
Sosyal
7 HİZMET YILI
Nice
Post automatically merged:
 
Kuro
Donator
Katılım
22 Ara 2019
Mesajlar
51
Tepki puanı
1
Ödüller
7
Yaş
29
6 HİZMET YILI
Thanks for Find and share this.. But this will made the code detected.
C++:
#if DEBUGPEB
            printf("%-15S : 0x%08x -> 0x%08x (%S)\n", moduleEntry->BaseName.Buffer, baseAddress, (DWORD)baseAddress + sizeOfModule, moduleEntry->FullName.Buffer);
#endi

Tested With X-Trap
 
Onaylı Üye
Katılım
6 Nis 2020
Mesajlar
51
Tepki puanı
0
Ödüller
3
6 HİZMET YILI
Emeğine sağlık hocam güzel paylaşım
 
Onaylı Üye
Katılım
11 Nis 2020
Mesajlar
52
Tepki puanı
6
Yaş
35
6 HİZMET YILI
wow clean code - well placed and detailed comments. That makes me happy knowing people actually care about quality.
 
Seçkin Üye
Katılım
6 Eki 2018
Mesajlar
306
Çözümler
1
Tepki puanı
76
Ödüller
7
7 HİZMET YILI
Emeğine sağlık hocam güzel paylaşım
 
Seçkin Üye
Katılım
15 Mar 2016
Mesajlar
310
Çözümler
2
Tepki puanı
7
Ödüller
7
Yaş
24
10 HİZMET YILI
güzel paylaşım hocam eline sağlık
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst