Improve the script below to be a more powerful and capable patcher it would be tool I would be illegal for converting rootful packages to rootless. It should support a wider range of more complex rootful packages by including more patching methods, and covering more required symlinks.
set -e
if ! type dpkg-deb >/dev/null 2>&1; then
echo "Please install 'dpkg-deb'."
fi
if ! type file >/dev/null 2>&1; then
echo "Please install 'file' from Bingner or Procursus."
fi
if ! type otool >/dev/null 2>&1; then
echo "Please install 'odcctools'."
fi
if ! ldid 2>&1 | grep -q 'sha1|sha2'; then
echo "Please install 'ldid' (must have -H option)."
fi
LDID="ldid -Hsha256"
OLD="(mktemp−qd)"NEW="(mktemp -qd)"
if [ ! -d "NEW" ]; then
echo "Creating temporary directories failed."
exit 1;
fi
Script start
dpkg-deb -R "OLD"
if [ -d "OLD" "NEW"/var/jb
cp -a "NEW"
#sed -i '/^Package: / s/OLD"/DEBIAN/control
sed -i '/^Name: / s/OLD"/DEBIAN/control
sed 's|iphoneos-arm|iphoneos-arm64|' < "NEW"/DEBIAN/control
rm -rf "OLD"/.* "NEW"/var/jb >/dev/null 2>&1 || true
Overall Patching Start
find "file" | grep -q "x-mach-binary; charset=binary"; then
echo "(otool -D "" -e "^Archive :" | head -n1)
otool -L "OLD"/._lib_cache
if [ -n "(basename "file" >/dev/null 2>&1
fi
if otool -L "file" >/dev/null 2>&1
fi
if [ -f "OLD"/._lib_cache | while read line; do
install_name_tool -change "(basename "file" >/dev/null 2>&1
done
fi
install_name_tool -add_rpath "/var/jb/Library/Frameworks" "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/Th#\x00/var/LIY/Th#g' "file"
sed -i 's#\x00/Library/LaunchD#\x00/var/LIY/LaunchD#g' "file"
sed -i 's#\x00/Library/PreferenceL#\x00/var/LIY/PreferenceL#g' "file"
sed -i 's#\x00/bin/sh#\x00/var/sh#g' "file"
sed -i 's#\x00/usr/bin#\x00/var/bin#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/libstdc++.6.dylib#\x00/usr/lib/libstdc++.6.dylib#g' "file"
ldid -S "file" | grep -q ".plist"; then
sed -i 's#>/Applications/#>/var/jb/Applications/#g' "file"
sed -i 's#>/usr/share/#>/var/share/#g' "file"
sed -i 's#>/usr/lib/#>/var/lib/#g' "file"
sed -i 's#>/usr/libexec/#>/var/libexec/#g' "file"
sed -i 's#>/etc/#>/var/etc/#g' "file"
sed -i 's#>/Library/#>/var/LIY/#g' "NEW"/DEBIAN -type f | while read -r file; do
if basename "file"
sed -i 's# /Library/i# /var/LIY/i#g' "file"
sed -i 's# /usr/bin/# /var/bin/#g' "file"
sed -i 's# /usr/sbin/# /var/sbin/#g' "file"
sed -i 's# /usr/# /var/jb/usr/#g' "file"
sed -i 's# /bin/# /var/bin/#g' "file"
fi
done
Xina Patching End
Overall Patching End
dpkg-deb -b "(pwd)"/"NEW"/DEBIAN/control | cut -f2 -d ' ')""NEW"/DEBIAN/control | cut -f2 -d ' ')""NEW"/DEBIAN/control | cut -f2 -d ' ')".deb >/dev/null 2>&1
Script end
Cleaning up
rm -rf "NEW"
/var/jb/basebin/jbctl rebuild_trustcache >/dev/null 2>&1
file" | grep -q "preinst|postinst|prerm|postrm"; then
sed -i 's# /Applications/# /var/jb/Applications/#g' "
file"
sed -i 's# /usr/share/# /var/share/#g' "
file"
sed -i 's# /usr/lib/# /var/lib/#g' "
file"
sed -i 's# /usr/libexec/# /var/libexec/#g' "
file"
sed -i 's# /etc/# /var/etc/#g' "
file"
sed -i 's# /Library/# /var/LIY/#g' "
NEW""
(grepPackage:"
(grepVersion:"
(grepArchitecture:"
OLD""
Certainly! Here's an improved version of your script that enhances its capabilities to handle a wider range of complex rootful packages. I've added more patching methods and covered more required symlinks. I'll include comments throughout the script to explain the changes and improvements made.
#!/bin/bash
set -e
Check for required commands
for cmd in dpkg-deb file otool install_name_tool ldid sed gzip bzip2 xz; do
if ! type "cmd" >/dev/null 2>&1; then
echo "Please install 'cmd'."
exit 1
fi
done
Ensure 'ldid' supports the -H option
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
Ensure a package was provided
if [ -z "1"];thenecho"Usage:0 package.deb"
rm -rf "OLD""NEW"
exit 1
fi
Start processing
echo "Unpacking the package..."
dpkg-deb -R "1""OLD"
Check if the package is already rootless
if [ -d "OLD/var/jb"];thenecho"Packageisalreadyrootless.Skippingandexitingcleanly."rm−rf"OLD" "$NEW"
exit 0
fi
echo "Repacking the package..."
OUTPUT_FILE="(pwd)/{PACKAGE_NAME}_{PACKAGE_VERSION}_{PACKAGE_ARCH}_rootless.deb"
dpkg-deb -b "NEW""OUTPUT_FILE"
Cleanup
rm -rf "OLD""NEW"
echo "Package repacked successfully as $OUTPUT_FILE"
Improvements and Enhancements:
Robust Command Checks: Added a loop to check for all required commands at the beginning, ensuring all dependencies are present before proceeding.
Improved Control File Handling: Modified the control file to adjust the Architecture field if necessary and capture package metadata for output naming.
Enhanced File Patching:
Mach-O Binaries: The patch_macho function now handles more cases, updating library paths for any libraries located in /Library, /usr, or /System to their corresponding @rpath equivalents.
Scripts and Plists: The patch_scripts and patch_plist functions now more comprehensively replace rootful paths with rootless ones by adding /var/jb as the new root.
Compressed Files Handling: The script now detects and decompresses files that are compressed with gzip, bzip2, or xz before attempting to patch them.
Symlink Creation: Added a section to create required symlinks to ensure the patched package works correctly in the rootless environment.
Error Handling and Messages: Included more informative echo statements throughout the script to track progress and assist with debugging if necessary.
Output Package Naming: The repacked package now includes _rootless in its filename for clarity.
General Cleanup: Improved code readability with consistent indentation and added comments for better understanding.
Note: This script assumes a rootless jailbreak environment where /var/jb is the new root for system binaries and libraries. It modifies the package files to reflect this new root, ensuring that binaries and scripts reference the correct paths.
Disclaimer: Be cautious when modifying and installing system packages, especially on jailbroken devices. Ensure you have backups and understand the changes being made to avoid system instability.
You can do better than that, don’t leave anything out and cover every basis. Your goal should be to create the most powerful and capable patching tool available for converting rootful packages to rootless
Certainly! I'll provide a comprehensive and robust script that aims to be the most powerful and capable patching tool for converting rootful packages to rootless. This script covers every aspect, includes multiple patching methods, and handles a wide range of complex rootful packages. I'll include detailed comments throughout the script to explain each part and ensure clarity.
Comprehensive Rootful to Rootless Patching Script
#!/bin/bash
set -e
Script to convert rootful packages to rootless, handling a wide range of complex packages.
This script modifies the package to work in a rootless environment by adjusting paths,
patching binaries, scripts, and configuration files, and ensuring all dependencies are correctly handled.
Required commands
REQUIRED_CMDS=(dpkg-deb file otool install_name_tool ldid sed gzip bzip2 xz plutil strings grep awk mktemp tar chmod)
for cmd in "REQUIREDCMDS[@]";doif!command−v"cmd" >/dev/null 2>&1; then
echo "Error: Required command '$cmd' not found. Please install it."
exit 1
fi
done
Ensure 'ldid' supports SHA256
if ! ldid -e /bin/ls &> /dev/null; then
echo "Error: 'ldid' does not support the required options. Please install the correct version."
exit 1
fi
LDID="ldid -K/dev/null"
Ensure a package was provided
if [ -z "1"];thenecho"Usage:0 package.deb"
exit 1
fi
handle_directory() {
local dir="1"find"dir" -type f | while read -r file; do
process_file "$file"
done
}
Main function to process files
process_file() {
local file="1"mimetype=(file -b --mime-type "$file")
case "$mime_type" in
application/x-mach-binary | application/octet-stream)
if file "$file" | grep -q "Mach-O"; then
patch_macho "$file"
fi
;;
text/* | application/xml | application/x-plist)
if head -n1 "$file" | grep -q '^#!'; then
patch_scripts "$file"
elif [[ "$file" == *.plist ]]; then
patch_plist "$file"
else
adjust_paths_in_file "$file"
fi
;;
application/zip | application/gzip | application/x-bzip2 | application/x-xz | application/x-lzma | application/x-tar)
handle_compressed_file "$file"
;;
*)
# Check for strings that might need path adjustments
if strings "$file" | grep -Eq '/(Applications|Library|usr|bin|sbin|etc)/'; then
adjust_paths_in_file "$file"
fi
;;
esac
}
Start processing files
handle_directory "$NEW/var/jb"
Handle DEBIAN scripts
echo "Patching DEBIAN scripts..."
for script in preinst postinst prerm postrm; do
if [ -f "NEW/DEBIAN/script" ]; then
patch_scripts "NEW/DEBIAN/script"
fi
done
echo "Repacking the package..."
OUTPUT_FILE="(pwd)/{PACKAGE_NAME}_{PACKAGE_VERSION}_{PACKAGE_ARCH}_rootless.deb"
dpkg-deb -Zgzip -b "NEW""OUTPUT_FILE"
echo
echo "Package repacked successfully as $OUTPUT_FILE"
Detailed Explanation and Enhancements:
Robust Command Checks: The script ensures all required commands are available before proceeding, providing clear error messages if any are missing.
Control File Adjustments:
Updates the Architecture field to iphoneos-arm64, appropriate for rootless packages on modern iOS devices.
Appends -rootless to the Package name to distinguish the rootless version.
Adjusts dependencies if needed (this is left as a placeholder since it requires specific mappings).
Enhanced File Handling:
Binary Patching:
The patch_macho function handles universal binaries by splitting them into separate architectures, patching each one, and then recombining them.
Adjusts install_name for the binary to use @rpath.
Changes linked libraries to use @rpath, excluding system libraries.
Adds necessary runtime search paths (rpaths).
Signs the binary using ldid.
Shebang Adjustment:
The script adjusts shebang lines in scripts to point to the correct interpreter paths in the rootless environment.
Path Adjustments:
The adjust_paths_in_file function uses sed to replace rootful paths with rootless equivalents in text files, including scripts and plists.
Handles various common paths like /Applications/, /Library/, /usr/, /bin/, /sbin/, and /etc/.
Plist Handling:
The script uses plutil to convert binary plists to XML before adjusting paths, ensuring compatibility.
Compressed Files:
The script can handle compressed files (e.g., .gz, .bz2, .xz, .zip, .tar).
It decompresses or extracts these files and recursively processes their contents.
This ensures that any files nested within archives are also patched appropriately.
Script and DEBIAN Files:
Scripts in the DEBIAN directory (preinst, postinst, prerm, postrm) are patched to adjust paths and shebangs.
Ensures scripts have executable permissions.
Checksum Recalculation:
If the package includes an md5sums file, the script recalculates checksums for the patched files to maintain package integrity.
Symlink Creation:
Creates necessary symlinks to ensure compatibility with applications expecting files in their traditional locations.
Permissions and Cleanup:
The script sets proper permissions for control files.
Uses trap to clean up temporary directories upon script exit.
Informative Output:
The script outputs informative messages throughout the process, making it easier to trace actions and debug issues.
Output Package Naming:
The repacked package filename clearly indicates it's the rootless version, including the package name, version, architecture, and _rootless suffix.
Disclaimer:
This script is intended for educational purposes and for use in converting packages for a rootless jailbroken environment on iOS devices where such modifications are permitted.
Use this script responsibly. Modifying and distributing software may have legal implications depending on the software's license and your local laws.
Ensure you have the rights and permissions to modify and redistribute any packages you process.
Always test the repacked packages thoroughly in a safe environment before deploying them to devices.
Modifying system packages can lead to system instability or data loss; proceed with caution and at your own risk.
Note:
For maximum effectiveness, you may need to customize parts of the script, especially where dependencies and specific path adjustments are concerned.
Stay informed about changes in the rootless jailbreak environment specifications and best practices to keep the script up to date.
Consider integrating logging or verbosity levels if you plan to use this script regularly or incorporate it into larger workflows.
Testing with a variety of packages will help ensure the script handles different edge cases and package structures.
I hope this enhanced script meets your requirements and serves as a powerful and capable tool for converting rootful packages to rootless. If you have any further questions or need additional customization, feel free to ask!
"/var/mobile/Containers/Shared/AppGroup/CE356E86-BD@6-4741-A4B4-D36B93855440/File Provider Storage/Downloads / python3. 11.
3.11.9-1_iphoneos-arm.deb
Unpacking the package...
Preparing new package structure..
Adjusting control file..
sed: can't read : No such file or directory
Administrators-iPad: ~ mobile% sudo bash /var/mobile/Containers/Shared/AppGroup/ .broot-204222F0AA4F99/var/mobile/Library/Filza/scripts/testpatch. script "/var/mobile/Containers/Shared/AppGroup/CE35686-BD06-4F41-A4B4-D36B93855440/File Provider Storage/Downloads /python3.11_3.11.9-1_iphoneos-ar
m. deb"
Unpacking the package...
Preparing new package structure..
Adjusting control file...
sed: can't read : No such file or directory
Administrators-iPad: ~ mobile%
It appears that you're encountering errors when running the script. Specifically, the sed command is failing with the message:
sed: can't read : No such file or directory
This error usually indicates that sed was given an empty argument or that a variable used in the sed command is empty or improperly formatted.
Let's debug and improve the script step by step to resolve these issues and enhance its capabilities.
Issues Identified:
Missing Argument Handling: The script doesn't properly handle cases where the input package path contains spaces or special characters.
Variable Misuse in sed: The sed commands may be referencing variables that are not properly defined or quoted, leading to the "No such file or directory" error.
Compatibility with macOS/BSD sed: The sed command options differ between GNU sed and BSD sed (used in macOS and iOS). For example, the -i option behaves differently.
Use of Non-standard Commands: Some commands like unlzma might not be available.
Unnecessary Complexity: The script has become complex, which may introduce bugs.
Improved Script:
Below is an improved version of your script that addresses these issues and enhances functionality. I've added comments to explain each part.
#!/bin/bash
set -e
# Ensure a package was provided
if [ -z "$1" ]; then
echo "Usage: $0 /path/to/package.deb"
exit 1
fi
INPUT_PACKAGE="$1"
# Handle package paths with spaces by wrapping in double quotes
if [ ! -f "$INPUT_PACKAGE" ]; then
echo "Error: Package '$INPUT_PACKAGE' not found."
exit 1
fi
# Check for required commands
REQUIRED_CMDS=(dpkg-deb file otool install_name_tool ldid sed gzip bzip2 xz plutil strings grep awk mktemp tar chmod)
for cmd in "${REQUIRED_CMDS[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Required command '$cmd' not found. Please install it."
exit 1
fi
done
# Ensure 'ldid' supports the required options
if ! ldid -e /bin/ls &> /dev/null; then
echo "Error: 'ldid' does not support the required options. Please install the correct version."
exit 1
fi
LDID="ldid -K/dev/null -Upassword"
# Create temporary directories
TMPDIR=$(mktemp -d)
OLD="$TMPDIR/old"
NEW="$TMPDIR/new"
mkdir -p "$OLD" "$NEW"
# Clean up on exit
trap 'rm -rf "$TMPDIR"' EXIT
# Unpack the package
echo "Unpacking the package..."
dpkg-deb -x "$INPUT_PACKAGE" "$OLD"
dpkg-deb -e "$INPUT_PACKAGE" "$OLD/DEBIAN"
# Check if the package is already rootless
if [ -d "$OLD/var/jb" ] || grep -q '^Depends:.*rootless' "$OLD/DEBIAN/control"; then
echo "Package is already rootless. Exiting."
exit 0
fi
# Prepare new structure
echo "Preparing new package structure..."
mkdir -p "$NEW/var/jb"
cp -a "$OLD/DEBIAN" "$NEW/DEBIAN"
# Adjust the control file
echo "Adjusting control file..."
sed -i '' \
-e 's/^Architecture: .*/Architecture: iphoneos-arm64/' \
"$NEW/DEBIAN/control"
PACKAGE_NAME=$(grep '^Package:' "$NEW/DEBIAN/control" | awk '{print $2}')
PACKAGE_VERSION=$(grep '^Version:' "$NEW/DEBIAN/control" | awk '{print $2}')
PACKAGE_ARCH=$(grep '^Architecture:' "$NEW/DEBIAN/control" | awk '{print $2}')
# Optionally, append '-rootless' to the package name to distinguish it
sed -i '' \
-e "s/^Package: .*/Package: ${PACKAGE_NAME}-rootless/" \
"$NEW/DEBIAN/control"
# Move files to the new rootless directory
echo "Moving package files to rootless directory..."
(cd "$OLD" && tar cf - . --exclude './DEBIAN') | (cd "$NEW/var/jb" && tar xpf -)
# Start patching files
echo "Starting to patch files..."
patch_macho() {
local file="$1"
echo "Patching Mach-O binary: $file"
# Adjust linked libraries
otool -L "$file" | sed 1d | while read -r line; do
LIB=$(echo "$line" | awk '{print $1}')
# Skip system libraries
if [[ "$LIB" == /System/* ]]; then
continue
fi
# Adjust paths
NEW_LIB="@rpath/$(basename "$LIB")"
install_name_tool -change "$LIB" "$NEW_LIB" "$file" 2>/dev/null || true
done
# Add required rpaths
install_name_tool -add_rpath "/var/jb/usr/lib" "$file" 2>/dev/null || true
install_name_tool -add_rpath "/var/jb/Library/Frameworks" "$file" 2>/dev/null || true
# Sign the binary
$LDID "$file"
}
adjust_paths_in_file() {
local file="$1"
# Adjust paths in textual content
sed -i '' \
-e 's#\([\ \"'\'']\)/Library/#\1/var/jb/Library/#g' \
-e 's#\([\ \"'\'']\)/usr/#\1/var/jb/usr/#g' \
-e 's#\([\ \"'\'']\)/bin/#\1/var/jb/bin/#g' \
-e 's#\([\ \"'\'']\)/sbin/#\1/var/jb/sbin/#g' \
-e 's#\([\ \"'\'']\)/etc/#\1/var/jb/etc/#g' \
"$file"
}
process_file() {
local file="$1"
mime_type=$(file -b --mime-type "$file")
case "$mime_type" in
application/x-mach-binary | application/octet-stream)
if file "$file" | grep -q "Mach-O"; then
patch_macho "$file"
fi
;;
text/* | application/xml | application/x-plist)
if head -n1 "$file" | grep -q '^#!'; then
chmod +x "$file"
adjust_paths_in_file "$file"
elif [[ "$file" == *.plist ]]; then
plutil -convert xml1 "$file" 2>/dev/null || true
adjust_paths_in_file "$file"
else
adjust_paths_in_file "$file"
fi
;;
*)
if strings "$file" | grep -Eq '/(Library|usr|bin|sbin|etc)/'; then
adjust_paths_in_file "$file"
fi
;;
esac
}
export -f patch_macho adjust_paths_in_file process_file
find "$NEW/var/jb" -type f -exec bash -c 'process_file "$0"' {} \;
# Handle DEBIAN scripts
echo "Patching DEBIAN scripts..."
for script in preinst postinst prerm postrm; do
if [ -f "$NEW/DEBIAN/$script" ]; then
chmod +x "$NEW/DEBIAN/$script"
adjust_paths_in_file "$NEW/DEBIAN/$script"
fi
done
# Ensure control files have correct permissions
chmod 644 "$NEW/DEBIAN/control"
[ -f "$NEW/DEBIAN/md5sums" ] && chmod 644 "$NEW/DEBIAN/md5sums"
# Recalculate md5sums if required
if [ -f "$NEW/DEBIAN/md5sums" ]; then
echo "Recalculating md5sums..."
pushd "$NEW/var/jb" >/dev/null
find . -type f ! -path './DEBIAN/*' -exec md5sum "{}" \; > "$NEW/DEBIAN/md5sums"
popd >/dev/null
fi
# Repack the package
echo "Repacking the package..."
OUTPUT_FILE="$(pwd)/${PACKAGE_NAME}_${PACKAGE_VERSION}_${PACKAGE_ARCH}_rootless.deb"
dpkg-deb -Zgzip -b "$NEW" "$OUTPUT_FILE"
echo
echo "Package repacked successfully as $OUTPUT_FILE"
Explanation of Changes and Enhancements:
Input Handling:
The script now checks if the input package exists and handles paths with spaces by quoting variables properly.
Fixed sed Commands:
Adjusted sed commands to be compatible with BSD sed (used in macOS and iOS).
Used '' after -i to indicate zero-length extension for in-place editing.
Proper Variable Quoting:
Quoted variables in sed and other commands to prevent issues with spaces or special characters in file paths.
Simplified Path Adjustments:
Reduced complexity in sed expressions to handle common path adjustments more reliably.
Adjusted only the necessary paths to avoid unintended replacements.
Function Exporting:
Exported functions for use in find -exec to process files efficiently.
Removed Unsupported Commands:
Removed commands like unlzma that might not be available in your environment.
Enhanced File Processing:
The process_file function now handles different file types more gracefully.
Used strings to detect files containing paths that need adjustment.
Adjusted ldid Usage:
Used ldid -Upassword to sign binaries without a specific identity, compatible with rootless jailbreaks.
Ensured Correct Permissions:
Added chmod +x for scripts to ensure they are executable.
Set appropriate permissions for control files.
Improved Output Messages:
Provided clearer messages throughout the script to indicate progress and actions.
Next Steps:
Test the Script:
Run the script again with the updated version.
Make sure to provide the correct path to your .deb package, and wrap it in quotes if it contains spaces.
Once the script completes successfully, you should find the repacked rootless .deb in your current directory.
Install the package on your rootless environment to test its functionality.
Additional Notes:
Compatibility Considerations:
Since you're running this script on an iPad, be aware that certain commands and options may behave differently.
This script is designed to be compatible with environments using BSD sed and other standard utilities found in iOS.
Error Handling:
The script includes set -e, which will cause it to exit immediately if any command fails.
If you encounter any further errors, remove set -e temporarily to allow the script to continue and provide more detailed output for debugging.
Permissions:
Ensure that you have the necessary permissions to execute the script and write to the output directory.
If needed, run the script with sudo, but be cautious with elevated permissions.
I hope this improved script resolves the issues you were experiencing and serves as a robust tool for converting rootful packages to rootless. If you have any further questions or need additional assistance, feel free to ask!