ssl pining_通过android上的ssl pining保护您的https连接

本文介绍如何使用SSL Pinning增强Android应用的HTTPS连接安全性,包括设置OkHTTP、获取及验证证书指纹等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ssl pining

Welcome to this serie of articles about Android Security and how you can improve tremendously the protection of your users, by implementing 3 things that provide a great effort/safety ratio.

欢迎阅读有关Android安全性的文章系列,以及如何通过实现3个提供极大努力/安全性比率的方法来极大地改善对用户的保护。

Today we are going to look at protecting the HTTPS connection. Your APIs, your user data use that link and it is crucial that the data is encrypted and not readable during the transfer, moreover that the server the App is communicating with is the one intended.

今天,我们将研究保护HTTPS连接。 您的API,您的用户数据使用该链接,并且在传输过程中对数据进行加密且不可读是至关重要的,此外,与应用程序通信的服务器是预期的服务器。

先决条件 (Prerequisite)

An HTTPS connection to your API, and if you do not use HTTPS yet, then start immediately.

与您的API的HTTPS连接,如果尚未使用HTTPS,请立即启动

You will need OkHTTP (or Retrofit), that is providing a method to setup SSL Pinning with just a couple of line of code.

您将需要OkHTTP (或Retrofit),它提供了一种仅用几行代码即可设置SSL Pinning的方法。

A browser or access to the HTTPS certificate.

浏览器或对HTTPS证书的访问。

OpenSSL

的OpenSSL

查找PIN码 (Finding the PIN)

HTTPS certificates are coming with a a Fingerprint, this is a hash (SHA1, SHA256) of the actual certificate. and we are going to use it to verify the authenticity of the server we are connecting to.

HTTPS证书附带一个指纹,这是实际证书的哈希(SHA1,SHA256)。 我们将使用它来验证所连接服务器的真实性。

With a Browser, connect to your HTTPS domain. Click on the padlock icon to show the Certificate details.

使用浏览器,连接到您的HTTPS域。 单击挂锁图标以显示证书详细信息。

Image for post
Medium HTTPS certificate
中型HTTPS证书

On the screenshot above, with Safari, we can see at the bottom the Fingerprints.

在上面的屏幕快照中,使用Safari,我们可以在底部看到“指纹”。

I do recommend you to use SHA-256 for your SSL Pinning as it is more secure than SHA1.

我确实建议您使用SHA-256进行SSL固定,因为它比SHA1更安全。

Keep the SHA-256 somewhere in your editor.

将SHA-256放在编辑器中的某个位置。

you can also use openssl command line to get the SHA-256

您还可以使用openssl命令行获取SHA-256

SHA256:openssl x509 -noout -fingerprint -sha256 -inform pem -in cert.pem

SHA256: openssl x509 -noout -fingerprint -sha256 -inform pem -in cert.pem

SHA256 with Base64 encode: openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dust -sha256 -binary | openssl enc -base64

使用Base64编码的SHA256: openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dust -sha256 -binary | openssl enc -base64 openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dust -sha256 -binary | openssl enc -base64

使用指纹来验证证书 (Using the Fingerprint to verify the certificate)

The idea of SSL Pinning is to checksum the SSL certificate that we are going to connect to and apply a Hash algorithm, in this instance SHA256.

SSL固定的想法是对我们将要连接的SSL证书进行校验和并应用哈希算法,在本例中为SHA256。

OkHTTP is going to apply the same SHA256 checksum algorithm on the certificate it is connecting to, and compare it with the provided and trusted SHA256 checksum found earlier.

OkHTTP将对要连接的证书应用相同的SHA256校验和算法,并将其与之前提供的受信任的SHA256校验和进行比较。

If the checksums match, it means the certificate OkHTTP is connected to, is genuine and therefore we can proceed with the requests.

如果校验和匹配,则表明OkHTTP连接的证书是真实的,因此我们可以继续进行请求。

OkHTTP is providing CertificatePinner to make that task easy.

OkHTTP提供了CertificatePinner来简化该任务。

The implementation looks like this:

实现看起来像这样:

val certificatePinner = CertificatePinner.Builder()
.add("www.domain.com",
// base64encoded SHA256
"sha256/dlpedDsLEvgbase64encodedSHA256ky16vi1obFFn/yOh=")
.build()// set the certificatePinner to OkHTTP builder
val okHttpBuilder = OkHttpClient.Builder()
okHttpBuilder.certificatePinner(certificatePinner)

