Skip to content

Instantly share code, notes, and snippets.

@aveao
Forked from gretel/amiibo.sh
Last active April 14, 2021 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aveao/eecd321b2a1801d4c6ff0ef7e7b6d835 to your computer and use it in GitHub Desktop.
Save aveao/eecd321b2a1801d4c6ff0ef7e7b6d835 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This is a script to convert encrypted amiibos to a format that can be used by chamtool and be recognized by switch
# bash amiibo.sh key_retail.bin ~/myownamiibodumps/tomnook.bin 04BA1234123412
# chamtool.py -p /dev/ttyACM0 -u /tmp/out.bin
# This is a fork of https://gist.github.com/gretel/dd80c854e22c2afd20f5aebc62015096
#requirements:
#sha1sum (part of coreutils)
#xxd (part of vim)
#hexdump
#amiitool (https://github.com/socram8888/amiitool)
hash xxd 2>/dev/null || { echo >&2 "require xxd but it's not installed. Aborting."; exit 1; }
hash sha1sum 2>/dev/null || { echo >&2 "require sha1sum but it's not installed. Aborting."; exit 1; }
hash hexdump 2>/dev/null || { echo >&2 "require hexdump but it's not installed. Aborting."; exit 1; }
hash amiitool 2>/dev/null || { echo >&2 "require amiitool but it's not installed or in the currect directory. Aborting."; exit 1; }
if [ $# -ne 3 ]
then
echo "usage: $0 key_file encrypted_dump_file blank_tagid"
exit 1
fi
if [ "$(sha1sum "$1" |cut -d' ' -f1)" != "bbdbb49a917d14f7a997d327ba40d40c39e606ce" ]
then
echo "key_file not sane"
exit 1
fi
base="${base%.*}"
#get the empty tag uid:
taguid="$3"
taguid0="$(echo "$taguid" | cut -b1,2)" # Byte 0 (should bx 0x04)
taguid1="$(echo "$taguid" | cut -b3,4)" # Byte 1 (we count from 0)
taguid2="$(echo "$taguid" | cut -b5,6)" # Byte 2
if [ ${#3} -eq 18 ]; then # Check if user provided a long taguid
taguid3="$(echo "$taguid" | cut -b9,10)" # Byte 4
taguid4="$(echo "$taguid" | cut -b11,12)" # Byte 5
taguid5="$(echo "$taguid" | cut -b13,14)" # Byte 6
taguid6="$(echo "$taguid" | cut -b15,16)" # Byte 7
uid="$(echo "$taguid" | cut -b1-16)"
BCC1="$(echo "$taguid" | cut -b17,18)" # Pull out the BCC1 for use later
elif [ ${#3} -eq 14 ]; then # Check if user provided a short taguid
taguid3="$(echo "$taguid" | cut -b7,8)" # Byte 3
taguid4="$(echo "$taguid" | cut -b9,10)" # Byte 4
taguid5="$(echo "$taguid" | cut -b11,12)" # Byte 5
taguid6="$(echo "$taguid" | cut -b13,14)" # Byte 6
# Convert 7byte to 9byte for script
BCC0="$(printf '%02X\n' $(( 0x88 ^ 0x$taguid0 ^ 0x$taguid1 ^ 0x$taguid2 )))" # Calculate the BCC0
BCC1="$(printf '%02X\n' $(( 0x$taguid3 ^ 0x$taguid4 ^ 0x$taguid5 ^ 0x$taguid6 )))" # Calculate the BCC1
uid="$taguid0$taguid1$taguid2$BCC0$taguid3$taguid4$taguid5$taguid6"
fi
if [ ${#uid} -ne 16 ]; then
echo "please pick a valid 7 or 9 byte uid"
exit
fi
# Generate the password from the tag
pw1="$(printf '%02X\n' $(( 0xAA ^ 0x$taguid1 ^ 0x$taguid3 )))"
pw2="$(printf '%02X\n' $(( 0x55 ^ 0x$taguid2 ^ 0x$taguid4 )))"
pw3="$(printf '%02X\n' $(( 0xAA ^ 0x$taguid3 ^ 0x$taguid5 )))"
pw4="$(printf '%02X\n' $(( 0x55 ^ 0x$taguid4 ^ 0x$taguid6 )))"
#decrypt the dump
#echo Using Amiibo Tool to decrypt ${2%%.*}
amiitool -d -k "$1" -i "$2" -o "${base}_dec.bin" || exit 2
#modify the uid record
echo "01D4: $uid" | xxd -r - "${base}_dec.bin"
#add password
echo "0214: $pw1$pw2$pw3$pw4" | xxd -r - "${base}_dec.bin" #pw
echo "0218: 8080" | xxd -r - "${base}_dec.bin"
#set the default values
# echo "0208: 000000" | xxd -r - ${base}_dec.bin
echo "0000: $BCC1" | xxd -r - "${base}_dec.bin"
# echo "0002: 0000" | xxd -r - ${base}_dec.bin
# enc_file="${base}_${uid}.bin"
enc_file="/tmp/out.bin"
#reencrypt the uid modified dump
#echo Using Amiibo Tool to encrypt ${2%%.*}
amiitool -e -k "$1" -i "${base}_dec.bin" -o "${enc_file}" || exit 3
rm "${base}_dec.bin"
echo "${enc_file}"
# echo "**** START OF HEXDUMP ****"
# hexdump -v -e " 4/1 \"0x%02X, \" \"\n\"" "enc.bin" > hexdump
# truncate -s -2 hexdump
# echo "" >> hexdump
# echo "" >> hexdump
# cat hexdump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment