2021-02-15 19:34:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -Eeuo pipefail
|
|
|
|
trap on_error SIGINT SIGTERM ERR
|
|
|
|
|
|
|
|
ScriptFullname=$(readlink -e "$0")
|
|
|
|
ScriptName=$(basename "$0")
|
|
|
|
TargetFile=""
|
2021-02-23 16:32:35 +00:00
|
|
|
BackupFile=""
|
2021-02-15 19:34:20 +00:00
|
|
|
|
2021-04-15 22:36:46 +00:00
|
|
|
RxSetGameUnranked='KFGameInfo\w+SetGameUnranked'
|
|
|
|
RxValidateForXP='KFGameInfo\w+ValidateForXP'
|
|
|
|
|
|
|
|
LoadAddr=''
|
|
|
|
|
2021-02-15 19:34:20 +00:00
|
|
|
function show_help ()
|
|
|
|
{
|
|
|
|
echo "Usage:"
|
2021-02-23 16:32:35 +00:00
|
|
|
echo "${ScriptName} FILENAME [BACKUPNAME] patch specified kf2 executable"
|
|
|
|
echo "${ScriptName} -h, --help show help"
|
2021-02-15 19:34:20 +00:00
|
|
|
echo ""
|
|
|
|
echo "Dependencies: dd, readelf, hexdump"
|
|
|
|
}
|
|
|
|
|
|
|
|
function on_error ()
|
|
|
|
{
|
|
|
|
trap - SIGINT SIGTERM ERR
|
|
|
|
echo "Process aborted."
|
|
|
|
restore
|
|
|
|
}
|
|
|
|
|
|
|
|
function backup ()
|
|
|
|
{
|
2021-02-23 16:32:35 +00:00
|
|
|
if [[ -z "$BackupFile" ]]; then
|
|
|
|
BackupFile="$TargetFile.orig"
|
|
|
|
fi
|
|
|
|
cp -f "$TargetFile" "$BackupFile"
|
2021-02-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function restore ()
|
|
|
|
{
|
2021-02-23 16:32:35 +00:00
|
|
|
if [[ -z "$BackupFile" ]]; then
|
|
|
|
BackupFile="$TargetFile.orig"
|
|
|
|
fi
|
|
|
|
if [[ -e "$BackupFile" ]]; then
|
|
|
|
mv -f "$BackupFile" "$TargetFile"
|
2021-02-15 19:34:20 +00:00
|
|
|
echo "Original executable restored"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-04-15 22:36:46 +00:00
|
|
|
function set_load_addr ()
|
2021-02-15 19:34:20 +00:00
|
|
|
{
|
2021-04-15 22:36:46 +00:00
|
|
|
LoadAddr=$(
|
2021-02-15 19:34:20 +00:00
|
|
|
readelf -l -W "$TargetFile" | \
|
|
|
|
grep -P 'LOAD\s+0x000000' | \
|
|
|
|
awk '{ print $3 }' | \
|
|
|
|
grep -Po '[1-9a-f][0-9a-f]+')
|
|
|
|
}
|
|
|
|
|
2021-04-15 22:36:46 +00:00
|
|
|
function target_addr () # $1: TargetRegexp
|
2021-02-15 19:34:20 +00:00
|
|
|
{
|
2021-04-15 22:36:46 +00:00
|
|
|
local Offset=$(readelf -s -W "$TargetFile" | \
|
|
|
|
grep -P "$1" | \
|
2021-02-15 19:34:20 +00:00
|
|
|
grep -v 'exec' | \
|
|
|
|
head -n 1 | \
|
|
|
|
awk '{ print $2 }' | \
|
2021-04-15 22:36:46 +00:00
|
|
|
grep -Po '[1-9a-f][0-9a-f]+')
|
|
|
|
echo $((16#$Offset - 16#$LoadAddr))
|
2021-02-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function patched ()
|
|
|
|
{
|
2021-04-15 22:36:46 +00:00
|
|
|
local ControlHexSGU=$(
|
2021-02-15 19:34:20 +00:00
|
|
|
hexdump -ve '1/1 "%.2x"' \
|
2021-04-15 22:36:46 +00:00
|
|
|
-s $(target_addr "$RxSetGameUnranked") \
|
2021-02-15 19:34:20 +00:00
|
|
|
-n 1 "$TargetFile")
|
2021-04-15 22:36:46 +00:00
|
|
|
local ControlHexVFX=$(
|
|
|
|
hexdump -ve '1/1 "%.2x"' \
|
|
|
|
-s $(target_addr "$RxValidateForXP") \
|
|
|
|
-n 6 "$TargetFile")
|
|
|
|
test "$ControlHexSGU" == "c3" && test "$ControlHexVFX" == "b801000000c3"
|
2021-02-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function apply_patch ()
|
|
|
|
{
|
|
|
|
printf '\xc3' | \
|
|
|
|
dd of="$TargetFile" \
|
|
|
|
bs=1 \
|
2021-04-15 22:36:46 +00:00
|
|
|
seek=$(target_addr "$RxSetGameUnranked") \
|
2021-02-15 19:34:20 +00:00
|
|
|
count=1 \
|
2021-04-15 22:36:46 +00:00
|
|
|
conv=notrunc
|
|
|
|
|
|
|
|
printf '\xb8\x01\x00\x00\x00\xc3' | \
|
|
|
|
dd of="$TargetFile" \
|
|
|
|
bs=1 \
|
|
|
|
seek=$(target_addr "$RxValidateForXP") \
|
|
|
|
count=6 \
|
|
|
|
conv=notrunc
|
|
|
|
|
2021-02-15 19:34:20 +00:00
|
|
|
echo "Successfully patched!"
|
|
|
|
}
|
|
|
|
|
|
|
|
function main ()
|
|
|
|
{
|
|
|
|
if ! [[ -w "$TargetFile" ]]; then
|
|
|
|
echo "Can't write file. Make sure the file exists and it's read/write accessible."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-04-15 22:36:46 +00:00
|
|
|
set_load_addr
|
|
|
|
|
2021-02-15 19:34:20 +00:00
|
|
|
if patched; then
|
|
|
|
echo "File already patched - skip."
|
|
|
|
exit 0
|
|
|
|
fi
|
2021-04-15 22:36:46 +00:00
|
|
|
|
2021-02-15 19:34:20 +00:00
|
|
|
backup && apply_patch
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ $# -eq 0 ]]; then show_help; exit 0; fi
|
|
|
|
case $1 in
|
|
|
|
-h|--help ) show_help ; ;;
|
2021-04-15 22:36:46 +00:00
|
|
|
* ) TargetFile="$1"; BackupFile="${2-}"; main ;;
|
2021-02-15 19:34:20 +00:00
|
|
|
esac
|