Patch an APK's Network Security Configuration to allow user/system certificates and cleartext traffic, without a full resource rebuild.
This utility locates the app's networkSecurityConfig XML by scanning APK resource entries and replaces it with a permissive configuration that:
- Allows cleartext traffic.
- Trusts both system and user certificate stores.
This is useful when you need to intercept an app's network traffic for analysis and the app restricts trust to a custom set of CAs or disallows cleartext.
- Works on both Linux and Windows hosts (Python 3.9+).
- Does not decompile all resources or run a full decode/rebuild cycle.
- Patches only the target
networkSecurityConfigXML entry inside the APK archive, then writes a patched APK copy. - Reduces build-time overhead and avoids common rebuild issues unrelated to network security config changes.
-
Install the tool with
uvor run it directly.uv tool install . patch-netsec-conf myapp.apkOr install using pip:
pip install . patch-netsec-conf myapp.apkThis produces a new file next to the input:
myapp_nons.apk
-
Sign the patched APK using your preferred signing tool.
Any APK modification invalidates the original signature. You must re-sign before installing. I recommend using APK Explorer & Editor (AEE) for this step.
- Scans XML file contents for
network-security-configmarkers (works even when filenames are obfuscated and files are binary AXML). - Detects the target XML entry format explicitly:
- Binary AXML if the file starts with Android binary XML magic bytes (
0x00080003, little-endian). - Plain-text XML only if content decodes as XML text (UTF-8/UTF-16) and starts with
<after trimming.
- Binary AXML if the file starts with Android binary XML magic bytes (
- Replaces binary targets with the bundled pre-generated binary AXML config from
network_security_config.xml. - Replaces plain-text targets with the bundled plain-text permissive config from
network_security_config_plain.xml. - Fails with an error if the target entry is neither recognized binary AXML nor plain-text XML (no implicit fallback).
$ patch-netsec-conf -h
usage: patch-netsec-conf [-h] [--xml-path XML_PATH] apk_path
Patch an APK's network security config to allow user/system certs and cleartext
positional arguments:
apk_path Input APK file path
optional arguments:
--xml-path XML_PATH
Path to the target XML inside the APK (e.g. res/xml/network_security_config.xml)
-h, --help show this help message and exitThis script is intended for educational and testing purposes only. Only run it on APKs you own or have explicit permission to modify. The author is not responsible for misuse.
Patching network security settings may change app behavior and security; use responsibly.