简体   繁体   English

Python脚本中嵌入的Execute.exe文件

[英]Execute .exe file embedded in Python script

How can I load an exe file—stored as a base64 encoded string—into memory and execute it without writing it to disk?如何将一个 exe 文件(存储为 base64 编码字符串)加载到 memory 中并在不写入磁盘的情况下执行它?

The point being, to put some kind of control/password/serial system in place and compile it with py2exe.关键是,放置某种控制/密码/串行系统并用 py2exe 编译它。 Then I could execute that embedded file when ever I want in my code.然后我可以在我的代码中随时执行该嵌入式文件。

All of the mechanisms Python has for executing a child process require a filename. Python 用于执行子进程的所有机制都需要一个文件名。

And so does the underlying CreateProcess function in the Win32 API, so there's not even an easy way around it by dropping down to that level. Win32 API 中的底层CreateProcess函数也是如此,因此通过下降到该级别甚至没有简单的方法来解决它。

There is a way to do this by dropping down to ZwCreateProcess / NtCreateProcess .一个办法由下降到做到这一点ZwCreateProcess / NtCreateProcess If you know how to use the low-level NT API, this post should be all you need to understand it.如果您知道如何使用低级 NT API,那么这篇文章应该就是您需要了解的全部内容。 If you don't… it's way too much to explain in an SO answer.如果你不......在SO答案中解释太多了。

Alternatively, of course, you can create or use a RAM drive, or even simulate a virtual filesystem, but that's getting a little silly as an attempt to avoid creating a file.或者,当然,您可以创建或使用 RAM 驱动器,甚至模拟虚拟文件系统,但为了避免创建文件,这有点愚蠢。

So, the right answer is to write the exe to a file, then execute it.因此,正确的答案是将 exe 写入文件,然后执行它。 For example, something like this:例如,这样的事情:

fd, path = tempfile.mkstemp(suffix='.exe')
code = base64.b64decode(encoded_code)
os.write(fd, code)
os.fchmod(fd, 0o711)
os.close(fd)
try:
    result = subprocess.call(path)
finally:
    os.remove(path)

This should work on both Windows and *nix, but it's completely untested, and will probably have bugs on at least one platform.这应该适用于 Windows 和 *nix,但它完全未经测试,并且可能至少在一个平台上存在错误。

Obviously, if you want to execute it multiple times, don't remove it until you're done with it.显然,如果您想多次执行它,请在完成之前不要removeremove Or just use some appropriate persistent directory, and write it only if it's missing or out of date.或者只是使用一些适当的持久目录,并且仅在它丢失或过时时才写入它。

encode exe:编码exe:

import base64
#encode exe file in base64 data

with open("Sample.exe", 'rb') as f:
    read_exe_to_basae64 = base64.b64encode(f.read())
    

#encoded data will be like (really big text, don't worry) for e.g.: 
b'TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAA9AHveeWEVjXlhFY15YRWN+n0bjXhhFY0QfhyNfmEVjZB+GI14YRWNUmljaHlhFY0AAAAAAAAAAAAAAA'

#decode exe file:

with open("Sample2.exe", 'wb') as f: 
    f.write(base64.b64decode(read_exe_to_basae64))

exe file will be created in folder. exe文件将在文件夹中创建。 If you don't want users to see it, just decode it in any random folder and delete it after use.如果您不想让用户看到它,只需将它解码到任何随机文件夹中,并在使用后将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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