# WEB04: INFORMATION DISCLOSURE, BUSINESS LOGIC
Yêu cầu:
- Đọc toàn bộ lý thuyết tại đây: <https://portswigger.net/web-security/information-disclosure>
<https://portswigger.net/web-security/logic-flaws>
- Hoàn thành các bài lab thuộc hai chủ đề Information Disclosure và Business Logic Vulnerabilities tại: <https://portswigger.net/web-security/all-labs>
Ouput:
- Chụp ảnh các bài labs đã giải được
- Viết writeups các bài đã giải được
- Điểm số sẽ tính theo tỉ lệ các bài labs giải được
---
# Information disclosure vulnerabilities

## Lab: Information disclosure in error messages
```!
This lab's verbose error messages reveal that it is using a vulnerable version of a third-party framework. To solve the lab, obtain and submit the version number of this framework.
```
Access vào bài lab, tiên hành fuzzing ta để ý ở request `GET /product?productId=1` nếu chỉnh giá trị của trường `productId` thành `'` server trả về lỗi như sau và ta có thể dễ dàng lấy được thông tin về framework được sử dụng cũng như version.

## Lab: Information disclosure on debug page
```!
This lab contains a debug page that discloses sensitive information about the application. To solve the lab, obtain and submit the SECRET_KEY environment variable.
```
Access vào lab, tiến hành check các HTTP message thì nhận thấy có một endpoint trở đến `phpinfo.php` bị lộ trong comment

Truy cập vào endpoint này, và lấy giá trị của SECRET_KEY để solve bài lab

## Lab: Source code disclosure via backup files
```!
This lab leaks its source code via backup files in a hidden directory. To solve the lab, identify and submit the database password, which is hard-coded in the leaked source code.
```
Access vào lab và check endpoint `/robots.txt`

Ta thấy có một endpoint bị disallow là `/backup`, truy cập vào và tiền hánh xem file `.bak`

Lấy database password và solve bài lab

## Lab: Authentication bypass via information disclosure
```!
This lab's administration interface has an authentication bypass vulnerability, but it is impractical to exploit without knowledge of a custom HTTP header used by the front-end.
To solve the lab, obtain the header name then use it to bypass the lab's authentication. Access the admin interface and delete Carlos's account.
You can log in to your own account using the following credentials: wiener:peter
```

Access vào bài lab, và dùng dirsearch để scan các directory, phát hiện ra endpoint admin

Tiến hành truy cập vào endpoint này server trả về như sau

=> Chỉ cho phép truy cập từ localhost
Nhưng điểm đặc biệt ở lab này đó là ta có thể sử dụng request method là TRACE và xem được tất cả thông tin của header thông qua response

TRACE request:

Thấy được một header mới được thêm bởi proxy là `X-Custom-IP-Authorization` (chức năng dùng để xác định IP của client connect đến server), thực hiện thêm header này vào gói tin http request và chỉnh giá trị thành `127.0.0.1`

=> Thành công vào được endpoint `/admin`
Tiếp theo xóa user carlos và solve bài lab

## Lab: Information disclosure in version control history
```!
This lab discloses sensitive information via its version control history. To solve the lab, obtain the password for the administrator user then log in and delete Carlos's account.
```
Dựa vào mô tả và lí thuyết, access vào bài lab và kiểm tra `/.git` folder

Download folder này về với lệnh
```
wget -r https://0a90002c03dd442ac02e3675001c00b4.web-security-academy.net/.git/
```

Check các log commit với `git log`

Từ nội dung của commit ta tiến hành diff hai commit này

=> Lấy được admin password
Login vào admin account và xóa đi user `carlos` -> solve bài lab

---
# Business logic vulnerabilities

## Lab: Excessive trust in client-side controls
```!
This lab doesn't adequately validate user input. You can exploit a logic flaw in its purchasing workflow to buy items for an unintended price. To solve the lab, buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Đề yêu cầu ta phải mua được món đồ có tên là `Lightweight l33t leather jacket`, access vào bài lab và thêm món đồ cần mua vào cart

Có thể thấy số tiền hiện tại ta đang có là $100, số tiền của sản phẩm lại là $1337, và khi ấn Place order sẽ trả về lỗi:

Ở đây ý tưởng sẽ là ta thêm vào giỏ hàng nhưng với giá trị của trường `price` sẽ là `10`

Lúc này giỏ hàng sẽ như sau

Ấn place order và solve bài lab

## Lab: High-level logic vulnerability
```!
This lab doesn't adequately validate user input. You can exploit a logic flaw in its purchasing workflow to buy items for an unintended price. To solve the lab, buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Ở bài lab này ta có thể lợi dụng và thêm vào cart món hàng cần mua với `quantity` là 1 số âm (ở đây là -1)

