-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkf2-ranked-patch
executable file
·124 lines (105 loc) · 2.36 KB
/
kf2-ranked-patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
set -Eeuo pipefail
trap on_error SIGINT SIGTERM ERR
ScriptFullname=$(readlink -e "$0")
ScriptName=$(basename "$0")
TargetFile=""
BackupFile=""
RxSetGameUnranked='KFGameInfo\w+SetGameUnranked'
RxValidateForXP='KFGameInfo\w+ValidateForXP'
LoadAddr=''
function show_help ()
{
echo "Usage:"
echo "${ScriptName} FILENAME [BACKUPNAME] patch specified kf2 executable"
echo "${ScriptName} -h, --help show help"
echo ""
echo "Dependencies: dd, readelf, hexdump"
}
function on_error ()
{
trap - SIGINT SIGTERM ERR
echo "Process aborted."
restore
}
function backup ()
{
if [[ -z "$BackupFile" ]]; then
BackupFile="$TargetFile.orig"
fi
cp -f "$TargetFile" "$BackupFile"
}
function restore ()
{
if [[ -z "$BackupFile" ]]; then
BackupFile="$TargetFile.orig"
fi
if [[ -e "$BackupFile" ]]; then
mv -f "$BackupFile" "$TargetFile"
echo "Original executable restored"
fi
}
function set_load_addr ()
{
LoadAddr=$(
readelf -l -W "$TargetFile" | \
grep -P 'LOAD\s+0x000000' | \
awk '{ print $3 }' | \
grep -Po '[1-9a-f][0-9a-f]+')
}
function target_addr () # $1: TargetRegexp
{
local Offset=$(readelf -s -W "$TargetFile" | \
grep -P "$1" | \
grep -v 'exec' | \
head -n 1 | \
awk '{ print $2 }' | \
grep -Po '[1-9a-f][0-9a-f]+')
echo $((16#$Offset - 16#$LoadAddr))
}
function patched ()
{
local ControlHexSGU=$(
hexdump -ve '1/1 "%.2x"' \
-s $(target_addr "$RxSetGameUnranked") \
-n 1 "$TargetFile")
local ControlHexVFX=$(
hexdump -ve '1/1 "%.2x"' \
-s $(target_addr "$RxValidateForXP") \
-n 6 "$TargetFile")
test "$ControlHexSGU" == "c3" && test "$ControlHexVFX" == "b801000000c3"
}
function apply_patch ()
{
printf '\xc3' | \
dd of="$TargetFile" \
bs=1 \
seek=$(target_addr "$RxSetGameUnranked") \
count=1 \
conv=notrunc
printf '\xb8\x01\x00\x00\x00\xc3' | \
dd of="$TargetFile" \
bs=1 \
seek=$(target_addr "$RxValidateForXP") \
count=6 \
conv=notrunc
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
set_load_addr
if patched; then
echo "File already patched - skip."
exit 0
fi
backup && apply_patch
}
if [[ $# -eq 0 ]]; then show_help; exit 0; fi
case $1 in
-h|--help ) show_help ; ;;
* ) TargetFile="$1"; BackupFile="${2-}"; main ;;
esac