Skip to content
SQL injection Write-up Advanced

Don't fear the bark, tsrewrite to dodge the mark

A SQL injection vulnerability was found, but a WAF blocked payloads containing SELECT and FROM. The attacker bypassed the WAF by using PostgreSQL's ts_rewrite function to execute a SELECT statement encoded to avoid the blocked keywords, enabling time-based blind exploitation.

synacktiv.com

Published on synacktiv.com

Written by Gaetan Ferry - 26/11/2020 - in Pentest - Download

The author, on synacktiv.com

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

Impact

An attacker could bypass the WAF and perform time-based blind SQL injection to extract arbitrary data from the database, though the technique is limited to time-based or blind extraction and may be blocked by stricter WAFs.

What the attacker needed

SQL injection vulnerability; WAF blocking SELECT and FROM; PostgreSQL database

How it was found

  1. Identify a SQL injection point where basic payloads work but SELECT/FROM are blocked.
  2. Discover that PostgreSQL's ts_rewrite function evaluates its second argument as a SQL query.
  3. Encode the desired SELECT statement (e.g., using escape encoding) to avoid the blocked keywords.
  4. Use decode and encode functions to convert the encoded string back to text for ts_rewrite.
  5. Craft a payload that uses ts_rewrite to execute a time-delay query (pg_sleep) to confirm execution.
  6. Extend the technique to extract data by using ts_rewrite to return query results, then cast to text for blind extraction.

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.

A SQL injection lets an attacker insert database commands into a web form. A firewall (WAF) tried to block common attack words like SELECT and FROM. The attacker found a database function that could run a hidden command without using those words, so they could still steal data.

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.