使用Frida绕过Android App的SSL Pinning

作者 原文传送
littley www.littley.top

简介

证书锁定(Certificate pinning)是远程服务器在应用程序本身中信任的硬编码证书,因此它将忽略设备证书存储,并将信任自己的硬编码证书,进一步的应用程序将使用该硬编码证书“安全地”与远程服务器通信。

环境需求

  • windows10
  • Android虚拟机(或真机)(本文采用的mumu模拟器)
  • 安装adb驱动
  • 安装frida (python为3.7)
  • 安装Burpsuite(本文采用burpsuite_pro_v2.0)

注:经测试如果Burpsuite只是做绕过sslping功能,在模拟器或手机中可以不用安装Burpsuite的证书,后面会讲如何使用该证书

下载并设置burpsuite证书

先运行burpsuit并设置好代理端口我这里用的是8888端口附图
使用Frida绕过Android App的SSL Pinning_第1张图片在浏览器让访问:http://192.168.1.107:8888/并点击CA Certificate下载证书

使用Frida绕过Android App的SSL Pinning_第2张图片
连接adb命令如下

adb connect 127.0.0.1:7555

使adb将证书上传到模拟器/data/local/tmp目录中,注意,这里是将cacert.der推送到模拟器重命名为cert-der.crt

adb push cacert.der /data/local/tmp/cert-der.crt

设置模拟器代理端口

使用Frida绕过Android App的SSL Pinning_第3张图片

运行frida脚本

将下面脚本保存名为fridascript.js

/* 

   Android SSL Re-pinning frida script v0.2 030417-pier

$ adb push burpca-cert-der.crt /data/local/tmp/cert-der.crt

   $ frida -U -f it.app.mobile -l frida-android-repinning.js --no-pause

https://techblog.mediaservice.net/2017/07/universal-android-ssl-pinning-bypass-with-frida/

   

   UPDATE 20191605: Fixed undeclared var. Thanks to @oleavr and @ehsanpc9999 !

*/

setTimeout(function(){

    Java.perform(function (){

     console.log("");

     console.log("[.] Cert Pinning Bypass/Re-Pinning");

var CertificateFactory = Java.use("java.security.cert.CertificateFactory");

     var FileInputStream = Java.use("java.io.FileInputStream");

     var BufferedInputStream = Java.use("java.io.BufferedInputStream");

     var X509Certificate = Java.use("java.security.cert.X509Certificate");

     var KeyStore = Java.use("java.security.KeyStore");

     var TrustManagerFactory = Java.use("javax.net.ssl.TrustManagerFactory");

     var SSLContext = Java.use("javax.net.ssl.SSLContext");

// Load CAs from an InputStream

     console.log("[+] Loading our CA...")

     var cf = CertificateFactory.getInstance("X.509");

     

     try {

      var fileInputStream = FileInputStream.$new("/data/local/tmp/cert-der.crt");

     }

     catch(err) {

      console.log("[o] " + err);

     }

     

     var bufferedInputStream = BufferedInputStream.$new(fileInputStream);

    var ca = cf.generateCertificate(bufferedInputStream);

     bufferedInputStream.close();

var certInfo = Java.cast(ca, X509Certificate);

     console.log("[o] Our CA Info: " + certInfo.getSubjectDN());

// Create a KeyStore containing our trusted CAs

     console.log("[+] Creating a KeyStore for our CA...");

     var keyStoreType = KeyStore.getDefaultType();

     var keyStore = KeyStore.getInstance(keyStoreType);

     keyStore.load(null, null);

     keyStore.setCertificateEntry("ca", ca);

     

     // Create a TrustManager that trusts the CAs in our KeyStore

     console.log("[+] Creating a TrustManager that trusts the CA in our KeyStore...");

     var tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();

     var tmf = TrustManagerFactory.getInstance(tmfAlgorithm);

     tmf.init(keyStore);

     console.log("[+] Our TrustManager is ready...");

console.log("[+] Hijacking SSLContext methods now...")

     console.log("[-] Waiting for the app to invoke SSLContext.init()...")

SSLContext.init.overload("[Ljavax.net.ssl.KeyManager;", "[Ljavax.net.ssl.TrustManager;", "java.security.SecureRandom").implementation = function(a,b,c) {

      console.log("[o] App invoked javax.net.ssl.SSLContext.init...");

      SSLContext.init.overload("[Ljavax.net.ssl.KeyManager;", "[Ljavax.net.ssl.TrustManager;", "java.security.SecureRandom").call(this, a, tmf.getTrustManagers(), c);

      console.log("[+] SSLContext initialized with our custom TrustManager!");

     }

    });

},0);

运行frida-server

adb connect 127.0.0.1:7555#上面已经连接这句可以略过
adb shell
cd /data/local/tmp

chmod 777 frida-server

./frida-server# 

选运行你的app,并用frida-ps -U查看你的应用程序包名,也可以通过其他方式获取包名
cmd

frida-ps -U

将 fridascript.js 注入到 到目标应用程序中

frida -U -f <包名> -l  fridascript.js --no-paus
例:frida -U -f com.twitter.android  -l  fridascript.js --no-paus

使用Frida绕过Android App的SSL Pinning_第4张图片
如图上执行成功,然后我们操作一下app去burpstie里看下
使用Frida绕过Android App的SSL Pinning_第5张图片
可以拦截到请求

相关参考资料
https://bbs.pediy.com/thread-222427.htm
https://www.freebuf.com/articles/terminal/214540.html

作者 原文传送
littley www.littley.top

你可能感兴趣的:(逆向)