AireServ
By:
AireServ

Blocking a directory of a website via DNS

January 5, 2017 1.4k views
Apache DNS Nginx Ubuntu

Hello, so I have been trying to block only a directory of a website for a few days now. I want to edit it and make it look like the site is telling the user you cannot view this page on this network; like DNS spoofing, but for a directory only.

I'm not very experienced with Nginx or Apache (in Ubuntu) so I'm not quite sure what my options are.

For an example, I want to block example.com/content/iwantthisblocked but I want everything else to be unblocked or "pass through."

Can anyone help me with this? It would be very helpful as I do not want to spend the money on an expensive web filter.

Thanks!

1 comment
  • I am meaning an external site such as sites.google.com/someone/theirwebsite or google.com/download.zip

4 Answers
jtittle1 January 6, 2017
Accepted Answer

@AireServ

As far as DNS goes, you won't be able to block directory access using DNS. DNS simply routes the entries from the DNS provider (i.e. DigitalOcean or your domain registrar) to your web server. Once the request is routed, the web server handles the request and is responsible for how it's handled (i.e. the response sent to the browser).

You can dig in to more routing options, of course, but for something as simple as blocking directory access, it'd be overkill since NGINX is capable of handling this for you.

  • @jtittle

    How would I go about doing so? I meant an external website (sorry didn't mention that) such as sites.google.com/someone/theirwebsite. Again, not very experienced with NGINX, sorry.

  • @jtittle

    Basically what I would do is edit sites.google.com/someone/theirwebsite to say this page is blocked on the current network (my entire network is running off of this DNS) but have the same URL so the user does not know I am blocking it.

    • @AireServ

      You won't be able to block access to a domain or domain's directory that you do not actually control. So if, for example, you're wanting to block users from accessing a file or directory on example.com, such as example.com/filename.zip or example.com/directory, unless you control example.com, and it's DNS / Web Server, you're options are limited.

      You could prevent your users from clicking on a link on your site that directs to that location, but you can't use your Droplet or modify your DNS to block another websites files or directories. Even using JavaScript, the user could just copy and paste the URL and navigate it that way.

      • @jtittle

        Darn, I will do that then. Thanks for the help!

      • @jtittle

        Is there any way I could do that if I have all connections pass through my compter and filter it there? A more permanent fix is on its way, but this is a quick temporary fix.

        • @AireServ

          The only way it'd be possible to for you to block access to a URL locally would be by using a blacklist (which most routers provide as a built-in utility).

          You could, also, setup a VPN on a DigitalOcean Droplet and then route your local traffic through the VPN (which, again, would require you to modify your local router configuration to use the VPN), but that's really the only other way.

          What it boils down to here is your router and whether it's capable of blacklisting websites. I know Netgear and LinkSys routers provide this (even third-party software that replaces theirs does), but beyond that, you'd have to login to your router and see if that functionality is available.

          Short of connecting to a VPN, your Droplet won't do any of this for you and you would then need to configure the VPN to block certain requests.

I am not sure what the intent is but you could change the permissions on the folder so it's not accessible to Apache and Apache will generate a 403 forbidden message to the end user.

chmod 000 /path/to/iwanthisblocked

Change mode to 644 if you want it to be accessible.

chmod 755 /path/to/iwanthisblocked

You can customize the 403 message. https://httpd.apache.org/docs/2.4/custom-error.html has details.

Gaurav

  • @GauravSabharwal

    That would work if I was hosting the website, but I am trying to block an external website (ex: sites.google.com/someone/theirwebsite

    • What kind of administrative control do you have over the external website and from where are you trying to block access? Maybe a real world use case might help me understand the problem better.

      • I have zero administrative control over the website, which is why I am using DNS to block the directory of it.

        Basically, I want to block (example) sites.google.com/someone/theirwebsite on my local network. When a user goes to that page, the DNS (or Apache/NGINX) will intervene and deny them access to it with a set message without redirecting the client.

@AireServ

Are you wanting to actually block access to the directory, or simply prevent visitors from being able to view the index listing of the directory?

If you want to simply prevent a directory listing, dropping an index.html or index.php file in will prevent others from being able to view a listing of other files (but it will not prevent them from being able to access other files if they happen to guess file names i.e. random_filename.png, for example).

The other methods will require you to modify NGINX configuration, primarily the server block for the website. You'd need to login via SSH and pull up the website server block which should be located in:

/etc/nginx/sites-available

or

/etc/nginx/sites-enabled

Generally, sites-available is symlinked to sites-enabled, so you'd modify the file in the first and the second would be automatically updated.

To block access, you'd want to drop a location block in to the file, which would look something like:

location ~ /folder1 {
    deny all;
}

You'll want to make sure you add that before other blocks, such as a location block that allows PHP files to be accessed:

location ~ \.php$ {
    ....
}

So, for example, it should look like this:

location ~ /folder1 {
    deny all;
}

location ~ \.php$ {
    ....
}

and not:

location ~ \.php$ {
    ....
}

location ~ /folder1 {
    deny all;
}

You can also block multiple directories using a single block, like so:

location ~ /(folder1|folder2|folder3) {
    deny all;
}

You would simply swap out folder1, folder2, and folder3 with the paths to the folders you want to block.

That being said, the above also will not block access to files, only to the directory itself. So if someone guesses a random filename, they will still be able to access it.

Example:

example.com/content/iwantthisblocked will be denied.

example.com/content/iwantthisblocked/filename.html will not be denied.

@AireServ

Unless your router allows for the configuration of a destination or redirect, no. Some routers provide more advanced options, some not so much. It all boils down to what the firmware allows for.

Have another answer? Share your knowledge.