是否可以在 Windows 中使用 python 获得对原始设备的写入访问权限? [英] Is it possible to get writing access to raw devices using python with windows?

查看:26
本文介绍了是否可以在 Windows 中使用 python 获得对原始设备的写入访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对 这个问题.我想知道您是否可以在写入模式下访问原始设备(即 \.PhysicalDriveN),如果是这种情况,如何访问.

在 Linux 上,写访问可以简单地通过使用例如open("/dev/sdd", "w+")(前提是脚本以 root 权限运行).我假设 Mac OS 的行为类似(使用 /dev/diskN 作为输入文件).

在 Windows 下尝试相同的命令(使用相应的路径)时,失败并出现以下错误:

IOError: [Errno 22] 无效模式 ('w+') 或文件名: '\\.\PhysicalDrive3'

但是,当尝试从 PhysicalDrive 中读取时,它确实有效(即使读取了正确的数据).该外壳在 Windows 7 下以管理员权限运行.

是否有其他方法可以使用 python 完成此任务,同时仍尽可能保持脚本与平台无关?

我进一步研究了 python 为文件处理提供的方法,并偶然发现了 操作系统打开.使用 os.open(drive_string, os.O_WRONLY|os.O_BINARY) 打开 PhysicalDrive 不会返回错误.到现在为止还挺好.现在我可以选择使用 os.write<直接写入这个文件描述符/a>,或使用 os.fdopen 来获取文件对象并以常规方式写信给它.可悲的是,这些可能性都不起作用.在第一种情况下(os.write()),我得到了这个:

<预><代码>>>>os.write(os.open("\\.\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "test")回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中OSError: [Errno 22] 无效参数

在第二种情况下,我可以创建一个具有写入权限的文件对象,但写入本身失败(好吧,在使用 .flush() 强制执行后):

<预><代码>>>>g = os.fdopen(os.open("\\.\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "wb")>>>g.write("测试")>>>g.flush()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中IOError: [Errno 22] 无效参数

解决方案

As eryksunagf 在评论中指出(但我一开始并没有真正理解它),解决方案相当简单:您必须在rb+ 模式,打开设备以进行更新(正如我现在发现的......)而无需尝试用新文件替换它(这不起作用,因为该文件实际上是一个物理驱动器).

写入时,您必须一次写入整个扇区(即 512 字节的倍数),否则会失败.

此外,.seek() 命令也可以只按扇区跳转.如果您尝试在扇区内寻找位置(例如位置 621),文件对象将跳转到您请求的位置所在扇区的开头(即到第二个扇区的开头,字节512).

This is sort of a follow-up to this question. I want to know if you can access raw devices (i.e. \.PhysicalDriveN) in writing mode and if this should be the case, how.

Using Linux, write access can simply be achieved by using e.g. open("/dev/sdd", "w+") (provided that the script is running with root permissions). I assume that Mac OS behaves similar (with /dev/diskN as input file).

When trying the same command under Windows (with the corresponding path), it fails with the following error:

IOError: [Errno 22] invalid mode ('w+') or filename: '\\.\PhysicalDrive3'

However, when trying to read from the PhysicalDrive, it does work (even the correct data is read). The shell is running with administrator permissions under Windows 7.

Is there any other way to accomplish this task using python while still keeping the script as platform-independent as possible?

Edit:

I looked a bit further into what methods python provides for file handling and stumbled across os.open. Opening the PhysicalDrive using os.open(drive_string, os.O_WRONLY|os.O_BINARY) returns no error. So far, so good. Now I have either the choice to write directly to this file-descriptor using os.write, or use os.fdopen to get a file-object and write to it in the regular way. Sadly, none of these possibilities works. In the first case (os.write()), I get this:

>>> os.write(os.open("\\.\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

In the second case, I can create a file object with write permissions, but the writing itself fails (well, after enforcing its execution using .flush()):

>>> g = os.fdopen(os.open("\\.\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "wb")
>>> g.write("test")
>>> g.flush()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument

解决方案

As eryksun and agf pointed out in the comments (but I didn't really get it at first), the solution is rather simple: you have to open the device in the rb+ mode, which opens the device for updating (as I have found out now..) without trying to replace it with a new file (which wouldn't work because the file is in fact a physical drive).

When writing, you have to write always a whole sector at a time (i.e. multiples of 512-byte), otherwise it fails.

In addition, the .seek() command can also jump only sector-wise. If you try to seek a position inside a sector (e.g. position 621), the file object will jump to the beginning of the sector where your requested position is (i.e. to the beginning of the second sector, byte 512).

这篇关于是否可以在 Windows 中使用 python 获得对原始设备的写入访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