【文件分析】将二进制文件转换为字节列表(字节列表转为文件)

文件转换为字节列表

import struct

myfile = '../../PEstudy/ixxx.exe' # 文件路径
def read_bin(file_name):
    """
    function: read a bin file, return the list of the content in file
    """
    with open(file_name, "rb") as f:
        f_content = f.read()
        content = struct.unpack("B" * len(f_content), f_content)
        f.close()
    return list(content)
bin_list = read_bin(myfile)

输出:

print(bin_list)
[77, 90, 144, 0, 3, 0, 0, 0, 4, 0, 0, 0, 255, 255, 0, 0, 184, 0, 0, 0, ....
print(bytes(bin_list))
b'MZ\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xff\xff\x00\x00\xb8\x00\x00\x00\x00\x00\x00\x00@\x00\x00
print(list(bytes(bin_list)))
[77, 90, 144, 0, 3, 0, 0, 0, 4, 0, 0, 0, 255, 255, 0, 0, 184, 0, 0, 0,...

使用PEview验证结果
在这里插入图片描述

0x4D转换为十进制为77

字节列表转回文件

bin_list转换回二进制可执行文件

fw = open("new_file.exe", "wb")
for i in bin_list:
    s = struct.pack('B', i)
    fw.write(s)
fw.close()

版权声明:本文为AwesomeP原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。