0

如何使用 Python 在特定扇区的磁盘映像文件 (60GB) 处编辑十六进制值?

Example:
Given 512,

File name: RAID.img
Typical File size: 60gb+
Sector: 3
Address Offset: 0000060A - 0000060F
Value: 0f, 0a , ab, cf, fe, fe

我能想到的代码:

fname = 'RAID.img'
with open(fname, 'r+b') as f:
    newdata = ('\x0f\x0a\xab\xcf\xfe\xfe')
    print newdata.encode('hex')

如何修改 Sector = 3 中的数据,地址从 0000060A - 0000060F?有一些图书馆可以使用吗?

4

1 回答 1

0

如果您知道要更新的数据的确切偏移量(字节位置),则可以使用file.seek,后跟file.write

#!/usr/bin/env python

offset = 0x60a
update = b'\x0f\x0a\xab\xcf\xfe\xfe'

with open('raid.img', 'r+b') as f:
    f.seek(offset)
    f.write(update)

如果您的数据文件很小(可能高达 1MB),您可以将完整的二进制文件读入 a bytearray,使用(修改)内存中的数据,然后将其写回文件:

#!/usr/bin/env python

offset = 0x60a
update = b'\x0f\x0a\xab\xcf\xfe\xfe'

with open('raid.img', 'r+b') as f:
    data = bytearray(f.read())
    data[offset:offset+len(update)] = update
    f.seek(0)
    f.write(data)
于 2017-11-04T14:33:44.900 回答