加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

Frida应用基础及APP https证书验证破解

发布时间:2019-06-03 21:30:08 所属栏目:建站 来源:牛冠杰
导读:一、Frida简介 1. 基础介绍 Frida是适用于开发人员、逆向工程师和安全研究人员的轻量级Hook工具,它允许将JavaScript代码或库注入Windows、macOS、GNU / Linux、iOS、Android和QNX上的本机应用程序。此外, Frida还提供了一些基于Frida API构建的简单工具

Apache http client 因为从api23起被Android抛弃,因此使用率较低。目前更多使用的是HttpURLConnection类或第三方库OKhttp3.0进行HTTPS通信。其中OKhttp3.0的部分运用与HttpURLConnection相同,客户端都可以通过实现X509TrustManager接口的checkServerTrusted方法,将服务器证书与APP预埋证书做对比,来完成强校验。此外,也可以再通过实现HostnameVerifier接口的verify方法,校验服务器证书中的一般名称(CN)是否与域名相符。通过使用上述方法,完成客户端对服务端的证书强校验。

(2) 应用分析

由于这次是自编译的文件,就不通过APK反编译等方法查看代码了,直接看主要通信类HttpClientSslHelper的源码。

  1. public class HttpClientSslHelper { 
  2.     public static boolean isServerTrusted1 = true;  //强校验关键参数 
  3.     private static SSLContext sslContext = null; 
  4.     public static OkHttpClient getSslOkHttpClient(Context context) { 
  5.         OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
  6.         builder.sslSocketFactory(getSslContextByCustomTrustManager(context).getSocketFactory()) 
  7.                 .hostnameVerifier(new HostnameVerifier() { 
  8.                     @Override  //实现自定义的HostnameVerifier 对象的verify校验方法 
  9.                     public boolean verify(String hostname, SSLSession session) { 
  10.                         Log.d("HttpClientSslHelper", "hostname = " + hostname); 
  11.                         if ("192.168.96.1".equals(hostname)) { 
  12.                             //根据isServerTrusted1的值进行校验,若为True,则校验成功,执行后续连接 
  13.                             return isServerTrusted1; 
  14.                         } else { 
  15.                             //由于是实验环境,if语句总为真,不会执行else语句 
  16.                             HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); 
  17.                             return hv.verify("localhost", session); 
  18.                         } 
  19.                     } 
  20.                 }); 
  21.         return builder.build(); 
  22.     } 
  23.  
  24.     public static SSLContext getSslContextByCustomTrustManager(Context context) { 
  25.         if (sslContext == null) { 
  26.             try { 
  27.                 CertificateFactory cf = CertificateFactory.getInstance("X.509","BC"); 
  28.                 InputStream caInput = new BufferedInputStream(context.getResources().getAssets().open("server.cer")); 
  29.                 final Certificate ca; 
  30.                 try { 
  31.                     ca = cf.generateCertificate(caInput); 
  32.                 } finally { 
  33.                     caInput.close(); 
  34.                 } 
  35.                 sslContext = SSLContext.getInstance("TLS"); 
  36.                 //自定义X509TrustManager接口的checkClientTrusted、checkServerTrusted和getAcceptedIssuers方法 
  37.                 sslContext.init(null, new X509TrustManager[]{new X509TrustManager() { 
  38.                     public void checkClientTrusted(X509Certificate[] chain, 
  39.                                                    String authType) throws CertificateException { 
  40.                         Log.d("HttpClientSslHelper", "checkClientTrusted --> authType = " + authType); 
  41.                         //校验客户端证书 
  42.                     } 
  43.  
  44.                     public void checkServerTrusted(X509Certificate[] chain, 
  45.                                                    String authType) throws CertificateException { 
  46.                         Log.d("HttpClientSslHelper", "checkServerTrusted --> authType = " + authType); 
  47.                         //校验服务器证书 
  48.                         for (X509Certificate cert : chain) { 
  49.                             cert.checkValidity(); 
  50.                             try { 
  51.                                  //关键点,用APP中内置的服务端证书server.cer来校验网络连接中服务器颁发的证书 
  52.                                 cert.verify(ca.getPublicKey()); 
  53.                             } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) { 
  54.                                 e.printStackTrace(); 
  55.                                 //校验失败,则更改isServerTrusted1值为false 
  56.                                 isServerTrusted1 = false; 
  57.                             } 
  58.                         } 
  59.                     } 
  60.  
  61.                     public X509Certificate[] getAcceptedIssuers() { 
  62.                         return new X509Certificate[0]; 
  63.                     } 
  64.                 }}, null); 
  65.             } catch (Exception e) { 
  66.                 e.printStackTrace(); 
  67.             } 
  68.         } 
  69.         return sslContext; 
  70.     } 

由上可知,将HttpClientSslHelper类的getSslContextByCustomTrustManager方法重写,重新实现checkServerTrusted方法,让其不修改isServerTrusted1的值,即可绕过证书强校验。

(3) 开始Hook

重新实现checkServerTrusted方法

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读