Follow redirect, từ request tiếp theo có thể thấy giỏ hàng của ta đã chứa món đồ với quantity là -1

Tuy nhiên khi ấn Place order thì bị báo lỗi

Vậy idea sẽ là làm cho total cart price là một số dương nhưng nhỏ hơn số tiền hiện có trong ví của ta (`100$`) bằng cách add "Lightweight l33t leather jacket" vào cart sau đó add thêm một lượng âm quantity sản phâm khác và tính toán cộng trừ để thỏa điều kiện
Chọn sản phẩm kế bên là "Waterproof Tea Bags"

Add với số lượng là -15

Tổng giá trị trong cart nhỏ hơn 100

=> Place order và solve bài lab

## Lab: Low-level logic flaw
```!
This lab doesn't adequately validate user input. You can exploit a logic flaw in its purchasing workflow to buy items for an unintended price. To solve the lab, buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Ở bài này, nếu số chữ số của trường `quantity` vượt quá 2 thì sẽ bị báo là "Invalid parameter: quantity", thử gửi nhiều request để thêm mỗi lần 99 sản phẩm jacket vào cart
Có thể thấy sau một hồi giá trị của total cart price là một số ấm, có thể đoán đây là lỗi integer overflow bên phía server

Clear cart và tính toán lại số request cần thiết

Dùng intruder để automatic send lần lượt khoảng cỡ 322 request

Dùng repeater, gửi thêm một lượng là 99 + 42

Tiếp theo ta cần lựa một món đồ khác để số tiền tăng lên sẽ không vượt quá 100

Món hàng cuối có thể sử dụng là

Add với quantity là 2, sau đó place order và solve bài lab

## Lab: Inconsistent handling of exceptional input
```!
This lab doesn't adequately validate user input. You can exploit a logic flaw in its account registration process to gain access to administrative functionality. To solve the lab, access the admin panel and delete Carlos.
```
Thử truy cập vào admin pannel

=> Cần đăng nhập như một user DontWannaCry mới dùng được chức năng này
Tại chức năng đăng nhập, ta có thể register với username, email và password

Sau khi ấn register, một link sẽ được gửi tới mail client của ta nhằm hoàn tất bước cuối trong quá trình đăng kí.
Điều đặt biệt ở bài lab này đó là tất cả các email có subdomain thuộc mail server của bài lab đều đc gửi tới cùng mail box.

=> Mục tiêu của ta là register được một user với mail address là `@dontwannacry.com`. Tới đây em có nghĩ đến cách chèn null byte để email address trở thành `@dontwannacry.com.exploit-0ac100e604f69bcfc4c7624701e20020.exploit-server.net` nhưng không thành công.
Còn một bug khác có thể dùng để truncate chuỗi đằng sau đó là SQL truncation. Có thể đoán được rằng khi ta nhập email, server sẽ tiến hành gửi link đến mail client trước, sau đó mới lưu vào database vì vậy nếu độ dài của email dài quá độ dài đã set trong database thì sẽ gây ra một phần substring của email bị truncate.
Register một account với chiều dài email trường lớn

Click vào link để hoàn tất quá trình đăng nhập

Thành công tạo được user với mail là `@dontwannacry.com`

Delete user carlos và solve bài lab

## Lab: Inconsistent security controls
```!
This lab's flawed logic allows arbitrary users to access administrative functionality that should only be available to company employees. To solve the lab, access the admin panel and delete Carlos.
```
Ở bài lab này thì không tồn tại bug như bài trước nhưng đơn giản hơn, sau khi register với email address `exploit-0a33005303097ab4c0dd80150143007e.exploit-server.net` ta có thể tự update lại email

=> Chỉ cần update thành mail address `dontwannacry.com` là xong
Access vào admin pannel và xóa user carlos để solve bài lab

## Lab: Weak isolation on dual-use endpoint
```!
his lab makes a flawed assumption about the user's privilege level based on their input. As a result, you can exploit the logic of its account management features to gain access to arbitrary users' accounts. To solve the lab, access the administrator account and delete Carlos.
You can log in to your own account using the following credentials: wiener:peter
```
Sau khi login vào account bài lab cho, ta có thể update email hoặc change password

Ở đây mục tiêu là cần vào được admin pannel vì thế ta sẽ sử tính năng change password đối với username là administrator

Bị báo là password hiện tại không đúng
Thử bỏ đi trường `current-password` và gửi lại request

=> Change pass của user admin thành công
Cuối cùng login lại với user administrator và xóa đi user carlos -> solve bài lab

## Lab: Insufficient workflow validation
```!
This lab makes flawed assumptions about the sequence of events in the purchasing workflow. To solve the lab, exploit this flaw to buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Thứ tự các requests khi mua một món đồ sẽ như sau

