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

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

发布时间:2019-06-04 00:04:46 所属栏目:建站 来源:牛冠杰
导读:一、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要领

(编辑:湖南网)

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

热点阅读