Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.