repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
make-apk-accept-ca-certificate.mdMarkdown71 lines3.4 KiBrendered raw download
---
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.<sup>[[1]](#references)</sup>

## Automatic

[`apk-mitm`](https://github.com/shroudedcode/apk-mitm) automates APK patching for HTTPS inspection and includes patches for several common certificate-pinning implementations.<sup>[[2]](#references)</sup>

## 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 `<application>` element if it is not already set:<sup>[[1]](#references)</sup>

`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:<sup>[[1]](#references)</sup>

```html
<network-security-config>
    <base-config>
        <trust-anchors>
            <!-- Trust preinstalled CAs -->
            <certificates src="https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/mobile-pentesting/android-app-pentesting/system" />
            <!-- Additionally trust user-added CAs -->
            <certificates src="https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src/mobile-pentesting/android-app-pentesting/user" />
        </trust-anchors>
    </base-config>
</network-security-config>
```

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)