Python :: ctypes :: GetProcAddress()返回错误

时间:2011-02-01 01:38:58

标签: c++ python winapi ctypes

以下是调试器类的一部分。我有以下代码来枚举debugee中的进程。首先,它枚举并将现有debugee进程的句柄加载到数组中。然后我试图在特定模块中获取特定功能的地址。在这种情况下,我试图从 msvcr100.dll 中获取 printf()的地址


def enumerate_module(self,pid):

    lphModule = (c_void_p * 1024)()
    lpcbNeeded = c_ulong(0)

    if psapi.EnumProcessModules(self.h_process,lphModule,sizeof(c_void_p)*1024, byref(lpcbNeeded)):
        print "[*] EnumProcessModules: %d modules detected" % int(lpcbNeeded.value / sizeof(c_void_p))
        for i in range(int(lpcbNeeded.value / sizeof(c_void_p))):
            FileName = ""
            ReadBuffer = create_string_buffer(MAX_PATH)
            psapi.GetModuleFileNameExA(self.h_process,lphModule[i],ReadBuffer,MAX_PATH)
            FileName += ReadBuffer.value
            print "[*] %d - 0x%08x - %s" % (i,lphModule[i],FileName)
        address = kernel32.GetProcAddress(lphModule[3],"printf")
        if address == False:
            error = GetLastError()
            print "[*] GetProcAddress() ERROR: %d - %s" % (error, FormatError(error))    
        print "[**] Getting printf() address is: 0x%008x" % address
        return True
    else:
        error = GetLastError()
        print "[*] GetModuleHandleA: %d - %s" % (error, FormatError(error))
        return False

由于一些奇怪的原因,我无法让它发挥作用。 GetPorcAddress()返回:

ERROR: 126 - The specified module could not be found.

Any ideas???

PS. This might clarify my question a little: Script output

Enter the PID of the process to attach to: 2476 Opening process: 2476 [*] DebugActiveProcess: 0 - The operation completed successfully. [*] EnumProcessModules: 4 modules detected [*] 0 - 0x00400000 - printf.exe [*] 1 - 0x7c900000 - ntdll.dll [*] 2 - 0x7c800000 - kernel32.dll [*] 3 - 0x78aa0000 - MSVCR100.dll [*] GetProcAddress() ERROR: 126 - The specified module could not be found. [**] Getting printf() address is: 0x00000000 [*] Finished debugging. Exitng...

正如您所见,msvcr100.dll已加载到 0x78aa0000 。据我所理解 它应该在其地址空间中有printf(),我应该能够 得到它的地址。此外,我在OllyDbg中加载了printf.exe并显示了相同的内容 你在我的脚本输出上看到的东西,我能够看到printf() msvcr100.dll的导出列表。

2 个答案:

答案 0 :(得分:2)

GetProcAddress获取进程中加载的DLL 中函数的地址,而不是其他进程。你应该看看Debug Help Library

根据您对GetProcAddress的请求,我的参考文献:

GetProcAddress

  

hModule [in]
  DLL模块的句柄,包含函数或变量。 LoadLibrary LoadLibraryEx GetModuleHandle 函数会返回此句柄。

LoadLibrary

  

将指定的模块加载到调用进程的地址空间....

LoadLibraryEx

  

将指定的模块加载到调用进程的地址空间....

GetModuleHandle

  

检索指定模块的模块句柄。该模块必须已由调用进程加载。

答案 1 :(得分:0)

我认为这意味着它无法在您的系统上找到该特定的DLL。这是一个简单的函数,它将返回printf地址:

from ctypes import *

kernel32 = windll.kernel32

def resolve_function(dll, func):
    handle = kernel32.GetModuleHandleA(dll)
    address = kernel32.GetProcAddress(handle, func)
    kernel32.CloseHandle(handle)
    return address

address = resolve_function('msvcrt.dll','printf')

print(address)

我还在学习这些东西,我不太确定msvcrt.dllmsvcr100.dll之间的区别。但是,我认为您需要链接msvcrt.dll而微软会发现msvcrXX.dll。请查看此页面以获取更多信息:http://msdn.microsoft.com/en-us/library/abx4dbyh