Protéger l'accès à vos dossier .svn

TechCrunch à récemment repris un article Russe indiquant que de nombreux sites (plus de 3300) laissaient en libre accès leur dossier .svn.

L'article a ensuite été repris par Chris Sullo via le HP Security Laboratory Blog, afin de vous proposer quelques techniques afin de vérifier que votre site affiche ou pas le contenu de ces fameux dossiers et comment faire dans ce cas.

Il est important de savoir que l'accès aux dossier .svn est un risque non négligeable puisque ces dossiers peuvent contenir le code source de vos fichier, et donc parfois les mots de passes d'accès aux divers services (base de donnée, réseaux sociaux via API, etc).

Vous qui utilisez Subversion, êtes-vous sûr que vos .svn ne sont pas accessibles ? vraiment ?

Afin de sécuriser votre serveur, voici deux petits morceaux d'Apache évitant vos visiteurs d'accéder à ces dossiers.

Le premier se place dans le fichier de configuration de votre VirtualHost, et interdit simplement l'accès aux répertoires .svn :

<Directory ~ “^\.svn”>
     Deny from all
</Directory>

(facile ;))

Le second viens se mettre dans un fichier .htaccess, dans le cas ou vous n'auriez pas accès à la configuration d'Apache (cas typique d'un hébergement standard) :

RewriteEngine on
# Hide .SVN Folders
RewriteRule ^(.*/)*\.svn/ / [F,L]

Merci @Fettes Programming Solutions pour les bouts de code très utiles ;)

Une autre alternative, si vous n'utilisez plus SVN en production, et que vous n'avez fait que copier les fichier, c'est de supprimer récursivement les dossiers .svn de votre site/application. Pour ce faire, nous avions listé les commandes (linux) possible lors d'un précédent article.

Filed under  //  General   Security   access   folder   htaccess   protect   svn  
Posted by Cyril Nicodème 

Script Shell de backup de dossier via FTP

J'ai optimisé le précédent script de backup de dossier en y ajoutant une sauvegarde via FTP.

J'ai mis du temps à mettre en place cette technique car cela ne me convenait pas pour la simple et bonne raison que le transfert de l'archive générée peut-être longue (et lourde), surtout si on sauvegarde une quantité conséquente de fichiers !

Mais après en avoir parlé à Pierre, il s'est avéré que le temps de transfert, même pour de gros fichier, se faisait de manière très rapide, surtout entre serveurs.

Donc à défaut d'avoir les moyens de se payer deux serveurs et de les mettres en mirroring (ca ne saurait tarder) (les deux serveurs .... pas l'argent ...), je suis bien obligé d'utiliser une sauvegarde via FTP.

Voici donc le fameux script que j'utilise :

#!/bin/bash
#
# Shell script (BASH) used in cron to made a backup of the indicated directory

# In order to run this script, you must have following tools installed:
# - gpg
# - ncftp

# And we will use theses :
# - tar
# - rm
# - chown
# - chmod
#
#
# Installation
# Customize the script according to your need. You need to setup :
# - A list of folders, separated by space, and finished with the last /
# - A GPG passphrase
# - The file where to log the messages                         (default /var/log/backup.log)
# - The number of days the backups are stored         (default 5)
# - The FTP credentials
# - The Folders in that FTP
# - The Email to send reports when error occurs
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 3.0 or above
# Copyright (C) 2005 ReFlectiv project.
# Feedback/comment/suggestions : http://www.reflectiv.net/
# -------------------------------------------------------------------------

# List of folders to save
LIST_F0LDERS="/etc/ /home/ /var/lib/mysql/";

# The password for the archive
GPG_PASS="my_passwd";

# File name where to log the messages
LOG_FILE="/var/log/backup.log";

# Number of days the archives are keeped
KEEPING_DAYS=5;

# FTP Credentials
FTP_SERVER="ftp.server.tld"
FTP_USER="user"
FTP_PASS="password"

# Backup directory on the FTP :
FTP_DIR="/backups/files"

# Email to send when error occurs
USER_EMAIL="email"

# Only change if your UNIX stores bin in diffrent location
GPG="/usr/bin/gpg";
NCFTP="/usr/bin/ncftp"

#######################################################################
# Do not change anything below
#######################################################################

# We mades some vars
UNIX_DATE=`date +"%Y_%m_%d"`;
CURRENT_DATE=`date +"%d/%m/%Y - %H:%M:%S"`;
TBZ_FILE="/tmp/"$UNIX_DATE".tbz";
GPG_FILE=$TBZ_FILE".gpg";

# Adding new clear entry to the log file
echo "" >> $LOG_FILE."tmp";
echo "----------------------------------------------------------------------------------------------------" >> $LOG_FILE."tmp";
echo "" >> $LOG_FILE."tmp";

