# WEB15: HTTP Request Smuggling
## Lab: HTTP request smuggling, basic CL.TE vulnerability
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding. The front-end server rejects requests that aren't using the GET or POST method.
To solve the lab, smuggle a request to the back-end server, so that the next request processed by the back-end server appears to use the method GPOST.
```
Theo gợi ý của đề bài, front end server sử dụng CL để "cắt request", còn backend server thì sử dụng TE, vậy ta có thể áp dụng cách khai thác như sau:

`Content-Length` là 6 => front end server chỉ thấy có một request tuy nhiên khi được forwared đến back end server, nó dùng TE và thấy có tới tận 2 request => ấn send 1 lần nữa lúc này request mới sẽ được append vào phần còn lại và ... "Unrecognized method GPOST"
## Lab: HTTP request smuggling, basic TE.CL vulnerability
```!
This lab involves a front-end and back-end server, and the back-end server doesn't support chunked encoding. The front-end server rejects requests that aren't using the GET or POST method.
To solve the lab, smuggle a request to the back-end server, so that the next request processed by the back-end server appears to use the method GPOST.
```
Front end server sử dụng TE và back end server sử dụng TL:

ở request smuggling ta thêm vào body là `x=bla` để request append sẽ được xem như data trong body và khiến back end server xử lí bth
## Lab: HTTP request smuggling, obfuscating the TE header
```!
This lab involves a front-end and back-end server, and the two servers handle duplicate HTTP request headers in different ways. The front-end server rejects requests that aren't using the GET or POST method.
To solve the lab, smuggle a request to the back-end server, so that the next request processed by the back-end server appears to use the method GPOST.
```
Mục tiêu là cần fuzzing bằng cách obfucate TE header, sau một hồi thì thu được kết quả như sau:

## Lab: HTTP request smuggling, confirming a CL.TE vulnerability via differential responses
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding.
To solve the lab, smuggle a request to the back-end server, so that a subsequent request for / (the web root) triggers a 404 Not Found response.
```
Dựa vào lí thuyết, ta dễ dàng có được payload như sau

## Lab: HTTP request smuggling, confirming a TE.CL vulnerability via differential responses
```!
This lab involves a front-end and back-end server, and the back-end server doesn't support chunked encoding.
To solve the lab, smuggle a request to the back-end server, so that a subsequent request for / (the web root) triggers a 404 Not Found response.
```
Tương tự như bài lab trước, nhưng ở đây là dạng TE . CL, ta chỉ cần tinh chỉnh lại 1 tí

## Lab: Exploiting HTTP request smuggling to bypass front-end security controls, CL.TE vulnerability
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding. There's an admin panel at /admin, but the front-end server blocks access to it.
To solve the lab, smuggle a request to the back-end server that accesses the admin panel and deletes the user carlos.
```
Từ mô ta đề bài => dạng CL . TE
Thử access đến endpoint /admin, ta thấy đã bị chặn bởi front end server:

Áp dụng smuggling, server trả về phản hồi như sau:

-> Cần thêm localhost vào Host header

Tiếp tục smuggling 1 request để xóa user carlos, solve bài lab

## Lab: Exploiting HTTP request smuggling to bypass front-end security controls, TE.CL vulnerability
```!
This lab involves a front-end and back-end server, and the back-end server doesn't support chunked encoding. There's an admin panel at /admin, but the front-end server blocks access to it.
To solve the lab, smuggle a request to the back-end server that accesses the admin panel and deletes the user carlos.
```
Từ mô ta đề bài => dạng TE . CL

## Lab: Exploiting HTTP request smuggling to reveal front-end request rewriting
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding.
There's an admin panel at /admin, but it's only accessible to people with the IP address 127.0.0.1. The front-end server adds an HTTP header to incoming requests containing their IP address. It's similar to the X-Forwarded-For header but has a different name.
To solve the lab, smuggle a request to the back-end server that reveals the header that is added by the front-end server. Then smuggle a request to the back-end server that includes the added header, accesses the admin panel, and deletes the user carlos
```
Nhận thấy trang web có tính năng search

