Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to prevent Android App from grabbing bags

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"Android App怎么防止抓包",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android App怎么防止抓包"吧!

正文

当我们进行网络请求的时候,一般通过URL的openConnection来建立连接,代码如下:

URLConnection conn = url.openConnection()

其实openConnection这个函数还有一个版本,可以传入一个proxy对象,代码如下:

public URLConnection openConnection(Proxy proxy) throws java.io.IOException

这样我们通过这个函数建立连接时传入一个Proxy.NO_PROXY,即可达到防止抓包的效果,如Charles等抓包工具就无法看到我们的链接信息了,代码如下

URLConnection conn = url.openConnection(Proxy.NO_PROXY)

官方对于Proxy.NO_PROXY描述如下:

/** * A proxy setting that represents a {@code DIRECT} connection, * basically telling the protocol handler not to use any proxying. * Used, for instance, to create sockets bypassing any other global * proxy settings (like SOCKS): *

* {@code Socket s = new Socket(Proxy.NO_PROXY);} * */public final static Proxy NO_PROXY = new Proxy();// Creates the proxy that represents a {@code DIRECT} connection.private Proxy() { type = Type.DIRECT; sa = null;}

我么可以看到NO_PROXY实际上就是type属性为DIRECT的一个Proxy对象,这个type有三种:

DIRECT

HTTP

SOCKS

官方描述如下:

public enum Type { /** * Represents a direct connection, or the absence of a proxy. */ DIRECT, /** * Represents proxy for high level protocols such as HTTP or FTP. */ HTTP, /** * Represents a SOCKS (V4 or V5) proxy. */ SOCKS};

这样因为是直连,所以不走代理。所以Charles等工具就抓不到包了,这样一定程度上保证了数据的安全。

当然这种方式只是通过代理抓不到包,如果直接通过路由还是可以抓包的。

补充:使用证书校验

这种方式要在app嵌入证书,以okhttp为例:

当okhttp使用X509TrustManager对服务器证书进行校验时,如果服务器证书的 subjectDN 和嵌入证书的 subjectDN 一致,我们再进行签名内容 signature 的比对,如果不一致,抛出异常。示例代码如下:

首先从本地读出证书,获取一个X509Certificate

val myCrt: X509Certificate by lazy { getCrt(R.raw.my_ca)}private fun getCrt(@RawRes raw: Int): X509Certificate { val certificateFactory = CertificateFactory.getInstance("X.509") val input = ApplicationContext.resources.openRawResource(raw) input.use { return certificateFactory.generateCertificate(input) as X509Certificate }}

检查服务器证书时对比嵌入的证书

private fun getTrustManagerInRelease(): X509TrustManager { return object : X509TrustManager { override fun checkClientTrusted(chain: Array, authType: String?) {} override fun getAcceptedIssuers(): Array = arrayOf() override fun checkServerTrusted(chain: Array, authType: String?) { val myCrt: X509Certificate = myCrt if (chain[0].subjectDN.name == myCrt.subjectDN.name) { if (!myCrt.signature!!.contentEquals(chain[0].signature)) { throw SSLHandshakeException("签名不符!") } } } }}

将自定义的 SSLSocketFactory 和 X509TrustManager 将入到 okhttp 客户端

private fun getClient(ssl: SSLSocketFactory, trustManager: X509TrustManager): OkHttpClient { return OkHttpClient.Builder() .retryOnConnectionFailure(true) .proxy(Proxy.NO_PROXY) .sslSocketFactory(ssl, trustManager) .build() }

这样一来便无法通过 Drony + Charles 进行抓包了

到此,相信大家对"Android App怎么防止抓包"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report