# Function called to log info into the file
function log {
    echo "["$CURRENT_DATE"] - "$1 >> $LOG_FILE".tmp";

    if [ ! $2 == "" ]; then
                mail -s "[$(hostname)] - Remote backup error" $USER_EMAIL > $LOG_FILE;
                rm $LOG_FILE".tmp";
        exit 1;
    fi
}

log "Starting remote backup";

# Preliminary tests
[ -x $GPG ] || log "gpg not found" 1;
[ -x $NCFTP ] || log "ncftp not found" 1;

for FOLDER in $LIST_FOLDERS
do
        # We test if the original folder exists
    [ -d $FOLDER ] || log "The folder "$FOLDER" does not exists !" 1;
done

# We make the archive
tar -cjf $TBZ_FILE $LIST_F0LDERS
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command tar" 1;

# We encrypt the archive
$GPG --yes -c --passphrase $GPG_PASS --s2k-cipher-algo RIJNDAEL256 $TBZ_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command gpg" 1;

# We delete the archive
rm $TBZ_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command rm" 1;

# We modify the rights for the file
chmod 400 $GPG_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command chmod" 1;

# We modify the owner and group of the file
chown $UID $GPG_FILE;
# If an error occured ...
[ $? -eq 0 ] || log "An error occured with the command chown" 1;


# We build the name of the archive from $KEEPING_DAYS days ago
OLD_ARCHIVE=`date --date $KEEPING_DAYS' days ago' "+%Y_%m_%d"`".tbz.gpg";

log "Starting update to FTP server "$FTP_SERVER;

$NCFTP -u$FTP_USER -p$FTP_PASS $FTP_SERVER > $LOG_FILE;
rm $LOG_FILE".tmp";

exit 0;

Le script effectue une sauvegarde des répertoires dans une archive tar.bz. Il chiffre ensuite cette archive avec gpg en utilisant un algorithme synchrone, puis envoie l'archive au serveur distant. Il prends aussi soin de bien supprimer les éléments qui ne sont plus utilisés afin d'éviter toute tentative de lecture (droits et accès aux fichier tbz et gpg puis suppression de tous les éléments).

Beaucoup de paramètres sont modifiable, comme vous pouvez le voir, et ce afin que vous puissiez l'adapter au mieux.

Le seul problème restant est que je n'ai pas réussi à faire taire ncftp ! Si quelqu'un connait une solution (l'argument -v quiet semble fonctionner pour les anciennes versions et n'y est vraisemblablement plus présent :s), je suis preneur !

Bon backup à vous :)

Filed under  //  Development   Scripts   Unix/Linux   backup   bash   dossier   fichier   file   folder   ftp   script   shell  
Posted by Cyril Nicodème 

Trouver une chaine dans des fichiers

Voici quelques commandes bash qui permettre de trouver une chaine contenue dans les fichiers d'un répertoire (utile par exemple pour trouver quels fichiers ont été infectés par le virus Gumblar :D)

La solution la plus simple est rgrep, mais qui n'est pas installé par défaut.

rgrep 'chaine' /arborescence

Sinon, vous avez la solution avec grep.
Cette méthode n'affiche que le fichier concerné

grep -lir "chaine" /arborescence

Cette méthode affiche le fichier concerné, la ligne ou la chaine à été trouvée, et la chaine trouvée :

grep -oHnr "chaine" /arborescence

Sinon, la méthode indiquée par Arkezis, toujours avec grep :

