10 étapes pour sécuriser votre site web à l'aide du fichier HTACCESS


Etape 1 : Register_Globals en mode OFF:



Pour ceux qui utilisent PHP 5.4 ou supérieur, passez à l'étape suivante. Pour les versions antérieures de PHP, je vous conseil de passer la valeur Register_Globals en mode OFF. De la désactiver.

Code:

php_value register_globals OFF
// ou mettez ce code si cela ne marche pas
SetEnv REGISTER_GLOBALS 0

En revanche, si votré hébergeur n'utilse pas cette possibilité, essayez de modifier le fichier php.ini en ajoutant ceci :

Code:

register_globals = 0

 


Etape 2 :  Protéger l'accès à ce fichier depuis un navigateur


Si vous souhaitez protéger un dossier ou l'accès à un fichier depuis un navigateur voici le code à ajouter :

 

Code:

order allow,deny
deny from all

 


Etape 3 : Refuser le listage du contenu d'un dossier


Code:

Options -Indexes

 


Etape 4 : Bloquer les attaques pirates et certains aspirateurs de site web.


Nous allons bloquer une grande partie des attaques automatiques et ce code est le minimum qu'il faut utiliser mais offre de superbes résultats. N'hésitez pas rechercher sur le net d'autres codes anti-pirates si vous en trouvez !

Code:

###FILTRE CONTRE CERTAINS ROBOTS DES PIRATES
RewriteEngine On
## EXCEPTION: TOUS LES ROBOTS MEMES ANONYMES OU BANNIS PEUVENT ACCEDER A CES FICHIERS
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteCond %{REQUEST_URI} !^/sitemap.xml
##
RewriteCond %{HTTP_USER_AGENT} ^-?$ [OR] ## ANONYMES
RewriteCond %{HTTP_USER_AGENT} ^curl|^Fetch API Request|GT::WWW|^HTTP::Lite|httplib|^Java|^LeechFTP|lwp-trivial|^LWP|libWeb|libwww|^PEAR|PECL::HTTP|PHPCrawl|PycURL|python|^ReGet|Rsync|Snoopy|URI::Fetch|urllib|WebDAV|^Wget [NC] ## BIBLIOTHEQUES / CLASSES HTTP DONT ON NE VEUT PAS. ATTENTION, CELA PEUT BLOQUER CERTAINES FONCTIONS DE VOTRE CMS. NE PAS TOUT EFFACER, MAIS CHERCHEZ LE NOM DE LA CLASSE HTTP CONCERNEE (DEMANDEZ AUX DEVELOPPEURS DE VOTRE CMS). CETTE LISTE BLOQUE 80% DES ROBOTS SPAMMEURS. IL FAUT LA CONSERVER.
## RewriteCond %{HTTP_USER_AGENT} ^[bcdfghjklmnpqrstvwxz ]{10,}|^[0-9a-z]{15,}|^[0-9A-Za-z]{19,}|^[A-Za-z]{3,} [a-z]{4,} [a-z]{4,} [OR] ## CEUX QUI INVENTENT DES NOMS AU HASARD, RETIREZ LES 2 DIESES EN DEBUT DE LIGNE POUR L'ACTIVER
RewriteRule (.*) - [F]

 


Etape 5 : Bloquer toute une série de failles potentielles


Nous allons bloquer une multitude de failles potentielles. La majorité des pirates utilisent ces moyens pour tester la faiblesse de votre site. Ici, on bloque avant qu'ils n'accèdent à votre CMS, blog ou e-commerce !

Code:

