-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpatch
More file actions
executable file
·87 lines (63 loc) · 2.01 KB
/
patch
File metadata and controls
executable file
·87 lines (63 loc) · 2.01 KB
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
#!/usr/bin/env bash
DIR="$( cd "$( dirname $(readlink -f "${BASH_SOURCE[0]}" ) )" >/dev/null && pwd )"
# Check the listing file exists before doing anything
if [[ ! -f "${DIR}/support/listing" ]]; then
echo "[!] Listing file doesn't exist, run init first please" >&2
exit 1
fi
binary=
libc_ver=
usage() {
echo "Usage: $0 -f <binary_to_patch> -l <libc_to_patch>" >&2
exit 1
}
while getopts ":f:l:" opt; do
case "$opt" in
f)
binary="${OPTARG}"
;;
l)
libc_ver="${OPTARG}"
;;
*)
usage
;;
esac
done
if [[ -z "${binary}" ]] || [[ -z "${libc_ver}" ]]; then
usage
fi
deb_url=$(grep "${libc_ver}" "${DIR}/support/listing")
if [[ -z "${deb_url}" ]]; then
echo "[!] Failed to find a relevant .deb" >&2
exit 1
elif [[ ! $(echo -n "${deb_url}" | grep -c '.deb') == "1" ]]; then
echo "[!] Got multiple candidates for .deb, candidates:" >&2
echo "${deb_url}"
exit 1
fi
echo "[+] Found .deb @ ${deb_url}, downloading into libc-patch/"
mkdir -p libc-patch
rm -rf libc-patch/*
curl -Sf# "${deb_url}" > libc-patch/${libc_ver}.deb
if [[ ! $? == "0" ]]; then
echo "[!] Failed to download deb" >&2
exit 1
fi
curl -Sf# "$(echo ${deb_url} | sed 's/libc6/libc6-dbg/')" > libc-patch/${libc_ver}.dbg.deb
if [[ ! $? == "0" ]]; then
echo "[!] Failed to download debug deb" >&2
exit 1
fi
pushd libc-patch > /dev/null 2>&1
echo "[+] Extracting debs"
dpkg-deb --extract ${libc_ver}.deb .
dpkg-deb --extract ${libc_ver}.dbg.deb .
echo "[+] Extracted debs, readding debug symbols.."
# Liberal use of find for max ceebs
eu-unstrip $(find lib -name "libc-*.so") $(find usr -name "libc-*.so") -o $(find lib -name "libc-*.so")
popd > /dev/null 2>&1
echo "[+] Creating backup and patching binary"
cp -v $binary "${binary}-patched"
patchelf --set-interpreter $(find libc-patch/lib -name "ld-*.so") \
--set-rpath $(dirname $(find libc-patch/lib -name "ld-*.so")) "${binary}-patched" && echo "[+] Binary patched"