V2EX 10月10日 18:56
动态SSL配置防抓包方案
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

为了防止应用被抓包,需要配置HTTPS证书。动态配置SSL证书可以在证书过期时跳过校验,确保应用正常运行。通过代码动态添加证书,判断证书有效性,如果无效或找不到证书则直接信任,有效则进行证书认证。这样可以避免静态配置证书过期后无法联网的问题。

🔒 动态配置SSL证书可以在证书过期时跳过校验,确保应用正常运行,避免静态配置证书过期后无法联网的问题。

📄 动态添加证书,支持从字节数组或assets中加载证书,方便灵活管理。

📅 动态判断证书有效性,如果证书过期或找不到则直接信任,有效则进行证书认证,平衡安全性与可用性。

🛡️ 通过公钥指纹验证证书指纹,防止证书伪造,提高安全性,避免中间人攻击。

🔄 支持重置SSL配置,方便测试和调试,确保配置的正确性和灵活性。

帮公司的安卓小哥来提个问题:之前项目是没有配置证书的,可以被抓包,现在为了防止抓包,就需要配置 https 证书。首先静态配置证书是属于写到 network_security_config_app.xml 这个证书,直接配置到网络认证文件里,这个种情况有个不好就是如果哪天证书过期了,那就无法通过网络认证了,但是不希望如此,希望如果过期了那就忽略认证,所以需要动态配置 ssl 不直接配置到文件中,通过代码去认证,先去判断证书有效,如果无效或者不能找到证书直接走信任证书,如果有效那就走证书认证,目前没配置的表现就是可以被抓包。请问大家,有过这方面的经验吗? GPT 代码帮忙解决了,但是因为是知识盲区想请教一下诸位。package com.wlld.common.network

import android.content.Contextimport android.util.Logimport com.wlld.common.utils.LogUtilsimport okhttp3.OkHttpClientimport javax.net.ssl.SSLContextimport javax.net.ssl.TrustManagerimport javax.net.ssl.X509TrustManagerimport java.security.SecureRandom

/**

package com.wlld.common.network

import android.content.Contextimport android.util.Logimport com.wlld.common.Rimport java.io.ByteArrayInputStreamimport java.io.InputStreamimport java.security.KeyStoreimport java.security.KeyStoreExceptionimport java.security.NoSuchAlgorithmExceptionimport java.security.PrivateKeyimport java.security.cert.Certificateimport java.security.cert.CertificateExceptionimport java.security.cert.CertificateFactoryimport java.security.cert.X509Certificateimport java.util.Dateimport javax.net.ssl.TrustManagerFactoryimport javax.net.ssl.X509TrustManager

/**

// // 如果自定义证书有效,优先使用// for (customCert in customCertificates) {// if (!isCertificateExpired(customCert)) {// // 检查自定义证书是否覆盖 weilaiqiyuan.com 域名// val subjectCN = extractCommonName(customCert.subjectDN.toString())// if (subjectCN.contains("*.weilaiqiyuan.com") || subjectCN.contains("weilaiqiyuan.com")) {// Log.d(TAG, "使用有效的自定义证书验证 weilaiqiyuan.com 域名: $subjectCN")// return true// }// }// }

    // 检查链中的证书是否与自定义证书匹配    for (cert in chain) {        for (customCert in customCertificates) {            if (cert.subjectDN == customCert.subjectDN) {                // 找到匹配的自定义证书                if (!isCertificateExpired(customCert)) {                    // 🔍 严格验证证书:比较公钥指纹,防止证书伪造                    if (verifyCertificateFingerprint(cert, customCert)) {                        Log.d(TAG, "✅ 证书验证通过(指纹匹配): ${cert.subjectDN}")                        return true                    } else {                        Log.w(TAG, "❌ 证书验证失败(指纹不匹配)- 可能是抓包攻击!")                        // 指纹不匹配,拒绝连接                        throw CertificateException("证书指纹验证失败,可能存在中间人攻击")                    }                } else {                    Log.w(TAG, "自定义证书已过期: ${cert.subjectDN}")                    return false                }            }        }    }        return false}/** * 验证证书指纹,防止证书伪造 */private fun verifyCertificateFingerprint(cert1: X509Certificate, cert2: X509Certificate): Boolean {    return try {        // 比较公钥的 SHA-256 指纹        val pubkey1 = cert1.publicKey.encoded        val pubkey2 = cert2.publicKey.encoded                val digest1 = java.security.MessageDigest.getInstance("SHA-256").digest(pubkey1)        val digest2 = java.security.MessageDigest.getInstance("SHA-256").digest(pubkey2)                val fingerprint1 = digest1.joinToString("") { "%02X".format(it) }        val fingerprint2 = digest2.joinToString("") { "%02X".format(it) }                val isValid = fingerprint1 == fingerprint2        Log.d(TAG, "证书指纹对比: $fingerprint1 vs $fingerprint2, 匹配: $isValid")        isValid    } catch (e: Exception) {        Log.e(TAG, "证书指纹验证失败", e)        false    }}/** * 从 SubjectDN 中提取 Common Name */private fun extractCommonName(subjectDN: String): String {    val cnPattern = "CN=([^,]+)".toRegex()    val match = cnPattern.find(subjectDN)    return match?.groupValues?.get(1) ?: ""}/** * 获取自定义证书信息 */fun getCertificateInfo(): List<String> {    return customCertificates.map { cert ->        "Subject: ${cert.subjectDN}, Issuer: ${cert.issuerDN}, Expires: ${cert.notAfter}, Expired: ${isCertificateExpired(cert)}"    }}

}

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

动态SSL配置 防抓包 HTTPS证书 证书校验 Android开发
相关文章