shzip

10th October 2016 at 12:06am
Bash CodeSnippets

Small script to compress and obfuscate your shell scripts as base 64.

#!/bin/bash

main () {
  if [[ $# -ne 1 ]] || ! [[ -f "$1" ]] ; then
    >&2 echo "Syntax: ${0##*/} <script>"
    return 64
  fi
  declare -x src="$1"
  declare -x shbang="$(head -1 "$src" | grep -o '^#!.*')"
  shbang="${shbang:-#!/bin/bash}"
  cat <<FOO
$shbang
eval "\$(base64 -d << 'EOF' | zcat
$(gzip -c "$src" | base64)
EOF
)"
FOO
}

main "$@"

Example of the script in use, compressing itself:

nicolaw@host:~$ shzip 
Syntax: shzip <script>
nicolaw@host:~$ shzip bin/shzip 
#!/bin/bash
eval "$(base64 -d << 'EOF' | zcat
H4sICM/W+lcAA3NoemlwAF2Qy07DMBBF9/6KG9siSSUTghCLEio2dJsFy1Ikx3EeUnErJ6DSJP/O
hCIqsbE812fmaCyCpGhdUuiuYexdtw5RjIEBbYXNBlJAOYsU2y3GEcGcqQpcpnyOHtA31hENrK5u
YU2zB3/5cr0+LiGHGyEWyYSsM7499Cv+A3rbf3iHlIqqpaO0Zqe9hTqi8+ZxHv0vbQrtanqIGqtL
qJT0RHKMqL09QO0RvongehHGc+cfPpxvSyUuO04zYXSPLFvnOZNnhNlPvQN/lREx9v4OqiQA4XO+
DslyogYmo/rUksxc7Gc4ZoQxUs8Dp99P5PKJM/YNSRLNo10BAAA=
EOF
)"
nicolaw@host:~$ shzip bin/shzip > compressed_script.sh
nicolaw@host:~$ bash compressed_script.sh 
Syntax: compressed_script.sh <script>
nicolaw@host:~$ 

Related