Và sẽ reflect input vào response

Thử smuggling CL . TE ngay request này

=> Header mà đề bài đề cập tới là `X-DcLMLy-Ip`
Tiếp tục smuggling để xóa user carlos

## Lab: Exploiting HTTP request smuggling to capture other users' requests
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding.
To solve the lab, smuggle a request to the back-end server that causes the next user's request to be stored in the application. Then retrieve the next user's request and use the victim user's cookies to access their account.
```
Trang web có chức năng Post comment

Dựa vào lý thuyết để steal victim session cookie, ta lợi dụng smuggling CL . TE ở chức năng này

-> Steal được session cookie của victim

Chỉnh lại thành cookie của victim và solve bài lab

## Lab: Exploiting HTTP request smuggling to deliver reflected XSS
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding.
The application is also vulnerable to reflected XSS via the User-Agent header.
To solve the lab, smuggle a request to the back-end server that causes the next user's request to receive a response containing an XSS exploit that executes alert(1).
```
Thử thay đổi giá trị của header `User-Agent`, ta thấy nó reflect trong response:

Vậy smuggling 1 request execute alert(1) như sau:

F5 lại trang và kết quả:

## Lab: Exploiting HTTP request smuggling to perform web cache poisoning
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding. The front-end server is configured to cache certain responses.
To solve the lab, perform a request smuggling attack that causes the cache to be poisoned, such that a subsequent request for a JavaScript file receives a redirection to the exploit server. The poisoned cache should alert document.cookie.
```
Check qua các tính năng của trang web, ta thấy có một thứ mới "Next post", request sẽ có dạng như sau:

Vậy nếu ta thử smuggling như sau thì các request phía sau đều sẽ bị redirect đến exploit server:

Cuối cùng là poison cache để trỏ đến exploit server:

Solve bài lab

## Lab: Exploiting HTTP request smuggling to perform web cache deception
```!
This lab involves a front-end and back-end server, and the front-end server doesn't support chunked encoding. The front-end server is caching static resources.
To solve the lab, perform a request smuggling attack such that the next user's request causes their API key to be saved in the cache. Then retrieve the victim user's API key from the cache and submit it as the lab solution. You will need to wait for 30 seconds from accessing the lab before attempting to trick the victim into caching their API key.
You can log in to your own account using the following credentials: wiener:peter
```
`GET /my-account` hiển thị API key của user

Ta thử smuggling với request như sau

Bởi vì đề bài gợi ý việc cache các static resource mà trong các request dễ thấy nhất chính là `/resources/js/tracking.js `

Sau một vài lần thử, ta lấy được API key của administrator:

Submit và solve bài lab

## Lab: H2.CL request smuggling
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests even if they have an ambiguous length.
To solve the lab, perform a request smuggling attack that causes the victim's browser to load and execute a malicious JavaScript file from the exploit server, calling alert(document.cookie). The victim user accesses the home page every 10 seconds.
```
Ta để ý nếu `GET /resources` thì sẽ được redirect đến `/resources/`

Vậy ý tưởng sẽ là smuggling request ngay tại chỗ này để redirect đến payload alert(document.cookie) trên exploit server

Note: ta cần canh thời gian và ấn send request liên tục để khi mà browser của victim trigger request load js resource thì ngay lúc đó nó sẽ bị redirect đến exploit server của ta.
## Lab: Response queue poisoning via H2.TE request smuggling
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests even if they have an ambiguous length.
To solve the lab, delete the user carlos by using response queue poisoning to break into the admin panel at /admin. An admin user will log in approximately every 15 seconds.
The connection to the back-end is reset every 10 requests, so don't worry if you get it into a bad state - just send a few normal requests to get a fresh connection.
```
Dựa vào lí thuyết và yêu cầu của bài, ta cần poison response queue để lấy được session cookie của admin khi login.
Tuy nhiên để connection không bị đóng do invalid request khi smuggling gây ra, cần phải smuggling một request hoàn chỉnh

Sau đó ta chỉ cần gửi request đến cho tới khi nhận được response 302 - session cookie khi mới login của admin

Cuối cùng dùng cookie này để delete user carlos

## Lab: HTTP/2 request smuggling via CRLF injection
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests and fails to adequately sanitize incoming headers.
To solve the lab, use an HTTP/2-exclusive request smuggling vector to gain access to another user's account. The victim accesses the home page every 15 seconds.
If you're not familiar with Burp's exclusive features for HTTP/2 testing, please refer to the documentation for details on how to use them.
```
Smuggling bằng cách inject newline vào request header:

