我正在尝试访问Windows 2000系统的物理内存(试图在没有内存转储工具的情况下执行此操作)。我的理解是我需要使用CreateFile函数来创建句柄。我已经使用旧版本的win32dd来帮助我完成这项工作。网上的其他文档指出我使用“\ Device \ PhysicalMemory”或“\\。\ PhysicalMemory”。不幸的是,我每次都得到相同的错误。Python CreateFile无法找到物理内存
Traceback (most recent call last):
File "testHandles.py", line 101, in (module)
File "testHandles.py", line 72, in createFileHandle
pywintypes.error: (3, 'CreateFile', 'The system cannot find the path specified.')
事实上,返回的错误号码对每个运行\\不同。\ PhysicalMemory == 3 \设备\ PhysicalMemory == 2.审议pywin32,win32file,的CreateFile,pyhandle和pywintypes没有产生有关不同返回值的信息。
这是我的代码。我正在使用py2exe在Windows 2000上运行它(并且它是成功编译的)。我意识到,我可能也有DeviceIoControl问题,但现在我专注于CreateFile。
# testHandles.py
import ctypes
import socket
import struct
import sys
import win32file
import pywintypes
def createFileHandle():
outLoc = pywintypes.Unicode("C:\\Documents and Settings\\Administrator\\My Documents\\pymemdump_dotPM.dd")
handleLoc = pywintypes.Unicode("\\\\.\\PhysicalMemory")
#handleLoc = pywintypes.Unicode("\\Device\\PhysicalMemory")
placeHolder = 0
BytesReturned = 0
# Device = CreateFile(L"\\\\.\\win32dd", GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
# CreateFile(fileName, desiredAccess , shareMode , attributes , creationDisposition , flagsAndAttributes , hTemplateFile)
#hMemHandle = win32file.CreateFile(handleLoc, GENERIC_ALL, SHARE_READ, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None)
hMemHandle = win32file.CreateFile(handleLoc, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None)
print "hMemHandle: %s" % hMemHandle
if (hMemHandle == NO_ERROR):
print "Could not build hMemHandle"
sys.exit()
# We send destination path to the driver.
#if (!DeviceIoControl(hMemHandle, 0x19880922, outLoc, (ULONG)(wcslen(outLoc) + 1) * sizeof(TCHAR), NULL, 0, &BytesReturned, NULL))
if (ctypes.windll.Kernel32.DeviceIoControl(hMemHandle, 0x19880922, outLoc, 5, NULL, 0, BytesReturned, NULL)):
print "Error: DeviceIoControl(), Cannot send IOCTL.\n"
else:
print "[win32dd] Physical memory dumped. You can now check %s.\n" % outLoc
# Dump memory
createFileHandle()
谢谢 切角
我在这里使用的方法(一次工作)应该适用于XPSP2之前的所有内容,因为它允许从用户模式域访问物理内存。你是对的,其他所有XPSP2和更高版本都需要内核模式,而且很可能不可能使用Python。 谢谢, Cutawa – Cutaway 2009-09-23 22:16:03
啊,多一点挖,你看起来是正确的。虽然我希望你至少需要以管理员身份运行才能使其工作。也许这就是问题所在。 – zdan 2009-09-24 00:04:15
我不相信“管理员”权限是必要的,但我可能是错的。我在我的测试盒上以这个用户身份运行。 – Cutaway 2009-09-24 00:12:59