此脚本是否容易受到shell命令绕过?

时间:2017-03-21 19:17:11

标签: php linux shell security file-upload

我正在使用此脚本从Android应用程序接收基本64位编码图像。我想知道是否有可能绕过POST请求中的PHP shell命令并使其在服务器中工作,例如,发送命令shell编码和名称如“shell.php”,“shell.php% 0delete0" 。根据脚本,一切都将保存为.png,所以我说这是安全的,但也许我错了,脚本实际上容易受到shell命令上传的攻击。<​​/ p>

<?php 
header('charset=utf-8');

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['image']) && isset($_POST['name'])) {
        $image = $_POST['image'];
        $name = $_POST['name'];
        file_put_contents("/var/www/html/admigas/android/uploads/$name".strval(date('_Ym')).".png",base64_decode($image));
        echo "Success";
    } else {
        echo "Wrong params";
    }
} else {
    echo "Nothing to do";
}

?>

1 个答案:

答案 0 :(得分:0)

所以,让我们一步一步地看一下代码。我的评论前面有#

<?php 
header('charset=utf-8');

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['image']) && isset($_POST['name'])) {
        # This is provided by the user:
        $image = $_POST['image'];
        # This is provided by the user:
        $name = $_POST['name'];
        # Where does $name come from? User input, of course!
        file_put_contents("/var/www/html/admigas/android/uploads/$name".strval(date('_Ym')).".png",base64_decode($image));
        echo "Success";
    } else {
        echo "Wrong params";
    }
} else {
    echo "Nothing to do";
}

因此,使用base64编码的有效负载发送name=../../../../reverse-shell.php%00将允许攻击者上传任意文件(在本例中为PHP代码),并可能在随后的PHP请求中执行它。

解决方案(以diff格式显示):

-           $name = $_POST['name'];
+           $name = preg_replace('#[^A-Za-z0-9\-_]#', '', $_POST['name']);

您肯定希望删除任何非URL安全字符以防止攻击。您可能需要totally rethink your strategy, of course