#!/bin/sh set -euo pipefail if [ $# -ne 1 ]; then printf 'usage: %s \n' "$(basename "$0")" 1>&2 exit 1 fi einfo() { printf ' \033[32;1m*\033[m %s\n' "$1" } eerror() { printf ' \033[31;1m*\033[m %s\n' "$1" } ebegin() { printf ' \033[32;1m*\033[m %s ...' "$1" } eend() { if [ "$1" -eq 0 ]; then printf '\033[1000C\033[5D\033[34;1m[ \033[32mok \033[34m]\033[m\n' else printf '\n \033[31;1m*\033[m %s\033[1000C\033[5D\033[34;1m[ \033[31m!! \033[34m]\033[m\n' "$2" fi return "$1" } if [ -r "$1.bak" -a ! -r "$1" ]; then einfo "Seems like the script broke last time you ran it :(" ebegin "Restoring the backup file to its original location" if ! mv -n "$1.bak" "$1"; then eend 1 "Couldn't move the file" fi eend 0 fi if [ -e "$1.bak" ]; then eerror 'Backed-up file already exists, it seems likely you already ran the' eerror 'patcher? If not (or you really want to run it again, which may' eerror 'break things), please rename the file:' eerror " $1.bak" eerror 'to something else.' exit 1 fi if [ ! -r "$1" ]; then eerror "File $1 does not exist or is not a regular readable file" exit 1 fi mv "$1" "$1.bak" ORIGINAL="$1.bak" TARGET="$1" extract-original-rsa() { xxd -ps -c 0 "$1" | grep -Eo '3c00520053004100.{1628}6c00750065003e00' | head -n1 | sed -E 's/([a-z0-9]{2})00/\1/g' | xxd -r -p } a2utf16() { sed -E 's/([A-Za-z0-9]{2})/\100/g' } ebegin 'Extracting original RSA key' ORIGINAL_RSA="$(extract-original-rsa "$ORIGINAL")" NEW_RSA='s9/kwnOq8bfUr8NfN7LN6y+e+crm+Ik2qkYqTt8LZ23j1o7LnBtIjWPRehEXguPn9tqNZ/XrBKGRtrUqBP3FpiqI9/7EfKvBUO+H3Q0imdnL8eYF70CGQInx2HQCAEDPiLPphmsEXxjLWVbVNbdrPz2GNMm63mX2SNq9VcT9AZNpmmpOR1YOTHZ4SVnZbNqZ2Y0P6b3g3yR/cm6lxJxHYKCGtC+nmBWWaHhUMn87bNXhPHu4MPuPNn147dS6iEJKEECJLjXKmXc71J4BD1giw/TmYPkkIM+Y509M/1/RNDSIEn4pw4WWBvtg2MXzPyGvimzuCjvJUX4ljap239F2pw==AQAB' if [ "$(printf '%s' "$ORIGINAL_RSA" | wc -c)" -ne "$(printf '%s' "$NEW_RSA" | wc -c)" ]; then eend 1 'Original and new RSA keys are not the same length' fi eend 0 ebegin 'Checking server domains' ORIGINAL_DOMAIN='auth3.vintagestory.at' NEW_DOMAIN='vsauth.xn--80andq.net' if [ "$(printf '%s' "$ORIGINAL_DOMAIN" | wc -c)" -ne "$(printf '%s' "$NEW_DOMAIN" | wc -c)" ]; then eend 1 'Original and new domains are not the same length' fi eend 0 einfo 'Preparing patch values' ORSA="$(printf '%s' "$ORIGINAL_RSA" | xxd -p -c 0 | a2utf16)" NRSA="$(printf '%s' "$NEW_RSA" | xxd -p -c 0 | a2utf16)" ODOMAIN="$(printf '%s' "$ORIGINAL_DOMAIN" | xxd -p -c 0 | a2utf16)" NDOMAIN="$(printf '%s' "$NEW_DOMAIN" | xxd -p -c 0 | a2utf16)" ebegin 'Patching' if xxd -p -c 0 "$ORIGINAL" | sed "s/$ORSA/$NRSA/g;s/$ODOMAIN/$NDOMAIN/g" | xxd -r -p > "$TARGET"; then eend 0 else eend $? 'Patch failed :(' fi ebegin 'Verifying' if [ "$(wc -c < "$ORIGINAL")" -ne "$(wc -c < "$TARGET")" ]; then eend 1 'Original and patched files are not the same size' fi eend 0 einfo 'All done! ^w^'