【问题标题】:How to block uploaded files? [duplicate]如何阻止上传的文件? [复制]
【发布时间】:2016-06-28 02:16:51
【问题描述】:

基于这两个问题:

我写了一个简单的密码保护文件上传器。

所以我有一个名为 password.php

的密码验证器
    <body>
        <?php
            $pass = $_POST['pass'];
            {?>
                <form method="POST" action="password.php">
                    Password <input type="password" name="pass"></input>
                    <input type="submit" name="submit" value="Ok"></input>
                </form>
            <?}
                if($pass == "admin")
                    {
                    include("../uploader.html");
                    }
        ?>
    </body>

从代码中可以看出。如果通过正常,它包括 public_html 文件夹中的 uploader.html

    <body>
        <form enctype="multipart/form-data" action="upload.php" method="POST">
            <input name="thefile" type="file" /><br>
            <input type="submit" value="Send" />
        </form>
    </body>

最后我们使用upload.php将“thefile”发送到名为files的文件夹中。

    <body>
        <?php
            $thefile_tmp = $_FILES['thefile']['tmp_name'];
            $thefile_name = $_FILES['thefile']['name'];
            if(is_uploaded_file($thefile_tmp)) 
                {
                move_uploaded_file($thefile_tmp, "files/$thefile_name");
                }
        ?>
    </body>

我将 files 文件夹的 chmod 更改为 777,一切正常。

我把密码给了一群学生,我让他们给我发报告。学生现在可以匿名上传恶意文件(比如说 loop.php)并在浏览器中打开它.../files/loop.php

所以我想保护我的网站免受此类报道的影响。我尝试将 files 文件夹的 chmod 设置为 773 或 776。在 776 上,我的代码无法发送文件。在 773 上,您仍然可以打开 files 文件夹中的内容。

问题/请求有没有一种简单的方法可以阻止打开上传的文件?


我使用的好简单的解决方案

根据@Fred -ii- 的建议,我在 upload.php 中添加了一个扩展验证程序。现在是这个样子

<body>
    <?php
        $thefile_tmp = $_FILES['thefile']['tmp_name'];
        $thefile_name = $_FILES['thefile']['name'];
        $thefile_ext = pathinfo($thefile_name, PATHINFO_EXTENSION);
        if(is_uploaded_file($thefile_tmp))
            {
            if( $thefile_ext == 'zip')
                {
                move_uploaded_file($thefile_tmp, "files/$thefile_name");
                }
            }
    ?>
</body>

替代解决方案

正如@alexander.polomodov 的回答。我只是将 .htaccess 文件放在 files 文件夹中,仍然可以发送文件,但没有人可以访问它。这种方法不适合我,因为我希望学生能够偷看彼此的报告。

【问题讨论】:

  • 您可以在上传时从文件中删除 .php 扩展名。因此,当服务器收到对最近上传文件的请求时,它不会执行该文件。
  • “我要求提供 .zip 文件” - 是的,在 cmets 中。不在你的问题中。是什么阻止您从可能的欺骗中添加到阵列中?你没有看完里面的所有答案吗?
  • 您删除了关于“我要求提供 .zip 文件” 的评论,现在您不再回复。看看下面那个人的回答,那么其中一个链接可能是骗子之一。
  • @Fred-ii- 对不起。我没有赶上。现在我正在尝试检查我的服务器上是否有 Apache 或 Nginx。
  • @Fred-ii- 我使用了您在链接中放置的解决方案,它工作正常。 PS。很抱歉删除了之前的评论。

标签: php html file-upload


【解决方案1】:

您使用什么网络服务器?

1) 如果您使用 Apache,请关注 answer

用字符串添加文件files/.htaccess:

Deny from all

2) 如果您使用 Nginx,请关注此answer

添加到您的 nginx 配置中:

location /files {
    deny all;
    return 404;
}

【讨论】:

  • 您可以尝试第一个变体,这可能是您的解决方案,因为 apache 是托管的常用网络服务器。
  • 我尝试添加.htaccess,但文件没有出现在服务器上
  • 您通常如何在网络服务器上上传文件?
  • 通过 WinSCP,但我通过 mRemoteNG 登录到终端。
  • 您将 .htacces 文件添加到文件目录中,但在 WinSCP 或 websers 中看不到它?我质疑你是因为你在网络服务器上看不到 .htaccess 文件是正常的(这些文件用于服务器而不是用于人:))
猜你喜欢
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 2014-10-26
  • 2020-07-08
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多