> For the complete documentation index, see [llms.txt](https://shangs.gitbook.io/shine/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shangs.gitbook.io/shine/write-up-ctf/ctf-competitions/m-ctf-training-2023-writeup-web.md).

# \[M☆CTF Training 2023] Writeup Web

## Ping-Pong

Bài này chỉ nhìn vào đã biết lỗ hổng

`Vuln:` Command Injection

`Tool:` Burp suite

`Payload:` %0acat+/proc/self/environ (%0a->xuống dòng)

<figure><img src="/files/xh3obik268E5YX8YGeOd" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/uwS8xXFP9DHgOzQAE2yL" alt=""><figcaption></figcaption></figure>

`Flag: MCTF{k0Mm4Nd_1njeCT10N_1T'5_E45y}`

## CTF-WIKI and CTF WIKI-2

<figure><img src="/files/8l21dwra7ZpYKjauUshD" alt=""><figcaption></figcaption></figure>

Hai bài này chung một lỗ hổng, nhưng chỉ khác nhau nơi giấu flag thôi.

`Vuln:` Directory traversal/path traversal

`Tool:` Burp suite

<figure><img src="/files/J470ctKfMMSVQlUkO4Mk" alt=""><figcaption></figcaption></figure>

### Wiki

Bài này flag giấu ở /proc/self/environ có thể dùng payload `../../../proc/self/environ`

Có một cái tiện hơn là web này được code bằng php-> check file /proc/self/cmdline.

Có thể LFI to RCE bằng cách **sử dụng các wrapper của PHP:**

* PHP cung cấp wrapper là các built-in được sử dụng cho các giao thức kiểu url. Nó sử dụng một số hàm hệ thống như fopen(), copy(), file\_exit() và file\_size().
* Các wrapper mà PHP cung cấp: Lưu ý: Để sử dụng các wrapper này thì chúng ta cần phải đáp ứng một số điều kiện môt trường.

{% hint style="info" %}
[file://](https://www.php.net/manual/en/wrappers.file.php) — Accessing local filesystem

[http://](https://www.php.net/manual/en/wrappers.http.php) — Accessing HTTP(s) URLs

[ftp://](https://www.php.net/manual/en/wrappers.ftp.php) — Accessing FTP(s) URLs

[php://](https://www.php.net/manual/en/wrappers.php.php) — Accessing various I/O streams

[zlib://](https://www.php.net/manual/en/wrappers.compression.php) — Compression Streams

[data://](https://www.php.net/manual/en/wrappers.data.php) — Data (RFC 2397)

[glob://](https://www.php.net/manual/en/wrappers.glob.php) — Find pathnames matching pattern

[phar://](https://www.php.net/manual/en/wrappers.phar.php) — PHP Archive

[ssh2://](https://www.php.net/manual/en/wrappers.ssh2.php) — Secure Shell 2

[rar://](https://www.php.net/manual/en/wrappers.rar.php) — RAR

[ogg://](https://www.php.net/manual/en/wrappers.audio.php) — Audio streams

[expect://](https://www.php.net/manual/en/wrappers.expect.php) — Process Interaction Streams
{% endhint %}

Sử dụng wrapper data://

* Đây là một hàm thực thi code từ xa. Ta có thể inject đoạn mã mà mình muốn thực thi vào url.
* Payload: `?file=data:,<?php system('cmd'); ?>`

<figure><img src="/files/ZrXJA1TCgpty15FkZuuX" alt=""><figcaption></figcaption></figure>

### Wiki 2

Bài này thì phải rce vì flag có tên file riêng không nằm bất kỳ trong file nào quan trọng của linux.

Lúc này vẫn sử dụng wrapper data để rce.

Payload như cũ: `?file=data:,<?php system('cmd'); ?>`

<figure><img src="/files/j13Gq8aKnJkPAz8dhMpC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/YRVD7MiFGjWZJRvYaWMH" alt=""><figcaption></figcaption></figure>

## Admin-secret

<figure><img src="/files/ba9oAbSQSlh8Goxs8waR" alt=""><figcaption></figcaption></figure>

Bài này lúc đầu cứ nghĩ nó là SQL Injection nhưng không....Nó có source code.

<figure><img src="/files/5dT7q9nDRJ5PCJKlGuPf" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" %}

```php
<?php
if (isset($_GET["source"])) highlight_file(__FILE__) && die();
$check = "Wrong!";
if (isset($_GET['username']) && isset($_GET['password'])){
    $username = $_GET['username'];
    $password = hash("SHA256", $_GET["password"]);
    if ($username == 'admin' && $password == 0){
        include'./flag.php';
    }}
?>
```

{% endcode %}

Phân tích code thì vuln nhìn đã có:  PHP Magic Tricks: Type Juggling.

Password được nhập sẽ được hash theo type sha256 và sau đó so sánh với không nhưng điều đang nói ở đây thay vì so sánh chặt chẽ là === thì lại ==.

Phân tích qua PHP Type Juggling:

* Toán tử so sánh **==** nếu như kiểu dữ liệu khác nhau nó sẽ đưa về một kiểu dữ liệu chung để thực hiện việc so sánh
* | PHP Comparisons: Loose | Value |
  | ---------------------- | ----- |
  | "0000" == int(0)       | TRUE  |
  | 0e12" == int(0)        | TRUE  |
  | "0e12345" == "0"       | TRUE  |
  | "0abc" == int(0)       | TRUE  |
  | "abc" == int(0)        | TRUE  |
  | "0e12345" == "0e54321" | TRUE  |
  | "0e12345" <= "1"       | TRUE  |
  | "1abc" == int(1)       | TRUE  |
  | "0xF" == "15"          | TRUE  |

Nếu ép kiểu 1 string `1e` theo sau là 1 string bất kỳ (ví dụ: `1e219229` ) về kiểu int, thì sẽ là `(int) 1`

Ở đây có thể dùng var\_dump để check. Nếu như sha256 encode dạng 0e23323 thì lúc này nó sẽ ép kiểu về int(0) -> pass&#x20;

Note: Có thể hiểu như 0^233333 = 0

<figure><img src="/files/7FNJDZeZlkfXnzyBbx1M" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/X67wDJUCWU4EogvHNqaj" alt=""><figcaption></figcaption></figure>

Khi đó bypass thành công == trong php(Tương tự MD5, SHA1, SHA224 cũng thế)

<pre><code><strong>username:admin&#x26;password:34250003024812
</strong></code></pre>

<figure><img src="/files/zW8daA3ebB27ytMklUgG" alt=""><figcaption></figcaption></figure>

## Easy JWT

<figure><img src="/files/px2AtNGMaAgxCsPbIIao" alt=""><figcaption></figcaption></figure>

Ở đây có hai chức năng đó là /gettoken và /flag. Ở đây phải làm sao đó để leak flag ra ngoài.

Phân tích qua xíu đoạn token khi nhập username:&#x20;

<figure><img src="/files/UifkE7FzKkCiJCOEGoth" alt=""><figcaption></figcaption></figure>

Chỗ cần xem xem ở key can\_get\_token: 0 , ở đây chắc phải thay thành 1 để lấy flag, quan trọng không có secret key. Mình brute force nhưng không được, thấy ảnh cạnh bên có tên là hint.jpg.

<figure><img src="/files/r6PrCGou4lftKRhvJRxm" alt=""><figcaption></figcaption></figure>

Ở đây Secret sẽ viết bằng tiếng ra rồi phiên âm qua tiếng anh, lấy chữ cái đầu và viết hoa.

Мамонт - М Титан - Т Утка - У Сыр - С Изуку Мидория - И

Secret: MTUCI

<figure><img src="/files/3IG5VocWWFTOW0GdE80t" alt=""><figcaption></figcaption></figure>

Sửa lại cookie rồi getflag

<figure><img src="/files/FzqQRdkimJPk6m21WAMY" alt=""><figcaption></figcaption></figure>
