Frida绕开SSL签名验证,通杀

脚本

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/FiddlerRoot.cer");
        }
        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 提取 SSL 双向认证中的证书 为了实现这一目标,可以利用 Frida 动态注入 JavaScript 脚本来挂钩应用程序内的特定函数调用。具体来说,在处理 HTTPS 请求的过程中,通常会涉及到 `SSLSocket` 或者更高层次封装的对象如 OkHttp 的 `OkHttpClient`。 对于 Android 应用程序而言,很多情况下网络库会在建立安全连接前执行 SSL Pinning 检查。如果要获取双向认证过程中交换的证书,则可以通过拦截这些操作来访问原始数据流并从中抽取所需的 X.509 证书对象[^2]。 下面是一个简单的例子展示怎样编写一段用于提取证书信息的脚本: ```javascript Java.perform(function () { var SSLSocketImpl = Java.use('com.android.org.conscrypt.OpenSSLSocketImpl'); // Hook the beginHandshake method to capture certificates during handshake process. SSLSocketImpl.beginHandshake.implementation = function() { console.log("[*] Handshake started"); this.beginHandshake(); var session = this.getSession(); if (session !== null && typeof(session) != 'undefined') { var peerCertificates = session.getPeerCertificates(); if(peerCertificates.length > 0){ for(var i=0; i<peerCertificates.length;i++){ var cert = peerCertificates[i]; console.log("Server Certificate [" + i + "] :"); console.log(cert.toString()); // Optionally save certificate as PEM file or send it over network } try{ var localCerts = session.getLocalCertificates(); if(localCerts!=null&&localCerts.length>0){ console.log("\nClient Certificates:"); for(i=0;i<localCerts.length;i++){ cert = localCerts[i]; console.log("Local Certificate [" + i + "] :"); console.log(cert.toString()); // Similarly, you can choose how to handle client-side certs here } }else{ console.log("No Client Certificates found."); } }catch(e){ console.error("Error retrieving Local Certificates:", e); } } else { console.warn("Failed to retrieve server certificates."); } } else { console.warn("Session object is not available after handshake completion."); } }; }); ``` 此代码片段展示了如何通过修改 `beginHandshake()` 方法的行为来捕获握手期间传递给客户端的服务端证书以及可能存在的客户端证书。需要注意的是实际环境中可能会遇到不同的类名或方法签名取决于所使用的加密库版本和其他因素[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值