1

We've recently discovered that certain scripts (if you know the path) can be executed directly from outside the website. (ideally the only way to execute a script should be by ssh-ing into the server or by setting a cron (or application features)

exmple.com/scripts_directory/script_sub_dir_1/script_1_name.php

Similarly we've discovered that a lot of images and videos can be accessed directly from outside the website, directly from the media file's path.

exmple.com/media_directory/media_sub_directory/media_file.mp4

Ideally the users of the website are supposed to be logging in to view any of the content since it is copyrighted, and to be paid for.

What can we do to:

  1. protect our site from scripts being executed from the url
  2. protect media files from being accessed (if the user is not logged in/outside the application).

These are some of the links I'm looking at: https://webmasters.stackexchange.com/questions/84615/protecting-video-being-from-downloaded Prevent direct access to a php include file

We have an nginx server using php 5.6.

Update:

The following locations are not accessible.

exmple.com/scripts_directory/script_sub_dir_1/

exmple.com/media_directory/media_sub_directory/

exmple.com/scripts_directory/

exmple.com/media_directory/

CP3O
  • 429
  • 1
  • 6
  • 21
  • I'm not familiar with nginx but with Apache you could configure a deny rule which would completely deny access to the `scripts_directory` so run a Google search for "nginx deny directory" or something to that effect. However, if that directory is critical for executing user actions via AJAX or w/e then you are going to break your site. As for the media file access I would suggest looking for Apache's `mod_xsendfile` equivalent in nginx. – MonkeyZeus Dec 26 '17 at 16:07
  • In nginx you can redirect all mp4 files to a PHP file, you have already made a user authentication the user is authenticated and releases the file. Anything I can post an example. – Valdeir Psr Dec 26 '17 at 16:12
  • For your first issue: http://nginx.org/en/docs/http/ngx_http_access_module.html – MonkeyZeus Dec 26 '17 at 16:13
  • `ngx_http_access_module` will block for all and not for unauthenticated users, only. – Valdeir Psr Dec 26 '17 at 16:15
  • @ValdeirPsr `ngx_http_access_module` is only for OP's first issue with the `scripts_directory` directory. – MonkeyZeus Dec 26 '17 at 16:17
  • @MonkeyZeus thank you for your comments, I understand from your comment that `ngx_http_access_module` would fix my scripts issue. 1. Would it allow the application to access the scripts? – CP3O Dec 26 '17 at 16:19
  • @ValdeirPsr Thank you for your comment. How would I redirect all mp4 files to a PHP file? Would this be some kind of temporary url system with a TTL hash? – CP3O Dec 26 '17 at 16:20
  • Assuming the PHP code is something like `include('scripts_directory/my_secret_script.php');` then it will continue to work no problem. – MonkeyZeus Dec 26 '17 at 16:21
  • @MonkeyZeus paths are relative not absolute, but yes they are all `"included"()`. – CP3O Dec 26 '17 at 16:23
  • @CP3O Sounds good. I guess it would be beneficial to show you what problematic code could look like as well. These would be an issue `file_get_contents( 'http://www.example.com/scripts_directory/my_secret_script.php' );` or `curl_setopt( $ch, CURLOPT_URL, 'http://www.example.com/scripts_directory/my_secret_script.php' );` or `Perform action!` or `Perform action!` – MonkeyZeus Dec 26 '17 at 16:26
  • 1
    @MonkeyZeus As far as I know, the situation of `Perform action! ` would not happen. Its all using function calls. – CP3O Dec 26 '17 at 16:32
  • @MonkeyZeus what do you think I can do for the media files? – CP3O Dec 26 '17 at 16:35
  • Sounds good, but don't be surprised if you discover some additional and gratuitous incompetence from the people that built your site. As for the media files it sounds like @ValdeirPsr knows much more than I do. – MonkeyZeus Dec 26 '17 at 17:10
  • have you tried [this](https://stackoverflow.com/questions/13262436/hotlink-protection)? I think your problem is "Hotlink Protection". I hope it helps you. – Wiguna R Dec 26 '17 at 21:26

1 Answers1

3

Prevent unauthorized people from downloading files.

To block access to the files, you can make a following configuration not Nginx:

In my case the file is: /etc/nginx/sites-available/default

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

This code will cause all access to the videos, be redirected to the file download.php

In this file we can check whether or not the user is logged in.

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");
}

Preventing people from accessing PHP files.

To block access to a given script, there are two ways.

  1. Add a constant in index.php and check the other files if it has already been created. If it was not created by index.php, it displays an error message.

index.php

<?php

define("APP_VERSION", "1.0.0");

my_script.php

<?php

if (!defined("APP_VERSION")) {
    die("Error");
}
  1. The other way is by setting a deny all as mentioned in the comments.

In my case the file is: /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;
}

You can also allow only a few ip's to have access. To do this simply add: allow YOUR-IP

Valdeir Psr
  • 702
  • 1
  • 7
  • 10