Created
June 1, 2018 23:21
-
-
Save bsidhom/9ab78a7c4db838460b83341f8e8965d2 to your computer and use it in GitHub Desktop.
Package an executable binary file into a python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -Eeuo pipefail | |
function main() { | |
local input_file="$1" | |
cat <<EOF | |
from __future__ import print_function | |
import bz2 | |
import os | |
import sys | |
data = ( | |
$(bzip2 <"$input_file" | base64 | sed "s/\(.*\)/ '\1'/") | |
) | |
def main(): | |
with open('main', 'wb') as file: | |
file.write(bz2.decompress(data.decode('base64'))) | |
os.chmod('main', 0o755) | |
os.execv('./main', ['main'] + sys.argv[1:]) | |
if __name__ == '__main__': | |
main() | |
EOF | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment