GHSA-hqjr-43r5-9q58 MobSF has SQL Injection in its SQLite Database Viewer Utils
DescriptionMobSF's `read_sqlite()` function in `mobsf/MobSF/utils.py` (lines 542-566) uses Python string formatting (`%`) to construct SQL queries with table names read from a SQLite database's `sqlite_master` table. When a security analyst uses MobSF to analyze a malicious mobile application containing a crafted SQLite database, attacker-controlled table names are interpolated directly into SQL queries without parameterization or escaping.This allows an attacker to:**Cause Denial of Service** -- A malicious table name causes the database viewer to crash, preventing the analyst from viewing ANY data in the SQLite database. A malicious app can use this to hide sensitive data (C2 server URLs, stolen credentials, API keys) from MobSF's analysis.**Achieve SQL Injection** -- The `SELECT * FROM` query on line 557 is provably injectable via `UNION SELECT`, allowing attacker-controlled data to be returned in query results. The current code structure (a `PRAGMA` statement that runs first on line 553) limits the full exploitation chain, but the underlying code is verifiably injectable.Root CauseThe vulnerable code in `mobsf/MobSF/utils.py:542-566`:**Lines 553 and 557** use `%` string formatting to interpolate `table` (a tuple from `sqlite_master`) directly into SQL strings. The `table` value is attacker-controlled when the SQLite database originates from a malicious application being analyzed.Attack VectorThe `read_sqlite()` function is called from two locations:**Dynamic Analysis File Viewer** (`mobsf/DynamicAnalyzer/views/common/device.py:64`):Triggered when an analyst clicks to view a `.db` file in device dataApplies to both Android and iOS dynamic analysis**iOS Static Analysis File Viewer** (`mobsf/StaticAnalyzer/views/ios/views/view_source.py:123`):Triggered when an analyst clicks to view a `.db` file during iOS static analysisAttack ScenarioAttacker creates a malicious Android APK (or iOS IPA) containing a SQLite database with a crafted table name in the `assets/` directoryThe SQLite database contains a table created with:Security analyst uploads the application to MobSF for analysisAnalyst browses the extracted files and clicks to view the SQLite databaseMobSF's `read_sqlite()` reads table names from `sqlite_master`, including the malicious name `x' UNION SELECT 'SQL_INJECTION_PROOF'--`The table name is interpolated into SQL queries via string formatting:`PRAGMA table_info('x' UNION SELECT 'SQL_INJECTION_PROOF'--')` -- causes syntax error (DoS)`SELECT * FROM 'x' UNION SELECT 'SQL_INJECTION_PROOF'--'` -- SQL injection (UNION SELECT returns attacker data)ImpactDenial of Service (Confirmed)When the malicious table name is the first table in `sqlite_master` (i.e., created first in the database), the `PRAGMA` statement on line 553 raises a `sqlite3.OperationalError`, which is caught by the outer `try/except`. This causes `read_sqlite()` to return an empty or partial result, preventing the analyst from viewing **any** database content.**Security impact:** A malicious app author can use this technique to **hide incriminating data** stored in SQLite databases from MobSF's analysis. This directly undermines MobSF's core purpose as a security analysis tool.SQL Injection (Confirmed in Isolation)The `SELECT * FROM` query on line 557 is demonstrably injectable. When the malicious table name `x' UNION SELECT 'SQL_INJECTION_PROOF'--` is interpolated, the resulting query:Successfully executes and returns attacker-controlled data via `UNION SELECT`. The `--` comments out the trailing single quote. This is verified by the PoC script.**Note:** In the current code structure, the `PRAGMA table_info()` statement on line 553 runs before the `SELECT * FROM` on line 557. The PRAGMA fails with a syntax error for injected payloads, which triggers the exception handler before the SELECT can execute. This limits the full exploitation chain. However, the code flaw is real and any future refactoring that changes the execution order or removes the PRAGMA would immediately expose the full SQL injection.Proof of ConceptFiles Provided(Gdrive)| File | Description |
|------|-------------|
| `poc_sqlite_injection.py` | Standalone PoC demonstrating the vulnerability |
| `malicious.db` | Crafted SQLite database (generated by PoC) |
| `create_malicious_apk.sh` | Script to package the malicious DB into an APK |
| `malicious_sqli.apk` | Pre-built APK for testing against MobSF |https://drive.google.com/drive/folders/1mNGkFfNowkaZ5J018HFi4IQcnjKaWCym?usp=sharingRunning the PoCPoC Output (Abbreviated)Suggested FixReplace string formatting with properly quoted identifiers. SQLite uses double quotes for identifiers:This escapes any double quotes within table names by doubling them (`"` → `""`), which is the standard SQL mechanism for identifier quoting. This prevents breakout from the double-quoted identifier context.Resources**CWE-89**: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')**OWASP SQL Injection**: https://owasp.org/www-community/attacks/SQL_Injection**Affected File**: `mobsf/MobSF/utils.py`, lines 542-566
CVSS v3.1 score: 5.3