【问题标题】:How can I prevent scripts from being executed directly from the URL?如何防止脚本直接从 URL 执行?
【发布时间】:2018-06-07 10:17:51
【问题描述】:

我们最近发现某些脚本(如果您知道路径)可以直接从网站外部执行。 (理想情况下,执行脚本的唯一方法应该是通过 ssh-ing 进入服务器或设置 cron(或应用程序功能)

exmple.com/scripts_directory/script_sub_dir_1/script_1_name.php

同样,我们发现很多图片和视频可以直接从网站外部访问,直接从媒体文件的路径访问。

exmple.com/media_directory/media_sub_directory/media_file.mp4

理想情况下,该网站的用户应该登录才能查看任何内容,因为它受版权保护,并且需要付费。

我们能做什么:

  1. 保护我们的网站免受从 url 执行的脚本
  2. 保护媒体文件不被访问(如果用户未登录/未登录应用程序)。

这些是我正在查看的一些链接: https://webmasters.stackexchange.com/questions/84615/protecting-video-being-from-downloaded Prevent direct access to a php include file

我们有一个使用 php 5.6 的 nginx 服务器。

更新:

以下位置无法访问。

exmple.com/scripts_directory/script_sub_dir_1/

exmple.com/media_directory/media_sub_directory/

exmple.com/scripts_directory/

exmple.com/media_directory/

【问题讨论】:

  • 我不熟悉 nginx,但是使用 Apache,您可以配置一个拒绝规则,该规则将完全拒绝对 scripts_directory 的访问,因此请运行 Google 搜索“nginx 拒绝目录”或类似的东西.但是,如果该目录对于通过 AJAX 或 w/e 执行用户操作至关重要,那么您将破坏您的网站。至于媒体文件访问,我建议在 nginx 中寻找 Apache 的 mod_xsendfile 等效项。
  • 在 nginx 中,您可以将所有 mp4 文件重定向到 PHP 文件,您已经进行了用户身份验证,用户通过身份验证并释放文件。我可以发布任何示例。
  • ngx_http_access_module 将阻止所有人,而不是仅阻止未经身份验证的用户。
  • @MonkeyZeus 据我所知,<a href="http://www.example.com/scripts_directory/my_secret_scr‌​ipt.php">Perform action!</a> 的情况不会发生。它都使用函数调用。

标签: php security nginx


【解决方案1】:

防止未经授权的人下载文件。

要阻止对文件的访问,您可以进行以下配置而不是 Nginx:

在我的例子中,文件是:/etc/nginx/sites-available/default

location ~ \.mp4$ {
        rewrite (.*) /download.php?$args last;
}

此代码将导致对视频的所有访问,被重定向到文件 download.php

在这个文件中我们可以检查用户是否登录。

download.php

<?php

/* Current Folder */
define("PATH", __DIR__);

/* Removes arguments from URL. (You can also do this check in nginx.) */
$request_uri = preg_replace("/\?.*/", "", $_SERVER['REQUEST_URI']);

/* File path */
$file = sprintf("%s%s",
        PATH,
        $request_uri);


/**
 * Checks whether the file exists (You can also do this check in nginx.)
 * Add your rule to see if the user has permission.
 */
if( file_exists($file) && Auth::isLogged()) {

        /* The Content-Type you can add "application/octet-stream" */
        header('Content-Type: video/mp4');
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
        readfile($file);
}
elseif (!file_exists($file)) {
        header("HTTP/1.1 404 Not Found");
} else {
        header("HTTP/1.1 401 Unauthorized");
}

防止人们访问 PHP 文件。

要阻止对给定脚本的访问,有两种方法。

  1. index.php 中添加一个常量并检查其他文件是否已创建。如果它不是由index.php 创建的,则会显示错误消息。

index.php

<?php

define("APP_VERSION", "1.0.0");

my_script.php

<?php

if (!defined("APP_VERSION")) {
    die("Error");
}
  1. 另一种方法是设置 deny all,如 cmets 中所述。

在我的例子中,文件是:/etc/nginx/sites-available/default

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}

# Change path
location ~ /scripts_directory/script_sub_dir_1/.*\.php$ {
    deny  all;
}

您也可以只允许少数 ip 访问。为此,只需添加:allow YOUR-IP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2012-12-21
    • 2014-04-02
    相关资源
    最近更新 更多