You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Finding columns with a useful data type in an SQL injection UNION attack
2
+
3
+
The reason for performing an SQL injection UNION attack is to be able to retrieve the results from an injected query. Generally, the interesting data that you want to retrieve will be in string form, so you need to find one or more columns in the original query results whose data type is, or is compatible with, string data.
4
+
5
+
Having already determined the number of required columns, you can probe each column to test whether it can hold string data by submitting a series of `UNION SELECT` payloads that place a string value into each column in turn. For example, if the query returns four columns, you would submit:
6
+
```sql
7
+
' UNION SELECT 'a',NULL,NULL,NULL--
8
+
'UNIONSELECTNULL,'a',NULL,NULL--
9
+
' UNION SELECT NULL,NULL,'a',NULL--
10
+
'UNIONSELECTNULL,NULL,NULL,'a'--
11
+
```
12
+
If the data type of a column is not compatible with string data, the injected query will cause a database error, such as:
13
+
14
+
`Conversion failed when converting the varchar value 'a' to data type int.`
15
+
16
+
If an error does not occur, and the application's response contains some additional content including the injected string value, then the relevant column is suitable for retrieving string data.
17
+
18
+
# Lab: SQL injection UNION attack, finding a column containing text
19
+
This lab contains an 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. To construct such an attack, you first need to determine the number of columns returned by the query. You can do this using a technique you learned in a [previous lab](https://portswigger.net/web-security/sql-injection/union-attacks/lab-determine-number-of-columns). The next step is to identify a column that is compatible with string data.
20
+
21
+
The lab will provide a random value that you need to make appear within the query results. To solve the lab, perform an [SQL injection UNION attack](https://portswigger.net/web-security/sql-injection/union-attacks) that returns an additional row containing the value provided. This technique helps you determine which columns are compatible with string data.
0 commit comments