我正在为我的逆向工程课程寻找一个可以在ntdll.dll
个Windows DLL中转换为jmp esp
的小工具,但我面临一些技术问题.
我写了以下脚本:
import ctypes
from ctypes import wintypes, WinDLL
def GetModuleHandle(module):
kernel32 = WinDLL('kernel32', use_last_error=True)
kernel32.GetModuleHandleW.restype = wintypes.HMODULE
kernel32.GetModuleHandleW.argtypes = [wintypes.LPCWSTR]
hMod = kernel32.GetModuleHandleW(module)
return hMod
def find_bytes_in_module(module_name: str, module_size: int, search_bytes: bytes) -> int:
# Get the handle of the module
hModule = GetModuleHandle(module_name)
# Read the contents of the module into a ctypes array and convert to a bytes array
ctypes_arr = ctypes.c_ubyte * module_size
module_data = bytes(ctypes_arr.from_address(hModule))
# Search for the wanted bytes in the module data
index = module_data.find(search_bytes)
return hModule + index
def main():
module_name = 'ntdll.dll'
module_size = 2 * 1024 * 1024 # ntdll.dll on my system is under 2 MB
search_bytes = b'\xff\xe4'
addr = find_bytes_in_module(module_name, module_size, search_bytes)
print(f'`jmp esp` gadget found inside {module_name} at the absolute address: {hex(addr)}')
return 0
if __name__ == '__main__':
main()
当我使用64位IPython终端运行find_bytes_in_module
内的命令时,以下代码可以工作,但由于我的课程限制,我只允许使用32位的Python解释器.
当我try 使用任何其他发行版本的python(我甚至try 了一个普通的64位python解释器)来运行这个脚本时,它在第module_data = bytes(ctypes_arr.from_address(hModule))
行崩溃.尤其是在try 将ctype数组转换为字节数组时.
退出代码是-1073741819
.我try 使用管理员权限运行终端,但得到了相同的结果.
有谁知道我该怎么解决这个问题吗?也许这甚至不是我应该走的方向(我正在试图找到从DLL开始到小工具的偏移量) 我使用的是64位的Windows10发行版,以防万一(希望它不会有问题).
编辑:我在Google上发现这个错误代码意味着访问冲突异常发生在程序的主线程上下文中,没有被程序的代码捕获,从而导致程序的main()/WinMain()入口点过早退出,终止了进程.然而,我不知道我应该采取什么不同的方式来防止这种情况发生,为什么当我在IPython中交互地做同样的事情时,它没有发生……