其他分享
首页 > 其他分享> > iOS App 签名的原理 && App 重签名(三)

iOS App 签名的原理 && App 重签名(三)

作者:互联网

目录

iOS App 重签名 - 准备工作

iOS App 重签名 - 手动重签名

iOS App 重签名 - 使用脚本重签名

iOS App 重签名 - 使用 XCode 重签名

  1. 新建工程 CodeSignature.project
  2. 在 CodeSignature.project 的根目录下新建(IPA目录),将需要重签名的 IPA 包放到该目录下
    CodeSignature.project
  3. 根据需要,选择工程的开发者团队并设置工程的 BundleID,然后 Command + B 编译工程
    设置开发者团队和BundleID
  4. 在 XCode 工程中添加一个脚本:Target - Build Phases - 加号(+) - New Run Script Phase
    注意:
    ① 脚本将在工程编译前执行
    ② 如果是使用下图的方式 2 - 通过路径添加脚本,建议将脚本一同放到工程根目录下,此时脚本路径为:
    ${SRCROOT}/ReSignature.sh
    XCode 中添加重签名脚本
  5. 这里采用直接在 XCode 中编写 Bash 代码的方式新建脚本,脚本代码如下:
    #!/bin/bash
    # Date:         16:32 2017-06-07
    # Mail:         994923259@qq.com
    # Author:       Created by Airths
    # Function:     This script is for resigning an .IPA package in XCode
    # Version:      1.0
    # Return:       0 for success, 1 for failure
    # Description:	
    #
    # XCode Enviroment Variables:
    #   SRCROOT - 工程根目录的路径
    #   BUILT_PRODUCTS_DIR - 工程编译生成的 .app 包的路径
    #   TARGET_NAME - 工程名称 / Target 名称
    #   PRODUCT_BUNDLE_IDENTIFIER - 工程的 BundleID
    #   EXPANDED_CODE_SIGN_IDENTITY 工程的证书
    
    # pragma -mark 用户变量
    # 要注入的 Framework 的名称(不包含后缀名)
    # 填空则代表不进行 Framework 注入
    frameworkName=""
    
    
    
    # pragma -mark 断言
    # 判断 framework 是否包含后缀
    if [[ "${frameworkName}" = *.farmework ]]
    then
        echo "frameworkName=${frameworkName}"
        echo "请不要包含 .framework 扩展名"
        exit 1
    fi
    
    
    
    # pragma -mark 执行重签名
    # 新建 IPA 目录
    ipaDirPath="${SRCROOT}/IPA"
    if [ ! -d "${ipaDirPath}" ]
    then
        mkdir "${ipaDirPath}"
    fi
    
    # 检查 IPA 文件是否存在
    ipaPath=$(set -- "${ipaDirPath}/"*.ipa; echo "$1")
    if [ ! -f "${ipaPath}" ]
    then
        echo "未检测到 IPA 包"
        echo "请将 IPA 包手动拷贝到目录:"
        echo "${ipaDirPath}"
        exit 1
    fi
    
    # 新建 Temp 目录
    tempDirPath="${SRCROOT}/Temp"
    if [ -d "${tempDirPath}" ]
    then
    	rm -rf "${tempDirPath}"
    fi
    mkdir "${tempDirPath}"
    
    # 1.将 IPA 包解压到 Temp 目录下并获取 .app 包的路径
    unzip -oqq "${ipaPath}" -d "${tempDirPath}"
    sourceAppDirPath=$(set -- "${tempDirPath}/Payload/"*.app; echo "$1")
    
    # 2.替换工程中的 .app 包
    targetAppDirPath="${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app"
    rm -rf "${targetAppDirPath}"
    cp -rf "${sourceAppDirPath}" "${targetAppDirPath}"
    echo "sourceAppDirPath = ${sourceAppDirPath}"
    echo "targetAppDirPath = ${targetAppDirPath}"
    
    # 3.删除 .app 包中的所有插件
    # 3.1删除 PlugIns 目录
    plugInsDirPath="${targetAppDirPath}/PlugIns"
    if [ -d "${plugInsDirPath}" ]
    then
        rm -rf "${plugInsDirPath}"
    fi
    # 3.2删除 Watch 目录
    watchDirPath="${targetAppDirPath}/Watch"
    if [ -d "${watchDirPath}" ]
    then
        rm -rf "${watchDirPath}"
    fi
    
    # 4.修改 Info.plist 文件的 Bundle ID
    infoPlistPath="${targetAppDirPath}/Info.plist"
    /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier ${PRODUCT_BUNDLE_IDENTIFIER}" "${infoPlistPath}"
    
    # 5.为 MachO 文件添加可执行权限
    machOName=`plutil -convert xml1 -o - $targetAppDirPath/Info.plist|grep -A1 Exec|tail -n1|cut -f2 -d\>|cut -f1 -d\<`
    machOPath="${targetAppDirPath}/${machOName}"
    chmod a+x "${machOPath}"
    
    # 6.重签名 Frameworks 目录下的所有 framework
    frameworksDirPath="${targetAppDirPath}/Frameworks"
    # 6.1判断 Frameworks 目录是否存在
    if [ -d "${frameworksDirPath}" ]
    then
        # > 进入 Frameworks 目录
        cd "${frameworksDirPath}"
        # 6.2判断 Frameworks 目录下是否存在 framework
        fmwkList=$1
        if [ "`ls -A ${fmwkList}`" != "" ]
        then
            # 6.3重签名 Frameworks 目录下的所有 framework
            for frameworkName in `ls`
            do
                codesign --force --sign "${EXPANDED_CODE_SIGN_IDENTITY}" "${frameworkName}"
            done
        fi
    fi
    
    # 7.使用 yololib 进行代码注入 - Framework 注入
    if [ ! -z "${frameworkName}" ]
    then
        yololib "${machOPath}" "Frameworks/${frameworkName}.framework/${frameworkName}"
    fi
    
  6. 编写完脚本之后,将调试用的 iPhone 连接到 MacBook 上,在 XCode 中选择调试用的真机
    Command + Shift + K,清空工程的 Build 文件夹(编译结果文件夹)
    Command + B,编译工程
    Command + R,将重签名后的 App 安装到调试用的 iPhone 上
    注意:在安装过程中,需要保持 iPhone 为解锁状态
  1. 配置的 Bash 脚本将在工程编译之前执行
  2. 每次编译和运行工程,配置的 Bash 脚本都会执行一次,如果需要解压效果不同,需要对原 IPA 包做更改
  3. 建议直接将脚本写在工程中,方便查错和更改
    同时将脚本保存一份,每次新建工程时,从预先准备的脚本中复制代码到工程里面即可
  4. XCode 编译之后,可以使用快捷键:Command + 9,查看脚本执行后输出的结果
  1. XCode 会根据在工程中配置的苹果开发者账号、BundleID、Capabilities,连接的 iPhone 的 UDID 等信息
    自动到苹果开发者网站注册 AppID、注册 设备 ID、申请 Certificate、申请 Provisioning Profile
    然后将 Certificate 和 Provisioning Profile下载到本地,并应用到工程中
  2. 在 XCode 中使用 Command + R 进行真机调试时
    XCode 会自动从工程的 Provisioning Profile 中抽取 entitlements.plsit(权限文件)
    然后使用 entitlements.plsit 和 Certificate 对 Product 下的 .app 包进行重签名
    并将 .app 包安装到真机上

注意

标签:Profile,IPA,app,iOS,Provisioning,WeChat,签名,App
来源: https://blog.csdn.net/Airths/article/details/107711246