grep "chaine" **/*

Ou la solution avec find :

find /arborescence -name "*extension_du_fichier" -exec grep -l "chaine" {} \;

Enfin, limiter la recherche à certaines extensions :

for i in `find /arborescence -type d`; do grep "chaine" $i/*.{txt,log}; done 2>/dev/null

Parfois très utile ! :)

Filed under  //  Development   Scripts   Unix/Linux   bash   chaine   fichier   find   folder   recursive   repertoire   rgrep   récursif   shell   string   trouver  
Posted by Cyril Nicodème 

Script Shell de backup de dossier

Voici un script shell qui permet de sauvegarder un certain nombre de répertoires dans un autre répertoire, en archivé, chiffré et daté.

Vous n'avez qu'à paramétrer :

  • Une liste de répertoire séparés par des espaces
  • Le dossier de destination
  • La passphrase pour GPG
  • Le fichier ou logguer les actions (default: /var/log/backup.log)
  • Le nombre de jours où l'on garde les archives (defaut: 5)
  • L'UID de l'utilisateur responsable des archives (défaut: root)
  • Le GID de l'utilisateur responsable des archives (défaut: root)
  • Le chmod pour l'accès au répertoire (défaut: 400)

Voici le code :

#!/bin/bash
#
# Shell script (BASH) used in cron to made a backup of the given directory in $1
# to the directory in $2

# In order to run this script, you must have following tools installed:
# - tar
# - gpg
# - rm
# - chown
# - chmod
#
#
# Installation
# Customize the script according to your need. You need to setup :
# - A list of folders, separated by space, and finished with the last /
# - The folder where the backup goes
# - A GPG passphrase
# - The file where to log the messages                         (default /var/log/backup.log)
# - The number of days the backups are stored         (default 5)
# - The owner uid                                                 (default root)
# - The owner gid                                                 (default root)
# - The owner rights                                                 (default 400)
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 ReFlectiv project.
# Feedback/comment/suggestions : http://www.reflectiv.net/
# -------------------------------------------------------------------------

# List of folders to save
LIST_F0LDERS=`/var/www/site1/ /var/www/site2/ /var/www/site3/`;

# Folder where the backups goes
BACKUP_FOLDER="/root/snapshoots/";

# The password for the archive
GPG_PASS="passphrase";

# File name where to log the messages
LOG_FILE="/var/log/backup.log";

# Number of days the archives are keeped
KEEPING_DAYS=5;

# UID & GID of the archived file
BU_UID="root";
BU_GID="root";

# Access rights to the archived file
BU_CHMOD=400;

# Only change if your UNIX stores bin in diffrent location
TAR="/bin/tar";
GPG="/usr/bin/gpg";
RM="/bin/rm";
CHOWN="/bin/chown";
CHMOD="/bin/chmod";
MKDIR="/bin/mkdir";

#######################################################################
# Do not change anything below
#######################################################################

# We mades the vars
UNIX_DATE=`date +"%Y_%m_%d"`;
CURRENT_DATE=`date +"%d/%m/%Y - %H:%M:%S"`;
TGZ_FILE=$UNIX_DATE".tgz";
GPG_FILE=$TGZ_FILE".gpg";

# Function called to log info into the file
function log {
        echo "["$CURRENT_DATE"] - "$1 >> $LOG_FILE;

        if [ ! $2 == "" ]; then
                exit 1;
        fi
}

log "Starting backup";

# Preliminary tests
[ -x $TAR ] || log "tar not find" 1;
[ -x $GPG ] || log "gpg not find" 1;
[ -x $RM ] || log "rm not find" 1;
[ -x $CHOWN ] || log "chown not find" 1;
[ -x $CHMOD ] || log "chmod not find" 1;
[ -x $MKDIR ] || log "mkdir not find" 1;

# We test if the file is correctly called
#[ $# -eq 2 ] || log "Invalid number of parameters" 1;

# We test if the destination folder exists
[ -d $BACKUP_FOLDER ] ||
log "The backup destination folder "$BACKUP_FOLDER" seems to not exists." 1;

for FOLDER in $LIST_F0LDERS
do
        # We test if the original folder exists
        [ -d $FOLDER ] || log "The folder "$FOLDER" does not exists !" 1;

        # We mades sample vars
        BACKUP_SUBFOLDER=`basename $FOLDER`;

        if [ ! -d $BACKUP_FOLDER$BACKUP_SUBFOLDER"/" ]; then
                # We create the folder
                $MKDIR $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";

                # We set the same rights and acces as the archived/encrypted file
                $CHOWN $BU_UID:$BU_GID $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";
                $CHMOD $BU_CHMOD $BACKUP_FOLDER$BACKUP_SUBFOLDER"/";
        fi

        # We goes to the backup folder
        cd $FOLDER;

        # We make the archive
        $TAR -czf $BACKUP_FOLDER$BACKUP_SUBFOLDER"/"$TGZ_FILE .
        # If an error occured ...
        [ $? -eq 0 ] || log "An error occured with the command tar" 1;

        # We goes to the backup directory
        cd $BACKUP_FOLDER$BACKUP_SUBFOLDER;

        # We encrypt the archive
        $GPG --yes -c --passphrase $GPG_PASS --s2k-cipher-algo RIJNDAEL256 $TGZ_FILE;
        # If an error occured ...
        [ $? -eq 0 ] || log "An error occured with the command gpg" 1;

        # We delete the archive
        $RM $TGZ_FILE;
        # If an error occured ...
        [ $? -eq 0 ] || log "An error occured with the command rm" 1;

        # We modify the user/groups for the file
        $CHOWN $BU_UID:$BU_GID $GPG_FILE;
        # If an error occured ...
        [ $? -eq 0 ] || log "An error occured with the command rm" 1;

        # We modify the chmod for the file
        $CHMOD $BU_CHMOD $GPG_FILE;
        # If an error occured ...
        [ $? -eq 0 ] || log "An error occured with the command rm" 1;

        OLD_ARCHIVE=`date --date $KEEPING_DAYS' days ago' "+%Y_%m_%d"`".tgz.gpg";
        if [ -f $OLD_ARCHIVE ]; then
                log "Deleting old archive "$OLD_ARCHIVE;
                rm $OLD_ARCHIVE;
        fi

        log $FOLDER" folder saved";
done

log "Backup finished successfully";
exit 0;

Filed under  //  Development   Scripts   Unix/Linux   backup   bash   dossier   folder   shell  
Posted by Cyril Nicodème