具有证书固定应用程序的Mitm代理

时间:2018-11-26 22:57:52

标签: android ssl-certificate mitmproxy certificate-pinning

我正在尝试模拟对信号的android消息传递应用程序的MITM攻击。它是开源的,因此我将mitmproxy-ca-cert.pem放在android应用程序中进行固定,并将其放入移动受信任证书中。我仍然没有任何查询到服务器。 客户端错误是

  

NonSuccessfulResponseCodeException:错误响应:502错误网关

1 个答案:

答案 0 :(得分:2)

如果我理解得很好,您正在尝试攻击使用证书固定连接API服务器的手机。

如果是这样,那么仅将mitmproxy-ca-cert.pem添加到移动信任存储是不够的,您需要根据google docs配置网络安全文件res/xml/network_security_config.xml

如果您仍然感到迷茫,请尝试阅读文章Hands on Mobile Api Security Pinning,以查看它是否有助于您回到正轨。

编辑

以下说明对Android API级别24及更高版本有效。

Bash脚本,用于从证书公钥生成哈希:

#!/bin/bash
# Heavily inspired on:
#   * https://medium.com/@appmattus/android-security-ssl-pinning-1db8acb6621e#ecea

set -eu

Main()
{
    local certificate_path="${1? Missing path to certificate.}"

    local certs="$( cat ${certificate_path} )"
    local rest=$certs

    while [[ "$rest" =~ '-----BEGIN CERTIFICATE-----' ]]; do

        cert="${rest%%-----END CERTIFICATE-----*}-----END CERTIFICATE-----"
        rest=${rest#*-----END CERTIFICATE-----}

        local certificate_name="$( echo "$cert" | grep 's:' | sed 's/.*s:\(.*\)/\1/' )"

        if [ -n "${certificate_name}" ]; then
            printf "\nCERTIFICATE NAME: \n ${certificate_name} \n"
        fi

        printf "\nCERTIFICATE PUBLIC KEY HASH:\n\n"

        echo "$cert" |
            openssl x509 -pubkey -noout |
            openssl rsa -pubin -outform der 2>/dev/null |
            openssl dgst -sha256 -binary |
            openssl enc -base64

        echo

        exit 0

    done
}

Main ${@}



将上述bash脚本保存在bin路径中,然后像这样使用它:

$ hash-certificate-public-key.sh ~/path/to/mitmproxy-ca-cert.pem

CERTIFICATE PUBLIC KEY HASH:

gsGj6crKw/RebflwkwGIKxngaZaVxP7UsUtuF71VKDw=

现在复制粘贴哈希并将其添加到此文件src/main/res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

    <!-- Official Android N API -->
    <!--https://android-developers.googleblog.com/2016/07/changes-to-trusted-certificate.html-->
    <domain-config>
        <domain>the-domain-to-pin.com</domain>
        <trust-anchors>
            <certificates src="user" />
            <!-- <certificates src="system" /> -->
        </trust-anchors>
        <pin-set>
            <!-- THE MITM CERTIFICATE HASH -->
            <pin digest="SHA-256">gsGj6crKw/RebflwkwGIKxngaZaVxP7UsUtuF71VKDw=</pin>
        </pin-set>
    </domain-config>

</network-security-config>

现在将其包含在AndroidManifest.xml中:

<application
        android:allowBackup="true"
        <!--omitted-->
        android:networkSecurityConfig="@xml/network_security_config">

如果尚未完成,则已将mitmproxy证书添加到Android设备中的用户信任存储中,然后重新编译该应用程序,现在您应该可以拦截请求了。