6

How can I load an exe file—stored as a base64 encoded string—into memory and execute it without writing it to disk?

The point being, to put some kind of control/password/serial system in place and compile it with py2exe. Then I could execute that embedded file when ever I want in my code.

7
  • Windows I presume? Why not implement the binary piece nativity in python? Commented May 6, 2013 at 20:31
  • tMC: It's a flash executable. Commented May 6, 2013 at 20:37
  • Why do you want to avoid writing it to disk? Commented May 6, 2013 at 20:45
  • abarnert: Because I don't want people to copy it, without passing my controls, if I write it to disk, all they have to do is execute the file created or copy it. I'd be too easy. Commented May 6, 2013 at 20:54
  • @leferreyra: Most users will be fooled by just hiding the file in a temporary directory. And those who won't… well, anyone who's read a basic cracking tutorial will know how to grab the image in memory and save it to disk and/or just b64decode it out of your source, so at best you're adding a few minutes to the time it takes them to crack your software. Is that worth hours of work on your end (and, most likely, bugs for your legit users)? Commented May 6, 2013 at 21:02

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so there seems to be no easy way to do it, and I don't want to get too deep into the Windows API, so. I guess I'll just have to write it in come obscure temp file.
0

encode 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. If you don't want users to see it, just decode it in any random folder and delete it after use.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.