证书别针
我没有将实施此证书的域名。我只有IP地址。
要通过 IP 地址获取服务器证书的 pin,您可以尝试以下 bash 脚本:
#!/bin/bash
# Heavily inspired on:
# * https://medium.com/@appmattus/android-security-ssl-pinning-1db8acb6621e#ecea
set -eu
Main()
{
local domain="${1? Missing domain name to extract and hash the certificate public key !!!}"
local domain="${domain##*://}"
local certs=$( openssl s_client -servername "${domain}" -host "${domain}" -port 443 -showcerts </dev/null 2>/dev/null | sed -n '/Certificate chain/,/Server certificate/p' )
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\n${certificate_name} \n\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 脚本保存到文件并调用它,如下所示:
bash hash-certificate-public-key-from-domain.bash ip-address-here
在我的个人网站上使用它的真实示例:
╭─exadra37@exadra37-Vostro-470 ~/Developer/Approov/currency-converter-demo ‹volley-pinning-with-approov_cleanup-proguard*›
╰─➤ ./bin/hash-certificate-public-key-from-domain.bash 68.183.252.187 130 ↵
CERTIFICATE NAME:
CN = exadra37.com
CERTIFICATE PUBLIC KEY HASH:
1O0wDRM/roe6UTctDVQ5aN/ASNYsGQFVzXYhO34t5GE=
╭─exadra37@exadra37-Vostro-470 ~/Developer/Approov/currency-converter-demo ‹volley-pinning-with-approov_cleanup-proguard*›
╰─➤ ./bin/hash-certificate-public-key-from-domain.bash exadra37.com
CERTIFICATE NAME:
CN = exadra37.com
CERTIFICATE PUBLIC KEY HASH:
1O0wDRM/roe6UTctDVQ5aN/ASNYsGQFVzXYhO34t5GE=
╭─exadra37@exadra37-Vostro-470 ~/Developer/Approov/currency-converter-demo ‹volley-pinning-with-approov_cleanup-proguard*›
╰─➤ ./bin/hash-certificate-public-key-from-domain.bash https://exadra37.com
CERTIFICATE NAME:
CN = exadra37.com
CERTIFICATE PUBLIC KEY HASH:
1O0wDRM/roe6UTctDVQ5aN/ASNYsGQFVzXYhO34t5GE=
如您所见,引脚的哈希值1O0wDRM/roe6UTctDVQ5aN/ASNYsGQFVzXYhO34t5GE=
始终相同。
- pin 字段将包含什么,我没有任何私钥。我在哪里可以找到私钥(sha256/XXXXXXXXXX)?
私钥是私有的,因此如果你能找到它,那么所有的安全性都会丢失,除非你还控制着你要连接的服务器。无论如何,您不需要它来执行证书固定,只需通过生成它的哈希值(也称为 pin)来固定证书的公钥,就像上面的 bash 脚本一样。
因此,sha256
您要查找的是上述脚本中引脚的输出,也就是在我的示例1O0wDRM/roe6UTctDVQ5aN/ASNYsGQFVzXYhO34t5GE=
中。
编辑
另一种方法是使用免费的Mobile Certificate Pinning Generator在线工具,该工具将提取公钥 pin,另外还可以为您生成配置文件:
生成的 Android 配置文件:
实施证书固定
虽然我不知道如何帮助您进行改造,但我可以向您展示一种更简单的实现固定的方法。
如今,对于 Android,存在一种更简单的方法,我在我的博客文章Securing HTTPS With Certificate Pinning中对此进行了描述,您可以在其中了解到,只需将证书 pin 添加到网络安全配置文件中,或者如果您需要支持低于 API 级别 24,那么您可能希望将TrustKit包与文件一起使用。
使用 TrustKit 的示例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>currency-converter-demo.pdm.approov.io</domain>
<trust-anchors>
<!--<certificates src="user" />-->
<certificates src="system" />
</trust-anchors>
<pin-set>
<!-- Pin for: currency-converter-demo.pdm.approov.io -->
<pin digest="SHA-256">qXHiE7hFX2Kj4ZCtnr8u8yffl8w9CTv6kE0U5j0o1XY=</pin>
<!-- Backup Pin for: currency-converter-demo.pdm.approov.io -->
<pin digest="SHA-256">47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=</pin>
</pin-set>
<!-- TrustKit Android API -->
<!-- enforce pinning validation -->
<trustkit-config enforcePinning="true" disableDefaultReportUri="true">
<!-- Add a reporting URL for pin validation reports -->
<report-uri>https://report.domain</report-uri>
</trustkit-config>
</domain-config>
</network-security-config>