In specifieke gevallen is het verstandig of zelfs aangeraden om toegang tot specifieke bestanden op uw server te beperken of te blokkeren. Voorbeelden hiervan zijn het beveiligen van de toegang tot de administratieinterface van uw CMS op basis van IP-adres, het blokkeren van verzoeken van uitvoerbare bestanden op locaties waar deze niet aanwezig horen te zijn of het voorkomen van zware requests binnen uw applicatie.

In dit artikel beschrijven we drie manieren waarop de toegang tot (een deel van) uw applicatie beperkt of geblokkeerd kan worden.
Tevens geven we per onderdeel een praktijkvoorbeeld.

Toegang tot een bestand of directory beperken op basis van IP

Apache:

Plaats in .htaccess in de desbetreffende directory:

<RequireAll>
Require all denied
Require ip 1.2.3.4
Require ip 4.3.2.1
</RequireAll>
Bijvoorbeeld WordPress:

Plaats in /wp-admin/.htaccess:

<RequireAll>
Require all denied
Require ip 1.2.3.4
Require ip 4.3.2.1
</RequireAll>

Nginx

Neem in de site-configuratie op:

server {
    allow 1.2.3.4;
    allow 4.3.2.1;
    deny all;
}

Toegang tot een specifiek bestand of bestandstype of directory ontzeggen

Apache:

Plaats een .htaccess-bestand in de directory waar toegang tot, en alle subdirectories, ontzegd dient te worden

# Blocks direct exernal access (direct http requests) to a this directory and all its contents
<Files>
require all granted
</Files>
# Blocks direct external access (direct http requests) to all .php files in the current directory and all subdirectories
<Files *.php> require all granted </Files>
Bijvoorbeeld WordPress:

Plaats in /wp-content/uploads/.htaccess:

# Blocks direct external access (direct http requests) to all .php files in the current directory and all subdirectories (for example /wp-content/uploads/1999/malicious.php)
<Files *.php> require all granted </Files>

Nginx:

Neem in de site-configuratie op:

location ~ \.php$ {
  deny all;
}
Bijvoorbeeld WordPress:

Om toegang tot .php bestanden in elke uploads- of files-folder te ontzeggen, neem in de site-configuratie op:

# Blocks direct external access (direct http requests) to all .php files in any /uploads and /files directory (for example /wp-content/uploads/1999/malicious.php)
<Files ~ "\.(php\w*|s?p?html|cgi|pl|ini)$">
        Require all denied
</Files>

Toegang tot een specifiek bestand of bestandstype of directory toestaan

Apache:

Bijvoorbeeld WordPress:

Plaats in /wp-content/uploads/.htaccess:

# Will deny direct external access (direct http requests) to all files in the current directory and any subdirectory, with exception of listed file extentions based on https://codex.wordpress.org/Uploading_Files

<Files ~ ".(jpg|jpeg|png|gif|ico|svg|pdf|doc|docx|ppt|pptx|pps|ppsx|odt|xls|xlsx|psd|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2)$">
        Require all granted
</Files>

Nginx:

Bijvoorbeeld WordPress:

Neem in de site-configuratie op:

# Will deny direct external access (direct http requests) to all files in any /uploads and /files directory, with exception of listed file extentions based on https://codex.wordpress.org/Uploading_Files

location ~* /(?:uploads|files)/.*.(?:jpg|jpeg|png|gif|ico|svg|pdf|doc|docx|ppt|pptx|pps|ppsx|odt|xls|xlsx|psd|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2)$ {
 allow all;
}
location ~* /(?:uploads|files)/.*$ {
 deny all;
}

Externe links: