Vulnerability: SQL Injection via Line Comment Creation #911
Closed
paul-gerste-sonarsource
started this conversation in
General
Replies: 2 comments
-
This has been addressed in v11.5.5 patch. Every negative number is now wrapped in parentheses. Thank you @paul-gerste-sonarsource for the find! The Fix: Line 888 in 1a4dfe6 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Closing, as this discussion has been concluded. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Affected Versions
Verified on
11.5.4
.Observations
Postgres supports two query modes:
When using the simple mode, libraries have to build SQL strings themselves for statements that involve dynamic values.
Since
pgFormatting
is enabled by default,pg-promise
escapes parameter values before inserting them into a query string. We found a case in which the parameter escaping does not work correctly and allows users to alter the structure of the query, potentially leading to SQL Injection.When a placeholder is directly preceded by a minus (
-
) and not separated by any whitespace,pg-promise
does not handle the particular case when a negative number is inserted for the placeholder. This leads to two consecutive minus signs in the query, turning them into the start of a line comment:In this example, the resulting query will cause a syntax error because of the missing value that the result column should be compared with.
Exploitation
To exploit this behavior and cause SQL Injection, the following conditions must be met by a parameterized query:
In this case, the incorrect sanitization will allow an attacker to create a line comment to alter the structure of the query. The attacker can then use Postgres' support of multi-line strings to escape from that line comment by using newline characters in the string:
By constructing a matching string payload, the attacker can inject SQL to alter the query, bypassing the protections that parameterized queries bring against SQL Injection attacks.
Here's proof-of-concept snippet:
Impact
The vulnerability allows the execution of arbitrary SQL statements on behalf of the application. Postgres supports stacked queries by default, so attackers can inject arbitrary SQL statements and are not tied to the structure of the query they inject into. If stacked queries are disabled, attackers are limited to injecting into the query. However, SQL's flexibility still allows them to extract any data from the database in most cases.
The final impact depends on the application using
pg-promise
in a vulnerable way, what data they store in the Postgres database, and if an attacker can control the required values of a vulnerable query. On the library level, the vulnerability breaks the assumption of developers who use parameterized queries to protect their applications against SQL Injections.Recommendations
We recommend breaking up the
--
pattern in a way that does not break the rest of a statement. Other Postgres client libraries do this by inserting a space before negative numbers (- -1
) or wrapping negative numbers in parentheses (-(-1)
). Some libraries only do this if a minus immediately precedes a negative integer; others always do it.Credits
Please credit this finding to: Paul Gerste (Sonar)
Beta Was this translation helpful? Give feedback.
All reactions