Skip to content

Instantly share code, notes, and snippets.

@Doridian
Last active December 4, 2023 07:06
Show Gist options
  • Select an option

  • Save Doridian/2fd40a50f40752b68edf4240c9775685 to your computer and use it in GitHub Desktop.

Select an option

Save Doridian/2fd40a50f40752b68edf4240c9775685 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eu
make_aten_header() {
printf 'ATEN\1\0'
}
run_aes() {
openssl aes-256-cbc "$1" -in "$2" -out "$3" -k CKSAM1SUCKSAM1SUASMUCIKSASMUCIKS -md md5
}
run_des() {
openssl des-ede3-cbc "$1" -in "$2" -out "$3" -K F1DA33A298120612060792FFAA998811998877445588AABB -iv 0000000000000000
}
run_crc32() {
# Checksum computed on contents (https://stackoverflow.com/questions/44804668/how-to-calculate-crc32-checksum-from-a-string-on-linux-bash)
gzip -1 -c | tail -c8 | head -c4
}
to_hex() {
hexdump -e '"%u"'
}
decrypt() {
TMP1="$(mktemp)"
# Decrypt outer shell
run_des -d "$1" "$TMP1"
# Checksum from the file
CSUM_READ="$(cat "$TMP1" | head -c4 | to_hex)"
CSUM_CALCULATED="$(dd if="$TMP1" skip=4 bs=1 status=none | run_crc32 | to_hex)"
if [ "$CSUM_READ" != "$CSUM_CALCULATED" ]
then
rm -f "$TMP1"
echo "Invalid CRC32 on backup! (0x$CSUM_READ != 0x$CSUM_CALCULATED)"
exit 1
fi
# Decrypt inner contents (the known ATEN\1\0 header before a .tar.gz)
TMP2="$(mktemp)"
dd if="$TMP1" skip=4 bs=1 status=none | run_aes -d /dev/stdin "$TMP2"
rm -f "$TMP1"
unset TMP1
HEADER_READ="$(dd if="$TMP2" count=6 bs=1 status=none)"
HEADER_GOOD="$(make_aten_header)"
if [ "$HEADER_READ" != "$HEADER_GOOD" ]
then
rm -f "$TMP2"
echo 'Invalid ATEN header'
exit 1
fi
# Strip ATEN header and print
dd if="$TMP2" of="$2" skip=6 bs=1 status=none
# rm temp files
rm -f "$TMP2"
echo 'Decryption OK'
}
encrypt() {
TMP1="$(mktemp)"
TMP2="$(mktemp)"
# Add ATEN header
make_aten_header > "$TMP1"
cat "$1" >> "$TMP1"
# AES encrypt (inner encryption)
run_aes -e "$TMP1" "$TMP2"
# Add CRC32 to DES source
cat "$TMP2" | run_crc32 > "$TMP1"
# Append AES data after CRC32
cat "$TMP2" >> "$TMP1"
# DES encrypt the whole thing
run_des -e "$TMP1" "$2"
# rm temp files
rm -f "$TMP1" "$TMP2"
echo 'Encryption OK'
}
CMD="$1"
shift 1
case "$CMD" in
-e)
encrypt "$@"
;;
-d)
decrypt "$@"
;;
*)
echo 'Usage ./superdec.sh -e/-d INPUT OUTPUT'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment