Bruno 'Shady'
Bruno 'Shady'

Reputation: 4516

How create a tempory file from base64 encoded string?

How can I create a file into the temporary files from a encoded string and then execute it?

Upvotes: 1

Views: 3837

Answers (4)

leoluk
leoluk

Reputation: 12981

Something like this:

import tempfile
import subprocess

with tempfile.TemporaryFile(suffix='.exe', delete=False) as tmp:
    tmp.write(EMBEDDED_EXECUTABLE.decode('base64'))
    tmp.close()
    subprocess.Popen(tmp.name)

Upvotes: 3

Peter O.
Peter O.

Reputation: 32908

You have the Windows tag, so I'll answer accordingly.

You will need to, among other valid possibilities, prefix an underscore before lowercase characters, and replace slashes with hyphens, in order to make the Base64 sequence a valid filename as well as unique.

Upvotes: 0

Hank Gay
Hank Gay

Reputation: 72029

You'll probably be interested in the tempfile module, as well as base64 and execfile.

BTW, you may want to provide some more detail about what you're trying to accomplish. This sounds like it has a good chance of being either a bad idea, unnecessary work, or both.

Upvotes: 3

jcomeau_ictx
jcomeau_ictx

Reputation: 38492

decode the string (pydoc base64); make a tempfile (pydoc tempfile), write the string; and use any number of the os functions to execute it (maybe pydoc os.popen).

Upvotes: 1

Related Questions