4 ms·
This trick is used in the demoscene. Instead of using -c, I use -n, tail -n +2 $0 The -n +2 option means “starting at line 2”, which is what you want if you
by dietrichepp 3y ago
This trick is used in the demoscene. Instead of using -c, I use -n,
tail -n +2 $0
The -n +2 option means “starting at line 2”, which is what you want if you cram your script into one line. You can make an executable packed with lzma this way,
a=`mktemp`;tail -n+2 $0|unxz>$a;chmod +x $a;$a;rm $a;exit
This is the polite way to do it, using mktemp. You can save some bytes if you don’t care about that stuff.
- londons_explore 3y agoThere must be a way to run something without needing a temp file...
- BasedAnon 3y agoBasically everything in Linux will create a temp file one way or another even pipes. For you to take a binary and run it directly it has to have an inode. At best you can use Python & the ctypes module to write a program into part of Python's memory and trick it into continuing execution from there.
- dietrichepp 3y agoPipes are not temp files, they’re more like kernel buffers associated with no file. When I think of temp file, I think of something that is at least associated with a filesystem. The reason that running a binary needs a file is because execve() takes a path as an argument. But, as you said, there are other ways to load code into memory.
- Denvercoder9 3y ago> Basically everything in Linux will create a temp file one way or another even pipes. pipe(2) doesn't create a file, at least not in the sense that I usually think about files (something that's accessible through the filesystem).
- dietrichepp 3y agoYes. You can load the decompressed code into memory, mprotect() it to give it execution permissions, then jump in. Hard to do in a shell, though.
- 10000truths 3y agoIf you're willing assume a Linux platform and a Python installation, you can do something like: echo -n "base64encodedelf" | base64 --decode | python3 -c 'import os;import sys;f=os.memfd_create(os.MFD_CLOEXEC);os.write(f,sys.stdin.buffer.read());os.execve(f,["dummy"],[])'
- londons_explore 3y agoSurprising that no combination of bash symbols lets you do this.
- Tepix 3y agoNeat. But is the base64 even necessary?
- erik_seaberg 3y agoI don’t know that every editor today will gracefully handle a blob that isn’t valid UTF-8. Emacs needed a library (so-long) to disable some features that choke trying to render very long lines.
- Tepix 3y agoYou'll to edit the shell script without the binary at the end and once you're done you append the blob.