代码
#!/usr/bin/env python
# encoding: utf-8import binasciicolnum_per_row = 16
file_path = 'demo.sys'
array_name = 'demo_shellcode'def exe_to_c_array(file_path, array_name):with open(file_path, 'rb') as file:binary_data = file.read()hex_data = binascii.hexlify(binary_data).decode()c_array = "const unsigned char {}[] = \n".format(array_name)row = "\""for i in range(2, len(hex_data) + 2, 2):# print(i, len(hex_data), hex_data[i-2], hex_data[i-1])row += "\\x" + hex_data[i-2] + hex_data[i-1]if i % (colnum_per_row * 2) == 0 or i >= len(hex_data):row += "\""if i + 2 < len(hex_data):row += "\n"c_array += rowrow = "\""c_array += ";";return c_arrayif __name__ == '__main__':# print(exe_to_c_array(file_path, array_name))with open("demo.h", 'w', encoding='utf-8') as f:f.write(exe_to_c_array(file_path, array_name))