0

Given a binary file, such as "Installer.exe", how can I read in the bytes of that file and then execute those bytes?

i.e.

with open('Installer.exe', 'rb') as file:
    data = file.read()
some_function_to_execute_bytes(data)

I am aware that it is possible to execute a file using subprocess, such as

import subprocess

subprocess.run('Installer.exe')

but I would like to run a string of bytes no matter how it is obtained (very similar to Possible to execute Python bytecode from a script? question, but the binary file is not Python code).

In my use case, I have a .exe file generated by pyinstaller in a MemoryFS object from pyfilesystem. Notably, a file in a MemoryFS object cannot be ran at the system level - simply because it does not exist on the file system; it is stored entirely in memory.

I would like a way to be able to read the binary file in memory and execute it. Writing it to a file on the local file system is not acceptable - increased time to perform the write operation, taking up extra file system space, needing more cleanup, etc.

from fs.memoryfs import MemoryFS
from fs.osfs import OSFS
from fs.copy import copy_file

external_path = 'some/external/location/that/should/not/run/binary/files/Installer.exe'
external_fs = OSFS('//external/server')
in_memory_fs = MemoryFS()
copy_file(external_fs, external_path, in_memory_fs, 'Installer.exe')

data = in_memory_fs.readbytes('Installer.exe')
some_function_to_execute_bytes(data)

Chris Collett
  • 1,074
  • 10
  • 15
  • 2
    Why not write the bytes to a file and then execute that file> – rdas Oct 29 '21 at 18:04
  • @rdas I don't need the file to be preserved, only ran. Additionally, the more times I have to write to the physical file system, the longer the operation takes (especially for very large executable files). – Chris Collett Oct 29 '21 at 18:06
  • You could create a file containing the byte code and execute it – Einliterflasche Oct 29 '21 at 18:06
  • 2
    Execute it as what? Machine code? Python bytecode? Why would a .exe have Python bytecode in it? Even if it did, such as if it was generated via PyInstaller, the code isn't just stored at the beginning of the file, it'd be in a specific place. And probably not just executable directly. If you want to execute the machine code, same deal; it's not just directly executable code that's stored in a .exe, EXEs are structured files that need to be parsed. I think this question needs way more details, including an example input and output. – Random Davis Oct 29 '21 at 18:06
  • @RandomDavis In my use case, the executable file is generated from `pyinstaller`. I can specify that in the question - if there is a `pyinstaller` native solution, that would be acceptable. – Chris Collett Oct 29 '21 at 18:10
  • 1
    There's already a tool for that, as specified in [this post](https://stackoverflow.com/questions/36581073/exe-to-python-with-pyinstaller). What's wrong with that tool? – Random Davis Oct 29 '21 at 18:11
  • [Executing a file that is in a buffer](https://stackoverflow.com/q/33773762/10792235) and [Execute .exe file embedded in Python script](https://stackoverflow.com/q/16406156/10792235) were both helpful in figuring this out. Summing up: there are undocumented ways of starting a process from memory, but is far better to have an exe written to a disk somewhere and run it. – Chris Collett Oct 29 '21 at 18:44

0 Answers0