Cocos2d-x iOS游戏逆向分析实战
一、目标分析好友赛游戏1.1 应用基本信息应用名称好友赛 (haoyousai)Bundle IDcom.oedere.lid23版本号1.0 (Build 59)目标平台iOS 15.0CPU架构arm64游戏类型棋牌类游戏1.2 技术栈识别通过静态分析和动态调试我们识别出以下技术栈graph TD A[Cocos2d-x游戏引擎] -- B[JavaScriptCore脚本引擎] A -- C[OpenGL ES图形渲染] B -- D[JSC字节码预编译] B -- E[明文JS脚本] F[游戏逻辑] -- G[房间管理] F -- H[牌局处理] F -- I[用户交互]1.3 应用结构分析haoyousai.app/ ├── haoyousai # 主可执行文件 (15.9MB) ├── Frameworks/ # 依赖框架 ├── script/ # 游戏脚本目录 ├── src/ # 源代码目录 ├── res/ # 资源文件 ├── project.json # Cocos项目配置 ├── project.manifest # 资源清单 └── main.js # 入口JS文件二、逆向工具开发cocos2dx_frida_toolkit.js2.1 工具架构设计我们开发了一个全面的 Frida 逆向分析工具整体架构如下// 工具架构示意图 class Cocos2dxFridaToolkit { // 1. 基础模块 - Helper Functions - Configuration Manager - Logger System // 2. 检测模块 - Cocos2dxDetector - ScriptEngineDetector // 3. 分析模块 - LuaScriptAnalyzer - JSScriptAnalyzer - CocosGameAnalyzer // 4. 监控模块 - InputOutputMonitor - PerformanceProfiler // 5. 控制模块 - ToolkitController - RPC Exports }2.2 关键技术实现2.2.1 脚本引擎检测class Cocos2dxDetector { // 检测Lua引擎 detectLuaEngine() { const exports [luaL_loadbuffer, lua_pcall, lua_getglobal]; return this.findExportsInMainModule(exports); } // 检测JavaScriptCore detectJavaScriptCore() { const exports [JSEvaluateScript, JSObjectCallAsFunction]; return this.findExportsInMainModule(exports); } // 检测SpiderMonkey detectSpiderMonkey() { const exports [JS_EvaluateScript, JS_ExecuteScript]; return this.findExportsInMainModule(exports); } // 检测Cocos JS绑定 detectCocosBindings() { const patterns [jsb_, cocos2d::, ScriptingCore::]; return this.searchExportsByPattern(patterns); } }2.2.2 脚本拦截与解密Lua脚本拦截class LuaScriptAnalyzer { hookLuaFunctions() { // Hook luaL_loadbuffer 拦截Lua脚本加载 Interceptor.attach(Module.findExportByName(null, luaL_loadbuffer), { onEnter: function(args) { const buffer args[1]; // 脚本缓冲区 const size args[2]; // 脚本大小 const chunkname args[3]; // 脚本名称 // 提取并保存脚本 this.scriptData Memory.readByteArray(buffer, size); this.scriptName Memory.readUtf8String(chunkname); }, onLeave: function(retval) { if (this.scriptData) { this.saveLuaScript(this.scriptName, this.scriptData); } } }); } }JavaScript脚本拦截class JSScriptAnalyzer { hookJavaScriptCore() { // Hook JSEvaluateScript 拦截JS执行 Interceptor.attach(Module.findExportByName(JavaScriptCore, JSEvaluateScript), { onEnter: function(args) { const script args[1]; // JS脚本字符串 const sourceURL args[3]; // 源URL // 读取脚本内容 const scriptStr this.readJSString(script); const urlStr this.readJSString(sourceURL); // 分析脚本内容 this.analyzeJSScript(scriptStr, urlStr); } }); } // 读取JS字符串的辅助函数 readJSString(jsStringRef) { const size this.JSStringGetMaximumUTF8CStringSize(jsStringRef); const buffer Memory.alloc(size); this.JSStringGetUTF8CString(jsStringRef, buffer, size); return buffer.readUtf8String(); } }2.3 性能优化与稳定性修复在开发过程中我们遇到了多个技术挑战并进行了优化2.3.1 超时崩溃问题修复问题原始实现中遍历所有模块导出和ObjC类导致Frida超时。解决方案// 优化前遍历所有模块 Process.enumerateModules().forEach(module { module.enumerateExports().forEach(export { // 处理每个导出 }); }); // 优化后只扫描主模块 const mainModule Process.enumerateModules()[0]; mainModule.enumerateExports().forEach(export { // 只处理主模块导出 }); // 限制ObjC类遍历数量 const maxHooksPerCategory CONFIG.maxHooksPerCategory || 50; let hookCount 0; for (let className in ObjC.classes) { if (hookCount maxHooksPerCategory) break; // 处理ObjC类 hookCount; }2.3.2 Hook稳定性修复问题尝试Hook数据符号地址导致崩溃。解决方案function isExecutableAddress(address) { const range Process.findRangeByAddress(address); return range range.protection.includes(x); } function safeAttach(address, callbacks) { if (!isExecutableAddress(address)) { logger.warn(地址 ${address} 不可执行跳过Hook); return null; } return Interceptor.attach(address, callbacks); }2.3.3 ObjC桥接修复问题直接传递JS字符串给ObjC方法导致类型不匹配。解决方案function nsStr(jsString) { // 使用NSString包装JS字符串 return ObjC.classes.NSString.stringWithUTF8String_(jsString); } function createDir(path) { const fileManager ObjC.classes.NSFileManager.defaultManager(); const nsPath nsStr(path); const errorPtr Memory.alloc(Process.pointerSize); // 正确传递参数 return fileManager.createDirectoryAtPath_withIntermediateDirectories_attributes_error_( nsPath, 1, // YES NULL, errorPtr ); }三、原生Tweak开发CardRecorder3.1 设计思路为了提供更稳定的游戏数据监控我们开发了原生iOS Tweak将关键功能从Frida脚本迁移到原生代码中。3.2 核心实现// CardRecorder.mm 核心代码分析 // 1. JavaScriptCore C-API Hook __attribute__((constructor)) static void CardRecorderInit(void) { // 加载JavaScriptCore框架 void *jscHandle dlopen(/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore, RTLD_NOW); // 获取JSEvaluateScript函数指针 JSEvaluateScript_t orig_JSEvaluateScript (JSEvaluateScript_t)dlsym(jscHandle, JSEvaluateScript); // 使用MSHookFunction进行Hook MSHookFunction( (void *)orig_JSEvaluateScript, (void *)hook_JSEvaluateScript, (void **)orig_JSEvaluateScript ); } // 2. Hook函数实现 static JSValueRef hook_JSEvaluateScript( JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef *exception) { // 捕获JSContext if (ctx !g_jsCtx) { g_jsCtx ctx; NSLog([CardRecorder] 捕获JSContext: %p, ctx); } // 检测游戏脚本 if (!g_injected script) { size_t maxSize JSStringGetMaximumUTF8CStringSize(script); if (maxSize 5000) { // 只处理大型脚本 char *buffer (char *)malloc(maxSize); JSStringGetUTF8CString(script, buffer, maxSize); // 检测关键词setRoomData if (strstr(buffer, setRoomData) ! NULL) { NSLog([CardRecorder] 检测到游戏脚本准备注入监控代码); injectCardMonitor(ctx); } free(buffer); } } // 调用原始函数 return orig_JSEvaluateScript(ctx, script, thisObject, sourceURL, startingLineNumber, exception); } // 3. 监控代码注入 static void injectCardMonitor(JSContextRef ctx) { const char *monitorJS (function(){ if(window.__cardHookInstalled) return; window.__cardHookInstalled true; setInterval(function(){ try { if(!iGame || !iGame.Data || !iGame.Data.roomData) return; var selfSeat iGame.Data.getSelfSeatNo ? iGame.Data.getSelfSeatNo() : 0; var players iGame.Data.roomData.players; var result {self_seat: selfSeat, my_hold: [], players: []}; players.forEach(function(p){ if(p.seat_no selfSeat){ result.my_hold (p.hold || []).filter(c c 0); } result.players.push({ seat: p.seat_no, out: p.out || [], kou: p.kou || [] }); }); window.__cardData JSON.stringify(result); } catch(e){} }, 1000); })();; // 在游戏JSContext中执行监控代码 JSStringRef jsStr JSStringCreateWithUTF8CString(monitorJS); JSValueRef exception NULL; JSEvaluateScript(ctx, jsStr, NULL, NULL, 0, exception); JSStringRelease(jsStr); }3.3 数据采集与存储// 定时数据采集 static void startCardPolling(void) { dispatch_source_t timer dispatch_source_create( DISPATCH_SOURCE_TYPE_TIMER, 0, 0, g_queue); dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), 1 * NSEC_PER_SEC, 0); dispatch_source_set_event_handler(timer, ^{ if (!g_jsCtx || !g_injected) return; // 读取游戏数据 NSString *cardData evalInGameJS(g_jsCtx, window.__cardData || \\); if (cardData ![cardData isEqualToString:g_lastCardData]) { g_lastCardData cardData; // 保存到文件 [self appendCardLog:cardData]; } }); dispatch_resume(timer); } // 文件存储 - (void)appendCardLog:(NSString *)json { NSString *docPath NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString *logPath [docPath stringByAppendingPathComponent:card_log.json]; NSFileManager *fm [NSFileManager defaultManager]; if (![fm fileExistsAtPath:logPath]) { [fm createFileAtPath:logPath contents:nil attributes:nil]; } NSFileHandle *fh [NSFileHandle fileHandleForWritingAtPath:logPath]; [fh seekToEndOfFile]; [fh writeData:[[json stringByAppendingString:\n] dataUsingEncoding:NSUTF8StringEncoding]]; [fh closeFile]; }四、动态分析结果4.1 脚本捕获与分析通过我们的工具成功捕获了以下关键脚本4.1.1 JSC字节码文件scripts/ ├── G212.jsc_1774770976633.bin # 游戏模块212 (101KB) ├── G30.jsc_1774770909867.bin # 游戏模块30 (29KB) ├── SYG30.jsc_1774770968778.bin # 系统模块30 (49KB) └── SYHall.jsc_1774770963515.bin # 大厅模块 (584KB)4.1.2 明文JS脚本scripts/ ├── js_1774770963709.js # 主游戏逻辑 (1.7MB) ├── js_1774770976843.js # 游戏模块 (302KB) ├── js_1774770968967.js # 系统模块 (147KB) └── 多个小型配置脚本 (88B-2KB)4.2 游戏架构解析通过分析捕获的脚本我们还原了游戏的架构// 游戏全局对象结构 window.iGame { Data: { roomData: { players: [{ seat_no: number, // 座位号 hold: number[], // 手牌 out: number[], // 出牌 kou: number[] // 扣牌 }], room_id: string, // 房间ID game_type: number // 游戏类型 }, getSelfSeatNo: function() { // 获取自己座位号 return number; }, setRoomData: function(data) { // 设置房间数据 // 游戏状态更新 } }, UI: { // UI相关方法 }, Network: { // 网络通信方法 } };4.3 游戏状态监控我们的工具能够实时监控游戏状态{ timestamp: 2026-03-29T21:00:00Z, self_seat: 1, my_hold: [11, 12, 13, 14, 15], players: [ { seat: 1, out: [21, 22], kou: [] }, { seat: 2, out: [31], kou: [41, 42] }, { seat: 3, out: [], kou: [51] }, { seat: 4, out: [61, 62, 63], kou: [] } ] }五、技术难点与解决方案5.1 多脚本引擎支持难点Cocos2d-x支持多种脚本引擎Lua、JavaScriptCore、SpiderMonkey。解决方案class ScriptEngineDetector { detectAllEngines() { const engines []; // 检测Lua if (this.detectLuaEngine()) { engines.push({ type: lua, version: this.getLuaVersion() }); } // 检测JavaScriptCore if (this.detectJavaScriptCore()) { engines.push({ type: javascriptcore, version: this.getJSCVersion() }); } // 检测SpiderMonkey if (this.detectSpiderMonkey()) { engines.push({ type: spidermonkey, version: this.getSMVersion() }); } return engines; } }5.2 脚本加密与混淆难点游戏脚本可能被加密或混淆。解决方案class ScriptDecryptor { decryptScript(encryptedData, encryptionType) { switch (encryptionType) { case xor: return this.xorDecrypt(encryptedData, this.findXorKey()); case base64: return this.base64Decode(encryptedData); case custom: return this.customDecrypt(encryptedData); default: return encryptedData; // 可能未加密 } } xorDecrypt(data, key) { const decrypted []; for (let i 0; i data.length; i) { decrypted.push(data[i] ^ key[i % key.length]); } return Buffer.from(decrypted); } findXorKey() { // 通过模式识别或动态分析查找XOR密钥 const commonPatterns [ [0x73, 0x63, 0x72, 0x69, 0x70, 0x74], // script [0x67, 0x61, 0x6D, 0x65], // game [0x63, 0x6F, 0x63, 0x6F, 0x73] // cocos ]; // 尝试常见密钥 for (const pattern of commonPatterns) { if (this.testXorKey(pattern)) { return pattern; } } // 动态分析查找 return this.dynamicFindXorKey(); } }5.3 性能与稳定性平衡难点Hook过多影响游戏性能Hook过少无法获取足够信息。解决方案class PerformanceOptimizer { constructor() { this.hookStats { totalHooks: 0, activeHooks: 0, performanceImpact: 0 }; this.config { maxHooks: 200, samplingRate: 0.1, // 10%采样率 enableLazyHook: true }; } shouldHookFunction(funcName, importance) { // 根据重要性决定是否Hook if (importance 0.8) return true; // 高重要性函数 if (this.hookStats.totalHooks this.config.maxHooks) { return false; // 达到Hook上限 } // 使用采样率控制Hook数量 if (Math.random() this.config.samplingRate) { return true; } return false; } lazyHook(address, callbacks, options {}) { if (this.config.enableLazyHook options.lazy) { // 延迟Hook只在需要时激活 return new LazyHook(address, callbacks); } else { return Interceptor.attach(address, callbacks); } } } class LazyHook { constructor(address, callbacks) { this.address address; this.callbacks callbacks; this.active false; this.interceptor null; } activate() { if (!this.active) { this.interceptor Interceptor.attach(this.address, this.callbacks); this.active true; } } deactivate() { if (this.active this.interceptor) { this.interceptor.detach(); this.active false; } } }六、实战应用场景6.1 游戏逻辑分析通过我们的工具可以深入分析游戏的核心逻辑// 分析游戏状态机 class GameStateAnalyzer { analyzeStateMachine() { const states new Set(); const transitions []; // Hook状态切换函数 Interceptor.attach(this.findFunction(changeGameState), { onEnter: function(args) { const oldState args[0]; const newState args[1]; states.add(oldState.toString()); states.add(newState.toString()); transitions.push({ from: oldState.toString(), to: newState.toString(), timestamp: Date.now() }); } }); return { states: Array.from(states), transitions: transitions, graph: this.generateStateGraph(transitions) }; } }6.2 网络协议分析class NetworkProtocolAnalyzer { analyzeNetworkProtocol() { // Hook网络发送函数 Interceptor.attach(Module.findExportByName(null, send), { onEnter: function(args) { const socket args[0]; const buffer args[1]; const length args[2]; const data Memory.readByteArray(buffer, length); this.packet { type: send, socket: socket, data: data, length: length, timestamp: Date.now() }; }, onLeave: function(retval) { this.analyzePacket(this.packet); } }); // Hook网络接收函数 Interceptor.attach(Module.findExportByName(null, recv), { onEnter: function(args) { const socket args[0]; const buffer args[1]; const length args[2]; this.socket socket; this.buffer buffer; this.length length; }, onLeave: function(retval) { if (retval 0) { const data Memory.readByteArray(this.buffer, retval); const packet { type: recv, socket: this.socket, data: data, length: retval, timestamp: Date.now() }; this.analyzePacket(packet); } } }); } analyzePacket(packet) { // 协议解析逻辑 const header packet.data.slice(0, 4); const body packet.data.slice(4); console.log([Network] ${packet.type} packet:, { length: packet.length, header: header.toString(hex), bodyLength: body.length }); } }6.3 自动化测试框架class AutomatedTestFramework { constructor() { this.testCases []; this.results []; } addTestCase(name, setup, execute, verify) { this.testCases.push({ name: name, setup: setup, execute: execute, verify: verify, status: pending }); } runTests() { console.log(开始执行 ${this.testCases.length} 个测试用例); this.testCases.forEach((testCase, index) { console.log([${index 1}/${this.testCases.length}] 执行测试: ${testCase.name}); try { // 执行测试 const context testCase.setup(); const result testCase.execute(context); const passed testCase.verify(result); testCase.status passed ? passed : failed; testCase.result result; console.log( ✓ 测试 ${testCase.name}: ${passed ? 通过 : 失败}); } catch (error) { testCase.status error; testCase.error error.message; console.log( ✗ 测试 ${testCase.name}: 错误 - ${error.message}); } }); return this.generateReport(); } generateReport() { const passed this.testCases.filter(tc tc.status passed).length; const failed this.testCases.filter(tc tc.status failed).length; const errors this.testCases.filter(tc tc.status error).length; return { summary: { total: this.testCases.length, passed: passed, failed: failed, errors: errors, successRate: (passed / this.testCases.length * 100).toFixed(2) % }, details: this.testCases.map(tc ({ name: tc.name, status: tc.status, result: tc.result, error: tc.error })) }; } }七、安全防护建议7.1 针对逆向分析的防护措施基于我们的逆向经验为游戏开发者提供以下防护建议// 1. 代码混淆 class CodeObfuscator { obfuscateJavaScript(code) { // 变量名混淆 code this.renameVariables(code); // 控制流扁平化 code this.flattenControlFlow(code); // 字符串加密 code this.encryptStrings(code); // 死代码插入 code this.insertDeadCode(code); return code; } } // 2. 反调试检测 class AntiDebugDetector { checkDebuggers() { const checks [ this.checkFrida(), this.checkPtrace(), this.checkSysctl(), this.checkExceptionPorts() ]; return checks.some(check check true); } checkFrida() { // 检测Frida特征 const fridaSignatures [ frida-agent, gum-js-loop, libfrida ]; const modules Process.enumerateModules(); return modules.some(module { return fridaSignatures.some(sig module.name.includes(sig) || module.path.includes(sig) ); }); } } // 3. 运行时完整性校验 class IntegrityChecker { verifyIntegrity() { // 校验代码段完整性 const textSegment Process.getModuleByName(haoyousai); const expectedHash this.calculateHash(textSegment.base, textSegment.size); const actualHash this.readStoredHash(); if (expectedHash ! actualHash) { this.handleTamperingDetected(); } // 校验关键函数 this.verifyCriticalFunctions(); } }7.2 数据加密建议// 使用强加密保护敏感数据 class DataProtector { encryptGameData(data, key) { // 使用AES-GCM加密 const iv crypto.randomBytes(12); const cipher crypto.createCipheriv(aes-256-gcm, key, iv); const encrypted Buffer.concat([ cipher.update(data), cipher.final() ]); const authTag cipher.getAuthTag(); return { iv: iv.toString(hex), data: encrypted.toString(hex), tag: authTag.toString(hex) }; } decryptGameData(encrypted, key) { const iv Buffer.from(encrypted.iv, hex); const data Buffer.from(encrypted.data, hex); const tag Buffer.from(encrypted.tag, hex); const decipher crypto.createDecipheriv(aes-256-gcm, key, iv); decipher.setAuthTag(tag); return Buffer.concat([ decipher.update(data), decipher.final() ]); } }八、总结与展望8.1 技术总结通过本次逆向分析实战我们取得了以下成果完整工具链开发开发了从动态分析到原生Tweak的完整工具链深度游戏理解深入理解了Cocos2d-x游戏的工作原理和架构稳定性优化解决了多个关键技术难题提升了工具稳定性实用价值工具具有实际应用价值可用于游戏分析、安全测试等场景8.2 技术亮点多引擎支持同时支持Lua、JavaScriptCore、SpiderMonkey等多种脚本引擎性能优化通过智能Hook管理和采样技术平衡性能与信息获取稳定性保障完善的错误处理和恢复机制扩展性设计模块化设计便于功能扩展和维护8.3 未来展望AI辅助分析结合机器学习技术自动识别游戏模式和逻辑跨平台支持扩展到Android平台和更多游戏引擎云端分析提供云端游戏分析服务自动化报告自动生成详细的分析报告和安全评估8.4 开源计划我们计划将核心工具开源包括cocos2dx_frida_toolkit.js完整的Frida逆向分析工具CardRecorder原生iOS Tweak实现示例脚本和文档常见游戏的分析模板