当前位置:网站首页>File upload vulnerability - upload labs (1~2)

File upload vulnerability - upload labs (1~2)

2022-07-07 12:24:00 hcjtn

File upload vulnerability ——upload-labs(1~2)

The user uploaded a pointing script file , And through this script, you get the ability to execute server-side commands

In everyday life , We often upload various files , But if there is no restriction on the upload type or the restriction is not strict , An attacker may upload an executable file or web script ( Cause file upload vulnerability ), Cause the website to be controlled or even fall

Harm caused by file upload vulnerability

  • Make hackers hang black chains on your website
  • Hang a malicious Trojan ( Automatically pop up advertisements , Brush Click ) Or mining
  • Cause file data leakage

The premise of file upload vulnerability

  1. The website upload function can be used normally
  2. File types allow uploading such as :php asp In a word, the Trojan horse can enter the interior
  3. The upload path can be determined
  4. Files can be accessed , Can be executed or included

Therefore, we can start from the following three points to prevent loopholes

such as :

According to the second point, we can prohibit users from uploading php asp that will do ,

for instance , If users want to upload avatars , We can only let users upload jpg png gif jpeg The format of ( White list ) Filter at the front , Check the format of user's Avatar

We should try to check in advance , The workload of the back-end can be greatly reduced

Upload vulnerability basic process :

  1. Whether the pictures can be uploaded normally
  2. Whether the uploaded content of the picture is infected ( Content substitution )
  3. Whether the uploaded domain name is the target server ( If not , Judge whether the image server parses php、asp、aspx、jsp、cfm、shtml etc. )
  4. Whether the uploaded directory supports parsing php、asp、aspx、jsp、pht、phtml etc.
  5. Judge black and white list verification

With upload-labs-1 For example

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-4N6R297m-1643720447610)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20220201192714673.png)]

To upload pictures , Found upload successful

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ao2WoYr8-1643720447611)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20220201192751583.png)]

Upload shell Find out :[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-i1BhKt45-1643720447611)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20220201192946481.png)]

Found a white list , So from the front we know , It is very likely that there is a filter at the front end , Let's check :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1SIsmpDw-1643720447612)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20220201194030034.png)]

Find out checkFile() It has the effect of checking and filtering , We delete it and submit Find out :

 Insert picture description here

Then use Chinese ant sword Link Find success

Find success

At the same time, we find that just filtering at the front end is not enough , We can easily delete , So apart from the front end , The backend must also be verified

upload-labs-2(MIME verification )

Let's take a look at its source code first :

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
    
    if (file_exists($UPLOAD_ADDR)) {
    
        if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) {
    
            if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
    
                $img_path = $UPLOAD_ADDR . $_FILES['upload_file']['name'];
                $is_upload = true;

            }
        } else {
    
            $msg = ' Incorrect file type , Please upload again !';
        }
    } else {
    
        $msg = $UPLOAD_ADDR.' Folder does not exist , Please create... By hand !';
    }
}

Find out :

  if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) {
    
            if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
    
                $img_path = $UPLOAD_ADDR . $_FILES['upload_file']['name'];
                $is_upload = true;

            }
        } else {
    
            $msg = ' Incorrect file type , Please upload again !';

At this time, we will have questions : How does the browser get the type of file we upload

This is about MIME type ( Multipurpose Internet mail extension type )

MIME(Multipurpose Internet Mail Extensions) Multipurpose Internet mail extension type . Is to set some kind of Extension Of file Use one kind Applications To open the way type , When the extension file is accessed , browser Will automatically use the specified application to open . Mostly used to specify some client-side customized file names , And some ways to open media files .

It's an Internet standard , Extended e-mail standards , To enable it to support :

Not ASCII Character text ; Non text attachments ( Binary system 、 voice 、 Image, etc ); By many parts (multiple parts) The body of the message ; Include non ASCII Character header information (Header information).

Common types :

Upload files multipart/from-data
Hypertext markup language text .text/html
xml file .xmltext/xml
XHTML file .xhtmlapplication/xhtml+xml
jpg Image format image/jpeg
RTF Text .rtfapplication/rtf
PDF file .pdfapplication/pdf
Microsoft Word file .wordapplication/msword

MIME usage :

Client usage :

  1. Tell the server , The type of file I uploaded
  2. Tell the server , The types of documents I can accept

Server use :

  • Tell the client , The data type I respond to

So when doing this problem, we should think of a reform Put it content-type Get rid of

So we use burp suite Carry out bag grabbing and package changing

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Aei1DeVo-1643720447613)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20220201205405896.png)]

Find will content-type Change it

 Insert picture description here

Change it to image/jpeg;

Click Send ,

 Insert picture description here
Find success

原网站

版权声明
本文为[hcjtn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130618271277.html