And that’s it! OkHTTP will now verify that the certificate it connects to, is genuine.

就是这样! OkHTTP现在将验证其连接的证书是真实的。

处理SSL证书到期 (Handling SSL Certificates expiry)

SSL Certificates have an expiry date, which mean the SHA-256 has also an expiry date. When the certificate will become expired, you will have to provide a new SHA-256 fingerprint for the new certificate. Thankfully the CertificatePinner can handle multiple PINs:

SSL证书具有到期日期,这意味着SHA-256也具有到期日期。 证书过期后,您将必须为新证书提供新的SHA-256指纹。 值得庆幸的是,CertificatePinner可以处理多个PIN:

val certificatePinner = CertificatePinner.Builder()
.add("www.domain.com",
// base64encoded SHA256 - old cert
"sha256/oLdCeRtDsLEvgbase64encodedSHA256ky16vi1obFFn/yOh=") .add("www.domain.com",
// base64encoded SHA256 - new cert
"sha256/nEwCeRtldpsDejbase64encodedSHA256ky16vi1obFFn/yOh=") .build()

So if you get the new certificate in advance you can hardcode the new fingerprint into your app and then ask your user to update the app if the fingerprint is expired, so they get the latest version with the new fingerprint.

因此,如果您提前获得了新证书,则可以将新指纹硬编码到您的应用中,然后要求您的用户更新该指纹是否过期的应用,以便他们获得带有新指纹的最新版本。

But there is other technical solutions, like downloading the PINs that you would encrypt on the server and decrypt on the App, using a passphrase, so PINs can dynamically change.

但是,还有其他技术解决方案,例如使用密码短语下载在服务器上加密并在应用程序上解密的PIN,以便PIN可以动态更改。

结论 (Conclusion)

In this article we have seen how OkHTTP with CertificatePinner can help improve the security of your app. Handling certificate expiry can be tricky, but it will improve your app’s security and protect against main-in-the-middle attacks.

在本文中,我们了解了带有CertificatePinner的OkHTTP如何帮助提高应用程序的安全性。 处理证书到期可能很棘手,但是它将提高应用程序的安全性并防止中间人攻击。

翻译自: https://medium.com/@sonique6784/protect-your-https-connection-with-ssl-pinning-on-android-21860326c3cf

ssl pining

### 应用抓包时网络异常的解决方案 当遇到应用抓包过程中显示网络异常的情况,即使已经确保根证书导入系统,仍可能存在其他原因导致问题。以下是可能的原因分析以及对应的解决方案: #### 1. **确认根证书安装是否成功** 尽管提到已将根证书导入系统,但仍需验证其有效性。可以通过以下命令检查Fiddler根证书的哈希值并将其与设备上的证书匹配: ```bash openssl x509 -subject_hash_old -in FiddlerRoot.pem ``` 此操作可以帮助确认根证书是否正确加载到目标设备上[^3]。 #### 2. **处理SSL Pinning的影响** 如果目标应用启用了SSL Pinning机制,则可能导致抓包失败或网络异常现象。针对这种情况,可采用以下工具和技术进行绕过: - 使用Lsposed框架配合插件`TrustMeAlready`来禁用SSL证书校验功能[^4]。 - 对于较低版本Android系统,还可以尝试Xposed框架下的模块`JustTrustMe`实现类似效果[^1]。 #### 3. **应对双向认证场景** 对于部分实施了更严格安全策略的应用程序来说,除了普通的单向SSL pining外还可能会涉及到客户端身份验证即所谓的“双向认证”。此时单纯依靠上述方法可能无法解决问题,需要进一步采取措施比如定位APK内部存储的具体公钥文件位置并通过修改源码或者hook相关函数的方式来完成整个流程中的信任链重建工作。 #### 4. **利用高级流量捕获技术** 假如常规手段均告失效,则可以探索更加深入的技术途径如借助R0Capture脚本来获取未被加密之前的数据流副本(前提是该应用程序并未部署私有的ssl通信协议栈): ```python import r0capture as rcap rcap.start_dump() # Your code here... rcap.stop_dump() ``` 以上就是关于如何解决在进行了必要的前期准备之后仍然遭遇到了所谓‘网络错误’状况的一些思路分享;当然每种情况都有所差异,在实际操作当中还需要灵活运用各种技巧加以调试优化直至最终达成目的为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值