---
# System prepended metadata

title: PortSwigger - Error-based
tags: [SQL injection, Web]

---

[ToC]
## Extracting sensitive data via verbose SQL error messages (Error-based)

:::info
:bulb: Misconfiguration of the database sometimes results in verbose error messages. These can provide information that may be useful pto an attacker
:::

### PRACTITIONER Lab: Visible error-based SQL injection

![](https://hackmd.io/_uploads/H1vCjdIq2.png)

By adding a single quotes we can see server response with status code 500 Internal Server Error and it also print all the failed query

In this case, detecting syntax errors during payload writting will be much simpler

#### 1. Comfirm the SQL injection vulnerable :heavy_check_mark: 
Just like previous exercise, server will not reponse result of the query, so we will try to check if the query is true or false by using CASE statement.

![](https://hackmd.io/_uploads/rk2K7gco3.png)

Hmmm there is an error :v 

We can notice that our payload is missing a little bit. May be it because `TrackingId` input string is limited.

![](https://hackmd.io/_uploads/SySKHl5jh.png)

Yup our theory was right :3 

And with false condition the server will return an error.

![](https://hackmd.io/_uploads/H139Llcs2.png)

#### 2. Figure out the database type and version

To check database type, I will use UNION statement. 

![](https://hackmd.io/_uploads/rkg0Ogco2.png)

![](https://hackmd.io/_uploads/rygpYeqj2.png)

Error query => Not Oracle, Microsoft or MySQL

![](https://hackmd.io/_uploads/S1BW9eqon.png)

No error => Database type is Postgre SQL

:::info
:bulb: With visible error base message, we can potentially elicit error messages that leak sensitive data returned by our malicious query
:::

In Postgre SQL we can using CAST statement. And it will work like this 

![](https://hackmd.io/_uploads/SJJonl5sh.png)


#### 3. Guessing the table name

![](https://hackmd.io/_uploads/S1wmkZ5jh.png)

With the response from error message, we can confirm if the table exists

![](https://hackmd.io/_uploads/rkzSM-9sn.png)

#### 4. Guessing the column name

![](https://hackmd.io/_uploads/r1njGZ9sn.png)

![](https://hackmd.io/_uploads/BJSAzbci3.png)

![](https://hackmd.io/_uploads/SkRymb5ih.png)

In table `users` there are two interesting column `username` and `password`

#### 5. Guessing username and password

I try to extract data from the first row of table by using `LITMIT 1`

![](https://hackmd.io/_uploads/By3krbqsh.png)

Fortunately, it administrator =)))

Do the same with password column

![](https://hackmd.io/_uploads/SyMuHbqo3.png)

Now let try to login

#### 6. Login to admin account

![](https://hackmd.io/_uploads/rk63SW5j3.png)

:::success
**Solved** :thumbsup: 
:::

