繁体   English   中英

从 32 位进程获取到 64 位进程内存的入口点

[英]get the entry point to a 64bit process memory from a 32bit process

我想获得我从 32 位进程编写的 64 位进程的入口点,就像您使用 EnumProcessModule 并获取主模块的内存地址一样。 我的最终目标是从我的 64 位进程中的内存中读取一个字节,从偏移量到它(条目 + 偏移量)。

但是我的 NtWow64ReadVirtualMemory64 函数一直失败。 我认为这与我的入口内存地址有关。

    #define PROC_BASIC_INFO 0
    #define NT_WOW64_QUERY_INFORMATION_PROCESS_64_NAME  "NtWow64QueryInformationProcess64"
    #define NT_WOW64_READ_VIRTUAL_MEMORY_64_NAME  "NtWow64ReadVirtualMemory64"

    typedef UINT64 SYM;
    typedef SIZE_T SIZE_T64;

    HWND   WINDOW_HANDLE;
    HANDLE PROC_HANDLE;
    DWORD PROC_ID;
    UINT address;
    UINT64 address64;
    SIZE_T bytesRead;
    SIZE_T64 bytesRead64;

    using namespace std;


    //initialize variables for importing of essential 64 bit reading functions
    //from ntdll
    typedef NTSTATUS(NTAPI *FUNC_NtReadVirtualMemory64)
    ( 
        IN  HANDLE  ProcessHandle,
        IN  PVOID64 BaseAddress,
        OUT PVOID   Buffer,
        IN  ULONGLONG BufferLength,
        OUT PULONGLONG ReturnLength OPTIONAL
    );
    typedef NTSTATUS (NTAPI *FUNC_NtWow64QueryInformationProcess64) 
    (
        IN  HANDLE ProcessHandle,
        IN  ULONG  ProcessInformationClass,
        OUT PVOID  ProcessInformation64,
        IN  ULONG  Length,
        OUT PULONG ReturnLength OPTIONAL
    );

    struct PROCESS_BASIC_INFORMATION64 {

        SYM Reserved1;
        SYM PebBaseAddress;
        SYM Reserved2[2];
        SYM UniqueProcessId;
        SYM Reserved3;
        /*
        NTSTATUS ExitStatus;
        ULONG64 PebBaseAddress;
        ULONG64 AffinityMask;
        LONG    BasePriority;
        UINT64  Reserved1;
        ULONG64 UniqueProcessId;
        ULONG64 InheritedFromUniqueProcessId;
        */
    };



    HINSTANCE ntdll = LoadLibrary("ntdll.dll");
    FUNC_NtWow64QueryInformationProcess64 NtWow64QueryInformationProcess64 = (FUNC_NtWow64QueryInformationProcess64)GetProcAddress(ntdll, NT_WOW64_QUERY_INFORMATION_PROCESS_64_NAME);
    FUNC_NtReadVirtualMemory64 NtReadVirtualMemory64 = (FUNC_NtReadVirtualMemory64)GetProcAddress(ntdll, NT_WOW64_READ_VIRTUAL_MEMORY_64_NAME);

    int Init32To64MemoryRead(const char* windowClass, const char* caption, SYM addressOffset)
    {

        DWORD cbNeeded;
        DWORD dwdResult;
        HMODULE mainModule;
        BOOL enumResult;
        ULONG read_length=0;
        HINSTANCE ntdll; 
        PROCESS_BASIC_INFORMATION64 procInfo;
        ZeroMemory(&procInfo, sizeof(procInfo));



        //Get the window handle
        WINDOW_HANDLE = FindWindow(windowClass, NULL);
        if (WINDOW_HANDLE == NULL)
        {
            //Window was not foud
            return 10;
        }

        //Get the process ID
        dwdResult = GetWindowThreadProcessId(WINDOW_HANDLE, &PROC_ID);

        if (dwdResult == 0)
        {
            //Getting Process ID failed
            return 20;
        }

        //Open the process
        PROC_HANDLE = OpenProcess(PROCESS_ALL_ACCESS, false, PROC_ID);

        if (PROC_HANDLE == NULL)
        {
            //Process failed to open
            return 30;
        }
        DWORD result;

        //Query Proc Information to get .exe entry point
        result = NtWow64QueryInformationProcess64( PROC_HANDLE, 0, &procInfo, sizeof(procInfo), &read_length);
        if (result != 0)
        {
            cerr << "Query Information Process has failed" << endl;

            return 40;
        }

        address64 =  (procInfo.PebBaseAddress + addressOffset);
        cerr << address64 << endl;

        string number;
        stringstream stristream;

        stristream << address64;
        stristream >> number;

        byte testByte = 0;
        (byte)ReadMemory64<byte>(testByte);

        system("PAUSE");
        return 1;
    }


template <typename _ret_t> _ret_t ReadMemory64(_ret_t& ret)
{

    NTSTATUS result = NtReadVirtualMemory64(PROC_HANDLE, (void*)address64, &ret, 8, NULL);
    ///* Debug # when too lazy for breakpoints
    cerr <<"value: " << ret << endl;
    cerr << "Error Code: " << GetLastError() << endl;
    if (result != 0)
    {
        cerr << "ReadMemory Failed.\r\nAddress: " << address64 << "\r\nSize: " << sizeof(_ret_t) << "\r\nResult: " << result << endl;
        cerr << "NtReadVirtualMemory64 has failed" << endl;
        system("PAUSE");

    } //*/
    return ret;
 };

我想知道我做错了什么。

编辑:经过进一步检查,我注意到 NtWow64ReadVirtualMemory 没有在用作缓冲区的变量“ret”中存储值。

我运行了一个简单的测试,发现当插入到函数"NtWow64ReadVirtualMemory64"时,我的缓冲区-"ret" 的值没有改变。
除了 NtReadMemory64 返回一个奇怪的数字(没有可用于 ntdll NtWow64 函数的文档,所以使用谷歌搜索它没有产生任何有用的东西)之外,代码确实编译和运行没有错误(编译和运行时)。
所以我想我要么提供了错误的缓冲区,要么没有从有效的内存地址读取。

因为我确实在函数之外显式地初始化了缓冲区,所以我认为我的问题是后者(不提供有效的内存地址)。

我在调用 NtReadVirtualMemory 时使用了以下内容

NTSTATUS result = NtReadVirtualMemory64(PROC_HANDLE, (void*)address64, &ret, 8, NULL);

显然,在调用 NtWow64ReadVirtualMemory64 时,我将 addr 强制转换为 32 位无效指针(void*)address64 ,并且由于 address64 是 UINT64 类型,因此强制转换截断了地址,并且我试图读取我不是的内存段无法读取我通过将转换更改为(PVOID64)address64其转换为本地 64 位指针来解决它。

比我想象的要简单,但经过几天的谷歌搜索和审查代码后发现它是地狱。

编辑:这并没有削减它,因为我的地址是错误的。 我需要通过进程的主模块在内存中的位置来获取“.exe”的入口点。
看看现在如何。
任何帮助表示赞赏!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM