您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Android App怎么防止抓包”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android App怎么防止抓包”吧!
當(dāng)我們進(jìn)行網(wǎng)絡(luò)請(qǐng)求的時(shí)候,一般通過URL的openConnection來建立連接,代碼如下:
URLConnection conn = url.openConnection()
其實(shí)openConnection這個(gè)函數(shù)還有一個(gè)版本,可以傳入一個(gè)proxy對(duì)象,代碼如下:
public URLConnection openConnection(Proxy proxy) throws java.io.IOException
這樣我們通過這個(gè)函數(shù)建立連接時(shí)傳入一個(gè)Proxy.NO_PROXY,即可達(dá)到防止抓包的效果,如Charles等抓包工具就無法看到我們的鏈接信息了,代碼如下
URLConnection conn = url.openConnection(Proxy.NO_PROXY)
官方對(duì)于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): * <P> * {@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實(shí)際上就是type屬性為DIRECT的一個(gè)Proxy對(duì)象,這個(gè)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 };
這樣因?yàn)槭侵边B,所以不走代理。所以Charles等工具就抓不到包了,這樣一定程度上保證了數(shù)據(jù)的安全。
當(dāng)然這種方式只是通過代理抓不到包,如果直接通過路由還是可以抓包的。
這種方式要在app嵌入證書,以okhttp為例:
當(dāng)okhttp使用X509TrustManager對(duì)服務(wù)器證書進(jìn)行校驗(yàn)時(shí),如果服務(wù)器證書的 subjectDN 和嵌入證書的 subjectDN 一致,我們?cè)龠M(jìn)行簽名內(nèi)容 signature 的比對(duì),如果不一致,拋出異常。示例代碼如下:
首先從本地讀出證書,獲取一個(gè)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 } }
檢查服務(wù)器證書時(shí)對(duì)比嵌入的證書
private fun getTrustManagerInRelease(): X509TrustManager { return object : X509TrustManager { override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String?) {} override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf() override fun checkServerTrusted(chain: Array<X509Certificate>, 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 進(jìn)行抓包了
到此,相信大家對(duì)“Android App怎么防止抓包”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。