--- title: "Make an APK Trust a User CA Certificate" section: "Mobile" sectionSlug: "mobile-pentesting" sourcePath: "src/mobile-pentesting/android-app-pentesting/make-apk-accept-ca-certificate.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/mobile-pentesting/android-app-pentesting/make-apk-accept-ca-certificate.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0" --- # Make an APK Trust a User CA Certificate Android applications can restrict which certificate authorities they trust. In an authorized test environment, repackaging an APK with a Network Security Configuration that trusts user-installed CAs can make HTTPS traffic inspection possible. This does not automatically bypass certificate pinning implemented elsewhere in the application.[[1]](#references) ## Automatic [`apk-mitm`](https://github.com/shroudedcode/apk-mitm) automates APK patching for HTTPS inspection and includes patches for several common certificate-pinning implementations.[[2]](#references) ## Manual Decompile the APK: ```bash apktool d app.apk ``` ![Decompiling an APK with apktool](https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/images/img9.png) In `AndroidManifest.xml`, add the following attribute to the `` element if it is not already set:[[1]](#references) `android:networkSecurityConfig="@xml/network_security_config"` Before adding: ![Android manifest before adding the network security configuration](https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/images/img10.png) After adding: ![Android manifest after adding the network security configuration](https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/images/img11.png) Create or update `res/xml/network_security_config.xml` with the following content. The `system` source keeps the preinstalled trust anchors, while `user` adds user-installed CAs:[[1]](#references) ```html ``` Rebuild the APK: ```bash apktool b app -o patched.apk ``` ![Rebuilding the patched APK with apktool](https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/images/img12.png) Repackaging invalidates the original signature, so sign the rebuilt APK before installing it. [See the APK signing section](/hacktricks/mobile-pentesting/android-app-pentesting/smali-changes#sign-the-new-apk). ## References - [1] [Android Developers - Network Security Configuration](https://developer.android.com/privacy-and-security/security-config) - [2] [apk-mitm - Prepare Android APK files for HTTPS inspection](https://github.com/shroudedcode/apk-mitm)