Skip to content

Instantly share code, notes, and snippets.

@evrardjp
Forked from ShoGinn/Arduino_amiibo_tool.bash
Created June 7, 2017 12:19
Show Gist options
  • Save evrardjp/c7b23ddde509f44365ccddb34a8c6a43 to your computer and use it in GitHub Desktop.
Save evrardjp/c7b23ddde509f44365ccddb34a8c6a43 to your computer and use it in GitHub Desktop.
Amiibo Companion Tool -- See Arduino Sketches https://github.com/konstantin-kelemen/arduino-amiibo-tools
#!/bin/bash
# This is a companion script to https://github.com/konstantin-kelemen/arduino-amiibo-tools
# The original post this was crafted for was https://games.kel.mn/en/create-amiibo-clones-with-arduino/
# For more info go to https://games.kel.mn/en/companion-script-to-simplify-amiibo-cloning-with-arduino/
#requirements:
#sha1sum (part of coreutils)
#xxd (part of vim)
#hexdump
#amiitool (https://github.com/socram8888/amiitool)
hash xxd 2>/dev/null || { echo >&2 "I require xxd but it's not installed. Aborting."; exit 1; }
hash sha1sum 2>/dev/null || { echo >&2 "I require sha1sum but it's not installed. Aborting."; exit 1; }
hash hexdump 2>/dev/null || { echo >&2 "I require hexdump but it's not installed. Aborting."; exit 1; }
hash ./amiitool 2>/dev/null || { echo >&2 "I 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
fi
if [ "$(sha1sum "$1" |cut -d' ' -f1)" != "bbdbb49a917d14f7a997d327ba40d40c39e606ce" ]
then
echo "key_file not sane"
exit
fi
#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 dec.bin
#modify the uid record
echo "01D4: $uid" | xxd -r - dec.bin
#add password
echo "0214: $pw1$pw2$pw3$pw4" | xxd -r - dec.bin #pw
echo "0218: 8080" | xxd -r - dec.bin
#set the default values
echo "0208: 000000" | xxd -r - dec.bin
echo "0000: $BCC1" | xxd -r - dec.bin
echo "0002: 0000" | xxd -r - dec.bin
#reencrypt the uid modified dump
echo Using Amiibo Tool to encrypt ${2%%.*}
./amiitool -e -k "$1" -i dec.bin -o enc.bin
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