-> send 2 lần ta nhận được 404 not found
Vẫn áp dụng kĩ thuật trong 1 bài lab trước để steal cookie của victim bằng cách khiến cho request victim append vào search param tại trang home


Set lại cookie trên trình duyệt, F5 và solve bài lab

## Lab: HTTP/2 request splitting via CRLF injection
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests and fails to adequately sanitize incoming headers.
To solve the lab, delete the user carlos by using response queue poisoning to break into the admin panel at /admin. An admin user will log in approximately every 10 seconds.
The connection to the back-end is reset every 10 requests, so don't worry if you get it into a bad state - just send a few normal requests to get a fresh connection.
```
Smuggling request với CRLF injection bằng cách add thêm header foo, lúc này response queue đã bị poison, thử send một vài request ta lấy được session cookie khi admin login

Xóa user carlos

Solve bài lab

## Lab: Bypassing access controls via HTTP/2 request tunnelling
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests and fails to adequately sanitize incoming header names. To solve the lab, access the admin panel at /admin as the administrator user and delete carlos.
The front-end server doesn't reuse the connection to the back-end, so isn't vulnerable to classic request smuggling attacks. However, it is still vulnerable to request tunnelling.
```
Thử CRLF inject tại header name, ta thấy server báo time out -> vulnerable

Để tìm các header "ẩn", lợi dụng endpoint `/`, gửi POST request với tham số `search`:

Chỉnh method thành `HEAD` đồng thời thêm reflected input vào `path` (bởi vì nếu không thêm server sẽ báo "Server Error: Received only ... of expected 8362 bytes of data")

Ấn send, ta đọc được response của tunnel request và lấy được các header "ẩn":

smuggling đến `GET /admin` với các header này -> access thành công vào `/admin`

Cuối cùng là delete user carlos

Solve bài lab

## Lab: Web cache poisoning via HTTP/2 request tunnelling
```!
This lab is vulnerable to request smuggling because the front-end server downgrades HTTP/2 requests and doesn't consistently sanitize incoming headers.
To solve the lab, poison the cache in such a way that when the victim visits the home page, their browser executes alert(1). A victim user will visit the home page every 15 seconds.
The front-end server doesn't reuse the connection to the back-end, so isn't vulnerable to classic request smuggling attacks. However, it is still vulnerable to request tunnelling.
```
Dùng header `Pragma` để check qua cache key, ta thấy nó không bao gồm request method.

Để ý các request đến `/resources` đều sẽ được redirect, ta sẽ lợi dụng điểm này để thêm payload xss vào query tring

Mặc khác, có thể CRLF inject tại value của `:path`:

tunneling request để làm cho `<script>alert(1)</script>` xuát hiện trong request:

Padding:

Tiếp theo là poison cache và solve bài lab

## Lab: CL.0 request smuggling
```!
This lab is vulnerable to CL.0 request smuggling attacks. The back-end server ignores the Content-Length header on requests to some endpoints.
To solve the lab, identify a vulnerable endpoint, smuggle a request to the back-end to access to the admin panel at /admin, then delete the user carlos.
This lab is based on real-world vulnerabilities discovered by PortSwigger Research. For more details, check out Browser-Powered Desync Attacks: A New Frontier in HTTP Request Smuggling.
```
Tại request các static resource, ta thử thêm body `abl`, server báo chỉ hỗ trợ GET, POST và HEAD => smuggling thành công

Smuggling request đến /admin (dùng pipeline để gửi 2 request `POST /resources/labheader/js/labHeader.js` và `GET /x`)

Smuggling request delete user carlos

Solve

