Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Last active February 7, 2023 08:21
Show Gist options
  • Select an option

  • Save alirezaarzehgar/ecf870a980f6ae6f4cb4de2ea93b3bef to your computer and use it in GitHub Desktop.

Select an option

Save alirezaarzehgar/ecf870a980f6ae6f4cb4de2ea93b3bef to your computer and use it in GitHub Desktop.
Embed binary in bash
#!/usr/bin/env bash
add_command() {
echo "
lstart=\$(awk '/^__'${1}'_PAYLOAD_BEGIN__/ { print NR + 1; exit 0; }' \$0)
lend=\$(awk '/^__'${1}'_PAYLOAD_END__/ { print NR + 1; exit 0; }' \$0)
cat \$0 | head -n +\${lend} | tail -n +\${lstart} > \${dir}/${1}
chmod +x \${dir}/${1}"
}
add_tag() {
echo __${1}_PAYLOAD_BEGIN__
cat $(which ${1})
echo
echo __${1}_PAYLOAD_END__
}
main() {
echo '#!/bin/sh
dir=$(mktemp -d)'
add_command ls
add_command who
add_command date
echo '
${dir}/ls
${dir}/who
${dir}/date'
echo 'rm -rf ${dir}
exit'
add_tag ls
add_tag who
add_tag date
}
main > shell.sh
#!/usr/bin/env bash
main() {
echo '#!/bin/sh
dir=$(mktemp -d)
lstart=$(awk "/^__BEGIN__/ { print NR + 1; exit; }" $0)
tail $0 -n +${lstart} > ${dir}/bin
chmod +x ${dir}/bin
${dir}/bin
rm -rf ${dir}
exit
__BEGIN__'
cat /bin/date
}
main > shell.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment