ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

TrollFools源码解析:深入理解MachO文件处理与dylib加载机制

2026/8/7 19:13:39 拓冰建站 浏览量
TrollFools源码解析:深入理解MachO文件处理与dylib加载机制

TrollFools源码解析:深入理解MachO文件处理与dylib加载机制

【免费下载链接】TrollFoolsIn-place tweak injection with insert_dylib and ChOma.项目地址: https://gitcode.com/gh_mirrors/tro/TrollFools

TrollFools是一个专注于MachO文件处理动态库注入的工具,通过insert_dylib和ChOma技术实现原地tweak注入。本文将深入解析其核心源码,带你掌握MachO文件解析、dylib加载机制及注入流程的实现细节。

MachO文件处理核心逻辑

MachO文件识别与保护检测

TrollFools通过InjectorV3+MachO.swift实现MachO文件的基础操作。核心函数isMachO(_ target: URL)使用MachOKit框架加载文件并判断是否为有效MachO格式:

func isMachO(_ target: URL) -> Bool { if (try? MachOKit.loadFromFile(url: target)) != nil { true } else { false } }

对于受保护的MachO文件,isProtectedMachO函数通过检查加密信息命令(LC_ENCRYPTION_INFO)的cryptid字段判断是否加密:

func isProtectedMachO(_ target: URL) throws -> Bool { let machOFile = try MachOKit.loadFromFile(url: target) // 检查32位/64位加密信息命令 switch machOFile { case let .machO(machOFile): for command in machOFile.loadCommands { switch command { case let .encryptionInfo(encryptionInfoCommand): return encryptionInfoCommand.cryptid != 0 case let .encryptionInfo64(encryptionInfoCommand): return encryptionInfoCommand.cryptid != 0 // ... } } // ... } return false }

递归解析依赖的dylib

linkedDylibsRecursivelyOfMachO函数实现了对MachO文件依赖dylib的深度遍历,通过解析LC_LOAD_DYLIBLC_LOAD_WEAK_DYLIB命令收集所有依赖:

func linkedDylibsRecursivelyOfMachO(_ target: URL, collected: OrderedSet<URL> = []) throws -> OrderedSet<URL> { if collected.contains(target) { return collected } var newCollected = collected newCollected.append(target) let loadedDylibs = try loadedDylibsOfMachO(target).compactMap({ resolveLoadCommand($0) }) for dylib in loadedDylibs { newCollected = try linkedDylibsRecursivelyOfMachO(dylib, collected: newCollected) } return newCollected }

dylib注入的关键实现

insert_dylib工具集成

TrollFools通过InjectorV3+Command.swift封装了insert_dylib命令行工具,实现动态库加载命令的插入:

func cmdInsertLoadCommandDylib(_ target: URL, name: String, weak: Bool = false) throws { let dylibs = try loadedDylibsOfMachO(target) if dylibs.contains(name) { return } var args = [name, target.path, "--inplace", "--overwrite", "--no-strip-codesig", "--all-yes"] if weak { args.append("--weak") } let retCode = try Execute.rootSpawn(binary: Self.insertDylibBinaryURL.path, arguments: args, ddlog: logger) // 错误处理... }

运行路径(rpath)管理

为确保注入的dylib能被正确找到,TrollFools使用install_name_tool管理MachO文件的运行路径:

func cmdInsertLoadCommandRuntimePath(_ target: URL, name: String) throws { let rpaths = try runtimePathsOfMachO(target) if rpaths.contains(name) { return } try cmdPseudoSign(target, force: true) let retCode = try Execute.rootSpawn(binary: Self.installNameToolBinaryURL.path, arguments: [ "-add_rpath", name, target.path ], ddlog: logger) // 错误处理... }

代码签名处理

注入过程会修改MachO文件,因此需要重新签名。cmdPseudoSign函数使用ldid工具进行伪签名,并保留原有 entitlements:

func cmdPseudoSign(_ target: URL, force: Bool = false) throws { // 检查是否已签名 // ... if preservesEntitlements { // 提取并保留 entitlements let receipt = try Execute.rootSpawnWithOutputs(binary: Self.ldidBinaryURL.path, arguments: ["-e", target.path]) // 写入临时文件并重新签名 try Execute.rootSpawn(binary: Self.ldidBinaryURL.path, arguments: ["-S\(xmlURL.path)", target.path]) } else { try Execute.rootSpawn(binary: Self.ldidBinaryURL.path, arguments: ["-S", target.path]) } }

注入流程与核心组件

注入主流程

InjectorV3+Inject.swift中的注入流程包含以下关键步骤:

  1. 定位可用的MachO文件(locateAvailableMachO
  2. 创建文件备份(makeAlternate
  3. 插入dylib加载命令(insertLoadCommandOfAsset
  4. 应用CoreTrust绕过(applyCoreTrustBypass

核心代码片段:

func inject() throws { guard let targetMachO = try locateAvailableMachO() else { throw Error.generic("No available MachO found") } try makeAlternate(targetMachO) do { try insertLoadCommandOfAsset(assetURL, to: targetMachO) try applyCoreTrustBypass(targetMachO) } catch { try? restoreAlternate(targetMachO) throw error } }

关键工具依赖

TrollFools集成了多个底层工具来实现注入功能,这些工具位于项目根目录:

  • insert_dylib:插入动态库加载命令
  • install_name_tool:修改动态库路径
  • ldid:iOS平台伪签名工具
  • ct_bypass:CoreTrust绕过工具

总结与实践建议

TrollFools通过优雅的代码设计,将复杂的MachO文件处理和dylib注入流程封装为简洁的API。核心亮点包括:

  1. 模块化设计:通过分类(Category)将不同功能分离到InjectorV3+MachO.swiftInjectorV3+Command.swift等文件
  2. 递归依赖解析:实现MachO文件依赖的深度遍历
  3. 签名保留机制:在修改MachO文件时保留原有签名信息

对于希望深入理解iOS逆向工程的开发者,建议重点研究:

  • InjectorV3+MachO.swift中的MachO解析逻辑
  • InjectorV3+Command.swift中的系统命令封装
  • InjectorV3+Inject.swift中的注入主流程

通过掌握这些核心技术,你将能够构建自己的动态库注入工具,或对现有工具进行扩展和优化。

【免费下载链接】TrollFoolsIn-place tweak injection with insert_dylib and ChOma.项目地址: https://gitcode.com/gh_mirrors/tro/TrollFools

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考