321 points · by BasedAnon · 3y ago · 141 comments
tail -c <number of bytes for the binary> $0
All you have to do is make sure you append an explicit 'exit' to the end of your program before your new 'data section', so that bash won't parse any of the 'data section'.One thing to bear in mind is that if you append binary data, it will be corrupted if you save it in most text editors so when I want to make changes I just delete all the binary and reappend it.
echo '...base64 data...' | base64 -d > somefile
in your script.Or add compression to reclaim at least some of the wasted space:
echo '...base64 gzipped data...' | base64 -d | gunzip > somefile
Also note that bash accepts line breaks in quoted strings and the base64 utility has an "ignore garbage" option that lets it skip over e.g. whitespace in its input. You can use those to break up the base64 over multiple lines: echo '
...base64 gzipped data...
...more data...
...even more data...
' | base64 -di | gunzip > somefileIt's also useful if you want to check the file into a git repo and want to keep using the diff and merge tools.
{ base64 -d | gunzip > output; } <<EOF12345
...data...
EOF12345 #!/bin/sh
sed -E '1,/^START-OF-TAR-DATA$/d' "$0" | tar xvzf -
exit
START-OF-TAR-DATA
and then run: cat extract.sh ../foobar.tar.gz > foobar.tar.gz.sh
You can then run foobar.tar.gz.sh to self-extract. And you still get the benefit of being able to modify the shell script without needing to count lines or characters without sacrificing any compression.The reasoning being that the base64'd binary data is safe from being corrupted when the file is edited in text editors, as a response to the warning stated on the last paragraph of the original post.
It's just to mitigate the wastefulness of base64 encoding. You end up with a file that is text editor friendly and not quite as bloated as directly encoding the binary would be - but of course the file is still larger than simply appending the binary directly like in the OP.
Also, if you don't care about text editor friendlyness, you could indeed just zip the binary and then append it to the script for an even smaller file.
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. 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"],[])'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.
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).
payload_offset=$(($(grep -na -m1 "^MARKER:$" "$0"|cut -d':' -f1) + 1))
and at the end of the code it just has:
exit 0
MARKER:
That's a great expression
Front Short, Back Long
[1] https://da.wikipedia.org/wiki/Frisure
#!/usr/bin/env bash
# read data starting from the provided section marker up to the next one or EOF
function section() {
local section="$1"
local source="${BASH_SOURCE[0]}"
awk '/^__[A-Z0-9]+__$/{f=0} f{print} /^'"${section}"'$/{f=1}' "${source}"
}
section __JSON__ | jq
section __YAML__ | ruby -ryaml -e 'p YAML.load(STDIN.read)'
exit
__JSON__
{ "a": 1 }
__YAML__
b:
- 1
- 2
- 3
My only wish is that shellcheck had a directive to stop yelling at me starting at a certain line.Usually I augment it with such functions for clarity:
# whatever raw data
function data() {
section __DATA__
}
# man/perldoc like
function doc() {
section __DOC__
}
# command line help
function help() {
section __HELP__
}But it does no? Shellcheck can be configured to ignore SC by putting a "disable" directive in a config file or even as a comment in your Bash source file. It would work if it's always the same annoying warning / SCxxx message you get.
https://github.com/koalaman/shellcheck/wiki/Directive
> Directives that replace or are immediately after the shebang apply to the entire script. Otherwise, they are scoped to the command that follows it
https://github.com/koalaman/shellcheck/wiki/Ignore
> Note that the directive must be on the first line after the shebang with versions before 0.4.6. As of 0.4.6 comments and whitespace are allowed before file-wide directives.
Long story but trust me that I had good intentions.
When everyone in the world INCLUDING cyber-security professionals seems to think that it's perfectly OK to download setup.exe or install.msi from sketchy driver mfgs. and run them. But SHARs and curl | sh is too scary!
It's not that any of these are a good idea, it's the blind-spot to traditional binary installers that's annoying. How do you audit them? Why are they OK (because they have to be)
rm -rf /path/to/directory
becomes rm -rf /path
if the connection accidentally drops half way. Or something less dramatic, but still leaving you in a broken state.Some people took the “don’t curl | sh” advice as “you have to inspect every line of a shell script installer you download”, which is of course absurd.
java -jar $0
It makes it very easy to setup and use Java based command line programs on a server. java.util.zip.ZipException: invalid CEN header (bad signature)
at java.base/java.util.zip.ZipFile$Source.zerror(ZipFile.java:1623)
if I try to do anything with a JAR file that has leading text. I'm creating it just using echo 'java -jar $0' | cat - test.jar > test.run.jar
Is there more to it?The “unzip” utility can be useful as a sanity check; run “unzip -t” to test the integrity of the file.
I have a small bash stub that I use. It’s roughly (I also add JAVA_OPTS, etc…):
#!/bin/bash
java -jar $0
Then… cat stub.sh myproject.jar > myexec
See: https://github.com/compgen-io/ngsutilsj/blob/master/src/scri...This is what the (now defunkt) Capsule project prepends to get this same effect
I don't append it in binary form, though. I uuencode it. That way, there is no danger in using text editors.
in fact I found an old archive of mine floating around on usenet and wrote a python script to unpack it. Looking at the original, it was using a scripting. language bootstrap to make a COM script unpack embedded the original code.
Or just use exec.
exec tail -c [number of bytes for the binary] $0just use
cat >outfile <<EOF
some
data
EOF
add base64 if binaryedit: after looking thru the thread I am deeply disappointed so little people know of that feature.
I am deeply disappointed you didn't know this at some point in the past. An alternative oulook: https://xkcd.com/1053/
And to clarify I'm not really disappointed in people, just the fact it is unknown
- truncate the file to extend it to the end of the last block
- write data to that area
- truncate the file back to its original size
An edit of that file will likely lose you data though.
IIRC, Perl copied it from BASIC, because BASIC came much before Perl.
And, again, IIRC, I've read about the shar (shell archive) method that someone else commented about in this thread (and which even has a Wikipedia entry), in either the classic Kernighan and Pike book, The Unix Programming Environment (which I've recommended here multiple times before), or in some Unix man pages, long ago.
So it's quite an old method.
What could possibly go wrong with emailing executable scripts?
Server side malware filters will strip the attachment.
Upon examination it contained binary data and a command to extract it to a file and then installed the application.
This was the “efficient” way to ship and install the binary.
``` #!/usr/local/bin/bash
echo "HELLO"
TAIL_REMOTE_MARKER=`awk '/^__THE_REMOTE_PART__/{flag=1;next}/^__END_THE_REMOTE_PART__/{flag=0;exit}flag' ${0}`
eval "$TAIL_REMOTE_MARKER"
exit 0
__THE_REMOTE_PART__
echo "WORLD"
__END_THE_REMOTE_PART__ ```
It's a cool abstraction, during development you can have assets just living on the file system and then "deploy" a flat file that accesses those assets the same way.
https://portswigger-cdn.net/burp/releases/download?product=c...
Wow, that triggered my wordplay radar, which I'm working on as a fun side line these days, thanks :)
port, suite (sweet)
swig, burp
Heh.
I am not saying don't do it. But that is mostly where I see this type of trick.