Skip to content
SQL injection Write-up Intermediate

When parameterization fails: SQL injection in Nim's db_postgres module using parameterized queries

Nim's db_postgres module escapes parameters by doubling single quotes and replacing null bytes with a backslash-zero sequence, which is unsafe when PostgreSQL's standard_conforming_strings is off. An attacker could inject SQL through a parameter containing a backslash, bypassing authentication or accessing unauthorised data.

blog.nns.ee

Published on blog.nns.ee

I discovered a potential SQL injection vulnerability in Nim's standard library module dbpostgres. This affects applications using this module with older PostgreSQL databases or configurations where standardconformingstrings is disabled.

The author, on blog.nns.ee

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The dbQuote procedure escapes null characters with a backslash sequence but does not account for PostgreSQL's standard_conforming_strings setting, so when that setting is off, a backslash in user input can escape the closing quote.

Impact

Applications using Nim's db_postgres module with PostgreSQL databases where standard_conforming_strings is disabled may be vulnerable to SQL injection attacks, which could lead to unauthorised data access, authentication bypass, or potentially other backend compromises.

What the attacker needed

PostgreSQL with standard_conforming_strings off; Application uses Nim db_postgres module; Attacker can control a parameter value

The fix

The module should properly handle the standard_conforming_strings setting, for example by rejecting null characters and escaping backslashes when the setting is off, or by using true parameterization.

How it was found

  1. Set up PostgreSQL with standard_conforming_strings set to off.
  2. Create a users table with an admin account.
  3. Write a Nim program that uses db_postgres with a parameterized query for username and password.
  4. Run the program with a backslash as the username and a crafted password to inject SQL.
  5. Observe that the query returns the admin username, bypassing authentication.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When a program builds a database query by inserting user input, it must escape special characters so the input cannot change the query's meaning. Nim's database module tried to do this but made a mistake for one PostgreSQL setting: it treated a backslash as an escape character when it was not. An attacker could then send a backslash to break out of the quoted string and add their own SQL commands.

What is SQL injection?

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.