Skip to content

Instantly share code, notes, and snippets.

@xioustic
Created June 16, 2018 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xioustic/f488da63304043c504f58ac86c424e93 to your computer and use it in GitHub Desktop.
Save xioustic/f488da63304043c504f58ac86c424e93 to your computer and use it in GitHub Desktop.
self encrypted bash script
# gets the line number in this script of the "PAYLOAD:" line
echoPayloadLinenum() {
echo $(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
}
# prints only the payload
echoPayload() {
local payload_linenum=$(echoPayloadLinenum)
local crypted_linenum=$((payload_linenum + 1))
tail -n +$crypted_linenum $0
}
# prints this script without the payload
echoSelf() {
payload_linenum=$(echoPayloadLinenum)
head -n $payload_linenum $0
}
# echoes the decrypted payload after user types in password
echoDecryptedPayload() {
PAYLOAD=$(echoPayload)
echo "$PAYLOAD" | gpg -d
}
# executes the decrypted payload after user types in password
runDecryptedPayload() {
SCRIPT=$(echoDecryptedPayload)
echo "$SCRIPT" | sh -
}
if [[ $* == *--encrypt* ]]; then
echo "encrypting new payload"
# clear payload
echo "$(echoSelf)" > $0
gpg -c -a >> $0
exit 0
else
runDecryptedPayload
exit 0
fi
PAYLOAD:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment