安全矩阵

 找回密码
 立即注册
搜索
查看: 3581|回复: 0

APP抓包大法(最全总结)

[复制链接]

991

主题

1063

帖子

4315

积分

论坛元老

Rank: 8Rank: 8

积分
4315
发表于 2021-3-28 08:58:07 | 显示全部楼层 |阅读模式
本帖最后由 gclome 于 2021-4-15 16:09 编辑

原文链接:APP抓包大法(最全总结)


前言:最近工作中遇到一些比较奇葩的App,一边测试一边搜集整理出了比较全的姿势。如有错误之处,还请各位师傅多多指教。


0x01 不走代理的App
如何判断:连接Fiddler代理-->抓不到包-->关闭Fiddler后正常通信。
解决方法:PC端模拟器+如下全局代理抓包工具,筛选出模拟器进程无需配置可以直接解密https流量

1. HTTPAnalyserv7


2. HTTPDebuggerpro


0x02 常规App抓包
1. 普通代理 Fiddler
PS:fiddler 抓app仍是http代理抓包,容易被检测限制
可以在FiddlerScript 中的handle下配置 抓取websocket (多是明文)

  1. static function OnWebSocketMessage(oMsg: WebSocketMessage) {

  2.      // Log Message to the LOG tab

  3.      FiddlerApplication.Log.LogString(oMsg.ToString());
  4. }  
复制代码


2. Charles + postern 可以新建一个VPN配置socks5代理
从而绕过更多抓包限制,Charles处理https包更优秀,配置如下简图,也可自行百度高级用法。



去掉本地windows代理,只抓取移动端的流量




手机端Postern左侧配置代理—添加代理服务器输入上图设置好的socks代理8889端口,代理类型选择socks5,再返回配置规则删除其他规则,添加匹配所有通过刚刚设置好的代理保存。如图:



开启postern,允许连接vpn,浏览器输入chls.pro这个网址下载安装证书。
Charles点击图中的第二步解密按钮,可解密https流量(8.0以上系统可以将证书放到系统证书目录下)

3. VPN手机端使用Httpcanary抓包
使用VPN,流量会强制走VPN通道,可以抓到更多的包。
配置安装证书,有root权限可以把证书安装到系统证书下(7.1及以下系统默认信任用户证书)


点击右下的小飞机即可抓包,右上角有过滤选项可以只抓http tcp等
左边设置目标应用可以指定进程,方便只抓取想要的数据包
PS:安卓7.1及以上,抓取https流量,需要root后把fiddler、burp、charles等工具的的证书安装到系统根证书下
  1. openssl x509 -inform PEM -subject_hash_old -in Desktop.pem |head -1  #获取hash值
  2. 用hash值.0重命名证书
  3. adb push 重命名后证书 /sdcard/
  4. mount -o rw,remount /      #挂载为可读写
  5. mv /sdcard/证书 /etc/security/cacerts/      #系统证书路径
  6. chmod 644 /etc/security/cacerts/证书  #修改权限644
  7. # /data/misc/user/0/cacerts-added   #用户证书路径
复制代码

0x03代理证书检测绕过

如何判断:0x02抓不到的有可能就是代理检测,更直观的判断就是App可以正常使用,打开httpcanary抓不到或者网络连接失败
说到代理检测:先简单介绍下数字证书常见的检测机制
一般来说主要检测证书中{证书链 签发关系 公钥 指纹}等这些内容,所以我们绕过代理检测也要从这些方面入手。
所以实际抓包测试中hook掉系统自带的检测api和常见框架中的检测api即可。然后再利用0x02中的抓包姿势就O了。

下介绍两种方式:
1. Xposed框架+JustTrustMe (0.3)




安装xp框架后直接安装justtrustme的apk,在模块里勾选中开启,然后一定!!重启模拟器/手机,打开关闭xp框架和模块一定要重启手机才能生效。
JustTrustMe的源码<可自行编译>(文末有编译好的apk等本文工具打包下载)
  1. 项目地址:https://github.com/Fuzion24/JustTrustMe
复制代码


从项目代码中可以看到作者hook了很多常见的系统函数、常见框架(okhttp等)HttpsURLConnection 下的API X509TrustManager、HostnameVerifier(域名验证),setSSLSocketFactory()中的sslcontext里的checksevercertificate(),setHostnameVerifier() 方法;okhttp/okhttp3框架中证书Pinner,certificatePinner 下的 check 方法, 设置通信组件中的setSSLSocketFactory() 方法等。

2. Frida + Hook.js
Frida请自行安装,可以参考https://www.jianshu.com/p/c349471bdef7
Frida:hook中常用的两个命令:
  1. frida -UF -l .\hook.js   #注入最前端的进程(当前的app)
  2. frida -U --no-pause -f com.xxx.xxx(包名) -l .\Hook.js  #启动前注入
复制代码


下面的JS代码类似于frida下的增强版的justtrustme ,Hook了下述的一些系统api和框架
1.SSLcontext
2.okhttp
3.webview
4.XUtils
5.httpclientandroidlib
6.JSSE
7.network_security_config (android 7.0+)
8.Apache Http client (support partly)
9.OpenSSLSocketImpl
10.TrustKit
11.Cronet
  1. Java.perform(function () {

  2.     /*
  3.     hook list:
  4.     1.SSLcontext
  5.     2.okhttp
  6.     3.webview
  7.     4.XUtils
  8.     5.httpclientandroidlib
  9.     6.JSSE
  10.     7.network\_security\_config (android 7.0+)
  11.     8.Apache Http client (support partly)
  12.     9.OpenSSLSocketImpl
  13.     10.TrustKit
  14.     11.Cronet
  15.     */

  16.     // Attempts to bypass SSL pinning implementations in a number of
  17.     // ways. These include implementing a new TrustManager that will
  18.     // accept any SSL certificate, overriding OkHTTP v3 check()
  19.     // method etc.
  20.     var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');
  21.     var HostnameVerifier = Java.use('javax.net.ssl.HostnameVerifier');
  22.     var SSLContext = Java.use('javax.net.ssl.SSLContext');
  23.     var quiet_output = false;

  24.     // Helper method to honor the quiet flag.

  25.     function quiet_send(data) {

  26.         if (quiet_output) {

  27.             return;
  28.         }

  29.         send(data)
  30.     }


  31.     // Implement a new TrustManager
  32.     // ref: https://gist.github.com/oleavr/3ca67a173ff7d207c6b8c3b0ca65a9d8
  33.     // Java.registerClass() is only supported on ART for now(201803). 所以android 4.4以下不兼容,4.4要切换成ART使用.
  34.     /*
  35. 06-07 16:15:38.541 27021-27073/mi.sslpinningdemo W/System.err: java.lang.IllegalArgumentException: Required method checkServerTrusted(X509Certificate[], String, String, String) missing
  36. 06-07 16:15:38.542 27021-27073/mi.sslpinningdemo W/System.err:     at android.net.http.X509TrustManagerExtensions.<init>(X509TrustManagerExtensions.java:73)
  37.         at mi.ssl.MiPinningTrustManger.<init>(MiPinningTrustManger.java:61)
  38. 06-07 16:15:38.543 27021-27073/mi.sslpinningdemo W/System.err:     at mi.sslpinningdemo.OkHttpUtil.getSecPinningClient(OkHttpUtil.java:112)
  39.         at mi.sslpinningdemo.OkHttpUtil.get(OkHttpUtil.java:62)
  40.         at mi.sslpinningdemo.MainActivity$1$1.run(MainActivity.java:36)
  41. */
  42.     var X509Certificate = Java.use("java.security.cert.X509Certificate");
  43.     var TrustManager;
  44.     try {
  45.         TrustManager = Java.registerClass({
  46.             name: 'org.wooyun.TrustManager',
  47.             implements: [X509TrustManager],
  48.             methods: {
  49.                 checkClientTrusted: function (chain, authType) { },
  50.                 checkServerTrusted: function (chain, authType) { },
  51.                 getAcceptedIssuers: function () {
  52.                     // var certs = [X509Certificate.$new()];
  53.                     // return certs;
  54.                     return [];
  55.                 }
  56.             }
  57.         });
  58.     } catch (e) {
  59.         quiet_send("registerClass from X509TrustManager >>>>>>>> " + e.message);
  60.     }





  61.     // Prepare the TrustManagers array to pass to SSLContext.init()
  62.     var TrustManagers = [TrustManager.$new()];

  63.     try {
  64.         // Prepare a Empty SSLFactory
  65.         var TLS_SSLContext = SSLContext.getInstance("TLS");
  66.         TLS_SSLContext.init(null, TrustManagers, null);
  67.         var EmptySSLFactory = TLS_SSLContext.getSocketFactory();
  68.     } catch (e) {
  69.         quiet_send(e.message);
  70.     }

  71.     send('Custom, Empty TrustManager ready');

  72.     // Get a handle on the init() on the SSLContext class
  73.     var SSLContext_init = SSLContext.init.overload(
  74.         '[Ljavax.net.ssl.KeyManager;', '[Ljavax.net.ssl.TrustManager;', 'java.security.SecureRandom');

  75.     // Override the init method, specifying our new TrustManager
  76.     SSLContext_init.implementation = function (keyManager, trustManager, secureRandom) {

  77.         quiet_send('Overriding SSLContext.init() with the custom TrustManager');

  78.         SSLContext_init.call(this, null, TrustManagers, null);
  79.     };

  80.     /*** okhttp3.x unpinning ***/


  81.     // Wrap the logic in a try/catch as not all applications will have
  82.     // okhttp as part of the app.
  83.     try {

  84.         var CertificatePinner = Java.use('okhttp3.CertificatePinner');

  85.         quiet_send('OkHTTP 3.x Found');

  86.         CertificatePinner.check.overload('java.lang.String', 'java.util.List').implementation = function () {

  87.             quiet_send('OkHTTP 3.x check() called. Not throwing an exception.');
  88.         }

  89.     } catch (err) {

  90.         // If we dont have a ClassNotFoundException exception, raise the
  91.         // problem encountered.
  92.         if (err.message.indexOf('ClassNotFoundException') === 0) {

  93.             throw new Error(err);
  94.         }
  95.     }

  96.     // Appcelerator Titanium PinningTrustManager

  97.     // Wrap the logic in a try/catch as not all applications will have
  98.     // appcelerator as part of the app.
  99.     try {

  100.         var PinningTrustManager = Java.use('appcelerator.https.PinningTrustManager');

  101.         send('Appcelerator Titanium Found');

  102.         PinningTrustManager.checkServerTrusted.implementation = function () {

  103.             quiet_send('Appcelerator checkServerTrusted() called. Not throwing an exception.');
  104.         }

  105.     } catch (err) {

  106.         // If we dont have a ClassNotFoundException exception, raise the
  107.         // problem encountered.
  108.         if (err.message.indexOf('ClassNotFoundException') === 0) {

  109.             throw new Error(err);
  110.         }
  111.     }

  112.     /*** okhttp unpinning ***/


  113.     try {
  114.         var OkHttpClient = Java.use("com.squareup.okhttp.OkHttpClient");
  115.         OkHttpClient.setCertificatePinner.implementation = function (certificatePinner) {
  116.             // do nothing
  117.             quiet_send("OkHttpClient.setCertificatePinner Called!");
  118.             return this;
  119.         };

  120.         // Invalidate the certificate pinnet checks (if "setCertificatePinner" was called before the previous invalidation)
  121.         var CertificatePinner = Java.use("com.squareup.okhttp.CertificatePinner");
  122.         CertificatePinner.check.overload('java.lang.String', '[Ljava.security.cert.Certificate;').implementation = function (p0, p1) {
  123.             // do nothing
  124.             quiet_send("okhttp Called! [Certificate]");
  125.             return;
  126.         };
  127.         CertificatePinner.check.overload('java.lang.String', 'java.util.List').implementation = function (p0, p1) {
  128.             // do nothing
  129.             quiet_send("okhttp Called! [List]");
  130.             return;
  131.         };
  132.     } catch (e) {
  133.         quiet_send("com.squareup.okhttp not found");
  134.     }

  135.     /*** WebView Hooks ***/

  136.     /* frameworks/base/core/java/android/webkit/WebViewClient.java */
  137.     /* public void onReceivedSslError(Webview, SslErrorHandler, SslError) */
  138.     var WebViewClient = Java.use("android.webkit.WebViewClient");

  139.     WebViewClient.onReceivedSslError.implementation = function (webView, sslErrorHandler, sslError) {
  140.         quiet_send("WebViewClient onReceivedSslError invoke");
  141.         //执行proceed方法
  142.         sslErrorHandler.proceed();
  143.         return;
  144.     };

  145.     WebViewClient.onReceivedError.overload('android.webkit.WebView', 'int', 'java.lang.String', 'java.lang.String').implementation = function (a, b, c, d) {
  146.         quiet_send("WebViewClient onReceivedError invoked");
  147.         return;
  148.     };

  149.     WebViewClient.onReceivedError.overload('android.webkit.WebView', 'android.webkit.WebResourceRequest', 'android.webkit.WebResourceError').implementation = function () {
  150.         quiet_send("WebViewClient onReceivedError invoked");
  151.         return;
  152.     };

  153.     /*** JSSE Hooks ***/

  154.     /* libcore/luni/src/main/java/javax/net/ssl/TrustManagerFactory.java */
  155.     /* public final TrustManager[] getTrustManager() */
  156.     /* TrustManagerFactory.getTrustManagers maybe cause X509TrustManagerExtensions error  */
  157.     var TrustManagerFactory = Java.use("javax.net.ssl.TrustManagerFactory");
  158.     TrustManagerFactory.getTrustManagers.implementation = function(){
  159.         quiet_send("TrustManagerFactory getTrustManagers invoked");
  160.         return TrustManagers;
  161.     }

  162.     var HttpsURLConnection = Java.use("javax.net.ssl.HttpsURLConnection");
  163.     /* libcore/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java */
  164.     /* public void setDefaultHostnameVerifier(HostnameVerifier) */
  165.     HttpsURLConnection.setDefaultHostnameVerifier.implementation = function (hostnameVerifier) {
  166.         quiet_send("HttpsURLConnection.setDefaultHostnameVerifier invoked");
  167.         return null;
  168.     };
  169.     /* libcore/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java */
  170.     /* public void setSSLSocketFactory(SSLSocketFactory) */
  171.     HttpsURLConnection.setSSLSocketFactory.implementation = function (SSLSocketFactory) {
  172.         quiet_send("HttpsURLConnection.setSSLSocketFactory invoked");
  173.         return null;
  174.     };
  175.     /* libcore/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java */
  176.     /* public void setHostnameVerifier(HostnameVerifier) */
  177.     HttpsURLConnection.setHostnameVerifier.implementation = function (hostnameVerifier) {
  178.         quiet_send("HttpsURLConnection.setHostnameVerifier invoked");
  179.         return null;
  180.     };

  181.     /*** Xutils3.x hooks ***/
  182.     //Implement a new HostnameVerifier
  183.     var TrustHostnameVerifier;
  184.     try {
  185.         TrustHostnameVerifier = Java.registerClass({
  186.             name: 'org.wooyun.TrustHostnameVerifier',
  187.             implements: [HostnameVerifier],
  188.             method: {
  189.                 verify: function (hostname, session) {
  190.                     return true;
  191.                 }
  192.             }
  193.         });

  194.     } catch (e) {
  195.         //java.lang.ClassNotFoundException: Didn't find class "org.wooyun.TrustHostnameVerifier"
  196.         quiet_send("registerClass from hostnameVerifier >>>>>>>> " + e.message);
  197.     }

  198.     try {
  199.         var RequestParams = Java.use('org.xutils.http.RequestParams');
  200.         RequestParams.setSslSocketFactory.implementation = function (sslSocketFactory) {
  201.             sslSocketFactory = EmptySSLFactory;
  202.             return null;
  203.         }

  204.         RequestParams.setHostnameVerifier.implementation = function (hostnameVerifier) {
  205.             hostnameVerifier = TrustHostnameVerifier.$new();
  206.             return null;
  207.         }

  208.     } catch (e) {
  209.         quiet_send("Xutils hooks not Found");
  210.     }

  211.     /*** httpclientandroidlib Hooks ***/
  212.     try {
  213.         var AbstractVerifier = Java.use("ch.boye.httpclientandroidlib.conn.ssl.AbstractVerifier");
  214.         AbstractVerifier.verify.overload('java.lang.String', '[Ljava.lang.String', '[Ljava.lang.String', 'boolean').implementation = function () {
  215.             quiet_send("httpclientandroidlib Hooks");
  216.             return null;
  217.         }
  218.     } catch (e) {
  219.         quiet_send("httpclientandroidlib Hooks not found");
  220.     }

  221.     /***
  222. android 7.0+ network_security_config TrustManagerImpl hook
  223. apache httpclient partly
  224. ***/
  225.     var TrustManagerImpl = Java.use("com.android.org.conscrypt.TrustManagerImpl");
  226.     // try {
  227.     //     var Arrays = Java.use("java.util.Arrays");
  228.     //     //apache http client pinning maybe baypass
  229.     //     //https://github.com/google/conscrypt/blob/c88f9f55a523f128f0e4dace76a34724bfa1e88c/platform/src/main/java/org/conscrypt/TrustManagerImpl.java#471
  230.     //     TrustManagerImpl.checkTrusted.implementation = function (chain, authType, session, parameters, authType) {
  231.     //         quiet_send("TrustManagerImpl checkTrusted called");
  232.     //         //Generics currently result in java.lang.Object
  233.     //         return Arrays.asList(chain);
  234.     //     }

  235.     // } catch (e) {
  236.     //     quiet_send("TrustManagerImpl checkTrusted nout found");
  237.     // }

  238.     try {
  239.         // Android 7+ TrustManagerImpl
  240.         TrustManagerImpl.verifyChain.implementation = function (untrustedChain, trustAnchorChain, host, clientAuth, ocspData, tlsSctData) {
  241.             quiet_send("TrustManagerImpl verifyChain called");
  242.             // Skip all the logic and just return the chain again :P
  243.             //https://www.nccgroup.trust/uk/about-us/newsroom-and-events/blogs/2017/november/bypassing-androids-network-security-configuration/
  244.             // https://github.com/google/conscrypt/blob/c88f9f55a523f128f0e4dace76a34724bfa1e88c/platform/src/main/java/org/conscrypt/TrustManagerImpl.java#L650
  245.             return untrustedChain;
  246.         }
  247.     } catch (e) {
  248.         quiet_send("TrustManagerImpl verifyChain nout found below 7.0");
  249.     }
  250.     // OpenSSLSocketImpl
  251.     try {
  252.         var OpenSSLSocketImpl = Java.use('com.android.org.conscrypt.OpenSSLSocketImpl');
  253.         OpenSSLSocketImpl.verifyCertificateChain.implementation = function (certRefs, authMethod) {
  254.             quiet_send('OpenSSLSocketImpl.verifyCertificateChain');
  255.         }

  256.         quiet_send('OpenSSLSocketImpl pinning')
  257.     } catch (err) {
  258.         quiet_send('OpenSSLSocketImpl pinner not found');
  259.     }
  260.     // Trustkit
  261.     try {
  262.         var Activity = Java.use("com.datatheorem.android.trustkit.pinning.OkHostnameVerifier");
  263.         Activity.verify.overload('java.lang.String', 'javax.net.ssl.SSLSession').implementation = function (str) {
  264.             quiet_send('Trustkit.verify1: ' + str);
  265.             return true;
  266.         };
  267.         Activity.verify.overload('java.lang.String', 'java.security.cert.X509Certificate').implementation = function (str) {
  268.             quiet_send('Trustkit.verify2: ' + str);
  269.             return true;
  270.         };

  271.         quiet_send('Trustkit pinning')
  272.     } catch (err) {
  273.         quiet_send('Trustkit pinner not found')
  274.     }

  275.     try {
  276.         //cronet pinner hook
  277.         //weibo don't invoke

  278.         var netBuilder = Java.use("org.chromium.net.CronetEngine$Builder");

  279.         //https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/CronetEngine.Builder.html#enablePublicKeyPinningBypassForLocalTrustAnchors(boolean)
  280.         netBuilder.enablePublicKeyPinningBypassForLocalTrustAnchors.implementation = function (arg) {

  281.             //weibo not invoke
  282.             console.log("Enables or disables public key pinning bypass for local trust anchors = " + arg);

  283.             //true to enable the bypass, false to disable.
  284.             var ret = netBuilder.enablePublicKeyPinningBypassForLocalTrustAnchors.call(this, true);
  285.             return ret;
  286.         };

  287.         netBuilder.addPublicKeyPins.implementation = function (hostName, pinsSha256, includeSubdomains, expirationDate) {
  288.             console.log("cronet addPublicKeyPins hostName = " + hostName);

  289.             //var ret = netBuilder.addPublicKeyPins.call(this,hostName, pinsSha256,includeSubdomains, expirationDate);
  290.             //this 是调用 addPublicKeyPins 前的对象吗? Yes,CronetEngine.Builder
  291.             return this;
  292.         };

  293.     } catch (err) {
  294.         console.log('[-] Cronet pinner not found')
  295.     }
  296. });
复制代码

0x04 总结

常见的App抓包姿势差不多就这些了,基本可以抓到未加固的或者debug版的App的数据包。代码混淆,入口加固,资源加固等App,混淆类的需要通过匹配函数的参数类型找到修改后的函数名自己重写方法、加固类的需要jadx-guif反编译后分析加固的的逻辑找到App的函数入口让其初始化解密后再重写相关函数等。后期再找案例分析。

参考链接:
https://www.anquanke.com/post/id/201219
https://www.jianshu.com/p/c349471bdef7

抓包工具打包下载,公众号回复:1148


















回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-11-29 00:44 , Processed in 0.014083 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表