移动应用安全基础篇——解密iOS加密数据
字数 1311 2025-08-18 11:38:37
iOS应用加密数据解密技术详解
一、环境准备与基础工具
在进行iOS应用逆向分析前,需要准备以下环境和工具:
- 越狱设备:用于运行逆向工具和分析应用
- Frida:动态插桩框架,用于运行时hook和修改应用行为
- Objection:基于Frida的运行时移动探索工具包
- frida-ios-dump:用于从iOS设备中dump应用ipa文件
- IDA Pro:反汇编和逆向工程工具
- Burp Suite/Charles:网络抓包工具
二、绕过SSL Pinning
许多iOS应用会使用SSL Pinning来防止中间人攻击,可以使用以下方法绕过:
objection ios sslpinning disable
这条命令会自动禁用应用的证书绑定检查,使抓包工具能够拦截HTTPS流量。
三、识别加密数据
通过抓包工具观察请求和响应,加密数据通常表现为:
- 非文本格式(如二进制数据)
- 无规律的长字符串
- 请求和响应都经过加密
四、Hook加密函数
iOS常用的加密函数位于libcommonCrypto.dylib中,主要是CCCrypt函数。
1. CCCrypt函数参数解析
CCCrypt(
CCOperation op, // 0加密/1解密
CCAlgorithm alg, // 加密算法
CCOptions options, // 加密选项
const void *key, // 密钥
size_t keyLength, // 密钥长度
const void *iv, // 初始化向量
const void *dataIn, // 输入数据
size_t dataInLength, // 输入数据长度
void *dataOut, // 输出数据
size_t dataOutLength, // 输出缓冲区长度
size_t *dataOutMoved // 实际输出数据长度
);
2. Frida Hook脚本示例
Interceptor.attach(Module.findExportByName('libcommonCrypto.dylib', 'CCCrypt'), {
onEnter: function (args) {
this.operation = args[0]; // 0加密/1解密
this.CCAlgorithm = args[1]; // 算法类型
this.CCOptions = args[2]; // 加密选项
this.keyBytes = args[3]; // 密钥
this.keyLength = args[4]; // 密钥长度
this.ivBuffer = args[5]; // 初始化向量
this.inBuffer = args[6]; // 输入数据
this.inLength = args[7]; // 输入长度
this.outBuffer = args[8]; // 输出缓冲区
this.outLength = args[9]; // 输出缓冲区长度
this.outCountPtr = args[10]; // 实际输出长度指针
console.log('CCCrypt called with parameters...');
if (this.operation == 0) { // 加密操作
console.log("In buffer (plaintext):");
console.log(hexdump(ptr(this.inBuffer), {
length: this.inLength.toInt32(),
header: true,
ansi: true
}));
console.log("Key:");
console.log(hexdump(ptr(this.keyBytes), {
length: this.keyLength.toInt32(),
header: true,
ansi: true
}));
console.log("IV:");
console.log(hexdump(ptr(this.ivBuffer), {
length: this.keyLength.toInt32(),
header: true,
ansi: true
}));
}
},
onLeave: function (retVal) {
if (this.operation == 1) { // 解密操作
console.log("Out buffer (plaintext):");
console.log(hexdump(ptr(this.outBuffer), {
length: Memory.readUInt(this.outCountPtr),
header: true,
ansi: true
}));
console.log("Key:");
console.log(hexdump(ptr(this.keyBytes), {
length: this.keyLength.toInt32(),
header: true,
ansi: true
}));
console.log("IV:");
console.log(hexdump(ptr(this.ivBuffer), {
length: this.keyLength.toInt32(),
header: true,
ansi: true
}));
}
}
});
3. 关键参数说明
- operation: 0x0表示加密,0x1表示解密
- CCAlgorithm: 0x0通常表示kCCAlgorithmAES128
- CCOptions: 0x1通常表示CBC模式
五、静态分析进阶技术
当简单的Hook无法捕获加密操作时,需要进行更深入的静态分析:
1. 使用frida-ios-dump获取应用二进制
python dump.py [BundleID]
2. IDA静态分析步骤
- 在IDA中打开应用二进制文件
- 在Strings窗口搜索关键字符串(如"login"、"password"等)
- 定位到引用这些字符串的代码位置
- 分析调用链,找到加密/解密相关函数
3. 示例分析流程
- 搜索"loginbypassword"字符串
- 找到调用该字符串的方法(如
callWebAPI:data:method:ssl:completionHandler:) - 反编译该方法,分析数据流
- 查找加密函数调用(如
+[RSA encryptString:privateKey:])
六、Hook自定义加密方法
对于使用自定义加密逻辑的应用,可以Hook其加密方法:
1. 使用Objection直接Hook
objection ios hooking watch method "+[RSA encryptString:privateKey:]" --dump-args
objection ios hooking watch method "+[RSA encryptString:privateKey:]" --dump-return
2. 自定义Frida脚本
if (ObjC.available) {
try {
var className = "RSA";
var funcName = "+ encryptString:privateKey:";
var hook = eval('ObjC.classes.' + className + '["' + funcName + '"]');
console.log("[*] Class Name: " + className);
console.log("[*] Method Name: " + funcName);
Interceptor.attach(hook.implementation, {
onEnter: function(args) {
var param1 = new ObjC.Object(args[2]); // 明文数据
console.log("Plaintext: " + param1);
var param2 = new ObjC.Object(args[3]); // 私钥
console.log("Private Key: " + param2);
},
onLeave: function(retval) {
var retur = new ObjC.Object(retval); // 加密结果
console.log("Ciphertext -> " + retur);
}
});
} catch(err) {
console.log("[!] Exception: " + err.message);
}
} else {
console.log("Objective-C Runtime is not available!");
}
七、常见加密模式识别
- AES-CBC:最常见的对称加密方式,需要密钥和IV
- RSA:非对称加密,通常用于加密传输密钥或签名
- 自定义加密:应用自己实现的加密逻辑,需要深入分析
八、实战技巧
- 字符串搜索:在二进制中搜索"encrypt"、"decrypt"、"AES"、"RSA"等关键词
- 方法跟踪:从网络请求方法回溯到加密方法
- 参数分析:观察加密方法的输入输出,确定密钥来源
- 动态调试:结合静态分析和动态Hook验证猜测
九、注意事项
- 逆向工程可能违反应用的服务条款
- 仅用于安全研究和授权测试
- 尊重开发者知识产权
- 测试前确保获得合法授权
通过以上技术,可以有效地分析iOS应用的加密机制,理解其数据传输安全实现,为安全评估提供依据。