日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Android?App如何防止抓包_Android

作者:BennuCTech ? 更新時間: 2022-05-29 編程語言

前言

App安全非常重要,尤其是數(shù)據(jù)安全。但是我們知道通過Charles等工具可以對App的網(wǎng)絡(luò)請求進(jìn)行抓包,如果我們的數(shù)據(jù)沒有進(jìn)行加密,這樣這些信息就會被清除的提取出來,會被不法分子進(jìn)行利用。保證數(shù)據(jù)安全有很多種方法,今天簡單聊一聊如何通過簡單幾步防止抓包。

正文

當(dāng)我們進(jìn)行網(wǎng)絡(luò)請求的時候,一般通過URL的openConnection來建立連接,代碼如下:

URLConnection conn = url.openConnection()

其實(shí)openConnection這個函數(shù)還有一個版本,可以傳入一個proxy對象,代碼如下:

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

這樣我們通過這個函數(shù)建立連接時傳入一個Proxy.NO_PROXY,即可達(dá)到防止抓包的效果,如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):
 * <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的一個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等工具就抓不到包了,這樣一定程度上保證了數(shù)據(jù)的安全。

當(dāng)然這種方式只是通過代理抓不到包,如果直接通過路由還是可以抓包的。

補(bǔ)充:使用證書校驗

這種方式要在app嵌入證書,以okhttp為例:

當(dāng)okhttp使用X509TrustManager對服務(wù)器證書進(jìn)行校驗時,如果服務(wù)器證書的 subjectDN 和嵌入證書的 subjectDN 一致,我們再進(jìn)行簽名內(nèi)容 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
    }
}
  • 檢查服務(wù)器證書時對比嵌入的證書
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)行抓包了

總結(jié)

原文鏈接:https://juejin.cn/post/7078077090506997767

欄目分類
最近更新