ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Man-in-the-Middle (MITM) Attacks in Android Development: How to Secure Your App

Dobri Kostadinov
ProAndroidDev
Published in
5 min readMar 9, 2025

Introduction

What is a Man-in-the-Middle (MITM) Attack?

How MITM Attacks Work

OWASP Implications

Real-World Consequences of MITM Attacks

How to Prevent MITM Attacks in Android Apps (Aligned with OWASP Guidelines)

1. Enforce HTTPS with TLS (SSL/TLS Security)

Implementation in Android

<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">yourdomain.com</domain>
</domain-config>
</network-security-config>

2. Certificate Pinning (M3: Insecure Communication)

What is Certificate Pinning?

Why is Certificate Pinning Important?

How Certificate Pinning Works

Implementation in Android

val client = OkHttpClient.Builder()
.certificatePinner(
CertificatePinner.Builder()
.add("yourdomain.com", "sha256/your-certificate-hash")
.build()
)
.build()

3. Use Strong Network Security Configurations (OWASP M3 & M9)

What is Network Security Configuration?

Why is Strong Network Security Configuration Important?

Implementation in Android

<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">yourdomain.com</domain>
<pin-set>
<pin digest="SHA-256">your-certificate-hash</pin>
</pin-set>
</domain-config>
</network-security-config>

4. Validate SSL/TLS Certificates Properly (M3: Insecure Communication)

Why SSL/TLS Certificate Validation is Important

Best Practices for Certificate Validation

5. Disable Insecure Cipher Suites and Protocols

Why Disabling Weak Ciphers is Critical

Best Practices for Secure Cipher Suites

Implementation in Android

val sslSocketFactory = SSLContext.getInstance("TLSv1.3").apply { init(null, null, null) }.socketFactory
val client = OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).trustManagers[0] as X509TrustManager)
.build()

Conclusion

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Dobri Kostadinov

15+ years in native Android dev (Java, Kotlin). Expert in developing beautiful android native apps.

Responses (2)

Write a response