【问题标题】:Mitm proxy with certificate pinned application带有证书固定应用程序的中间人代理
【发布时间】:2019-04-28 15:28:51
【问题描述】:

我正在尝试模拟对 Signal 的 android 消息应用程序的 MITM 攻击。它是开源的,所以我将mitmproxy-ca-cert.pem 放在android 应用程序中用于固定,也放在移动受信任的证书中。我仍然没有收到任何对服务器的查询。 客户端的错误是

NonSuccessfulResponseCodeException:错误响应:502 Bad Gateway

【问题讨论】:

标签: android ssl-certificate mitmproxy certificate-pinning


【解决方案1】:

如果我理解得很好,您正试图攻击使用证书固定连接 API 服务器的移动设备。

如果是这样,将mitmproxy-ca-cert.pem添加到移动信任商店是不够的,您需要按照google docs配置网络安全文件res/xml/network_security_config.xml

如果您仍然迷路,请尝试关注文章Hands on Mobile Api Security Pinning,看看它是否可以帮助您重回正轨。

编辑

以下说明适用于 Android API 级别 24 及更高版本。

另一个编辑

使用我在下面提供的 bash 脚本的更好方法是使用免费的Mobile Certificate Pinning Generator 在线工具来获取公钥 pin 哈希并为我们生成正确的 Android 网络安全配置文件:

Bash script 从证书公钥生成哈希:

#!/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 设备中的用户信任存储区,然后重新编译应用程序,现在您应该可以拦截请求了。

注意

代码示例已从Currency Converter Demo App 存储库中提取,该存储库被用作文章Steal that API Key with a Man in the Middle Attack 和文章Securing HTTPS with Certificate Pinning on Android 的一部分

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    相关资源
    最近更新 更多