繁体   English   中英

如何在windows中打开磁盘并读取低级数据?

[英]How to open disks in windows and read data at low level?

我知道在 linux 中它就像 /dev/sda 一样简单,但在 Windows 中,您如何打开磁盘并开始读取低级别的数据?

在 python 我试过了:

f = open("K:", "r")

我得到这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 13] Permission denied: 'K:'

即使作为管理员,我也会收到此错误。

来自http://support.microsoft.com/kb/100027

要在基于 Win32 的应用程序中打开物理硬盘驱动器以进行直接磁盘访问(原始 I/O),请使用以下形式的设备名称

\\.\PhysicalDriveN

其中 N 是 0、1、2 等,代表系统中的每个物理驱动器。

要打开逻辑驱动器,直接访问的形式是

\\.\X: 

其中 X:是硬盘驱动器分区号、软盘驱动器或 CD-ROM 驱动器。

请记住,windows 和其他操作系统中的所有对象都是文件。 要从驱动器 E 打开和读取 16 字节的数据:使用以下代码:

# Open a Disk in binary format read only 16 bytes
file = "\\\\.\\E:"
with open(file,'rb') as f:
    print("Disk Open")
    data = f.read(16)
    # Convert the binary data to upper case hex ascii code
    hex_data = " ".join("{:02X}".format(c) for c in data)
    print(hex_data)

两者都为我工作。 要访问分区 C:或整个驱动器,需要管理员权限。 这是一个替换 open() 的示例:

def open_physical_drive(
    number,
    mode="rb",
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None,
):
    """
    Opens a physical drive in read binary mode by default
    The numbering starts with 0
    """
    return open(
        fr"\\.\PhysicalDrive{number}",
        mode,
        buffering,
        encoding,
        errors,
        newline,
        closefd,
        opener,
    )


def open_windows_partition(
    letter,
    mode="rb",
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None,
):
    """
    Opens a partition of a windows drive letter in read binary mode by default
    """
    return open(
        fr"\\.\{letter}:", mode, buffering, encoding, errors, newline, closefd, opener
    )


# first 16 bytes from partition C:
# on Linux it's like /dev/sda1
with open_windows_partition("C") as drive_c:
    print(drive_c.read(16))


# first 16 bytes of first drive
# on Linux it's like /dev/sda
with open_physical_drive(0) as drive_0:
    print(drive_0.read(16))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM