dsdukbc60905239 2018-05-29 09:55
浏览 53
已采纳

如何拒绝执行特定apache目录下的任何文件?

I'm using apache2 and php. I built a form that lets the user to upload files to a specific directory. I already implemented some other security things, but I would also like to deny the execution of any file on that directory. They're meant to be only downloaded and not executed by users or scripts.

I've got the following code for htaccess, but it's a fake one, not sure of the syntax, nor if it's the best way of doing it:

<Location "/example/mydir/">
    <Files .>
        ForceType application/octet-stream
        Header set Content-Disposition attachment
    </Files>
</Location>

Could you please help me correct that code or point me to best practices?

  • 写回答

1条回答 默认 最新

  • doufu8887 2018-05-29 10:29
    关注

    Providing file upload/download facilities is a huge can of worms. In addition to the possibility of attacking your server there's also questions about data smuggling. malware and intellectual property. But since you are specifically asking about the former...

    Disabling PHP execution in this way only provides a single layer of prevention. if that layer fails for some reason then your security is gone. Also, this only prevents execution of the content if the webserver is pointed directly at the URL of the file - it doesn't provide any protection if someone can trick the existing php code into including the content.

    A minimal approach would be to store the content outside of directories accessible by URLs (i.e. outside of the document root and any other mapped directories).

    This does not prevent the inclusion vulnerability but eliminates the direct addressing vulnerability. All access to the content must then be mediated by a PHP script. But on the upside, its a lot easier to avoid OMGWTFs like:

    ForceType application/octet-stream

    Switching off PHP execution (php_flag engine off) is a better solution to disabling execution than changing the mime type. Forcing a mime type like this is always a bad idea.

    An alternative/complementary approach, would be to encode the files thus preventing code inclusion vulnerabilities.

    It's also a good idea to allocate your own filenames to the artefacts on your host filesystem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测