前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【移动安全】移动应用安全基础篇——破掉iOS加密数据

【移动安全】移动应用安全基础篇——破掉iOS加密数据

作者头像
用户1631416
发布2019-05-28 20:30:51
3.2K0
发布2019-05-28 20:30:51
举报
文章被收录于专栏:玄魂工作室玄魂工作室

声 明

本文由Tide安全团队成员“tales”首发于TideSec专栏:

https://zhuanlan.freebuf.com/column/index/?name=TideSec

文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途以及盈利等目的,否则后果自行承担!

本篇为tales移动安全专题第三篇。

1、移动应用安全基础篇——Android、ios环境准备 https://www.freebuf.com/column/199666.html

2、移动应用安全基础篇——绕过iOS越狱检测 https://www.freebuf.com/column/201114.htm

介绍

如今,在做APP安全测试的时候,越来越多的APP数据使用加密传输,一般的做法都需要去逆向APP并寻找到加解密算法。今天主要介绍一下iOS的一些逆向基础知识,教大家碰到加密数据的APP后该如何去解密。

今天主要是针对两款有不同加密方式的iOS应用,难度由低到高。

案例一:

首先解决挂代理抓不到包的问题

使用objection ios sslpinning disable绕过证书绑定

在登录处抓包发现,request包和response包都为加密传输:

appmon提供的scripts

hack.lu提供的scripts

通过参考github上的js脚本,改写了个较为全面的hook.js脚本:

代码语言:javascript
复制
// Intercept the CCCrypt call.
Interceptor.attach(Module.findExportByName('libcommonCrypto.dylib', 'CCCrypt'), {
    onEnter: function (args) {
        // Save the arguments
        this.operation   = args[0]
        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(' + 
            'operation: '   + this.operation    +', ' +
            'CCAlgorithm: ' + this.CCAlgorithm  +', ' +
            'CCOptions: '   + this.CCOptions    +', ' +
            'keyBytes: '    + this.keyBytes     +', ' +
            'keyLength: '   + this.keyLength    +', ' +
            'ivBuffer: '    + this.ivBuffer     +', ' +
            'inBuffer: '    + this.inBuffer     +', ' +
            'inLength: '    + this.inLength     +', ' +
            'outBuffer: '   + this.outBuffer    +', ' +
            'outLength: '   + this.outLength    +', ' +
            'outCountPtr: ' + this.outCountPtr  +')')

        if (this.operation == 0) {
            // Show the buffers here if this an encryption operation
            console.log("In buffer:")
            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) {
            // Show the buffers here if this a decryption operation
            console.log("Out buffer:")
            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
            }))
        }
    }
})

使用frida hook CCCrypt函数

可以直观的看到加密请求数据和解密返回数据为明文。

operation: 0×0代表加密,0×1代表解密,CCAlgorithm: 0×0指加密方式是kCCAlgorithmAES128,CCOptions: 0×1指模式是cbc,key=DATA_KEY20150116和iv=20150116

参阅CommonCryptor.h各参数意义

案例二:

在登录处抓包发现,request包和response包都为加密传输:

使用hook.js脚本发现hook不到

老方法,首先使用frida-ios-dump对该APP进行一键dump

frida-ios-dump,该工具基于frida提供的强大功能通过注入js实现内存dump 然后通过python自动拷贝到电脑生成ipa文件,通过配置完成之后真的就是一条命令砸壳。

砸壳完成后会生成ipa文件,我们解压缩然后使用IDA加载完二进制文件 然后在String窗口搜索loginbypassword(这个是登录时的信息),搜索后进入对应的类,接下来我们进入这个类看它用了哪些方法

找到这个字符串引用的代码位置

之后双击callWebAPI:data:method:ssl:completionHandler: 找到[WebService callWebAPI:data:method:ssl:completionHandler:] 然后F5一下

浏览该类发现可以看到data等关键加密信息,接着我们尝试搜索data前面的setValue:forKey

[_priv_NBSSafeMutableDictionary setValue:forKey:]查看该类发现无结果,返回上一步重新查看加密所在的类

v87由v86 = -[WebService returnDictionaryWithDataPath:](v11, “returnDictionaryWithDataPath:”, v201)返回

查看returnDictionaryWithDataPath:

v8 = +[RSA encryptString:privateKey:](&OBJC_CLASS___RSA, “encryptString:privateKey:”, v4, v6); v4由convertToJsonData:返回(明文)v6由AppPrivate返回(密钥)

查看密钥返回函数AppPrivate和encryptString:privateKey函数

然后使用frida进行hook可以看到解密后的信息

使用objection

代码语言:javascript
复制
ios hooking watch method “+[RSA encryptString:privateKey:]” –dump-args
ios hooking watch method “+[RSA encryptString:privateKey:]” –dump-return

直接使用objection的这两句命令可以达到同样的效果

附JS:

代码语言:javascript
复制
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("args[2] -> " + param1);

            var param2 = new ObjC.Object(args[3]);
            console.log("args[3] -> " + param2);
          },
          onLeave: function(retval) {
            var retur = new ObjC.Object(retval);
            console.log("retval -> " + retur);     
          }
        });
    }
    catch(err){
        console.log("[!] Exception2: " + err.message);
    }
}
else{
    console.log("Objective-C Runtime is not available!");
}

---------------

本文转载自Tide安全团队微信公众号

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 玄魂工作室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本篇为tales移动安全专题第三篇。
    • 1、移动应用安全基础篇——Android、ios环境准备 https://www.freebuf.com/column/199666.html
      • 2、移动应用安全基础篇——绕过iOS越狱检测 https://www.freebuf.com/column/201114.htm
      • 介绍
      • 案例一:
      • 案例二:
      • 使用objection
      相关产品与服务
      移动应用安全
      移动应用安全(Mobile Application Security,MS)针对移动应用普遍存在的破解、篡改、重打包等各类安全风险,提供Android应用加固、iOS源码混淆、SDK加固等多种加固技术,拥有丰富的行业经验,已服务于金融、互联网、车联网、物联网,运营商等多个行业。稳定、简单、有效,让移动安全建设不再是一种负担。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档