问题找到了使用Python的DLL绝对路径 - pefile,WIN32API,win32con,wintypes,WINDLL

问题描述 投票:0回答:1

这是一个更大的,非常复杂的工作的一部分,我已经削减到只要领。我使用Python和这里的目标是枚举所有的DLL,并为他们找到绝对路径,我可以再喂到pefile,从而加载它。

我想出了partilaly作品和部分没有。在某些情况下,突出部分找到地址Python.exe或无法在所有发现任何东西。其他时候,它完美的作品。我在寻找如何,我可以通过正确这项技术寻找这些DLL的绝对路径的想法...或者是做一些完全不同的。是否有可能别的东西,我需要进口?

以下是我想出了:

import pefile
import win32api
import win32con
from ctypes import windll
from ctypes import wintypes
import sys
import os
peName= sys.argv[1] 
pe = pefile.PE(peName)
PE_DLLS = []

def getDLLs():
    global PE_DLLS
    for entry in pe.DIRECTORY_ENTRY_IMPORT:
        print entry.dll
        PE_DLLS.append(entry.dll)

def extractDLL(dllName):

    # Part of this loadlibrary comes from: https://www.programcreek.com/python/example/53932/ctypes.wintypes.HANDLE
    print dllName
    try:
        dllHandle = win32api.LoadLibraryEx(dllName, 0, win32con.LOAD_LIBRARY_AS_DATAFILE)
        windll.kernel32.GetModuleHandleW.restype = wintypes.HMODULE
        windll.kernel32.GetModuleHandleW.argtypes = [wintypes.LPCWSTR]
        windll.kernel32.GetModuleFileNameW.restype = wintypes.DWORD
        windll.kernel32.GetModuleFileNameW.argtypes = [wintypes.HANDLE, wintypes.LPWSTR, wintypes.DWORD]
        h_module_base = windll.kernel32.GetModuleHandleW(dllName)
        module_path = wintypes.create_unicode_buffer(255)
        windll.kernel32.GetModuleFileNameW(h_module_base, module_path, 255)
        pe = pefile.PE(module_path.value)
        win32api.FreeLibrary(dllHandle)

        print "\t*" + module_path.value

    except:
        print "\t*" + dllName + " could not be found."
        pass
i = 0
getDLLs()
for dll in PE_DLLS:
    extractDLL(PE_DLLS[i])
    i +=1

当我和一个文件,如为执行它说IDA,我得到如下:

USER32.dll
        *C:\WINDOWS\System32\USER32.dll
ADVAPI32.dll
        *C:\WINDOWS\System32\ADVAPI32.dll
WSOCK32.dll
        *C:\Python27\python.exe
SHELL32.dll
        *C:\WINDOWS\System32\SHELL32.dll
IDA.dll
        *IDA.dll could not be found.
Qt5PrintSupport.dll
        *C:\Python27\python.exe
Qt5Widgets.dll
        *C:\Python27\python.exe
Qt5Gui.dll
        *C:\Python27\python.exe
Qt5Core.dll
        *C:\Python27\python.exe
MSVCP140.dll
        *C:\Python27\python.exe
ole32.dll
        *C:\WINDOWS\System32\ole32.dll
OLEAUT32.dll
        *C:\WINDOWS\System32\OLEAUT32.dll
KERNEL32.dll
        *C:\WINDOWS\System32\KERNEL32.DLL
VCRUNTIME140.dll
        *C:\Python27\python.exe
api-ms-win-crt-convert-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-math-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-string-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-runtime-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-utility-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-stdio-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-time-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-heap-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll
api-ms-win-crt-locale-l1-1-0.dll
        *C:\WINDOWS\System32\ucrtbase.dll

注意以下问题:1,有的上前为Python.exe; 2.一些查不出(IDA); 3.一些在年底相似的DLL,它给了ucrtbase.dll。对任何这些任何帮助,将不胜感激,因为我希望它能够找到的所有DLL。

还有就是为什么我包括IDA但它给出了三个独特的错误,没有特别的理由,那就是比别人我测试了。

我曾想过寻找一个dll硬盘,如果没有找到,但可能与命名相同的DLL遇到的问题。此外,分析师可能的东西命名为常见的DLL,但实际上是一个恶意软件的神器,而且可以发现,只有搜索硬盘。最后,无需搜索可以很有点慢下来。但我愿意接受任何创造性的想法。

python portable-executable absolute-path
1个回答
0
投票

我可以看到至少有两个问题与您的代码:

  1. 报价为LoadLibraryExLOAD_LIBRARY_AS_DATAFILE标志的文档:

因此,你不能叫象GetModuleFileName,或的GetModuleHandle函数调用GetProcAddress

您的代码也因此未定义的行为是不能保证工作或返回有意义的结果。

  1. 您正在试图解决静态的东西,是由系统动态地进行。当Windows的加载器加载一个模块,然后检查IAT找到导入的模块,它然后进入一个相当复杂的机制称为Dynamic-Link Library Search Order依赖于Dynamic-Link Library Search Order(强调动态)。

请注意,你可能还从程序错过延迟加载导入或简单为LoadLibrary。

最好的办法是复制什么系统不得到同样的结果。

© www.soinside.com 2019 - 2024. All rights reserved.