Exe content conversion then executing using python

I wanna if there is any that we convert data of pe file exe into string and execut it using eval and exec
In python file

What string are you referring to from a windows pe .exe?
You need to provide more details.

Bro for example I have exe I copied it content and covert it into a string and then execute it in python script as string using eval,exec
I tried and came up with this


import base64
with open(“HelloWorld.exe”, “rb”) as f:
pe_data = f.read()
encoded_data = base64.b64encode(pe_data).decode()
with open(“payload.py”, “w”) as f:
f.write(f"payload = ‘{encoded_data}’\n")
f.write(“import base64\n”)
f.write(“import ctypes\n”)
f.write(“decoded_data = base64.b64decode(payload)\n”)
f.write(“buffer = ctypes.create_string_buffer(decoded_data)\n”)
f.write(“func = ctypes.cast(buffer, ctypes.CFUNCTYPE(ctypes.c_void_p))\n”)


Came up with this but whenever I tried to execute payload.py it gives error like violation writing

The .exe contains binary intel instructions not python source code.
What you are doing makes no sense.

An EXE file is a chunked format containing all kinds of information. It contains executable binary code, but also a lot of other things. It doesn’t make any sense to simply load it into memory and then jump to it.

Have you considered just invoking it as a subprocess?

yes I tried got violation error and sometimes it works but never gives output of exe.

Start here. Portable Executable - Wikipedia If that seems daunting, I strongly recommend NOT trying to manually run an exe file. That’s why program loaders exist, and why nearly every programming language makes it easy to just invoke a program using the system’s program loader.

There is absolutely no point trying to load up an exe file and run it without understanding what’s in the file.

Thanks for your help