Repository: OWASP/owasp-mastg

Issue:
New Risk - Insecure Random Usage [insecure-random]


## Description

Create a new risk for "Insecure Random Usage (MASVS-CRYPTO-1)" using the following information:

Using a non-cryptographically secure PRNG in a security context, such as authentication, poses significant risks. An attacker could potentially guess the generated numbers and gain access to privileged data or functionality. Predicting or regenerating random numbers can lead to encryption breaches, compromise sensitive user information, or enable user impersonation.



Create "`risks/MASVS-CRYPTO/1-***-****/insecure-random/risk.md`" including the following content:

```yaml
---
title: Insecure Random Usage
alias: insecure-random
platform: [android, ios]
profiles: [L1, L2]
mappings:
  masvs-v1: [MSTG-CRYPTO-6]
  masvs-v2: [MASVS-CRYPTO-1]
  mastg-v1: [MASTG-TEST-0063, MASTG-TEST-0016]

---

## Overview

## Impact

## Modes of Introduction

## Mitigations

```

**To complete the sections follow the guidelines from [Writing MASTG Risks & Tests](https://docs.google.com/document/d/1EMsVdfrDBAu0gmjWAUEs60q-fWaOmDB5oecY9d9pOlg/edit?usp=sharing)**

Use at least the following references:

- <https://developer.android.com/reference/java/security/SecureRandom>
- <https://developer.android.com/topic/security/risks/weak-prng>
- <https://developer.android.com/privacy-and-security/cryptography#deprecated-functionality>
- <https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf>



**MASTG v1 Refactoring:**

If the risk has a MASVS v1 ID, you can use it to [search for related tests in the MASTG](https://mas.owasp.org/MASTG/tests/) and use them as input to define your risks and associated tests.

- [MASTG-TEST-0063 - Testing Random Number Generation (ios)](https://mas.owasp.org/MASTG/tests/android/MASVS-CRYPTO/MASTG-TEST-0063/)
- [MASTG-TEST-0016 - Testing Random Number Generation (android)](https://mas.owasp.org/MASTG/tests/android/MASVS-CRYPTO/MASTG-TEST-0016/)

## Acceptance Criteria

- [ ] The risk has been created in the correct directory (`risks/MASVS-CRYPTO/1-***-****/insecure-random/risk.md`)
- [ ] The risk content follows the [guidelines](https://docs.google.com/document/d/1EMsVdfrDBAu0gmjWAUEs60q-fWaOmDB5oecY9d9pOlg/edit?usp=sharing)
- [ ] At least one GitHub Issue has been created for the corresponding tests (derived from "Modes of Introduction")
- [ ] The risk indicates the related MASTG v1 tests in its metadata.

                    


Discussion:

Resolving patch:
diff --git a/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/example-1/mitm_sensitive_logger.py b/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/example-1/mitm_sensitive_logger.py
new file mode 100644
--- /dev/null
+++ b/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/example-1/mitm_sensitive_logger.py
@@ -0,0 +1,43 @@
+from mitmproxy import http
+
+# This data would come from another file and should be defined after identifying the data that is considered sensitive for this application.
+# For example by using the Google Play Store Data Safety section.
+SENSITIVE_DATA = {
+    "precise_location_latitude": "37.7749",
+    "precise_location_longitude": "-122.4194",
+    "name": "John Doe",
+    "email_address": "john.doe@example.com",
+    "phone_number": "+11234567890",
+    "credit_card_number": "1234 5678 9012 3456"
+}
+
+SENSITIVE_STRINGS = SENSITIVE_DATA.values()
+
+def contains_sensitive_data(string):
+    return any(sensitive in string for sensitive in SENSITIVE_STRINGS)
+
+def process_flow(flow):
+    url = flow.request.pretty_url
+    request_headers = flow.request.headers
+    request_body = flow.request.text
+    response_headers = flow.response.headers if flow.response else "No response"
+    response_body = flow.response.text if flow.response else "No response"
+
+    if (contains_sensitive_data(url) or 
+        contains_sensitive_data(request_body) or 
+        contains_sensitive_data(response_body)):
+        with open("sensitive_data.log", "a") as file:
+            if flow.response:
+                file.write(f"RESPONSE URL: {url}\n")
+                file.write(f"Response Headers: {response_headers}\n")
+                file.write(f"Response Body: {response_body}\n\n")
+            else:
+                file.write(f"REQUEST URL: {url}\n")
+                file.write(f"Request Headers: {request_headers}\n")
+                file.write(f"Request Body: {request_body}\n\n")
+def request(flow: http.HTTPFlow):
+    process_flow(flow)
+
+def response(flow: http.HTTPFlow):
+    process_flow(flow)
+


Test patch:
diff --git a/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-insecure-random-use/test.md b/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-insecure-random-use/test.md
new file mode 100644
--- /dev/null
+++ b/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-insecure-random-use/test.md
@@ -0,0 +1,28 @@
+---
+platform: android
+title: Insecure Random API Usage
+type: [static]
+mitigations:
+- android-use-secure-random
+prerequisites:
+- identify-sensitive-data
+- identify-security-relevant-contexts
+---
+
+## Overview
+
+Android apps sometimes use insecure pseudorandom number generators (PRNGs) such as `java.util.Random`, which is essentially a linear congruential generator. This type of PRNG generates a predictable sequence of numbers for any given seed value, making the sequence reproducible and insecure for cryptographic use. In particular, `java.util.Random` and `Math.random()` ([the latter](https://franklinta.com/2014/08/31/predicting-the-next-math-random-in-java/) simply calling `nextDouble()` on a static `java.util.Random` instance) produce identical number sequences when initialized with the same seed across all Java implementations.
+
+## Steps
+
+1. Run a [static analysis](../../../../../techniques/android/MASTG-TECH-0014.md) tool on the app and look for insecure random APIs.
+
+## Observation
+
+The output should contain a **list of locations where insecure random APIs are used**.
+
+## Evaluation
+
+Inspect the app source code using the provided location information.
+
+The test case fails if you can find random numbers generated using those APIs that are used in security-relevant contexts.
diff --git a/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-non-random-use/test.md b/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-non-random-use/test.md
new file mode 100644
--- /dev/null
+++ b/risks/MASVS-CRYPTO/1-strong-crypto/insecure-random/android-non-random-use/test.md
@@ -0,0 +1,28 @@
+---
+platform: android
+title: Non-random Sources Usage
+type: [static]
+mitigations:
+- android-use-secure-random
+prerequisites:
+- identify-sensitive-data
+- identify-security-relevant-contexts
+---
+
+## Overview
+
+Android applications sometimes use non-random sources to generate "random" values, leading to potential security vulnerabilities. Common practices include relying on the current time, such as `Date().getTime()`, or accessing `Calendar.MILLISECOND` to produce values that are easily guessable and reproducible.
+
+## Steps
+
+1. Run a [static analysis](../../../../../techniques/android/MASTG-TECH-0014.md) tool on the app and look for uses of non-random sources.
+
+## Observation
+
+The output should contain a **list of locations where non-random sources are used**.
+
+## Evaluation
+
+Inspect the app source code using the provided location information.
+
+The test case fails if you can find security-relevant values, such as passwords or tokens, generated using non-random sources.
diff --git a/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/test.md b/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/test.md
new file mode 100644
--- /dev/null
+++ b/risks/MASVS-PRIVACY/1-data-minimization/sensitive-data-in-network-traffic/android-data-in-traffic-capture/test.md
@@ -0,0 +1,29 @@
+---
+platform: android
+title: Sensitive Data in Network Traffic Capture
+type: [dynamic, network]
+prerequisites:
+- identify-sensitive-data
+- privacy-policy
+- app-store-privacy-declarations
+---
+
+## Overview
+
+Attackers may capture network traffic from Android devices using an intercepting proxy, such as [OWASP ZAP](https://www.zaproxy.org/), [Burp Suite](https://portswigger.net/burp), or [mitmproxy](https://mitmproxy.org/), to analyze the data being transmitted by the app. This works even if the app uses HTTPS, as the attacker can install a custom root certificate on the Android device to decrypt the traffic. Inspecting traffic that is not encrypted with HTTPS is even easier and can be done without installing a custom root certificate for example by using [Wireshark](https://www.wireshark.org/).
+
+## Steps
+
+1. Start the device.
+
+2. Start [logging sensitive data from network traffic](../../../../../techniques/android/MASTG-TECH-0100.md).
+
+3. Launch and use the app going through the various workflows while inputting sensitive data wherever you can. Especially, places where you know that will trigger network traffic.
+
+## Observation
+
+The **network traffic sensitive data log** including decrypted HTTPS traffic contains the sensitive data you entered in the app.
+
+## Evaluation
+
+The test case fails if you can find the sensitive data you entered in the app within the **network traffic sensitive data log** that is not stated in the App Store Privacy declarations.
diff --git a/risks/MASVS-STORAGE/2-prevent-data-leakage/data-in-logs/android-data-in-logs-frida/test.md b/risks/MASVS-STORAGE/2-prevent-data-leakage/data-in-logs/android-data-in-logs-frida/test.md
new file mode 100644
--- /dev/null
+++ b/risks/MASVS-STORAGE/2-prevent-data-leakage/data-in-logs/android-data-in-logs-frida/test.md
@@ -0,0 +1,32 @@
+---
+platform: android
+title: Leakage of Sensitive Data via Logging APIs
+apis: [Log, Logger, System.out.print, System.err.print, java.lang.Throwable#printStackTrace]
+type: [dynamic]
+---
+
+## Overview
+
+On Android platforms, logging APIs like `Log`, `Logger`, `System.out.print`, `System.err.print`, and `java.lang.Throwable#printStackTrace` can inadvertently lead to the leakage of sensitive information. Log messages are recorded in logcat, a shared memory buffer, accessible since Android 4.1 (API level 16) only to privileged system applications that declare the `READ_LOGS` permission. Nonetheless, the vast ecosystem of Android devices includes pre-loaded apps with the `READ_LOGS` privilege, increasing the risk of sensitive data exposure. Therefore, direct logging to logcat is generally advised against due to its susceptibility to data leaks.
+
+## Steps
+
+1. Install and run the app.
+
+2. Navigate to the screen of the mobile app you want to analyse the log output from.
+
+3. Execute a [method trace](https://mas.owasp.org/MASTG/techniques/android/MASTG-TECH-00xx/) by attaching to the running app, targeting logging APIs and save the output.
+
+## Observation
+
+The **method trace output** contains a list of locations where logging APIs are used in the app for the current execution.
+
+## Evaluation
+
+The test case fails if you can find sensitive data being logged using those APIs.
+
+For example, the following output leaks a key via `Log`:
+
+```shell
+Log.println_native(0, 4, "tag", "key: 12345678")
+```