網(wǎng)站首頁 編程語言 正文
前言
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
相關(guān)推薦
- 2022-08-21 Android使用貝塞爾曲線畫心形_Android
- 2022-04-12 安裝zsh&oh-my-zsh(沒有root權(quán)限)
- 2022-01-05 實(shí)體類[notmapped]特殊 “The specified type member ‘‘ is
- 2022-10-14 element form表單數(shù)據(jù)未雙向綁定
- 2022-05-16 解析Sentry?Relay?二次開發(fā)調(diào)試_python
- 2022-07-16 typescript封裝websocket,監(jiān)測心跳,斷開重連
- 2022-07-09 Python二分查找+字符串模板+textwrap模塊,_python
- 2022-08-23 構(gòu)建?Python?命令行參數(shù)的?4?種常見方式_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支