This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge

IOS underlying principles + reverse article summary

This article mainly introduces application re-signing through shell scripts

Supplement knowledge

Before re-signing using shell scripts, you need to understand the following knowledge: Common script commands + permissions

Execute related commands

  • The Shell is a special interactive tool that gives users a way to start programs, manage files in the file system, and manage processes running on the system.

  • A Shell is generally a command line tool. It allows you to enter text commands, then interpret the commands and execute them in the kernel.

  • Shell script is a script file that is pre-placed into a text file with various commands for one-time execution.

Switch bash and ZSH

  • chsh -s /bin/bash

  • chsh -s /bin/zsh

Common Script Commands

  • 1, $source FileName

    • Meaning: reads and executes the command in FileName in the current shell environment

    • Features:

      • Commands can force a script to immediately affect the current environment (typically used to load configuration files).

      • The command forces all commands in the script to be executed, ignoring file permissions.

    • For example, source 123.sh will go to the shell directory and stay in the directory after the script is executed

  • 2. ‘bashFileName’ + ‘bashFileName’ + ‘bashFileName’ + ZSH FileName

    • What it means: to recreate a subshell that executes the sentences in the script.

    • For example, ZSH /bash 123.sh — does not enter the directory and stays in the current directory

<! --123.sh contains --> mkdir shell CD shell touch 123.txtCopy the code
  • 3, $./FileName
    • Meaning: To read and execute commands in a file. But there is oneThe premise, script fileYou need to have executable rights.
    • ./ — Indicates the current directory
    • Such as:
      • ./123.sh — Cannot be executed
      • View permission: ls -l 123.sh
      • chmod +x 123.sh: Grants the executable permission

      • ./123.sh: Executes shell files and stays in the current directory

See how many shells your MAC has

  • cd /private/etc

  • cat shells

bash: macOS default shell, only in macOS Serial

-csh: replaced by TCSH -dash: Default shell for Linux -ksh: compatible with bash -sh: replaced by bash -tcsh: entire C shell - 'ZSH' : Now the default shell for MacOSCopy the code

Users, groups, and permissions

Unix and Linux are both multi-user, multi-task systems, so they have the concept of users and groups. Then the same file permissions also have corresponding users and groups.

The directory structure

  • Windows in the tray
  • The Mac/: Let everyone have a piece of their own independent area to do things

/home: Yes User –/usr: Is a system resource

  • The Mac file properties are as follows (Note: Control + L — clear the screen)

File types and permissions

  • Permissions are10A character,File type +3 file permissions(Its position does not change, if there is a minus sign (-) in the corresponding position, it indicates that this permission is not available)
  • For example,drwxr-xr-x
    • d: File type (dDirectory,-File)
    • rwx: Permission of the file owner
    • r-x: Permissions of other users in this group
    • r-x: Indicates the permission of a user not in this group

Change the permissions

To change file permissions, run the chmod command. There are two methods of setting: number type change and symbol type change.

  • [user]+[group]+[other]

  • [read] + [write] + [execute]

  • Number type:

– Comparison of different authority numbers:r:4 w:2 x:1(1, 1 moves 1 bit to the left, 1 moves 2 bits to the left)r: 0100w: 0010x: 0001 – If a file permission is [-rwxr-xr-x] -user: 4+2+1 = 7 -group: 4+0+1 = 5 -other: 4+0+1 = 5 – Command:Chmod 755 File name(777 — RWXRWXRWX)

symbols

  • Command structure: chmod [u, g, o, a][+(add), -(remove), =(set)] [r, w, x] File name, where -u: indicates user

    - 'g' : indicates group - 'o' : indicates other - 'a' : indicates allCopy the code
  • For example, chmod a+x 123.txt

  • Default: chmod +x 123.x

Method 3: Shell script

In contrast to the coDesign and Xcode resignations in iOS Reverse 10: App Resignations (1) above, we can also use shell scripts directly for automatic resignations

Shell re-signing demonstration

The preparatory work

  • Configure the shell in the project:Build Phase -> + Run Script, can be inCompile time execution
    • . /Represent project catalog
    • SRCROOT(Environment variable) — the root directory

