MASTG-DEMO-0058: Using KeyGenParameterSpec with a Broken ECB Block Mode
Download MASTG-DEMO-0058 APK Open MASTG-DEMO-0058 Folder Build MASTG-DEMO-0058 APK
Sample¶
This code demonstrates the risks of using AES in ECB mode (which is a broken mode of operation) using three scenarios:
- Importing a raw AES key into AndroidKeyStore with the purpose "decrypt" and mode "ECB"
- Importing a raw AES key into AndroidKeyStore with the purpose "encrypt" and mode "ECB"
- Generating an AES key in AndroidKeyStore with the purpose "encrypt" or "decrypt" and mode "ECB"
Current versions of Android prohibit the use of keys with ECB in some cases. For example, it is possible to use such a key for decryption but not to encrypt data by default, unless randomized encryption is explicitly disabled (bad practice).
| MastgTest.kt | |
|---|---|
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
When executing the code, you will see the following results for each of the three scenarios:
- Decryption succeeds because that's always allowed.
- Encryption succeeds. The import succeeds in this case because we explicitly disable randomized encryption (bad practice). Otherwise,
KeyStore.setEntrywould fail with an error similar to the one for scenario 3. - Encryption cannot even happen because the generation fails (
KeyGenerator.initspecifically) due to randomized encryption not being disabled. The error says"Randomized encryption (IND-CPA) required but may be violated by block mode: ECB. See android.security.keystore.KeyGenParameterSpec documentation".
Steps¶
- Install the app on a device ( Installing Apps)
- Make sure you have Frooky installed on your machine and the frida-server running on the device
- Run
run.shto spawn the app with Frida - Click the Start button
- Stop the script by pressing
Ctrl+Cand/orqto quit the Frida CLI
These are the relevant methods we are hooking to detect the use of ECB and whether randomized encryption is disabled:
- Setting block modes:
- Enabling/disabling randomized encryption:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 | |
Observation¶
The output shows all instances of block modes that were found at runtime. A backtrace is also provided to help identify the location in the code. If randomized encryption is disabled, that is also indicated in the output.
| output.json | |
|---|---|
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
Evaluation¶
The test fails because the KeyGenParameterSpec.Builder#setBlockModes(...) and KeyProtection.Builder#setBlockModes(...) methods have been called with ECB.
1 2 3 | |
1 2 3 4 5 6 7 8 9 10 11 | |
Regardless of whether the encryption succeeds or not, ECB should never be used in security-sensitive apps. Also, being present in the app may indicate issues in other parts of the app ecosystem (e.g., backend services).