Summary
A SQL injection weakness in MariaDB affects applications that rely on mysql_real_escape_string() together with the text protocol and the Big5 character set.
When unvalidated user input is escaped this way and sent to the database, attackers may still inject arbitrary SQL, potentially leading to complete data compromise.
Affected context
This vulnerability arises in applications that:
- Accept raw user-controlled input for SQL queries
- Attempt to sanitize it using
mysql_real_escape_string()or equivalent escaping - Communicate with MariaDB/MySQL using the text protocol
- Use the Big5 character set for the database connection or schema
In this context, the escaping routine does not reliably neutralize dangerous characters for Big5-encoded data, leaving injection vectors open despite attempted sanitization.
Exploit preconditions
- Attack surface: Application inputs that are concatenated into SQL statements and sent over the text protocol.
- Required access: Ability to supply or influence data fields that become part of SQL queries (e.g., form fields, query parameters, headers).
- Trigger point: Execution of dynamically constructed SQL strings that have only been passed through
mysql_real_escape_string()while using the Big5 character set. - Why exploitation is possible: The escaping logic is not fully compatible with Big5 multibyte encoding, allowing crafted byte sequences to bypass or break out of the intended quoting context and inject additional SQL tokens.
Why this happens
The root cause is reliance on string escaping as a primary defense for dynamically built SQL, combined with character-set nuances that the escaping routine does not correctly handle.
In multibyte encodings such as Big5, certain byte sequences can be interpreted differently by the server than by the escaping function, creating gaps where quotes or control characters are not effectively neutralized.
This aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command, where user-controlled data is incorporated into SQL syntax without robust, context-aware parameterization.
Potential impact
- Attackers may inject arbitrary SQL commands into queries.
- Data stored in the affected database can be read, modified, or deleted.
- Application authentication and authorization data may be exposed or tampered with.
- Logic embedded in stored procedures or triggers can be abused to escalate impact.
- In some environments, SQL-level compromise can aid in broader system or infrastructure access.
General mitigation guidance
Mitigation typically focuses on eliminating SQL construction patterns that concatenate user input and depend on escaping.
Safer patterns include:
- Using parameterized queries and prepared statements for all dynamic SQL, independent of character set.
- Structuring queries so that user input is always treated as data, never as SQL syntax (e.g., no dynamic column or table names derived directly from user input).
- Minimizing reliance on connection-level character sets that are historically error-prone for security-sensitive parsing, when possible.
- Applying strict input validation and whitelisting for fields that must influence SQL structure (such as sort order or limited enumerated values).
- Running database accounts with least privilege to reduce the blast radius of any successful injection.
How teams detect issues like this
Detection typically involves reviewing both application code and database interaction patterns:
- Static analysis rules that flag string concatenation used to assemble SQL, especially when paired with escaping routines instead of parameter binding.
- Code review checklists that explicitly verify use of prepared statements for any user-influenced query components.
- Logging and monitoring of database queries to identify anomalous patterns, such as unexpected operators, stacked statements, or tautological conditions originating from user inputs.
- Security scanning tools and dynamic analysis that attempt to inject typical SQL payload patterns into exposed input fields to detect vulnerable paths.
- Policy enforcement in CI/CD pipelines that disallow new usages of raw SQL string concatenation with user inputs without parameters.