Ở đây để ý có một request là `GET/cart/order-confirmation` đùng để confirm lại việc mua hàng
Vì lỗi này liên quan đến workflow của server nên idea khai thác sẽ là add jacket vào cart:

Sau đó gửi request để confirm

Và kết quả:

## Lab: Authentication bypass via flawed state machine
```!
This lab makes flawed assumptions about the sequence of events in the login process. To solve the lab, exploit this flaw to bypass the lab's authentication, access the admin interface, and delete Carlos.
You can log in to your own account using the following credentials: wiener:peter
```
Ngay say khi đăng nhập, ta sẽ được redirect đến một trang để chọn role


Mục tiêu của bài lab này là vào được admin pannel và xóa đi user carlos, thử xem role mặc định là gì nếu ta không chọn role hay nói cách khác, sẽ không gửi request `GET /role-selector`
Bật tính năng Intercept và thực hiện Drop ngay tại request `GET /role-selector`

Thử access vào admin pannel

Vào thành công, xóa đi user carlos và solve bài lab

## Lab: Flawed enforcement of business rules
```!
This lab has a logic flaw in its purchasing workflow. To solve the lab, exploit this flaw to buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Ở bài này ta có thể dùng coupon để giảm giá khi mua hàng, coupon code đầu tiên được hiện khi access vào lab

Cái thứ hai có thể được lấy thông qua tính năng Sign up lần đầu tiên

Sau đó add jacket vào cart và sử dụng coupon, ta không thể sử dụng một coupon liên tiếp nhưng có thể áp dụng xen kẽ

Có thể bên phía server chỉ check các coupon được sử dụng lần cuối vì vậy dẫn tới lỗi về mặt logic.
Vậy chỉ cần Place order và solve bài lab

## Lab: Infinite money logic flaw
```!
This lab has a logic flaw in its purchasing workflow. To solve the lab, exploit this flaw to buy a "Lightweight l33t leather jacket".
You can log in to your own account using the following credentials: wiener:peter
```
Ở bài này ngay tại My Account có cho nhập Gift cards để redeem lại tiền.

Gift card code này có thể lấy được thông qua việc mua sản phẩm gift card.

Bên cạnh đó ta còn có coupon giảm giá 30% khi mua sản phẩm vậy ý tưởng sẽ lần mua với 70% nhưng redeem với 100%

Tạo macros như sau

Sau đó send một GET request bình thường tới Intruder và setup các option

Chỉnh Resource Pool để gửi từng request lần lượt

Kết quả:

Vậy việc còn lại là đợi đến khi số tiền lớn hơn `133700` và place order là xong

## Lab: Authentication bypass via encryption oracle
```!
This lab contains a logic flaw that exposes an encryption oracle to users. To solve the lab, exploit this flaw to gain access to the admin panel and delete Carlos.
You can log in to your own account using the following credentials: wiener:peter
```
Sau một hồi fuzzing, ở tính năng Stay logged in khi "Login"

ta nhận thấy giá trị của trường này có vẻ đã được encrypt và base64 encode

Một điều nữa đó là ở tính năng comment, nếu ta nhập một email không hợp lệ thì server sẽ báo lỗi

Đồng thời xuất hiện thêm một cookie có tên là `notification`

-> Có thể đoán được error message cũng được encrypt và lưu trong cookie này sau đó hiển thị ở trang comment.
Vậy thử thay giá trị của cookie stay-logged-in vào giá trị của notification

=> Trả một chuỗi có format `<username>:<timestamp lúc login>`
Gửi lại email với format là `administrator:<timestamp>`

Và F5 lại trang

Lúc này vẫn chưa theo như ý muốn của ta lắm bởi vì vẫn cần phải loại bỏ đi substring
`Invalid email address: `
Trong quá trình fuzzing, nhận ra bên phía server có sử dụng mã hóa dạng block với size là 16

Vậy ta cần padding thêm vào trước để input là một số chia hết cho 16
Chuỗi input sẽ là `Invalid email address: <email>`
=> Input sau khi padding sẽ như sau:
```
Invalid email address: XXXXXXXXXadministrator:1672851351684
```
Post comment với email là `XXXXXXXXXadministrator:1672851351684`
-> Được cookie notification là `dDalRZzvDHgS%2ftbVeVT0ASJCmY3oI8GG2MzRcLhoSRL%2fQ4geYdOqH7wF8xMU5VELaV%2fgbq3gpNq8xpflNFmmfg%3d%3d`
Ta sẽ cần decode, bỏ đi 32 byte đầu tiên sau đó re-encode lại

Chỉnh lại giá trị của cookie stay-logged-in, truy cập vào admin pannel, xóa đi user carlos và solve bài lab

###### tags: `portswigger`