简体   繁体   中英

How to read and write "Unicode (UTF-16 little endian)" text to and from a binary file in Python?

I have a binary file "x.bin" which is a windows executable.

I am trying to modify a value of a string inside this executable using Python, viewing this string in a text editor I am told that the text encoding is "Unicode (UTF-16 little endian)" and I can see null bytes wrapped around the text in the string.

However, when I attempt to read from this file in binary mode in Python and I then convert it to hexadecimal, I am told that the hex is not present in the file when doing print("10002000" in _hex) and therefore cannot replace any data.

I also cannot access the file in the "r" mode using encoding="utf-16-le" as this would attempt to decode the data and would fail.

Is there a way to access binary data using the UTF-16 encoding in Python?

Edit:

_hex is the result after calling data.hex()

I am using this method for reading from the file

with open("resources\\rfreeze.bin", "rb") as f:
    data = f.read()

Your question is a bit unclear, but to edit an executable you simply need to replace the target bytes with another set of bytes of the same length. Here's an example:

test.c - Simple program with an embedded UTF-16LE string (in Windows, anyway):

#include <stdio.h>

int main() {
    wchar_t* s = L"Hello";
    printf("%S\n",s);
    return 0;
}

test.py - replace the string with another string

with open('test.exe','rb') as f:
    data = f.read()

target = 'Hello'.encode('utf-16le')
replacement = 'ABCDE'.encode('utf-16le')

if len(target) != len(replacement):
    raise RuntimeError('invalid replacement')

data = data.replace(target,replacement)

with open('new_test.exe','wb') as f:
    f.write(data)

Demo:

C:\>cl /W4 /nologo test.c
test.c

C:\>test.exe
Hello

C:\>test.py

C:\>new_test.exe
ABCDE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM