是否有可能使用python与Windows写入访问原始设备? [英] Is it possible to get writing access to raw devices using python with windows?

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

问题描述

这是后续。我想知道你是否可以在书写模式下访问裸设备(例如 >

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

命令在Windows下(与相应的路径),它失败,并出现以下错误:

  IOError:[Errno 22] invalid mode 'w +')或文件名:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'但是,当从PhysicalDrive尝试读取时,它确实有效(即使读取了正确的数据)。在Windows 7下,shell正在以管理员权限运行。



有没有其他方法可以使用python来完成这个任务,同时仍然保持脚本尽可能不依赖于平台? / b>

编辑:



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

 >>> os.write(\\\\\\\\\\\\\\\\\\\\), 
文件< stdin>,第1行,位于< module>
OSError:[Errno 22]无效参数

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

 >>> g = os.fdopen(os.open(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\) g.write(test)
>>> g.flush()
Traceback(最近一次调用的最后一个):
在< module>文件中的< stdin>
IOError:[Errno 22]无效参数


解决方案

由于 eryksun agf 在注释中指出(但我起初并没有明白),解决方法很简单:您必须在 rb + 模式下打开设备,它打开更新设备(正如我现在发现的.. ),而不是试图用一个新的文件替换它(这是行不通的,因为该文件实际上是一个物理驱动器)。

在编写时,你必须始终写整个扇区一次(即512字节的倍数),否则失败。
$ b

另外, .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).

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

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