Skip to content

Instantly share code, notes, and snippets.

@wenliabc2007
Forked from Teutates/repack-rootless.sh
Created March 30, 2024 08:22
Show Gist options
  • Save wenliabc2007/6d181563cdaaa7f8ab612166e99b9e7f to your computer and use it in GitHub Desktop.
Save wenliabc2007/6d181563cdaaa7f8ab612166e99b9e7f to your computer and use it in GitHub Desktop.

Revisions

  1. @Teutates Teutates revised this gist Dec 27, 2022. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion repack-rootless.sh
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,9 @@ if [ -d "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" ]; then
    mv "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" "$TEMPDIR_NEW/var/jb/usr/lib/TweakInject"
    fi

    for file in $(find "$TEMPDIR_NEW" -type f -exec sh -c "file -ib '{}' | grep -q 'x-mach-binary; charset=binary'" \; -print); do
    find "$TEMPDIR_NEW" -type f | while read -r file; do
    if file -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 /usr/lib/'[^/]'\*.dylib | cut -d' ' -f1 | tr -d "[:blank:]" > "$TEMPDIR_OLD"/._lib_cache
    if [ -n "$INSTALL_NAME" ]; then
    @@ -79,6 +81,7 @@ for file in $(find "$TEMPDIR_NEW" -type f -exec sh -c "file -ib '{}' | grep -q '
    install_name_tool -add_rpath "/var/jb/usr/lib" "$file" >/dev/null 2>&1

    $LDID -s "$file"
    fi
    done

    fakeroot dpkg-deb -b "$TEMPDIR_NEW" "$(pwd)"/"$(grep Package: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Version: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Architecture: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')".deb
  2. @Teutates Teutates revised this gist Dec 23, 2022. 1 changed file with 20 additions and 4 deletions.
    24 changes: 20 additions & 4 deletions repack-rootless.sh
    Original file line number Diff line number Diff line change
    @@ -56,15 +56,31 @@ sed 's|iphoneos-arm|iphoneos-arm64|' < "$TEMPDIR_OLD"/DEBIAN/control > "$TEMPDIR
    rm -rf "$TEMPDIR_OLD"/DEBIAN
    mv -f "$TEMPDIR_OLD"/.* "$TEMPDIR_OLD"/* "$TEMPDIR_NEW"/var/jb >/dev/null 2>&1 || true

    for file in $(find "$TEMPDIR_NEW" -type f -exec sh -c "file -ib '{}' | grep -q 'x-mach-binary; charset=binary'" \; -print); do
    $LDID -s "$file"
    done

    if [ -d "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" ]; then
    mkdir -p "$TEMPDIR_NEW/var/jb/usr/lib"
    mv "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" "$TEMPDIR_NEW/var/jb/usr/lib/TweakInject"
    fi

    for file in $(find "$TEMPDIR_NEW" -type f -exec sh -c "file -ib '{}' | grep -q 'x-mach-binary; charset=binary'" \; -print); do
    INSTALL_NAME=$(otool -D "$file" | grep -v -e ":$" -e "^Archive :" | head -n1)
    otool -L "$file" | tail -n +2 | grep /usr/lib/'[^/]'\*.dylib | cut -d' ' -f1 | tr -d "[:blank:]" > "$TEMPDIR_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 "$TEMPDIR_OLD"/._lib_cache ]; then
    cat "$TEMPDIR_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 "/usr/lib" "$file" >/dev/null 2>&1
    install_name_tool -add_rpath "/var/jb/usr/lib" "$file" >/dev/null 2>&1

    $LDID -s "$file"
    done

    fakeroot dpkg-deb -b "$TEMPDIR_NEW" "$(pwd)"/"$(grep Package: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Version: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Architecture: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')".deb

    ### Real script end
  3. @Teutates Teutates revised this gist Dec 23, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions repack-rootless.sh
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,10 @@ if ! type file >/dev/null 2>&1; then
    echo "Please install file."
    fi

    if ! type fakeroot >/dev/null 2>&1; then
    echo "Please install fakeroot."
    fi

    if ! ldid 2>&1 | grep -q procursus; then
    echo "Please install Procursus ldid."
    fi
  4. @Teutates Teutates created this gist Dec 23, 2022.
    69 changes: 69 additions & 0 deletions repack-rootless.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/bin/sh

    ### Procursus 2

    ### INFO: Repacks deb as rootless with iphoneos-arm64 arch, moves legacy tweak dir to
    ### new directory, and resigns. Does not do any further modification.

    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."
    fi

    if ! ldid 2>&1 | grep -q procursus; then
    echo "Please install Procursus ldid."
    fi

    LDID="ldid -Hsha256"

    if [ -z "$1" ] || ! file "$1" | grep -q "Debian binary package" ; then
    echo "Usage: $0 [/path/to/deb]"
    exit 1;
    fi

    echo "Creating workspace"
    TEMPDIR_OLD="$(mktemp -d)"
    TEMPDIR_NEW="$(mktemp -d)"

    if [ ! -d "$TEMPDIR_OLD" ] || [ ! -d "$TEMPDIR_NEW" ]; then
    echo "Creating temporary directories failed."
    exit 1;
    fi

    ### Real script start

    dpkg-deb -R "$1" "$TEMPDIR_OLD"

    if [ -d "$TEMPDIR_OLD/var/jb" ]; then
    echo "Deb already rootless. Skipping and exiting cleanly."
    rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"
    exit 0;
    fi

    mkdir -p "$TEMPDIR_NEW"/var/jb
    cp -a "$TEMPDIR_OLD"/DEBIAN "$TEMPDIR_NEW"
    sed 's|iphoneos-arm|iphoneos-arm64|' < "$TEMPDIR_OLD"/DEBIAN/control > "$TEMPDIR_NEW"/DEBIAN/control

    rm -rf "$TEMPDIR_OLD"/DEBIAN
    mv -f "$TEMPDIR_OLD"/.* "$TEMPDIR_OLD"/* "$TEMPDIR_NEW"/var/jb >/dev/null 2>&1 || true

    for file in $(find "$TEMPDIR_NEW" -type f -exec sh -c "file -ib '{}' | grep -q 'x-mach-binary; charset=binary'" \; -print); do
    $LDID -s "$file"
    done

    if [ -d "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" ]; then
    mkdir -p "$TEMPDIR_NEW/var/jb/usr/lib"
    mv "$TEMPDIR_NEW/var/jb/Library/MobileSubstrate/DynamicLibraries" "$TEMPDIR_NEW/var/jb/usr/lib/TweakInject"
    fi

    fakeroot dpkg-deb -b "$TEMPDIR_NEW" "$(pwd)"/"$(grep Package: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Version: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')"_"$(grep Architecture: "$TEMPDIR_NEW"/DEBIAN/control | cut -f2 -d ' ')".deb

    ### Real script end

    echo "Cleaning up"
    rm -rf "$TEMPDIR_OLD" "$TEMPDIR_NEW"