find "NEW"−typef∣whileread−rfile;doiffile−ib"file" | grep -q "x-mach-binary; charset=binary"; then
echo "$file"
INSTALL_NAME=$(otool -D "$file" | grep -v -e ":$" -e "^Archive :" | head -n1)
otool -L "$file" | tail -n +2 | grep -e /System | grep /Library/'[^/]'\*.dylib | cut -d' ' -f1 | tr -d "[:blank:]" > "$OLD"/._lib_cache
if [ -n "$INSTALL_NAME" ]; then
install_name_tool -id @rpath/"$(basename "$INSTALL_NAME")" "$file" >/dev/null 2>&1
fi
if otool -L "$file" | grep -q CydiaSubstrate; then
install_name_tool -change /Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate @rpath/libsubstrate.dylib "$file" >/dev/null 2>&1
fi
if [ -f "$OLD"/._lib_cache ]; then
cat "$OLD"/._lib_cache | while read line; do
install_name_tool -change "$line" @rpath/"$(basename "$line")" "$file" >/dev/null 2>&1
done
fi
install_name_tool -add_rpath "/var/jb/Library/Frameworks" "$file" >/dev/null 2>&1
install_name_tool -add_rpath "/var/jb/usr/lib" "$file" >/dev/null 2>&1
Xina Patching Start
#XinaPatch Binaries
sed -i 's#\x00/Library/MobileSub#\x00/var/LIY/MobileSub#g' "$file"
sed -i 's#\x00/Library/Sn#\x00/var/LIY/Sn#g' "$file"
sed -i 's#\x00/Library/Th#\x00/var/LIY/Th#g' "$file"
sed -i 's#\x00/Library/Application Support#\x00/var/LIY/Application Support#g' "$file"
sed -i 's#\x00/Library/LaunchD#\x00/var/LIY/LaunchD#g' "$file"
sed -i 's#\x00/Library/PreferenceB#\x00/var/LIY/PreferenceB#g' "$file"
sed -i 's#\x00/Library/PreferenceL#\x00/var/LIY/PreferenceL#g' "$file"
sed -i 's#\x00/Library/Frameworks#\x00/var/LIY/Frameworks#g' "$file"
sed -i 's#\x00/bin/sh#\x00/var/sh#g' "$file"
sed -i 's#\x00/usr/lib#\x00/var/lib#g' "$file"
sed -i 's#\x00/usr/bin#\x00/var/bin#g' "$file"
#Revert Exceptions
sed -i 's#\x00/var/lib/libobjc.A.dylib#\x00/usr/lib/libobjc.A.dylib#g' "$file"
sed -i 's#\x00/var/lib/libc++.1.dylib#\x00/usr/lib/libc++.1.dylib#g' "$file"
sed -i 's#\x00/var/lib/libSystem.B.dylib#\x00/usr/lib/libSystem.B.dylib#g' "$file"
sed -i 's#\x00/var/lib/libstdc++.6.dylib#\x00/usr/lib/libstdc++.6.dylib#g' "$file"
sed -i 's#\x00/var/lib/libMobileGestalt.dylib#\x00/usr/lib/libMobileGestalt.dylib#g' "$file"
ldid -S "$file"
#Patch .plist files
elif basename "file" | grep -q ".plist"; then
sed -i 's#>/Applications/#>/var/jb/Applications/#g' "file"
sed -i 's#>/Library/i#>/var/LIY/i#g' "file"
sed -i 's#>/usr/share/#>/var/share/#g' "file"
sed -i 's#>/usr/bin/#>/var/bin/#g' "file"
sed -i 's#>/usr/lib/#>/var/lib/#g' "file"
sed -i 's#>/usr/sbin/#>/var/sbin/#g' "file"
sed -i 's#>/usr/libexec/#>/var/libexec/#g' "file"
sed -i 's#>/usr/#>/var/jb/usr/#g' "file"
sed -i 's#>/etc/#>/var/etc/#g' "file"
sed -i 's#>/bin/#>/var/bin/#g' "file"
sed -i 's#>/Library/#>/var/LIY/#g' "file"
fi
done
#Patch /DEBIAN preinst, postinst, prerm, postrm
find "NEW"/DEBIAN−typef∣whileread−rfile;doifbasename"file" | grep -q "preinst|postinst|prerm|postrm"; then
sed -i 's# /Applications/# /var/jb/Applications/#g' "file"
sed -i 's# /Library/i# /var/LIY/i#g' "file"
sed -i 's# /usr/share/# /var/share/#g' "file"
sed -i 's# /usr/bin/# /var/bin/#g' "file"
sed -i 's# /usr/lib/# /var/lib/#g' "file"
sed -i 's# /usr/sbin/# /var/sbin/#g' "file"
sed -i 's# /usr/libexec/# /var/libexec/#g' "file"
sed -i 's# /usr/# /var/jb/usr/#g' "file"
sed -i 's# /etc/# /var/etc/#g' "file"
sed -i 's# /bin/# /var/bin/#g' "file"
sed -i 's# /Library/# /var/LIY/#g' "$file"
fi
done
Certainly! Here's an improved version of your script for converting rootful iOS packages to rootless ones. I've added comments and made several enhancements for better reliability and readability.
#!/bin/bash
set -e
# Check for required tools
if ! command -v dpkg-deb >/dev/null 2>&1; then
echo "Please install 'dpkg-deb'."
exit 1
fi
if ! command -v file >/dev/null 2>&1; then
echo "Please install 'file' from Bingner or Procursus."
exit 1
fi
if ! command -v otool >/dev/null 2>&1; then
echo "Please install 'odcctools'."
exit 1
fi
if ! ldid 2>&1 | grep -q 'sha1\|sha2'; then
echo "Please install 'ldid' (must have -H option)."
exit 1
fi
LDID="ldid -Hsha256"
# Create temporary directories
OLD="$(mktemp -qd)"
NEW="$(mktemp -qd)"
if [ ! -d "$OLD" ] || [ ! -d "$NEW" ]; then
echo "Creating temporary directories failed."
exit 1
fi
### Script start
# Check if the input DEB file is provided
if [ $# -lt 1 ]; then
echo "Usage: $0 <deb_file>"
exit 1
fi
DEB_FILE="$1"
# Extract the DEB package
dpkg-deb -R "$DEB_FILE" "$OLD"
# Check if the package is already rootless
if [ -d "$OLD/var/jb" ]; then
echo "Deb already rootless. Skipping and exiting cleanly."
rm -rf "$OLD" "$NEW"
exit 0
fi
# Prepare the new package structure
mkdir -p "$NEW/var/jb"
cp -a "$OLD/DEBIAN" "$NEW/DEBIAN"
# Modify the control file
CONTROL_FILE="$NEW/DEBIAN/control"
# Optionally, append '-rootless' to the package name
# Uncomment the following line if desired
# sed -i '/^Package: / s/$/-rootless/' "$CONTROL_FILE"
# Modify the Name field to indicate rootless
sed -i '/^Name: / s/$/ (Xinafied-rootless)/' "$CONTROL_FILE"
# Update the architecture if needed
sed -i 's|iphoneos-arm|iphoneos-arm64|' "$CONTROL_FILE"
# Move the package contents to the new rootless path
rm -rf "$OLD/DEBIAN"
rsync -a "$OLD"/ "$NEW/var/jb/"
#### Overall Patching Start
find "$NEW" -type f | while read -r file; do
if file -ib "$file" | grep -q "x-mach-binary; charset=binary"; then
# Handle Mach-O binaries
# Get the install name
INSTALL_NAME=$(otool -D "$file" | grep -v -e ":$" -e "^Archive :" | head -n1)
# Get linked libraries from /System/Library
otool -L "$file" | tail -n +2 | grep -E '/System/Library/[^ ]+\.dylib' | awk '{print $1}' > "$OLD/._lib_cache"
# Update the install name
if [ -n "$INSTALL_NAME" ]; then
install_name_tool -id "@rpath/$(basename "$INSTALL_NAME")" "$file" >/dev/null 2>&1
fi
# Update CydiaSubstrate linkage
if otool -L "$file" | grep -q "CydiaSubstrate"; then
install_name_tool -change "/Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate" "@rpath/libsubstrate.dylib" "$file" >/dev/null 2>&1
fi
# Update library paths
if [ -f "$OLD/._lib_cache" ]; then
while read -r line; do
install_name_tool -change "$line" "@rpath/$(basename "$line")" "$file" >/dev/null 2>&1
done < "$OLD/._lib_cache"
fi
# Add rpath entries
install_name_tool -add_rpath "/var/jb/Library/Frameworks" "$file" >/dev/null 2>&1 || true
install_name_tool -add_rpath "/var/jb/usr/lib" "$file" >/dev/null 2>&1 || true
### Xina Patching Start
# Patch binary paths
sed -i -e 's|\x00/Library/MobileSub|\x00/var/LIY/MobileSub|g' \
-e 's|\x00/Library/Sn|\x00/var/LIY/Sn|g' \
-e 's|\x00/Library/Th|\x00/var/LIY/Th|g' \
-e 's|\x00/Library/Application Support|\x00/var/LIY/Application Support|g' \
-e 's|\x00/Library/LaunchD|\x00/var/LIY/LaunchD|g' \
-e 's|\x00/Library/PreferenceB|\x00/var/LIY/PreferenceB|g' \
-e 's|\x00/Library/PreferenceL|\x00/var/LIY/PreferenceL|g' \
-e 's|\x00/Library/Frameworks|\x00/var/LIY/Frameworks|g' \
-e 's|\x00/bin/sh|\x00/var/sh|g' \
-e 's|\x00/usr/lib|\x00/var/lib|g' \
-e 's|\x00/usr/bin|\x00/var/bin|g' \
"$file"
# Revert exceptions for specific libraries
sed -i -e 's|\x00/var/lib/libobjc.A.dylib|\x00/usr/lib/libobjc.A.dylib|g' \
-e 's|\x00/var/lib/libc++.1.dylib|\x00/usr/lib/libc++.1.dylib|g' \
-e 's|\x00/var/lib/libSystem.B.dylib|\x00/usr/lib/libSystem.B.dylib|g' \
-e 's|\x00/var/lib/libstdc++.6.dylib|\x00/usr/lib/libstdc++.6.dylib|g' \
-e 's|\x00/var/lib/libMobileGestalt.dylib|\x00/usr/lib/libMobileGestalt.dylib|g' \
"$file"
# Re-sign the binary
$LDID -S "$file"
elif [[ "$(basename "$file")" =~ \.plist$ ]]; then
# Patch .plist files
sed -i -e 's|>/Applications/|>/var/jb/Applications/|g' \
-e 's|>/Library/i|>/var/LIY/i|g' \
-e 's|>/usr/share/|>/var/share/|g' \
-e 's|>/usr/bin/|>/var/bin/|g' \
-e 's|>/usr/lib/|>/var/lib/|g' \
-e 's|>/usr/sbin/|>/var/sbin/|g' \
-e 's|>/usr/libexec/|>/var/libexec/|g' \
-e 's|>/usr/|>/var/jb/usr/|g' \
-e 's|>/etc/|>/var/etc/|g' \
-e 's|>/bin/|>/var/bin/|g' \
-e 's|>/Library/|>/var/LIY/|g' \
"$file"
fi
done
# Patch DEBIAN scripts (preinst, postinst, prerm, postrm)
for script in preinst postinst prerm postrm; do
if [ -f "$NEW/DEBIAN/$script" ]; then
sed -i -e 's| /Applications/| /var/jb/Applications/|g' \
-e 's| /Library/i| /var/LIY/i|g' \
-e 's| /usr/share/| /var/share/|g' \
-e 's| /usr/bin/| /var/bin/|g' \
-e 's| /usr/lib/| /var/lib/|g' \
-e 's| /usr/sbin/| /var/sbin/|g' \
-e 's| /usr/libexec/| /var/libexec/|g' \
-e 's| /usr/| /var/jb/usr/|g' \
-e 's| /etc/| /var/etc/|g' \
-e 's| /bin/| /var/bin/|g' \
-e 's| /Library/| /var/LIY/|g' \
"$NEW/DEBIAN/$script"
fi
done
#### Overall Patching End
# Build the new DEB package
PACKAGE_NAME=$(grep '^Package:' "$CONTROL_FILE" | cut -d' ' -f2)
VERSION=$(grep '^Version:' "$CONTROL_FILE" | cut -d' ' -f2)
ARCHITECTURE=$(grep '^Architecture:' "$CONTROL_FILE" | cut -d' ' -f2)
OUTPUT_DEB="$(pwd)/${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb"
dpkg-deb -b "$NEW" "$OUTPUT_DEB"
echo "Converted package created at: $OUTPUT_DEB"
### Clean up temporary directories
rm -rf "$OLD" "$NEW"
Improvements Made:
Path Corrections in sed Commands:
Updated the sed commands to modify the control file in the correct directory ($NEW/DEBIAN/control instead of $OLD/DEBIAN/control).
Ensured that path modifications in the package's scripts and binaries are accurate and do not overwrite previous changes.
Robust File Moving with rsync:
Replaced the mv command with rsync to properly handle hidden files (dotfiles) and avoid issues with moving . and .. directories.
This ensures all files, including hidden ones, are moved correctly to the new package structure.
Enhanced Error Checking:
Added checks to verify that required utilities are installed before proceeding.
The script now exits with an error message if a required tool is missing.
Improved Handling of DEBIAN Scripts:
Added a loop to patch all DEBIAN scripts (preinst, postinst, prerm, postrm) if they exist.
This ensures that any scripts included in the package are appropriately modified for rootless paths.
Consistent Use of Variables:
Defined CONTROL_FILE for clarity and consistency when modifying the control file.
Extracted package information (PACKAGE_NAME, VERSION, ARCHITECTURE) using variables for easy reference.
Added Usage Information:
Included a usage message that prompts the user if they forget to provide the DEB file as an argument.
Simplified find Conditions:
Used regex with bash's [[ ]] for checking .plist files, improving readability.
Replaced Deprecated Commands:
Replaced type with command -v for checking the availability of commands.
Included Informative Output:
Notes:
Optional Package Name Modification: The line that appends -rootless to the package name is commented out. Uncomment it if you wish to modify the package name to reflect that it's rootless.
Use of LDID: The LDID variable is used to re-sign binaries after modification. Ensure that ldid is properly installed and supports the -H option.
Safety Checks: The script uses set -e to exit immediately if any command exits with a non-zero status, preventing potential issues from proceeding unnoticed.
Assumptions:
The script assumes that the input DEB file is valid and that the control file contains the necessary fields (Package, Version, Architecture).
Make sure to adjust the paths and sed expressions if your environment differs.
Final Steps: The last command (/var/jb/basebin/jbctl rebuild_trustcache) from the original script was omitted. If required, you can execute this command manually after running the script.
This improved script should provide a more reliable and maintainable way to convert rootful iOS packages to rootless ones compatible with XinaA15 or similar rootless jailbreaks.