Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Execute .exe file embedded in Python script
- #option 1
- All of the mechanisms Python has for executing a child process require a filename.
- 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.
- There is a way to do this by dropping down to ZwCreateProcess/NtCreateProcess. If you know how to use the low-level NT API, this post should be all you need to understand it. If you don't… it's way too much to explain in an SO answer.
- 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.
- So, the right answer is to write the exe to a file, then execute it. 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.
- #Obviously, if you want to execute it multiple times, don't remove it until you're done with it. Or just use some appropriate #persistent directory, and write it only if it's missing or out of date.
- ####option2
- ###problem """
- how i can Embed an exe file in python program ?
- i want read an exe file then convert it to base64 and store in a string
- format and Embedd in python code such this :
- Exe='''R0lGODdhFQAVAPMAAAQ2PESapISCBASCBMTCxPxmNCQ iJJya/ISChGRmzPz+/PxmzDQyZ
- DQyZDQyZDQyZCwAAAAAFQAVAAAElJDISau9Vh2WMD0gqHHelJw nsXVloqDd2hrMm8pYYiSHYfMMRm
- 53ULlQHGFFx1MZCciUiVOsPmEkKNVp3UBhJ4Ohy1UxerSgJGZM MBbcBACQlVhRiHvaUsXHgywTdyc
- ....'''
- then i want execute it without converting and saving it in a separate
- exe file . in the other hand i want Embedding exe file ...
- how i can do it ??
- """
- #This script :
- import base64
- data=open("D:\\toto.exe","rb").read()
- data64='''import base64,os
- data="""'''+base64.b64encode(data)+'''"""
- f=open(r"C:\\temporaire.exe","wb").write(base64.b6 4decode(data))
- os.system(r"C:\\temporaire.exe")
- '''
- f=open("64exe.py","w").write(data64)
- Create a script (ascii file) "64exe.py" with the exe "D:\toto.exe"
- The script "64exe.py" (re)-create C:\temporaire.exe and run it.
- #It's a little basic template, for your answer.
- #*sorry for my bad english*
- #Johnmahuguatgmaildotcom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement