我有一个包含 exe 的十六进制文件的 txt 文件。我在 python 中读取了该文件,但无法运行该 .exe 文件。 任何形式的帮助都会得到满足...
谢谢
import binascii
def getExeFile():
file1=input("Enter an exe file name(path):")
with open(file1, 'rb') as f:
content1 = f.read()
bucket1=open("f1.txt", 'w')
bucket1.write(str(binascii.hexlify(content1)))
print(binascii .hexlify(content1))
bucket1.close()
def getNonExeFile():
file2=input("Enter a non-exe file name(path):")
with open(file2, 'rb') as f:
content2 = f.read()
bucket2=open("f2.txt", 'w')
bucket2.write(str(binascii.hexlify(content2)))
print(binascii .hexlify(content2))
bucket2.close()
getExeFile()
getNonExeFile()
print("End")
最佳答案
转储到临时文件;更改其权限,使其可执行并在子进程中运行
示例:
from os import chown
from subprocess import check_call
from tempfile import NamedTemporaryFile
with NamedTemporaryFile(delete=False) as f:
f.write(get_hex_from_file("mydata.dat"))
chown(f.name, 0755)
check_call(f.name)
当然,我在这里假设您正在某种 UNIX 机器上执行此操作,并且本例中的“EXE”意味着某种 ELF/A.OUT/COFF 可执行文件! -- 尽管如此;相同的原理和代码(经过一些调整)可能适用于其他平台;例如:Windows。
参见:
关于python - 如何运行文本文件中包含的可执行文件(exe 的十六进制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257542/