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

Posted

技术标签:

【中文标题】如何在windows中打开磁盘并在低级别读取数据?【英文标题】:How to open disks in windows and read data at low level? 【发布时间】:2011-09-25 05:51:32 【问题描述】:

我知道在 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:'

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

【问题讨论】:

我想我必须使用win32扩展win32file?有人知道吗? 【参考方案1】:

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

打开物理硬盘驱动器 直接磁盘访问(原始 I/O) 基于 Win32 的应用程序,使用设备 表格名称

\\.\PhysicalDriveN

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

要打开一个逻辑驱动器,直接访问 格式为

\\.\X: 

其中 X: 是 硬盘分区盘符,软盘 磁盘驱动器或 CD-ROM 驱动器。

【讨论】:

你怎么知道哪个驱动器是哪个? 我猜磁盘管理器是正确的,但在做任何破坏性的事情之前我会仔细检查:-) 用 python 做到这一点:blog.lifeeth.in/2011/03/reading-raw-disks-with-python.html CreateFile 的其他参数是什么?【参考方案2】:

请记住,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)

【讨论】:

【参考方案3】:

两者都为我工作。要访问分区 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"\\.\PhysicalDrivenumber",
        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))

【讨论】:

以上是关于如何在windows中打开磁盘并在低级别读取数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 JavaScript 打开本地磁盘文件?

BufferedOutputStream 实际上是如何在低级别工作的?

当我调用 fseek() 时,在低级别会发生啥?

如何从磁盘加载的 JS 在 Mobile Safari 中打开数据库

Visual Basic 如何读取 CSV 文件并在数据网格中显示值?

如何在 C# 中清空/刷新 Windows READ 磁盘缓存?