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

📅 2026/8/7 18:48:18
TrollFools源码解析:深入理解MachO文件处理与dylib加载机制
TrollFools源码解析深入理解MachO文件处理与dylib加载机制【免费下载链接】TrollFoolsIn-place tweak injection with insert_dylib and ChOma.项目地址: https://gitcode.com/gh_mirrors/tro/TrollFoolsTrollFools是一个专注于MachO文件处理和动态库注入的工具通过insert_dylib和ChOma技术实现原地tweak注入。本文将深入解析其核心源码带你掌握MachO文件解析、dylib加载机制及注入流程的实现细节。MachO文件处理核心逻辑MachO文件识别与保护检测TrollFools通过InjectorV3MachO.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 }递归解析依赖的dyliblinkedDylibsRecursivelyOfMachO函数实现了对MachO文件依赖dylib的深度遍历通过解析LC_LOAD_DYLIB和LC_LOAD_WEAK_DYLIB命令收集所有依赖func linkedDylibsRecursivelyOfMachO(_ target: URL, collected: OrderedSetURL []) throws - OrderedSetURL { 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通过InjectorV3Command.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工具进行伪签名并保留原有 entitlementsfunc 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]) } }注入流程与核心组件注入主流程InjectorV3Inject.swift中的注入流程包含以下关键步骤定位可用的MachO文件locateAvailableMachO创建文件备份makeAlternate插入dylib加载命令insertLoadCommandOfAsset应用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修改动态库路径ldidiOS平台伪签名工具ct_bypassCoreTrust绕过工具总结与实践建议TrollFools通过优雅的代码设计将复杂的MachO文件处理和dylib注入流程封装为简洁的API。核心亮点包括模块化设计通过分类Category将不同功能分离到InjectorV3MachO.swift、InjectorV3Command.swift等文件递归依赖解析实现MachO文件依赖的深度遍历签名保留机制在修改MachO文件时保留原有签名信息对于希望深入理解iOS逆向工程的开发者建议重点研究InjectorV3MachO.swift中的MachO解析逻辑InjectorV3Command.swift中的系统命令封装InjectorV3Inject.swift中的注入主流程通过掌握这些核心技术你将能够构建自己的动态库注入工具或对现有工具进行扩展和优化。【免费下载链接】TrollFoolsIn-place tweak injection with insert_dylib and ChOma.项目地址: https://gitcode.com/gh_mirrors/tro/TrollFools创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考