SQL Injection Writeup
PortSwigger
Last updated
PortSwigger
Last updated
Pham Quoc Trung
BurpSuite
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.
Ở đâ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à
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.
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.
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ì.
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.
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.
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.
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.
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
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à:
Có được tài khoản mật khẩu của administrator
, giờ chỉ cần đăng nhập là xong
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.
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
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.
Khi đã có được tài khoản administrator thì chỉ cần đăng nhập để hoàn thành bài lab.
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.
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.
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:
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.
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:
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.
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:
Ở đâ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
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
Đăng nhập bằng tài khoản administrator
để hoàn thành bài lab
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.
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
.
Table USERS_WUOOUV
trông khá hợp lí, thử lấy ra các cột sử dụng all_tab_columns
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.
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.
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
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ị.
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 username
và password
như đề bài, payload của mình sẽ dạng như sau:
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:
Mình sẽ chuyển request này sang Intruder và đặt payload vào độ dài của password
Set payload thành number và cho chạy từ 1 tới 100.
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.
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.
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.
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
Password: p68vv9eutoq2iyf2h8hd
Đăng nhập vào administrator để hoàn thành bài lab.
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.
Ở 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
Đế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:
Để sang Intruder và gán payload vào độ dài của password để tìm ra độ dài password.
Mình sẽ cho chạy từ 1 tới 100
Như thấy trong hình thì bài này vẫn sử dụng mật khẩu 20 kí tự
Tiếp theo, mình sẽ bruteforce mật khẩu thông qua payload như sau:
Vị trí payload và cách setup từng payload mình vẫn làm y hệt lab trước
Tiến hành tấn công. Sau một hồi chờ đợi thì mình ra được password là 083eokymrgsaja6qow1p
Hoàn thành bài lab
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.
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
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
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?
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.
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
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.
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:
Và mình đã hoàn thành bài lab
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.
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
Để 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
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.
Tương tự thì mình sẽ dò ra mật khẩu của admin thông qua payload như sau:
Vị trí payload các thứ vẫn sẽ như các lab tương tự
Sau một hồi thì mình ra được password là fie8nhh6eqjgwi6wsgow
Đăng nhập và hoàn thành bài lab
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.
Ở 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.
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
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
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
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.
Vậy thì đơn giản là mình sẽ sử dụng concat string để in ra các username
và password
trong bảng users
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
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.
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
Và mình đã hoàn thành bài lab :v
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.
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
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:
Mình sẽ thử với câu lệnh lấy mật khẩu của admin. Khi đó payload sẽ như sau:
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 administrator
là khszua702zcxed16fqcl
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
© 2024,Pham Quoc Trung. All rights reserved.
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
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:
To solve the lab, display the database version string.