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

前提:

Android使用Charles抓取Https请求的报文时,Android和Charles都正确安装了证书以后出现抓包失败,报错SSLHandshake: Received fatal alert: certificate_unknown,以下图所示:html

缘由:

安卓7以后调整了安全策略会致使部分手机抓包失败,请参考此连接:https://android-developers.googleblog.com/2016/07/changes-to-trusted-certificate.htmlandroid

文中提到默认状况下,针对API Level 24及更高版本的应用程序再也不信任用户或管理员添加的CA用于安全链接。意思就是就算你在手机上安装了受信任的证书也是没卵用的。浏览器

解决办法 一:

前提你的手机上已经正确安装了Charles证书:安全

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

    <?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.xmldom

    res/xml/network_security_config.xml:google

    <?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

解决办法 二:

手机上是否有装证书均可以使用下面的方法:spa

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

    <?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.xmlcode

    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

相关文章
相关标签/搜索