使用Python修改二进制文件 [英] Modifying binary file with Python

查看:411
本文介绍了使用Python修改二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修补十六进制文件. 我有两个名为"patch 1"和"patch 2"的补丁文件(十六进制)

要修补的文件是一个16 MB的文件,名为"file.bin".

在过去的6到7个小时里,我尝试了许多不同的方法来弄清楚该如何做.我可以整天将字符串写到文件中,但是我正在尝试执行以下操作:

打开带有读取字节的patch1.bin 用读取的字节打开patch2.bin 用写字节打开file.bin

我想定位到0xc0010和0x7c0010的位置,并应用patch1.bin 那么我想寻求0x040000并应用patch2.bin

所以我总共要应用3个补丁,然后关闭"file.bin"

如果有人感冒给我一个例子,我将非常感激:)

我首先尝试了这个:

patch1 = open("patch1", "r");
patch2 = open("patch2", "r");
main = open("file.bin", "w");

main.seek(0xC0010);
main.write(patch1);
main.seek(0x7C0010);
main.write(patch1);
main.seek(0x40000);
main.write(patch2);
main.close();

,但被告知我正在尝试向文件中写入字符串,但实际上这不是我想要的,哈哈 然后我尝试了这个:

infile1 = open("patch1.bin", "rb") 
new_pos1 = int("0x00", 16)
infile1.seek(new_pos1, 0)
infile2 = open('file.bin', 'wb')
new_pos2 = int('0xc0010', 16)
infile2.seek(new_pos2, 0xc0010)
chunk1 = int("6FFFE0", 16)         #this is how long patch1 file is
data1 = infile1.read(chunk1)
with open("file.bin", "a") as outfile:
    outfile.write(data1)

但是它也不起作用,无论我尝试了什么,我都无法以他正确的偏移量写入数据.

我确实设法将patch1写入file.bin,但是它没有在正确的偏移位置打补丁,事实上,它删除了file.bin并只是在其位置复制了patch1.当然哪个是错误的.

我必须提醒您,我是python和程序设计的新手,但我确实是在努力学习和学习它,因此,我们将研究任何好的例子,并希望对我来说是一个很好的学习课程:)

感谢伙计们和加尔斯帮助我弄清楚我做错了什么:)

解决方案

您只需要使用seekwrite.使用seek跳转到该位置,使用write覆盖现有数据.

with file('patch1.bin', 'rb') as fh:
    patch1 = fh.read()

with file('patch2.bin', 'rb') as fh:
    patch2 = fh.read()

with file('file.bin', 'r+b') as fh:
    # apply patch1
    fh.seek(0xc0010)
    fh.write(patch1)
    fh.seek(0x7c0010)
    fh.write(patch1)
    # apply patch2
    fh.seek(0x040000)
    fh.write(patch2)

i am trying to patch a hex file. i have two patch files (hex) named "patch 1" and "patch 2"

the file to be patched is a 16 MB file named "file.bin".

i have tried many different way for the past 6 or 7 hours to figure out how to do it. I can write a string to a file all day long, but i am trying to do the following:

open patch1.bin with read bytes open patch2.bin with read bytes open file.bin with write bytes

i want to seek to positions 0xc0010, and 0x7c0010, and apply patch1.bin then i want to seek to 0x040000 and apply patch2.bin

so all in all i will have 3 patches applied, then close the "file.bin"

if someone cold give me an example i would very much appreciate it :)

i tried this first:

patch1 = open("patch1", "r");
patch2 = open("patch2", "r");
main = open("file.bin", "w");

main.seek(0xC0010);
main.write(patch1);
main.seek(0x7C0010);
main.write(patch1);
main.seek(0x40000);
main.write(patch2);
main.close();

but was informed i was was trying to write a string to a file, when indeed its not what i wanted, lol then i tried this:

infile1 = open("patch1.bin", "rb") 
new_pos1 = int("0x00", 16)
infile1.seek(new_pos1, 0)
infile2 = open('file.bin', 'wb')
new_pos2 = int('0xc0010', 16)
infile2.seek(new_pos2, 0xc0010)
chunk1 = int("6FFFE0", 16)         #this is how long patch1 file is
data1 = infile1.read(chunk1)
with open("file.bin", "a") as outfile:
    outfile.write(data1)

but it did not work either, as no matter what i tried, i could not get it to write the data at he correct offset.

I did manage a few times to write the patch1 to file.bin, but it did not patch at the right offset, as a matter of fact it deleted the file.bin and just copied patch1 in its place. which ofcourse is wrong.

i must remind you i am new to python and programming, but i am really trying to dig my feet into it and learn, so any good examples will be examined and hopefully will be a good learning lesson for me :)

thanks guys and gals for helping me figure out what i was doing wrong :)

解决方案

You only need to use seek and write. Use seek to jump to the position and write to overrwite the existing data.

with file('patch1.bin', 'rb') as fh:
    patch1 = fh.read()

with file('patch2.bin', 'rb') as fh:
    patch2 = fh.read()

with file('file.bin', 'r+b') as fh:
    # apply patch1
    fh.seek(0xc0010)
    fh.write(patch1)
    fh.seek(0x7c0010)
    fh.write(patch1)
    # apply patch2
    fh.seek(0x040000)
    fh.write(patch2)

这篇关于使用Python修改二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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