作为改进应用程序安全性的努力的一部分,我想保护我的客户端不受"中间人";攻击。
我有一个常见的用例,我的应用程序从CDN服务器下载大文件(10-50兆(。要做到这一点,我正在使用系统的DownloadMnager
是否有办法通过其API设置任何特定的TrustManager
或特定的服务器证书密钥?是否有其他方法将请求固定到特定的受信任服务器?
看起来没有这样的API,但如果真的是这样,我会感到惊讶,因为GooglePlay使用系统的下载管理器下载apk,然后安装它们。。。
作为一个系统范围内可用的API,我怀疑是否有任何可能的方法将DownloadManager
限制为特定的服务器证书。作为对您提到的例子的回应,Google Play很可能是通过观察下载完成情况来安装下载的APK。
但是,如果我的理解是正确的,您可以通过使用本SO文章中讨论的改装库的文件下载方法来实现您的目标,而证书固定可以通过使用以下SelfSigningClientBuilder
类来构建改装客户端来实现:
SelfSigningClientBuilder.kt
import android.content.Context
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.security.*
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.util.*
import java.util.concurrent.TimeUnit
import javax.net.ssl.*
object SelfSigningClientBuilder {
private const val NET_TIMEOUT_READ = 80L
private const val NET_TIMEOUT_WRITE = 120L
private const val NET_TIMEOUT_CONNECT = 75L
@JvmStatic
fun createClient(context: Context, isCertificateNeeded: Boolean = true): OkHttpClient {
val interceptor = getInterceptor()
if (isCertificateNeeded)
try {
val cf = CertificateFactory.getInstance("X.509")
// assuming the CA certificate is put inside res/raw folder named as ca_cert.pem
val cert = context.resources.openRawResource(R.raw.ca_cert)
val ca = cf?.generateCertificate(cert)
cert.close()
val keyStoreType = KeyStore.getDefaultType()
val keyStore = KeyStore.getInstance(keyStoreType)
keyStore.load(null, null)
keyStore.setCertificateEntry("ca", ca)
val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
val tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
tmf.init(keyStore)
val trustManagers = tmf.trustManagers
if (trustManagers.size != 1 || trustManagers[0] !is X509TrustManager) {
throw IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers))
}
val trustManager = trustManagers[0] as X509TrustManager
val sslContext = SSLContext.getInstance("SSL")
sslContext!!.init(null, trustManagers, null)
return OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManager)
.readTimeout(NET_TIMEOUT_READ, TimeUnit.SECONDS)
.writeTimeout(NET_TIMEOUT_WRITE, TimeUnit.SECONDS)
.connectTimeout(NET_TIMEOUT_CONNECT, TimeUnit.SECONDS)
// .retryOnConnectionFailure(true)
.addInterceptor(interceptor)
.build()
} catch (e: KeyStoreException) {
e.printStackTrace()
} catch (e: CertificateException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: KeyManagementException) {
e.printStackTrace()
}
return OkHttpClient.Builder()
.readTimeout(NET_TIMEOUT_READ, TimeUnit.SECONDS)
.writeTimeout(NET_TIMEOUT_WRITE, TimeUnit.SECONDS)
.connectTimeout(NET_TIMEOUT_CONNECT, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
}
private fun getInterceptor(): Interceptor {
return Interceptor { chain ->
val originalRequest = chain.request()
val request: Request = originalRequest.newBuilder()
.header("custom-header", "my-header-value")
.method(originalRequest.method(), originalRequest.body())
.build()
chain.proceed(request)
}
}
}
然后按照这里的代码段构建改装客户端,然后使用它下载文件:
val retrofit = Retrofit.Builder()
.client(SelfSigningClientBuilder.createClient(context, true)
.baseUrl("https://yourdomain.com/")
.build()
在使用这个改进客户端时,我已经解析了Wireshark、Burp-suite和Charles Proxy的所有REST API请求——它们都不能显示实际的请求文本,而不是一些胡言乱语的数据。因此,我希望您的文件内容在遵循此过程时不会受到MITM攻击。