Summary
phpMyFAQ contains an unauthenticated SQL injection flaw where the BuiltinCaptcha::garbageCollector() and BuiltinCaptcha::saveCaptcha() methods interpolate unsanitized User-Agent headers directly into DELETE and INSERT queries.
By sending crafted requests to the public /api/captcha endpoint, an attacker can perform time-based blind SQL injection and potentially extract sensitive data such as user credentials, admin tokens, and SMTP configuration secrets from the database.
Affected context
This vulnerability occurs in phpMyFAQ deployments that expose the /api/captcha endpoint without additional access control or filtering.
The issue arises when the application's built-in CAPTCHA implementation logs or stores User-Agent strings and passes them into SQL statements without proper escaping, binding, or validation.
Environments with reachable database backends and valuable secrets stored in the phpMyFAQ database—such as user accounts, administrative session data, or outbound email credentials—are particularly at risk.
Exploit preconditions
- Attack surface: Public HTTP interface via GET /api/captcha
- Required access: No authentication; attacker only needs network access to the phpMyFAQ instance
- Trigger point: BuiltinCaptcha::garbageCollector() and BuiltinCaptcha::saveCaptcha() when they construct SQL DELETE and INSERT statements using the incoming User-Agent header
- Why exploitation is possible: The User-Agent header is treated as trusted data and is interpolated directly into SQL queries (string concatenation) instead of being handled with parameterized statements or sanitized, enabling time-based blind SQL injection
Why this happens
The root cause aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
The BuiltinCaptcha methods use dynamically constructed SQL statements that embed the raw User-Agent header without neutralizing control characters (such as quotes, delimiters, and operators). This pattern treats attacker-controlled input as part of the SQL command structure rather than as data.
Because the endpoint is unauthenticated and uses a common header field as input, the SQL injection becomes reachable by default, turning a routine logging or persistence feature into a high-impact injection point.
Potential impact
- Unauthorized extraction of sensitive data such as:
- Usernames, email addresses, and hashed passwords
- Admin session tokens or API tokens
- SMTP and other integration credentials stored in the database
- Enumeration of database schema and internal configuration tables
- Tampering with application data, including inserting or deleting records
- Creation of backdoor administrative accounts or elevation of existing user privileges
- Potential pivoting to other systems that rely on credentials stored in the phpMyFAQ database
General mitigation guidance
Effective mitigation focuses on breaking the direct trust relationship between incoming HTTP headers and SQL statements.
Recommended defensive patterns include:
- Using parameterized queries (prepared statements) for all database access, ensuring user-controlled values are always bound as data, not concatenated into SQL strings.
- Applying strict input handling for header-derived values, including length limits, character whitelists, and canonicalization; treat HTTP headers as untrusted input on par with query parameters.
- Centralizing database access patterns behind a data access layer that enforces safe query construction by default.
- Minimizing storage of unnecessary header information and avoiding storing raw headers in relational schemas unless absolutely required.
- Reducing the privilege of the database account used by the application (e.g., least-privilege accounts that cannot access unrelated tables or perform dangerous operations).
How teams detect issues like this
Detection strategies can combine code-level analysis with behavioral monitoring:
- Static analysis and code review:
- Search for SQL query construction that concatenates request-derived data, particularly HTTP headers such as User-Agent, into query strings.
- Flag any direct use of user input in WHERE, VALUES, or ORDER BY clauses without binding or escaping.
- Enforce coding standards that mandate parameterized queries and disallow ad-hoc string-based SQL assembly.
- Automated tooling:
- Use static application security testing (SAST) tools configured with rules for CWE-89 to detect unsafe SQL construction patterns.
- Incorporate pull-request checks that inspect changes to database access code for adherence to safe query patterns.
- Runtime monitoring:
- Enable database logging for anomalous queries (e.g., queries containing suspicious timing constructs or unusual User-Agent strings).
- Use web application firewalls (WAF) or reverse proxies to flag or block requests with typical SQL injection signatures in headers, including User-Agent.