Shell re-signing demonstration

  • 1. Create the shell file touch 123.sh in the root directory

  • 2. Add the execution permission to chmod +x 123.sh

  • 3. Run the 123.sh:./123.sh command in Run Script

The re-signature script in 123.sh is as follows:

  • 1. Decompress IPA to Temp

  • 2. Copy the extracted. App into the project

  • Delete Extension and WatchAPP because individual certificates cannot sign Extention

  • 4. Update the CFBundleIdentifier file in info.plist

  • 5. Give execute permission to mach-o files

  • 6, re-signed third-party FrameWorks

  • 7. Replace the signature

${SRCROOT}/Temp = ${SRCROOT}/Temp = ${SRCROOT} TARGET_IPA_PATH="${ASSETS_PATH}/*. Ipa "# Empty the Temp folder rm -rf "${SRCROOT}/Temp" mkdir -p "${SRCROOT}/Temp" #---------------------------------------- # 1. Unzip -oqq "$TARGET_IPA_PATH" -d "$TEMP_PATH" =$(set -- "$TEMP_PATH/Payload/"*.app; Echo "$1") # echo path is: $TEMP_APP_PATH "# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # 2. # BUILT_PRODUCTS_DIR # TARGET_NAME target name of app package TARGET_APP_PATH="$BUILT_PRODUCTS_DIR/$target_name. app" echo "App path :$TARGET_APP_PATH" rm -rf "$TARGET_APP_PATH" mkdir -p "$TARGET_APP_PATH" cp -rf "$TEMP_APP_PATH/" "$TARGET_APP_PATH" #---------------------------------------- # 3. Rm -rf "$TARGET_APP_PATH/PlugIns" rm -rf "$TARGET_APP_PATH/Watch" # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # 4. Update info.plist file CFBundleIdentifier # Set :"Set: # PlistBuddy change the BundleID in info.plist to BundleID /usr/libexec/plistbuddy -c "Set :CFBundleIdentifier $PRODUCT_BUNDLE_IDENTIFIER" "$TARGET_APP_PATH/Info.plist" #---------------------------------------- # 5. To execute permissions on MachO file # to MachO file path APP_BINARY = ` plutil - convert xml1 - o - $TARGET_APP_PATH/Info. The plist | grep - A1 Exec | tail - n1 | the cut - f2 - d \ > | the cut - f1 - d \ < ` # on executable permissions chmod + x "$TARGET_APP_PATH / $APP_BINARY" # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # 6. TARGET_APP_FRAMEWORKS_PATH="$TARGET_APP_PATH/ FrameWorks "if [-d "$TARGET_APP_FRAMEWORKS_PATH"]; Then for FRAMEWORK in "$TARGET_APP_FRAMEWORKS_PATH/"* do # signature #--force --sign replace signature # EXPANDED_CODE_SIGN_IDENTITY Certificate of current project /usr/bin/codesign --force --sign "$EXPANDED_CODE_SIGN_IDENTITY" "$FRAMEWORK" done fiCopy the code

conclusion

  • Common Shell script commands

    • 1. Source FileName: The command in FileName is read and executed. The current environment is affected immediately and the command stays in the directory after the script is executed

    • 2. Bash/ZSH FileName: re-create a subshell in which the command in FileName is executed and stays in the current directory

    • 3../FileName: reads and executes the command in FileName, provided that./FileName must have executable permission (that is, x).

  • File Types and Permissions (File types +3 groups of file permissions)

    • Type:
      • dDirectory:
      • -: file
    • Jurisdiction:
      • Group 1: permissions of the file owner

      • Group 2: Permissions for other users in this group

      • Group 3: Permissions of users not in this group

    • To change permissions, run the chmod command
      • Number type changeFor example, chmod 755 File name
        • r:4 w:2 x:1
      • Sign type changeFor example, chmod a+x 123.txt
        • U: indicates user

        • G: indicates group

        • O: other

        • A: indicates all

  • Step for re-signing shell scripts:

    • 1. Decompress IPA to Temp

    • 2. Copy the extracted. App into the project

    • Delete Extension and WatchAPP because individual certificates cannot sign Extention

    • 4. Update the CFBundleIdentifier file in info.plist

    • 5. Give execute permission to mach-o files

    • 6, re-signed third-party FrameWorks

    • 7. Replace the signature