### FILTRE CONTRE XSS, REDIRECTIONS HTTP, base64_encode, VARIABLE PHP GLOBALS VIA URL, MODIFIER VARIABLE _REQUEST VIA URL, TEST DE FAILLE PHP, INJECTION SQL SIMPLE
RewriteEngine On
RewriteCond %{REQUEST_METHOD} (GET|POST) [NC]
RewriteCond %{QUERY_STRING} ^(.*)(%3C|<)/?script(.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)(%3D|=)?javascript(%3A|:)(.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)document.location.href(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^.*(%24&x).* [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(127.0).* [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)(%3D|=)(https?|ftp|mosConfig)(%3A|:)//(.*)$ [NC,OR] ## ATTENTION A CETTE REGLE. ELLE PEUT CASSER CERTAINES REDIRECTIONS RESSEMBLANT A: http://www.truc.fr/index.php?r=http://www.google.fr ##
RewriteCond %{QUERY_STRING} ^.*(_encode|localhost|loopback).* [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)GLOBALS(=|[|%[0-9A-Z]{0,2})(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)_REQUEST(=|[|%[0-9A-Z]{0,2})(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)(SELECT(%20|+)|UNION(%20|+)ALL|INSERT(%20|+)|DELETE(%20|+)|CHAR(|UPDATE(%20|+)|REPLACE(%20|+)|LIMIT(%20|+)|CONCAT(%20|+)|DECLARE(%20|+))(.*)$ [NC]
RewriteRule (.*) - [F]

 


Etape 6 : Bloquer certaines requêtes dangereuses


Nous allons bloquer des requêtes bizarres et potentiellement risquées

Code:

### DES FAUX URLS OU VIEUX SYSTEMES OBSOLETES, ON LES NEUTRALISE
RedirectMatch 403 (../|base64|boot.ini|eval(|(null)|^[-_a-z0-9/.]*//.*|/etc/passwd|^/_vti.*|^/MSOffice.*|/fckeditor/|/elfinder/|zoho/|/jquery-file-upload/server/|/assetmanager/|wwwroot|e107_)
# DESACTIVE LES METHODES DE REQUETES TRACE TRACK DELETE
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(TRACE|DELETE|TRACK) [NC]
RewriteRule ^.* - [F]

 


Etape 7 : Autoriser l'affichage de certains fichiers et bloquer les autres


Nous allons avec cette étape, découvrir comment autoriser l'accès à certains fichiers, et pas à d'autres. index.php est le fichier lu par défaut. Le but est de refuser l'accès au pirate d'afficher sur son navigateur de fichiers ou format de fichier non autorisé.

Attention ! N'oubliez pas de tester ces blocages et de les adapter !

Code:

### SEUL LE FICHIER index.php EST SERVI COMME PREMIER FICHIER PAR DEFAUT. LES AUTRES SONT INTERDITS
DirectoryIndex index.php

### INTERDIRE LES AUTRES TYPES DE FICHIER INDEX

order allow,deny
deny from all


### INTERDIRE L'AFFICHAGE DE CERTAINS FORMATS DE FICHIER 
### EXÉCUTÉS PAR LE SERVEUR MAIS INTERDIT D'AFFICHAGE PAR LE NAVIGATEUR WEB

deny from all


### INTERDIRE L'AFFICHAGE DE CERTAINS FICHIERS COMME config, option, login, setup, install, admin, home, default, readme, changelog, xmlrpc.
### A ADAPTER SI CELA POSE PROBLEME, NOTAMMENT RETIREZ wp-(login|admin|config)| SI VOUS UTILISEZ WORDPRESS

order allow,deny
deny from all

 


Etape 8 : Refusez le hotlinking


Le hotlinking consiste à remplacer un domaine par le votre et par exemple .fr par fr, com, net, org ou autres extensions de noms de domaines en gardant le avant le . (point)

Code:

### ON EVITE LE VOL D'IMAGES, VIDEO, SON, FEUILLE DE STYLE, PDF ET ZIP
### LES VISITEURS DOIVENT PASSER PAR LE SITE. 
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^https?://[-a-z0-9.]*mondomaine.fr$ [NC] 
RewriteCond %{HTTP_REFERER} !^https?://[-a-z0-9.]*mondomaine.fr/.*$ [NC] 
RewriteCond %{HTTP_REFERER} !^https?://.*(translate|paypal|google|bing|yahoo|yandex|baidu|facebook|qwant|duck|ixquick|pinterest|twitter).*$ [NC] ## CES DOMAINES PEUVENT AFFICHER LES ELEMENTS DU SITE
RewriteCond %{HTTP_REFERER} !^mobile?://.*$ [NC] ## LES CONNEXIONS A TRAVERS LES APP DES MOBILES PEUVENT AFFICHER LES ELEMENTS DU SITE
RewriteRule .*.(gif|jpe?g?|jp2|png|svgz?|css|pdf|zip|gz|js|mp3|m4a|mp4|mov|divx|avi|wma?v?|wmp|swf|flv|docx?|xlsx?|pptx?|vbs|rtf|asf?x?|odt|ods|odp|odg|odb|eot|ttf|woff|woff2)$ - [NC,F]

 


Etape 9 : Sécurisons encore un peu !


Si toutefois des pirates ont réussi à pénétrer dans votre site, il est fort possible qu'ils tentent d'installer un script leur permettant de prendre en quelque sorte les commandes de tous vos scripts ! Cette commande est à tester avec votre site web. Elle est indispensable car très puissante mais utilisez-la en dernier. Si vous rencontrer toutefois des erreurs 500 ou autres, supprimez les lignes qui sont suceptibles de vous apporter des problèmes.

Code:

### FILTRE CONTRE PHPSHELL.PHP, REMOTEVIEW, c99Shell et autres
RewriteEngine On
RewriteCond %{REQUEST_URI} .*((php|my)?shell|remview.*|phpremoteview.*|sshphp.*|pcom|nstview.*|c99|r57|webadmin.*|phpget.*|phpwriter.*|fileditor.*|locus7.*|storm7.*).(p?s?x?htm?l?|txt|aspx?|cfml?|cgi|pl|php[3-9]{0,1}|jsp?|sql|xml) [NC,OR]
RewriteCond %{REQUEST_METHOD} (GET|POST) [NC]
RewriteCond %{QUERY_STRING} ^(.*)=/home/loginftp/(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^work_dir=.*$ [OR]
RewriteCond %{QUERY_STRING} ^command=.*&output.*$ [OR]
RewriteCond %{QUERY_STRING} ^nts_[a-z0-9_]{0,10}=.*$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)cmd=.*$ [OR] ## ATTENTION A CETTE REGLE. ELLE PEUT CASSER VOTRE SITE ##
RewriteCond %{QUERY_STRING} ^c=(t|setup|codes)$ [OR]
RewriteCond %{QUERY_STRING} ^act=((about|cmd|selfremove|chbd|trojan|backc|massbrowsersploit|exploits|grablogins|upload.*)|((chmod|f)&f=.*))$ [OR]
RewriteCond %{QUERY_STRING} ^act=(ls|search|fsbuff|encoder|tools|processes|ftpquickbrute|security|sql|eval|update|feedback|cmd|gofile|mkfile)&d=.*$ [OR]
RewriteCond %{QUERY_STRING} ^&?c=(l?v?i?&d=|v&fnot=|setup&ref=|l&r=|d&d=|tree&d|t&d=|e&d=|i&d=|codes|md5crack).*$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)([-_a-z]{1,15})=(ls|cd|cat|rm|mv|vim|chmod|chdir|concat|mkdir|rmdir|pwd|clear|whoami|uname|tar|zip|unzip|gzip|gunzip|grep|more|ln|umask|telnet|ssh|ftp|head|tail|which|mkmode|touch|logname|edit_file|search_text|find_text|php_eval|download_file|ftp_file_down|ftp_file_up|ftp_brute|mail_file|mysql|mysql_dump|db_query)([^a-zA-Z0-9].+)*$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)(wget|shell_exec|passthru|system|exec|popen|proc_open)(.*)$
RewriteRule (.*) - [F]

 


Etape 10 : Bloquer l'exécution de tout script côté serveur (PHP, Perl, etc...) dans un dossier


Cette astuce vous permettra par exemple de protgérer un dossier d'upload ou tout dossier très sensible. Créez donc un fichier HTACCESS pour chaque dossier sensible. Cette méthode permet d'empêcher d'exécuter un script directement depuis le dossier concerné. Nous allons donc protéger l'exécution directe du fichier par un navigateur.

Code:

# Aucun script dans le dossier et ses sous-dossiers, que ce soit PHP, PERL ou autre CGI, ne pourra s'executer si ExecCGI est inactif. Et interdit d'afficher la liste des fichiers.
OPTIONS -ExecCGI  -Indexes

Ne copiez-collez pas tous ces codes d'un coup !

Copiez certains codes en testant à chaque fois, ne mettez rien au hasard et lisez attentivement les instructions de ce tuotriel.

N'oubliez pas de placer ces lignes avant les lignes de récriture de liens dans le fichier HTACESS 

Voila pour ce tutoriel, 

À bientôt pour d'autres tutos !