滴水逆向联盟

标题: 关于重写WriteProcessMemory的一处细节 [打印本页]

作者: xiaomingerniu    时间: 2024-1-1 22:18
标题: 关于重写WriteProcessMemory的一处细节
请问下老师:
我用IDA查看Kernel32.dll中WriteProcessMemory函数的实现流程,发现他是先调用NtProtectVirtualMemory将目标进程内存的访问权限改为可写,后调用NtWriteVirtualMemory执行写入。
在给NtWriteVirtualMemory传参时,代码如下:


.text:7C802265 8D 45 08             lea     eax, [ebp+hProcess]
.text:7C802268 50                      push    eax             ; NumberOfBytesWritten


即上述两行代码,应该是将EBP+8处的地址入栈吧?也就是将传入的hProcess的地址入栈?为什么IDA会将其解释为将NumberOfBytesWritten入栈?
函数BOOL __stdcall WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten)中也只传入了NumberOfBytesWritten的地址,用于将结果存入改地址啊?

我不看参考他的注释,仅看堆栈偏移对应的参数来重写,也能成功写入数据。请问这是为什么呢?
BOOL WINAPI MyWriteProcessMemory(
                                                                HANDLE hProcess,                               // handle to process
                                                                LPVOID lpBaseAddress,                       // base of memory area
                                                                LPVOID lpBuffer,                                       // data buffer
                                                                DWORD nSize,                                       // number of bytes to write
                                                                LPDWORD lpNumberOfBytesWritten    // number of bytes written
                                                                )
{
        DWORD dwRet = FALSE;

        __asm
        {
                lea eax,dword ptr [ebp + 0x08]        //&hProcess
                push eax
                push dword ptr [ebp + 0x14]                //nSize
                push dword ptr [ebp + 0x10]                //lpBuffer
                push dword ptr [ebp + 0x0c]                //lpBaseAddress
                push dword ptr [ebp + 0x08]                //hProcess

                //push dword ptr [ebp + 0x18]                //lpNumberOfBytesWritten

                call MyNtWriteVirtualMemory
                mov dword ptr [dwRet],eax
        }
       
        if (dwRet == 0)
        {
                return TRUE;
        }
        else
        {
                return FALSE;
        }
}


肯定老师指点一二,谢谢谢谢!!!







作者: xiaomingerniu    时间: 2024-1-6 14:20
本帖最后由 xiaomingerniu 于 2024-1-8 08:00 编辑

经过分析,我猜测是系统在WriteProcessMemory内部,将存放hProcess的栈地址,用于临时存放用于返回实际写入数据长度的储存地址(即number of bytes written),再经过ZwFlushInstructionCache函数后,将实际写入数据的长度写入最初传入的参数lpNumberOfBytesWritten(number of bytes written)地址。
也不知道分析的对不对。

后面我按自己得传参方式中过中断门也实现了三环部分的功能:
BOOL WINAPI MyWriteProcessMemoryPro(
                                                                 HANDLE hProcess,                                // handle to process
                                                                 LPVOID lpBaseAddress,                        // base of memory area
                                                                 LPVOID lpBuffer,                                // data buffer
                                                                 DWORD nSize,                                    // number of bytes to write
                                                                 LPDWORD lpNumberOfBytesWritten        // number of bytes written
                                                                 )
{
        __asm
        {
                pushad
                pushfd

                push dword ptr [ebp + 0x18]                //lpNumberOfBytesWritten
                push dword ptr [ebp + 0x14]                //nSize
                push dword ptr [ebp + 0x10]                //lpBuffer
                push dword ptr [ebp + 0x0c]                //lpBaseAddress
                push dword ptr [ebp + 0x08]                //hProcess

                //NtWriteVirtualMemory Begin
                mov eax,0x115

                //KiIntSystemCall
                lea edx,dword ptr ss:[esp]                //指向第一个参数
                int 0x2e
               
                //NtWriteVirtualMemory End:retn 0x14
                add esp,0x14

                popfd
                popad
        }

       if (*lpNumberOfBytesWritten != nSize)
        {
                return FALSE;
        }
        else
        {
                return TRUE;
        }
}

如有错误恳请老师指教!谢谢!







欢迎光临 滴水逆向联盟 (http://www.dtdebug.com/) Powered by Discuz! X3.2