Try   HackMD

Création d'un site sécurisé sous Apache et Aperçu de la CGI avec Python.

Structure du répertoire

Dans cette partie nous allons mettre en place 2 sites sur un server Apache.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Créer un hôte virtuel

  • Créer les répertoires dans /var/www/
    ​​​​mkdir -p /var/www/vhosts/site1
    ​​​​mkdir -p /var/www/vhosts/site2
    

Création du Site 1

  • Créer un fichier index.html
    nano /var/www/vhosts/site1/index.html

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Créer de nouveaux fichiers d'hôtes virtuels

  • Créer un fichier d'hôte virtuel
    nano /etc/apache2/sites-available/port_vhosts.conf

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Activer le nouveau fichier de l'hôte virtuel

    ​​​​a2dissite 000-default.conf
    ​​​​a2ensite port_vhosts.conf
    
  • Redémarrer le service Apache

    ​​​​systemctl restart apache2 ou /etc/init.d/apache2 restart
    ​​​​systemctl status apache2 ou /etc/init.d/apache2 status
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Testez les résultats

  • Tester les configurations en allant sur le domaine
    http://192.168.64.133/

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Nous pouvons remarquer que les accents ne sont pas pris en compte donc nous allons corriger le problème d'encodage.

    • Vérifier les locales générées sur le système.
      grep -v "^#" /etc/locale.gen

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • Décommenter la ligne pour activer le paramètre AddDefaultCharset
      nano /etc/apache2/conf-enabled/charset.conf

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • Vérifier le résultat

      ​​​​​​systemctl restart apache2
      ​​​​​​systemctl status apache2
      
    • Sur un navigateur Web :

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

Sécurisation de base

  • Lorsqu'on essaye de naviguer sur une adresse eronnée, nous constatons que des données sensible concernant le server sont visible.

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Nous allons donc masquer les informations.
    nano /etc/apache2/conf-enabled/security.conf

    • Désactiver : « #ServerTokens OS »
    • Ajouter : « ServerTokens Prod »
    • Activer : « ServerSignature Off »
    • Désactiver : « #ServerSignature On »
    • Activer : « TraceEnable Off »
    • Déactiver : « #TraceEnable On »
  • Sur un navigateur Web :
    http://192.168.64.133/t

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Configurer le SSL avec Apache 2

  • Le protocole TLS permet à deux machines de communiquer de manière sécurisée. Les informations échangées entre les deux machines sont de ce fait pratiquement inviolables. Il doit assurer l'authentification du serveur grâce à un certificat. La confidentialité des données grâce au chiffrement et l’intégrité des données.

  • Un certificat permet de fournir diverses informations concernant l'identité de son détenteur (la personne qui publie les données). Ce certificat s'accompagne d'une clé publique qui est indispensable pour que la communication entre les machines soit chiffrée.

  • Activer le module SSL d’Apache, Activer le site «default-ssl» d’Apache

    ​​​​a2enmod ssl
    ​​​​a2ensite default-ssl
    ​​​​service apache2 reload
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Installez le paquet OpenSSL

    ​​​​apt-get update
    ​​​​apt-get install openssl
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Générer le certificat

    ​​​​    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -sha256 - out /etc/apache2/server.crt -keyout /etc/apache2/server.key
    
  • Modifier les permissions sur la clé
    chmod 440 /etc/apache2/server.crt

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Configuration d’Apache
    /etc/apache2/sites-available/default-ssl

  • Modifiez ces deux options si nécessaire afin d’indiquer le chemin vers les fichiers, Suite aux dernières vulnérabilités découvertes au sein du protocole SSL en 2014, il est recommandé également d'effectuer la configuration suivante dans Apache pour plus de sécurité :

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • activez le module SSL

    ​​​​a2enmod ssl
    ​​​​a2ensite default-ssl
    ​​​​service apache2 reload
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Désactiver le site HTTP
    a2dissite default

  • Rediriger le HTTP vers HTTPS automatiquement, modifier le fichier suivant
    nano /etc/apache2/sites-available/port_vhosts.conf

  • Dans le virtualhost, ajoutez la ligne suivante :
    Redirect permanent / https://192.168.64.133/site1

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Redémarrer le service Apache2
    service apache2 reload

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Vous pouvez remarquer que lorsque vous allez naviguer sur http://192.168.64.133, le site vous redirigera vers https://192.168.64.133/site1

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Cliquer sur accéder à la page web

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Protéger une page web avec .htaccess

  • Créer un fichier .htaccess dans le répertoire /var/www/vhosts/site1/
    nano /var/www/vhosts/site1/.htaccess

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Créer le fichier ".htpasswd"
    htpasswd -c /var/www/site/.htpasswd nom_utilisateur

  • Consulter le fichier .htpasswd
    cat /var/www/vhosts/site1/.htpasswd

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Modifier la configuration d'Apache
    nano /etc/apache2/apache2.conf

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Rendre publique une sous-partie du site protégé
    mkdir /var/www/vhosts/site1/open nano /var/www/vhosts/site1/open/.htaccess

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Lors de connexion sur https://192.168.64.133/site1

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Lors de connexion sur https://192.168.64.133/site1/open

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Aperçu de la CGI avec Python

  • Modifier le fichier de configuration Apache
    nano /etc/apache2/apache2.conf

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Activer le module CGI

    ​​​​a2enmod cgi
    ​​​​systemctl restart apache2
    ​​​​systemctl status apache2
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Nous allons maintenant créer une page index.py sur le site 2
    nano /var/www/vhosts/site2/index.py

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Résultats :
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Nous allons maintenant créer un formulaire
    nano /var/www/vhosts/site2/formulaire.py

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Résultats :
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Créer un fichier livredor.py
    nano /var/www/vhosts/site2/livredor.py

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Résultats :

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Créer un fichier compteur.py
    nano /var/www/vhosts/site2/compteur.py

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Résultats :

    • 1ère visite

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • 50ème visite

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →