forked from haxi0/Derootifier
-
Notifications
You must be signed in to change notification settings - Fork 11
/
patch.sh
executable file
·339 lines (282 loc) · 11.9 KB
/
patch.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
set -e
shopt -s dotglob
LDID="ldid -Hsha256"
ECHO="echo -e"
FIND="find"
SED="sed"
if [ "$(sw_vers -productName)" == "macOS" ]; then
export TMPDIR=$(dirname "$1")
LOG() { $ECHO "$@\n"; }
SED="gsed"
FIND="gfind"
else
LOG() { return; }
#LOG() { $ECHO "$@\n"; }
export TMPDIR=/var/mobile/RootHidePatcher
fi
LOG "TMPDIR=$TMPDIR"
if [ $(whoami) != "root" ]; then
$ECHO "Please run as root user (sudo)."
exit 1;
fi
if ! type dpkg-deb >/dev/null 2>&1; then
$ECHO "Please install dpkg-deb."
exit 1;
fi
if ! type file >/dev/null 2>&1; then
$ECHO "Please install file."
exit 1;
fi
if ! type awk >/dev/null 2>&1; then
$ECHO "Please install awk."
exit 1;
fi
if ! ldid 2>&1 | grep -q procursus; then
$ECHO "Please install Procursus ldid."
exit 1;
fi
if [ -z "$1" ]; then
$ECHO "Usage: $0 /path/to/deb [/path/to/output] [DynamicPatches|AutoPatches]"
exit 1;
fi
if ! file "$1" | grep -q "Debian binary package" ; then
$ECHO "*** Not a valid package!"
exit 1;
fi
$ECHO "creating workspace..."
debfname=$(basename "$1")
TEMPDIR_OLD=$(mktemp -d "$TMPDIR/$debfname.old.XXXXXX")
TEMPDIR_NEW=$(mktemp -d "$TMPDIR/$debfname.new.XXXXXX")
chmod 0755 "$TEMPDIR_OLD" "$TEMPDIR_NEW"
if [ ! -d "$TEMPDIR_OLD" ] || [ ! -d "$TEMPDIR_NEW" ]; then
$ECHO "*** Creating temporary directories failed!\n"
exit 1;
fi
### Real script start
dpkg-deb -R "$1" "$TEMPDIR_OLD"
chmod -R 755 "$TEMPDIR_OLD"/DEBIAN
chmod 644 "$TEMPDIR_OLD"/DEBIAN/control
DEB_PACKAGE=$(grep '^Package:' "$TEMPDIR_OLD"/DEBIAN/control | cut -f2 -d ' ' | tr -d '\n\r')
DEB_VERSION=$(grep '^Version:' "$TEMPDIR_OLD"/DEBIAN/control | cut -f2 -d ' ' | tr -d '\n\r')
DEB_ARCH=$(grep '^Architecture:' "$TEMPDIR_OLD"/DEBIAN/control | cut -f2 -d ' ' | tr -d '\n\r')
DEB_MAINTAINER=$(grep '^Maintainer:' "$TEMPDIR_OLD"/DEBIAN/control | $SED -E 's/^Maintainer:\s*//' | tr -d '\n\r')
# not tweaks
if [[ {ellekit,oldabi} =~ "$DEB_PACKAGE" ]] || [ "$DEB_MAINTAINER" == "Procursus Team <[email protected]>" ]; then
$ECHO "*** Not a tweak package!\ncontact @RootHideDev to update it.\n\nskipping and exiting cleanly."
rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
exit 1;
elif [[ {"xyz.cypwn.xinam1ne","app.legizmo.moonstone","lunotech11.legizmo.lighthouse","lunotech11.legizmo.kincaid","lunotech11.legizmo.jupiter","lunotech11.legizmo.grace"} =~ "$DEB_PACKAGE" ]]; then
$ECHO "*** This package is not compatible, please contact its developer to update it.\n\nskipping and exiting cleanly."
rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
exit 1;
fi
OUTPUT_PATH="$TMPDIR/$DEB_PACKAGE"_"$DEB_VERSION"_"iphoneos-arm64e".deb
if [ ! -z "$2" ]; then OUTPUT_PATH=$2; fi;
### Derootifier Script ##################################
Derootifier() {
mv -f "$TEMPDIR_OLD"/* "$TEMPDIR_NEW"/
find "$TEMPDIR_NEW" -type f -size +0c \! -path "*/var/mobile/Library/pkgmirror/*" \! -path "*.lproj/*" \! -path "*.png" \! -path "*.svg" \! -path "*.strings" \! -path "*.lua" | while read -r file; do
fname=$(basename "$file")
fpath=/$(realpath --relative-base="$TEMPDIR_NEW" "$file")
ftype=$(file -b "$file")
if echo $ftype | grep -q "Mach-O"; then
$ECHO "=> $fpath"
$ECHO -n "patch..."
otool -L "$file" | tail -n +2 | cut -d' ' -f1 | tr -d "[:blank:]" > "$TEMPDIR_OLD"/._lib_cache
if [ -f "$TEMPDIR_OLD"/._lib_cache ]; then
cat "$TEMPDIR_OLD"/._lib_cache | while read line; do
if echo "$line" | grep -q ^/usr/lib/ ; then
install_name_tool -change "$line" @rpath/"${line#/usr/lib/}" "$file"
elif echo "$line" | grep -q ^/Library/Frameworks/ ; then
install_name_tool -change "$line" @rpath/"${line#/Library/Frameworks/}" "$file"
fi
done
fi
install_name_tool -add_rpath "/usr/lib" "$file"
install_name_tool -add_rpath "@loader_path/.jbroot/usr/lib" "$file"
install_name_tool -add_rpath "/Library/Frameworks" "$file" >/dev/null
install_name_tool -add_rpath "@loader_path/.jbroot/Library/Frameworks" "$file"
$ECHO -n "resign..."
if echo $ftype | grep -q "executable"; then
$LDID -M "-S$(dirname $(realpath $0))/roothide.entitlements" "$file"
else
$LDID -S "$file"
fi
$ECHO "~ok."
fi
done
$SED -i '/^$/d' "$TEMPDIR_NEW"/DEBIAN/control
$SED -i 's|iphoneos-arm|iphoneos-arm64e|g' "$TEMPDIR_NEW"/DEBIAN/control
find "$TEMPDIR_NEW" -name ".DS_Store" -delete
dpkg-deb -Zzstd -b "$TEMPDIR_NEW" "$OUTPUT_PATH"
chown 501:501 "$OUTPUT_PATH"
### Real script end
$ECHO "\nfinished. cleaning up..."
if [ "$(sw_vers -productName)" != "macOS" ]; then
rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
rm -f $1
fi
}
####################################################################
if [ $DEB_ARCH == "iphoneos-arm" ] && [ -z "$3" ]; then
Derootifier $@
exit 0
elif [ $DEB_ARCH != "iphoneos-arm64" ]; then
$ECHO "$DEB_ARCH\n*** Not a rootless package!\n\nskipping and exiting cleanly."
rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
exit 1;
fi
mv -f "$TEMPDIR_OLD"/DEBIAN "$TEMPDIR_NEW"/
#dpkg-deb -c "$1" > "$TEMPDIR_NEW"/DEBIAN/list
#$SED -i -E 's|^\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+||g' "$TEMPDIR_NEW"/DEBIAN/list
#$SED -i -E 's|^(\.\/)?|/|g' "$TEMPDIR_NEW"/DEBIAN/list
#$SED -i -E '1s|^\/$|/.|' "$TEMPDIR_NEW"/DEBIAN/list
#
#if [ ! -e "$TEMPDIR_NEW"/DEBIAN/md5sums ]; then
# cd "$TEMPDIR_OLD"
# eval md5sum $($FIND "$TEMPDIR_OLD" -type f -printf "\"%P\"\n") > "$TEMPDIR_NEW"/DEBIAN/md5sums #E2BIG on ios
# cd -
#fi
if [ -d "$TEMPDIR_OLD/var/jb" ]; then
subitemcount=$(ls -A "$TEMPDIR_OLD/var/jb")
if [ -n "$subitemcount" ]; then mv -f "$TEMPDIR_OLD"/var/jb/* "$TEMPDIR_NEW"/ ; fi
rmdir "$TEMPDIR_OLD"/var/jb
fi
rmdir "$TEMPDIR_OLD"/var >/dev/null 2>&1 || true
rootfsfiles=$(ls "$TEMPDIR_OLD")
if [ ! -z "$rootfsfiles" ]; then
mkdir "$TEMPDIR_NEW"/rootfs
mv -f "$TEMPDIR_OLD"/* "$TEMPDIR_NEW"/rootfs/
fi
# some packages have both /var/jb/var/xxx and /var/xxx, same file same name
if [ ! -z "$3" ]; then
mkdir -p "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror
rsync -a "$TEMPDIR_NEW"/ "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/ --exclude /var/mobile/Library/pkgmirror
mv "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/DEBIAN "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/DEBIAN.$DEB_PACKAGE
# append after "Package" : "Status: install ok installed" > "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/DEBIAN.$DEB_PACKAGE/control
fi
#rm -f "$TEMPDIR_NEW"/DEBIAN/list "$TEMPDIR_NEW"/DEBIAN/md5sums
lsrpath() {
otool -l "$@" |
awk '
/^[^ ]/ {f = 0}
$2 == "LC_RPATH" && $1 == "cmd" {f = 1}
f && gsub(/^ *path | \(offset [0-9]+\)$/, "") == 2
' | sort | uniq
}
find "$TEMPDIR_NEW" -type f -size +0c \! -path "*/var/mobile/Library/pkgmirror/*" \! -path "*.lproj/*" \! -path "*.png" \! -path "*.svg" \! -path "*.strings" \! -path "*.lua" | while read -r file; do
LOG "$file"
fixedpaths=""
fname=$(basename "$file")
fpath=/$(realpath --relative-base="$TEMPDIR_NEW" "$file")
ftype=$(file -b "$file")
if echo $ftype | grep -q "Mach-O"; then
$ECHO "=> $fpath"
$ECHO -n "patch..."
lsrpath "$file" | while read line; do
if [[ $line == /var/jb/* ]]; then
newpath=${line/\/var\/jb\//@loader_path\/.jbroot\/}
LOG "change rpath" "$line" "$newpath"
install_name_tool -rpath "$line" "$newpath" "$file"
fi
done
otool -L "$file" | tail -n +2 | cut -d' ' -f1 | tr -d "[:blank:]" | while read line; do
if [[ $line == /var/jb/* ]]; then
newlib=${line/\/var\/jb\//@loader_path\/.jbroot\/}
LOG "change library" "$line" "$newlib"
install_name_tool -change "$line" "$newlib" "$file"
fi
done
$ECHO -n "resign..."
if echo $ftype | grep -q "executable"; then
$LDID -M "-S$(dirname $(realpath $0))/roothide.entitlements" "$file"
else
$LDID -S "$file"
fi
$ECHO "~ok."
fixedpaths=$(strings - "$file" | grep /var/jb || true)
if [ "$3" == "AutoPatches" ]; then
ln -s /usr/lib/DynamicPatches/AutoPatches.dylib "$file".roothidepatch
fi
elif ! [[ {png,strings} =~ "${fname##*.}" ]]; then
if [[ {preinst,prerm,postinst,postrm,extrainst_} =~ "$fname" ]]; then
$SED -i 's|iphoneos-arm64|iphoneos-arm64e|g' "$file"
$SED -i 's|/var/jb/|/-var/jb/-|g' "$file"
$SED -i 's|/var/jb|/-var/jb-|g' "$file"
$SED -i 's| /Applications/| /rootfs/Applications/|g' "$file"
$SED -i 's| /Library/| /rootfs/Library/|g' "$file"
$SED -i 's| /private/| /rootfs/private/|g' "$file"
$SED -i 's| /System/| /rootfs/System/|g' "$file"
$SED -i 's| /sbin/| /rootfs/sbin/|g' "$file"
$SED -i 's| /bin/| /rootfs/bin/|g' "$file"
$SED -i 's| /etc/| /rootfs/etc/|g' "$file"
$SED -i 's| /lib/| /rootfs/lib/|g' "$file"
$SED -i 's| /usr/| /rootfs/usr/|g' "$file"
$SED -i 's| /var/| /rootfs/var/|g' "$file"
$SED -i '1s|^#!\s*\/rootfs\/|#! \/|' "$file" #revert shebang
$SED -i 's|/-var/jb/-|/|g' "$file"
$SED -i 's|/-var/jb-|/var/jb|g' "$file"
fi
if [ "${fname##*.}" == "plist" ]; then
plutil -convert xml1 "$file" >/dev/null
if [[ {/Library/LaunchDaemons} =~ $(dirname "$fpath") ]]; then
$SED -i 's|/var/jb/|/|g' "$file"
elif [[ {/Library/libSandy} =~ $(dirname "$fpath") ]]; then
$SED -i 's|/var/jb/|/-var/jb/-|g' "$file"
$SED -i 's|/var/jb|/-var/jb-|g' "$file"
$SED -i 's|>/<|>/rootfs/<|g' "$file"
$SED -i 's|>/Applications/|>/rootfs/Applications/|g' "$file"
$SED -i 's|>/Library/|>/rootfs/Library/|g' "$file"
$SED -i 's|>/private/|>/rootfs/private/|g' "$file"
$SED -i 's|>/System/|>/rootfs/System/|g' "$file"
$SED -i 's|>/sbin/|>/rootfs/sbin/|g' "$file"
$SED -i 's|>/bin/|>/rootfs/bin/|g' "$file"
$SED -i 's|>/etc/|>/rootfs/etc/|g' "$file"
$SED -i 's|>/lib/|>/rootfs/lib/|g' "$file"
$SED -i 's|>/usr/|>/rootfs/usr/|g' "$file"
$SED -i 's|>/var/|>/rootfs/var/|g' "$file"
$SED -i 's|/-var/jb/-|/|g' "$file"
$SED -i 's|/-var/jb-|/var/jb|g' "$file"
fi
fi
fixedpaths=$(strings - "$file" | grep /var/jb || true)
if [ ! -z "$fixedpaths" ]; then
$ECHO "=> $fpath"
fi
fi
if [ ! -z "$fixedpaths" ]; then
$ECHO "*****fixed-paths-warnning*****\n$fixedpaths\n*******************************\n"
fi
done
if [ ! -z "$3" ]; then
cp "$TEMPDIR_NEW"/DEBIAN/*.roothidepatch "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/DEBIAN.$DEB_PACKAGE/ >/dev/null 2>&1 || true
chown -R 501:501 "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/
chmod -R 0755 "$TEMPDIR_NEW"/var/mobile/Library/pkgmirror/
fi
$SED -i '/^$/d' "$TEMPDIR_NEW"/DEBIAN/control
$SED -i 's|iphoneos-arm64|iphoneos-arm64e|g' "$TEMPDIR_NEW"/DEBIAN/control
$SED -i '/^Conflicts: /s/roothide/r-o-o-t-l-e-s-s-/g' "$TEMPDIR_NEW"/DEBIAN/control
if [ "$3" == "AutoPatches" ]; then
PreDepends="rootless-compat(>= 0.9)"
elif [ "$3" == "DynamicPatches" ]; then
$SED -i "/^Version\:/d" "$TEMPDIR_NEW"/DEBIAN/control
echo "Version: $DEB_VERSION~roothide" >> "$TEMPDIR_NEW"/DEBIAN/control
PreDepends="patches-$DEB_PACKAGE(= $DEB_VERSION~roothide)"
fi
if [ "$PreDepends" != "" ]; then
if grep -q '^Pre-Depends:' "$TEMPDIR_NEW"/DEBIAN/control; then
$SED -i "s/^Pre-Depends\:/Pre-Depends: $PreDepends,/" "$TEMPDIR_NEW"/DEBIAN/control
else
echo "Pre-Depends: $PreDepends" >> "$TEMPDIR_NEW"/DEBIAN/control
fi
fi
find "$TEMPDIR_NEW" -name ".DS_Store" -delete
dpkg-deb -Zzstd -b "$TEMPDIR_NEW" "$OUTPUT_PATH"
chown 501:501 "$OUTPUT_PATH"
### Real script end
$ECHO "\nfinished. cleaning up..."
if [ "$(sw_vers -productName)" != "macOS" ]; then
rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
rm -f $1
fi