c/c++ how can i get base address of .exe running process?

前端 未结 2 1698
一整个雨季
一整个雨季 2021-01-17 08:04

Im looking for a method/function that i can use to get base address of \"program.exe\"+03262C08 -> B4895A0. This address is from Cheat Engine and base addres

相关标签:
2条回答
  • 2021-01-17 08:31

    Windows has been using Address Space Layout Randomization for about a decade now, but the module base in EXE's is far older than that. Simply ignore it, it's now meaningless.

    And don't forget: each process has its own address space. A pointer in one process is meaningless in the other.

    0 讨论(0)
  • 2021-01-17 08:35

    To use ReadProcessMemory or WriteProcessMemory on an address which is found via pointer chain on a process and dynamically get the module base address at runtime you need to accomplish these steps:

    • Find the Process with ToolHelp32Snapshot
    • Find the Module with ToolHelp32Snapsho
    • Get a handle to the process with correct permissions
    • Walk the pointer chain, de-referencing and adding offsets
    • Then you can Call ReadProcessMemory or WriteProcessMemory
    • You must run as administrator

    For this example I will use a simple assault cube cheat I've made

    #include <iostream>
    #include <vector>
    #include <Windows.h>
    #include "proc.h"
    
    int main()
    {
        //Get ProcId of the target process
        DWORD procId = GetProcId(L"ac_client.exe");
    
        //Getmodulebaseaddress
        uintptr_t moduleBase = GetModuleBaseAddress(procId, L"ac_client.exe");
    
        //Get Handle to Process
        HANDLE hProcess = 0;
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, procId);
    
        //Resolve base address of the pointer chain
        uintptr_t dynamicPtrBaseAddr = moduleBase + 0x10f4f4;
    
        std::cout << "DynamicPtrBaseAddr = " << "0x" << std::hex << dynamicPtrBaseAddr << std::endl;
    
        //Resolve our ammo pointer chain
        std::vector<unsigned int> ammoOffsets = { 0x374, 0x14, 0x0 };
        uintptr_t ammoAddr = FindDMAAddy(hProcess, dynamicPtrBaseAddr, ammoOffsets);
    
        std::cout << "ammoAddr = " << "0x" << std::hex << ammoAddr << std::endl;
    
        //Read Ammo value
        int ammoValue = 0;
    
        ReadProcessMemory(hProcess, (BYTE*)ammoAddr, &ammoValue, sizeof(ammoValue), nullptr);
        std::cout << "Curent ammo = " << std::dec << ammoValue << std::endl;
    
        //Write to it
        int newAmmo = 1337;
        WriteProcessMemory(hProcess, (BYTE*)ammoAddr, &newAmmo, sizeof(newAmmo), nullptr);
    
        //Read out again
        ReadProcessMemory(hProcess, (BYTE*)ammoAddr, &ammoValue, sizeof(ammoValue), nullptr);
    
        std::cout << "New ammo = " << std::dec << ammoValue << std::endl;
    
        getchar();
        return 0;
    }
    

    The header file proc.cpp:

    DWORD GetProcId(const wchar_t* procName)
    {
        DWORD procId = 0;
        HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hSnap != INVALID_HANDLE_VALUE)
        {
            PROCESSENTRY32 procEntry;
            procEntry.dwSize = sizeof(procEntry);
    
            if (Process32First(hSnap, &procEntry))
            {
                do
                {
                    if (!_wcsicmp(procEntry.szExeFile, procName))
                    {
                        procId = procEntry.th32ProcessID;
                        break;
                    }
                } while (Process32Next(hSnap, &procEntry));
    
            }
        }
        CloseHandle(hSnap);
        return procId;
    }
    
    uintptr_t GetModuleBaseAddress(DWORD procId, const wchar_t* modName)
    {
        uintptr_t modBaseAddr = 0;
        HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
        if (hSnap != INVALID_HANDLE_VALUE)
        {
            MODULEENTRY32 modEntry;
            modEntry.dwSize = sizeof(modEntry);
            if (Module32First(hSnap, &modEntry))
            {
                do
                {
                    if (!_wcsicmp(modEntry.szModule, modName))
                    {
                        modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
                        break;
                    }
                } while (Module32Next(hSnap, &modEntry));
            }
        }
        CloseHandle(hSnap);
        return modBaseAddr;
    }
    
    uintptr_t FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets)
    {
        uintptr_t addr = ptr;
        for (unsigned int i = 0; i < offsets.size(); ++i)
        {
            ReadProcessMemory(hProc, (BYTE*)addr, &addr, sizeof(addr), 0);
            addr += offsets[i];
        }
        return addr;
    }
    
    0 讨论(0)
提交回复
热议问题