Advertisement
johnmahugu

Execute .exe file embedded in Python script

Mar 2nd, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. Execute .exe file embedded in Python script
  2. #option 1
  3. All of the mechanisms Python has for executing a child process require a filename.
  4.  
  5. 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.
  6.  
  7. 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.
  8.  
  9. 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.
  10.  
  11. So, the right answer is to write the exe to a file, then execute it. For example, something like this:
  12.  
  13. fd, path = tempfile.mkstemp(suffix='.exe')
  14. code = base64.b64decode(encoded_code)
  15. os.write(fd, code)
  16. os.fchmod(fd, 0o711)
  17. os.close(fd)
  18. try:
  19.     result = subprocess.call(path)
  20. finally:
  21.     os.remove(path)
  22. #This should work on both Windows and *nix, but it's completely untested, and will probably have bugs on at least one #platform.
  23.  
  24. #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.
  25. ####option2
  26. ###problem """
  27. how i can Embed an exe file in python program ?
  28. i want read an exe file then convert it to base64 and store in a string
  29. format and Embedd in python code such this :
  30.  
  31. Exe='''R0lGODdhFQAVAPMAAAQ2PESapISCBASCBMTCxPxmNCQ iJJya/ISChGRmzPz+/PxmzDQyZ
  32. DQyZDQyZDQyZCwAAAAAFQAVAAAElJDISau9Vh2WMD0gqHHelJw nsXVloqDd2hrMm8pYYiSHYfMMRm
  33. 53ULlQHGFFx1MZCciUiVOsPmEkKNVp3UBhJ4Ohy1UxerSgJGZM MBbcBACQlVhRiHvaUsXHgywTdyc
  34. ....'''
  35.  
  36. then i want execute it without converting and saving it in a separate
  37. exe file . in the other hand i want Embedding exe file ...
  38. how i can do it ??
  39. """
  40. #This script :
  41.  
  42. import base64
  43. data=open("D:\\toto.exe","rb").read()
  44. data64='''import base64,os
  45. data="""'''+base64.b64encode(data)+'''"""
  46. f=open(r"C:\\temporaire.exe","wb").write(base64.b6 4decode(data))
  47. os.system(r"C:\\temporaire.exe")
  48. '''
  49. f=open("64exe.py","w").write(data64)
  50. Create a script (ascii file) "64exe.py" with the exe "D:\toto.exe"
  51. The script "64exe.py" (re)-create C:\temporaire.exe and run it.
  52.  
  53. #It's a little basic template, for your answer.
  54. #*sorry for my bad english*
  55.  
  56. #Johnmahuguatgmaildotcom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement