移动应用安全基础篇——解密iOS加密数据

栏目: 编程工具 · 发布时间: 4年前

内容简介:如今,在做APP安全测试的时候,越来越多的APP数据使用加密传输,一般的做法都需要去逆向APP并寻找到加解密算法。今天主要介绍一下iOS的一些逆向基础知识,教大家碰到加密数据的APP后该如何去解密。今天主要是针对两款有不同加密方式的iOS应用,难度由低到高。

移动应用安全基础篇——解密iOS加密数据

介绍

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

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

案例一:

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

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

移动应用安全基础篇——解密iOS加密数据

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

移动应用安全基础篇——解密iOS加密数据

appmon提供的scripts

hack.lu提供的scripts

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

// 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函数

移动应用安全基础篇——解密iOS加密数据

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

参阅CommonCryptor.h各参数意义

案例二:

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

移动应用安全基础篇——解密iOS加密数据

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

移动应用安全基础篇——解密iOS加密数据

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

frida-ios-dump,该 工具 基于frida提供的强大功能通过注入js实现内存dump

然后通过 python 自动拷贝到电脑生成ipa文件,通过配置完成之后真的就是一条命令砸壳。

移动应用安全基础篇——解密iOS加密数据

砸壳完成后会生成ipa文件,我们解压缩然后使用IDA加载完二进制文件

然后在String窗口搜索loginbypassword(这个是登录时的信息),搜索后进入对应的类,接下来我们进入这个类看它用了哪些方法

移动应用安全基础篇——解密iOS加密数据

移动应用安全基础篇——解密iOS加密数据

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

移动应用安全基础篇——解密iOS加密数据

之后双击callWebAPI:data:method:ssl:completionHandler:

找到[WebService callWebAPI:data:method:ssl:completionHandler:]

然后F5一下

移动应用安全基础篇——解密iOS加密数据

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

移动应用安全基础篇——解密iOS加密数据

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

v87由v86 = -WebService returnDictionaryWithDataPath:返回

查看returnDictionaryWithDataPath:

移动应用安全基础篇——解密iOS加密数据

v8 = +RSA encryptString:privateKey:;

v4由convertToJsonData:返回(明文)v6由AppPrivate返回(密钥)

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

移动应用安全基础篇——解密iOS加密数据

然后使用frida进行hook

移动应用安全基础篇——解密iOS加密数据

使用objection

ios hooking watch method “+[RSA encryptString:privateKey:]” –dump-args
ios hooking watch method “+[RSA encryptString:privateKey:]” –dump-return

移动应用安全基础篇——解密iOS加密数据

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

附JS:

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!");
}

以上所述就是小编给大家介绍的《移动应用安全基础篇——解密iOS加密数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

疯狂HTML 5/CSS 3/JavaScript讲义

疯狂HTML 5/CSS 3/JavaScript讲义

李刚 / 电子工业出版社 / 2012-5-1 / 69.00元

疯狂HTML 5/CSS 3/JavaScript讲义,ISBN:9787121168635,作者:李刚 编著一起来看看 《疯狂HTML 5/CSS 3/JavaScript讲义》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具