## Lab: Client-side desync
```!
This lab is vulnerable to client-side desync attacks because the server ignores the Content-Length header on requests to some endpoints. You can exploit this to induce a victim's browser to disclose its session cookie.
To solve the lab:
Identify a client-side desync vector in Burp, then confirm that you can replicate this in your browser.
Identify a gadget that enables you to store text data within the application.
Combine these to craft an exploit that causes the victim's browser to issue a series of cross-domain requests that leak their session cookie.
Use the stolen cookie to access the victim's account.
```
Tại request đến home page, thêm giá trị content-length lớn hơn len của body, ta thấy request vẫn bth đồng thời trả về 302 đến `/en`

Thử smuggling như sau:

Check comment ở postId 9-> "bắt" thành công request của user khác

Payload khai thác client desync như sau

Note: "cors" để trigger cors error chặn follow redirect


Set lại cookie và solve bài lab

*Cùng nhìn lại một tí về vụ redirect đã nói ở trên, thử setup với `mode` là `no-cors`. Ta thấy request "bắt" được sẽ như sau:*

Thử đóng giả victim và test, ta có tab Network như sau:

=> Response cho request đầu tiên trigger 1 redirect (thêm 1 get request đến `/en`) và vì thế hoàn tất quá trình smuggling
## Lab: Browser cache poisoning via client-side desync
```!
This lab is vulnerable to client-side desync attacks. You can exploit this to induce a victim's browser to poison its own cache.
To solve the lab:
- Identify a client-side desync vector in Burp, then confirm that you can trigger the desync from a browser.
- Identify a gadget that enables you to trigger an open redirect.
- Combine these to craft an exploit that causes the victim's browser to poison its cache with a malicious resource import that calls alert(document.cookie) from the context of the main lab domain.
```
Nếu `GET /..%2f` -> server trả về lỗi, thử CL.0 ngay tại đây bằng cách thêm một smuggling request `GET /blba`

*(tab group)*


-> Thành công tìm đc attack vector
Tiếp theo chuyển thành fetch (sử dụng protocol relative url), và test trên console

Check Network tab, ta thấy có 3 entry được hiển thị

- Request đầu được gửi bằng fetch đến `/..%2f`
- Request thứ hai được trigger bơi `location=` và đồng thời server response lại 302 ưng với smuggled request
- Request đến exploit server của ta
Để ý, ở `/login` có import script từ `/resources/js/analytics.js`, vì vậy ta sẽ poison cache của browser ngay tại đây, setup exploit server trả về polygot js + html như sau:

Lần đầu khi victim access vào exploit server lúc này window.name dĩ nhiên khác `skip`, sẽ thực hiện client side desync để poison browser cache của `/resources/js/analytics.js` **trả về response** redirect đến exploit server và ngay sau đó **lại được redirect** đến exploit server một lần nữa. Ở lần này, `window.name` đã bằng `skip` nên được redirect về lại `/login` để import file js và thực thi `alert(document.cookie)`.
Solve

## Lab: Server-side pause-based request smuggling
```!
This lab is vulnerable to pause-based server-side request smuggling. The front-end server streams requests to the back-end, and the back-end server does not close the connection after a timeout on some endpoints.
To solve the lab, identify a pause-based CL.0 desync vector, smuggle a request to the back-end to the admin panel at /admin, then delete the user carlos.
```
Ta thấy server sử dụng `Server: Apache/2.4.52` và [bị lỗi](https://github.com/advisories/GHSA-2hwm-6xjf-3xmx) smuggling attack không đóng connection, dấn đến "pause-based" request smuggling.
Và từ bài phân tích của portswigger, ta tiến hành đi tìm các endpoint trigger server-level redirect:

Sau một hồi fuzzing, endpoint `/resources` chính là cái cần tìm

Setup Turbo Intruder

Chạy Attack, đợi 61s và kết quả

=> Access thành công vào endpoint /admin
Chỉnh lại Host header thành `localhost`

Attack -> bypass được 401 tại `/admin`. Tới đây lưu lại giá trị của csrf token

Chỉnh lại python script ở turbo intruder editor

và solve

###### tags: `portswigger`