是否可以在 Windows 中使用 python 获得对原始设备的写入访问权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7135398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to get writing access to raw devices using python with windows?
提问by Martin Matysiak
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.
这是对这个问题的一种跟进。我想知道您是否可以\\.\PhysicalDriveN
在写入模式下访问原始设备(即),如果应该这样,如何访问。
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).
使用 Linux,可以简单地通过使用 eg 来实现写访问open("/dev/sdd", "w+")
(假设脚本以 root 权限运行)。我假设 Mac OS 的行为类似(/dev/diskN
作为输入文件)。
When trying the same command under Windows (with the corresponding path), it fails with the following error:
在 Windows 下(使用相应路径)尝试相同的命令时,失败并显示以下错误:
IOError: [Errno 22] invalid mode ('w+') or filename: '\\.\PhysicalDrive3'
However, when trying to readfrom the PhysicalDrive, it does work(even the correct data is read). The shell is running with administrator permissions under Windows 7.
但是,当尝试从 PhysicalDrive读取时,它确实有效(甚至读取了正确的数据)。该外壳在 Windows 7 下以管理员权限运行。
Is there any other way to accomplish this task using python while still keeping the script as platform-independent as possible?
有没有其他方法可以使用 python 完成此任务,同时仍使脚本尽可能独立于平台?
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.fdopento 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:
我进一步研究了 python 为文件处理提供的方法,并偶然发现了os.open。使用打开 PhysicalDriveos.open(drive_string, os.O_WRONLY|os.O_BINARY)
不会返回错误。到现在为止还挺好。现在我可以选择使用os.write直接写入这个文件描述符,或者使用os.fdopen获取文件对象并以常规方式写入它。可悲的是,这些可能性都不起作用。在第一种情况 ( os.write()
) 中,我得到以下信息:
>>> 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 cancreate a file object with write permissions, but the writing itself fails (well, after enforcing its execution using .flush()
):
在第二种情况下,我可以创建一个具有写入权限的文件对象,但写入本身失败(好吧,在使用 强制执行后.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
回答by Martin Matysiak
As eryksunand agfpointed 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).
正如eryksun和agf在评论中指出的那样(但我一开始并没有真正理解它),解决方案相当简单:您必须在rb+
模式下打开设备,这会打开设备进行更新(正如我所发现的现在..) 而不尝试用新文件替换它(这不起作用,因为该文件实际上是一个物理驱动器)。
When writing, you have to write always a whole sector at a time (i.e. multiples of 512-byte), otherwise it fails.
写入时,您必须一次写入整个扇区(即 512 字节的倍数),否则会失败。
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
).
此外,该.seek()
命令还可以仅按扇区跳转。如果您尝试在扇区内寻找位置(例如 position 621
),文件对象将跳转到您请求的位置所在扇区的开头(即到第二个扇区的开头, byte 512
)。
回答by user2174865
Possibly in Win 7 you have to do something more extreme, such as locking the volume(s) for the disk beforehand with DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, ...)
可能在 Win 7 中,您必须做一些更极端的事情,例如使用 DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, ...) 预先锁定磁盘的卷
In Win 7 you don't have to do that; opening and writing with 'rb+' mode works fine.
在 Win 7 中,您不必这样做;使用“rb+”模式打开和写入工作正常。