MASTG-DEMO-0057: Network Security Configuration Allows User-Added Certificates
Download MASTG-DEMO-0057 APK Open MASTG-DEMO-0057 Folder Build MASTG-DEMO-0057 APK
Sample¶
This sample Kotlin code fetches the badssl server https://mitm-software.badssl.com/ using HttpsURLConnection
, which is not normally allowed because the certificate is not trusted by the system. However, due to the Network Security Configuration that permits user-added CA certificates, the connection is allowed to proceed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
1 2 3 4 5 6 7 8 9 |
|
Running the app
You don't need to run the app on a device or emulator to test this demo because it focuses on the Network Security Configuration. However, if you want to run the app and verify that the connection is allowed, some additional preparation is required:
- Obtain the root CA certificate for the server. For this example, obtain it here: https://github.com/chromium/badssl.com/blob/master/certs/src/crt/ca-mitm-software.crt
- Copy the certificate file onto the device or emulator. For example, use the command
adb push ca-mitm-software.crt /sdcard/Download/
. - On the device open Settings > Security > Encryption & credentials > Install from storage and select your certificate file. Confirm it installs under "User credentials".
Steps¶
First of all we obtain the AndroidManifest.xml file (AndroidManifest_reversed.xml
), which contains the android:networkSecurityConfig
attribute pointing to the app's Network Security Configuration file (network_security_config.xml
).
Let's run our semgrep rule against the sample code.
../../../../rules/mastg-android-network-insecure-trust-anchors.yml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
run.sh | |
---|---|
1 |
|
Observation¶
The rule has identified an element in the Network Security Configuration that allows user-added CA certificates.
output.txt | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Evaluation¶
The test fails due to the <certificates src="user" />
element in the Network Security Configuration which allows user-added CA certificates.