Charles Android 抓包失敗SSLHandshake: Received fatal alert: certificate_unknown


前提:

Android使用Charles抓取Https請求的報文時,Android和Charles都正確安裝了證書之后出現抓包失敗,報錯SSLHandshake: Received fatal alert: certificate_unknown,如下圖所示:

原因:

安卓7之后調整了安全策略會導致部分手機抓包失敗,請參考此鏈接:https://android-developers.googleblog.com/2016/07/changes-to-trusted-certificate.html

文中提到默認情況下,針對API Level 24及更高版本的應用程序不再信任用戶或管理員添加的CA用於安全連接。意思就是就算你在手機上安裝了受信任的證書也是沒卵用的。

解決辦法 一:

前提你的手機上已經正確安裝了Charles證書:

  1. 在你的AndroidManifest.xml文件中添加如下配置:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
        <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
    ...
    </application>
    </manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  2. 在res目錄下新建一個xml文件夾,之后在res/xml/路徑下新建文件network_security_config.xml

    res/xml/network_security_config.xml:

    <?xml version="1.0" encoding="utf-8"?>
        <network-security-config>
            <domain-config>
            <domain includeSubdomains="true">你要抓取的域名</domain>
            <trust-anchors>
            <certificates src="user"/>//信任用戶自己安裝的證書
            </trust-anchors>
        </domain-config>
    </network-security-config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

解決辦法 二:

手機上是否有裝證書都可以使用下面的方法:

  1. 在你的AndroidManifest.xml文件中添加如下配置:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
        <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
    ...
    </application>
    </manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  2. 在res目錄下新建一個xml文件夾,之后在res/xml/路徑下新建文件network_security_config.xml

    res/xml/network_security_config.xml:

    <?xml version="1.0" encoding="utf-8"?>
        <network-security-config>
            <domain-config>
            <domain includeSubdomains="true">你要抓取的域名</domain>
            <trust-anchors>
            <certificates src="@raw/證書文件名"/>
            </trust-anchors>
        </domain-config>
    </network-security-config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  3. 在res目錄下新建一個raw文件夾,將手機上安裝的證書文件放入res/raw/目錄下,證書格式:pem,ca等(chales的話就是將你在手機瀏覽器打開http://charlesproxy.com/getssl下載的證書放入即可),步驟2中的證書文件名,就是你放入res/raw/目錄下文件的名字

配置完重新運行項目,就可以看到報文了!

更多配置方法請參考Google-Android

轉載:https://blog.csdn.net/mrxiagc/article/details/75329629


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM