Vsftpd ssl chroot mysql

De Le Wiki du Forum-Debian.fr
Révision datée du 12 septembre 2010 à 05:55 par Lol (discussion | contributions)
Aller à la navigation Aller à la recherche
Cet article est une ébauche concernant Vsftpd. N'hésitez pas à contribuer ou à en discuter.

Installation d'un serveur FTP avec utilisateurs Virtuels sur une Squeeze

Pourquoi une squeeze ?

  • Squeeze est en passe dans les semaines/mois de devenir la future stable
  • Vsftpd V 2.3.0-1 0 devrait donc bien finir pas passer en stable
  • Inutile de faire un tuto Lenny (il en existe beaucoup sur le net)

Malgré de nombreuses critiques concernant la sécurité, le protocole FTP (File transfert protocol) est toujours très utilisé. J'ai choisi VSFTPD, disponible dans les dépôts, mais il en existe beaucoup d'autres (pure-ftp, proftp, ftpd, wu-ftp, wzdftpd, rssh, etc.)ici :

Concernant rssh, un excellent tuto existe ici : Serveur sftp + rssh + chroot - Discussion ici

Je n'aborderais ici que la mise en place d'un serveur vsftpd sur un réseau local, sans traiter des question de sécurité relatives à Internet.

Nous sécuriserons les connexion avec SSL et grâce à un chroot (qui permet de "bloquer" les utilisateurs dans un répertoire) et mettrons en place une base de données dans mysql pour gérer nos utilisateurs virtuels. Ces utilisateurs virtuels seront tous "mappés" sur un utilisateur système dédié à cela : ftp

Nous mettrons en places des dossiers personnels et un dossier partagé commun à tous les utilisateurs.

Installation des programmes

# apt-get install vsftpd mysql-server-5.1 libpam-mysql

Préparation

base mysql

Créons la base de données; Celle-ci contiendra deux tables :

  • users : table des utilisateurs virtuels
  • log : évènements de connexion

Vous pouvez vous passer de la table log si vous le souhaitez

# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 475 Server version: 5.1.49-1 (Debian)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE vsftpd; Query OK, 1 row affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON vsftpd.* TO 'vsftpd'@'localhost' IDENTIFIED BY 'vsftpd'; Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)

mysql> USE vsftpd; Database changed mysql> CREATE TABLE `vsftpd`.`users` (`id_user` int(11) NOT NULL auto_increment,`login` varchar(50) NOT NULL,`password` varchar(100) NOT NULL,`active` int(11) NOT NULL,PRIMARY KEY (`id_user`)) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE `vsftpd`.`log` (`id_log` int(11) NOT NULL auto_increment,`login` varchar(50) NOT NULL,`message` varchar(200) NOT NULL,`pid` varchar(10) NOT NULL,`host` varchar(30) NOT NULL,`time` datetime default NULL,PRIMARY KEY (`id_log`)) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

Query OK, 0 rows affected (0.02 sec)

mysql> quit; Bye

Création des utilisateurs :
$ mysql -u <votre_user_mysql> --password=<votre_password_mysql> -e "INSERT INTO vsftpd.users (login,password,active) VALUES('utilisateur',md5('motdepasse'),1)"
$ mysql -u vsftpd --password=vsftpd -e "INSERT INTO vsftpd.users (login,password,active) VALUES('essai1',md5('essai1'),1)"
$ mysql -u vsftpd --password=vsftpd -e "INSERT INTO vsftpd.users (login,password,active) VALUES('essai2',md5('essai2'),1)"


Voici le scripts SQL à exécuter pour créer ces deux tables si vous préférez :

CREATE TABLE `vsftpd`.`users` ( `id_user` int(11) NOT NULL auto_increment, `login` varchar(50) NOT NULL, `password` varchar(100) NOT NULL, `active` int(11) NOT NULL, PRIMARY KEY (`id_user`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

CREATE TABLE `vsftpd`.`log` ( `id_log` int(11) NOT NULL auto_increment, `login` varchar(50) NOT NULL, `message` varchar(200) NOT NULL, `pid` varchar(10) NOT NULL, `host` varchar(30) NOT NULL, `time` datetime default NULL, PRIMARY KEY (`id_log`) ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

La table des utilisateurs contient un champ 'active' qui aura une valeur de 1 ou 0 (actif/inactif) La table de log, nous stockera le login, le nom de l'évènement, le pid, le host, la date et l'heure de l'évènement.

Création d'un certificat

# cd /etc/ssl/private
# openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout vsftpd.pem -out vsftpd.pem
Generating a 1024 bit RSA private key
..............++++++
................++++++
writing new private key to 'vsftpd.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:FR
State or Province Name (full name) [Some-State]:Quelquepart
Locality Name (eg, city) []:Ville
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:votre.serveur.ltd
Email Address []:proftpd@votre.serveur.ltd
# head -15 vsftpd.pem > vsftpd.key
# chmod 600 *

Réglage communication vsftpd/mysql

L'authentification des utilisateurs virtuels passe par pam.d, il faut donc régler ce fichier afin que Vsftpd puisse vérifier l'existence et les mots de passe des utilisateurs virtuels dans la base Mysql.

# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.orig
# nano /etc/pam.d/vsftpd
#%PAM-1.0
auth sufficient pam_unix.so
account sufficient pam_unix.so
auth required /lib/security/pam_mysql.so verbose=0 user=<votre_user_mysql> passwd=<votre_pass_mysql> host=127.0.0.1 db=vsftpd table=users usercolumn=login passwdcolumn=password crypt=3 where=users.active=1 sqllog=yes logtable=log logmsgcolumn=message logusercolumn=login logpidcolumn=pid loghostcolumn=host logtimecolumn=time
account required /lib/security/pam_mysql.so verbose=0 user=<votre_user_mysql> passwd=<votre_pass_mysql> host=127.0.0.1 db=vsftpd table=users usercolumn=login passwdcolumn=password crypt=3 where=users.active=1 sqllog=yes logtable=log logmsgcolumn=message logusercolumn=login logpidcolumn=pid loghostcolumn=host logtimecolumn=time

Préparation des répertoires personnels

# mkdir /home/vsftpd
# mkdir /home/vsftpd/essai1
# mkdir /home/vsftpd/essai2

Préparation de dossiers partagés

Explication : Nous allons créer un dossier qui sera accessible par deux utilisateurs de serveur ftp en lecture/écriture, situé en dehors du chroot. Les dossiers à l'intérieur du chroot sont liés au dossier se trouvant à l'extérieur.

Création des repertoires de destination des montages

# mkdir /home/vsftpd/essai1/dossier
# mkdir /home/vsftpd/essai2/dossier
# chmod 777 /home/vsftpd/essai2/dossier
# chown -R ftp:ftp /home/vsftpd
# chmod 777 /mnt/dossier
# nano /etc/fstab
/mnt/dossier /home/vsftpd/essai1/dossier none bind,defaults,auto 0 0
/mnt/dossier /home/vsftpd/essai2/dossier none bind,defaults,auto 0 0
# mount -a

Préparation des fichiers personnels

Les directives écrites dans les fichiers personnels sont "supérieures" à celles de /etc/vsftpd.conf. vous avez donc une grande marge de manœuvre, et vous pouvez mettre ici toutes les directives qui ne s'appliquerons qu'a un utilisateur.

# mkdir /etc/vsftpd
# mkdir /etc/vsftpd/vsftpd_user_conf
# echo "local_root=titi" > /etc/vsftpd/vsftpd_user_conf/essai1
# echo "write_enable=YES" >> /etc/vsftpd/vsftpd_user_conf/essai1
# echo "anon_upload_enable=YES" >> /etc/vsftpd/vsftpd_user_conf/essai1
# echo "anon_mkdir_write_enable=YES" >> /etc/vsftpd/vsftpd_user_conf/essai1
# echo "anon_other_write_enable=YES" >> /etc/vsftpd/vsftpd_user_conf/essai1

Recommencez pour tous les utilisateurs

Configuration de vsftpd

$ su -
# cd /etc
# mv vsftpd.conf vsftpd.conf.default

# Example config file /etc/vsftpd.conf

  1. The default compiled in settings are fairly paranoid. This sample file
  2. loosens things up a bit, to make the ftp daemon more usable.
  3. Please see vsftpd.conf.5 for all compiled in defaults.
  4. READ THIS: This example file is NOT an exhaustive list of vsftpd options.
  5. Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
  6. capabilities.
  7. Run standalone? vsftpd can run either from an inetd or as a standalone
  8. daemon started from an initscript.

listen=YES

  1. Run standalone with IPv6?
  2. Like the listen parameter, except vsftpd will listen on an IPv6 socket
  3. instead of an IPv4 one. This parameter and the listen parameter are mutually
  4. exclusive.
  5. listen_ipv6=YES
  6. Allow anonymous FTP? (Beware - allowed by default if you comment this out).

anonymous_enable=NO

  1. Uncomment this to allow local users to log in.

local_enable=YES

  1. Uncomment this to enable any form of FTP write command.

write_enable=YES

  1. Default umask for local users is 077. You may wish to change this to 022,
  2. if your users expect that (022 is used by most other ftpd's)
  3. local_umask=022
  4. Uncomment this to allow the anonymous FTP user to upload files. This only
  5. has an effect if the above global write enable is activated. Also, you will
  6. obviously need to create a directory writable by the FTP user.
  7. anon_upload_enable=YES
  8. Uncomment this if you want the anonymous FTP user to be able to create
  9. new directories.
  10. anon_mkdir_write_enable=YES
  11. Activate directory messages - messages given to remote users when they
  12. go into a certain directory.

dirmessage_enable=YES

  1. If enabled, vsftpd will display directory listings with the time
  2. in your local time zone. The default is to display GMT. The
  3. times returned by the MDTM FTP command are also affected by this
  4. option.

use_localtime=YES

  1. Activate logging of uploads/downloads.

xferlog_enable=YES

  1. Make sure PORT transfer connections originate from port 20 (ftp-data).

connect_from_port_20=YES

  1. If you want, you can arrange for uploaded anonymous files to be owned by
  2. a different user. Note! Using "root" for uploaded files is not
  3. recommended!
  4. chown_uploads=YES
  5. chown_username=whoever
  6. You may override where the log file goes if you like. The default is shown
  7. below.

xferlog_file=/var/log/vsftpd.log

  1. If you want, you can have your log file in standard ftpd xferlog format.
  2. Note that the default log file location is /var/log/xferlog in this case.

xferlog_std_format=YES

  1. You may change the default value for timing out an idle session.

idle_session_timeout=600

  1. You may change the default value for timing out a data connection.

data_connection_timeout=120

  1. It is recommended that you define on your system a unique user which the
  2. ftp server can use as a totally isolated and unprivileged user.
  3. nopriv_user=ftpsecure
  4. Enable this and the server will recognise asynchronous ABOR requests. Not
  5. recommended for security (the code is non-trivial). Not enabling it,
  6. however, may confuse older FTP clients.
  7. async_abor_enable=YES
  8. By default the server will pretend to allow ASCII mode but in fact ignore
  9. the request. Turn on the below options to have the server actually do ASCII
  10. mangling on files when in ASCII mode.
  11. Beware that on some FTP servers, ASCII support allows a denial of service
  12. attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
  13. predicted this attack and has always been safe, reporting the size of the
  14. raw file.
  15. ASCII mangling is a horrible feature of the protocol.
  16. ascii_upload_enable=YES
  17. ascii_download_enable=YES
  18. You may fully customise the login banner string:

ftpd_banner=Welcome to blah FTP service.

  1. You may specify a file of disallowed anonymous e-mail addresses. Apparently
  2. useful for combatting certain DoS attacks.
  3. deny_email_enable=YES
  4. (default follows)
  5. banned_email_file=/etc/vsftpd.banned_emails
  6. You may restrict local users to their home directories. See the FAQ for
  7. the possible risks in this before using chroot_local_user or
  8. chroot_list_enable below.
  9. chroot_local_user=YES
  10. You may specify an explicit list of local users to chroot() to their home
  11. directory. If chroot_local_user is YES, then this list becomes a list of
  12. users to NOT chroot().

chroot_local_user=YES chroot_list_enable=YES

  1. (default follows)

chroot_list_file=/etc/vsftpd.chroot_list

  1. You may activate the "-R" option to the builtin ls. This is disabled by
  2. default to avoid remote users being able to cause excessive I/O on large
  3. sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
  4. the presence of the "-R" option, so there is a strong case for enabling it.
  5. ls_recurse_enable=YES
  6. Debian customization
  7. Some of vsftpd's settings don't fit the Debian filesystem layout by
  8. default. These settings are more Debian-friendly.
  9. This option should be the name of a directory which is empty. Also, the
  10. directory should not be writable by the ftp user. This directory is used
  11. as a secure chroot() jail at times vsftpd does not require filesystem
  12. access.

secure_chroot_dir=/var/run/vsftpd/empty

  1. This string is the name of the PAM service vsftpd will use.

pam_service_name=vsftpd

  1. This option specifies the location of the RSA certificate to use for SSL
  2. encrypted connections.

rsa_cert_file=/etc/ssl/private/vsftpd.pem

guest_enable=YES user_config_dir=/etc/vsftpd/vsftpd_user_conf local_root=/home/vsftpd virtual_use_local_privs=YES

ssl_enable=YES require_ssl_reuse=NO

  1. force_local_data_ssl=NO

force_local_logins_ssl=YES ssl_sslv2=YES ssl_sslv3=YES ssl_tlsv1=YES

Voici les différences entre mon fichier et le fichier original :

# diff /etc/vsftpd.conf /etc/vsftpd.conf.orig

23c23 < anonymous_enable=NO --- > anonymous_enable=YES 26c26 < local_enable=YES --- > #local_enable=YES 29c29 < write_enable=YES --- > #write_enable=YES 68c68 < xferlog_file=/var/log/vsftpd.log --- > #xferlog_file=/var/log/vsftpd.log 72c72 < xferlog_std_format=YES --- > #xferlog_std_format=YES 75c75 < idle_session_timeout=600 --- > #idle_session_timeout=600 78c78 < data_connection_timeout=120 --- > #data_connection_timeout=120 101c101 < ftpd_banner=Welcome to blah FTP service. --- > #ftpd_banner=Welcome to blah FTP service. 117,118c117,118 < chroot_local_user=YES < chroot_list_enable=YES --- > #chroot_local_user=YES > #chroot_list_enable=YES 120c120 < chroot_list_file=/etc/vsftpd.chroot_list --- > #chroot_list_file=/etc/vsftpd.chroot_list 145,157d144 < < guest_enable=YES < user_config_dir=/etc/vsftpd/vsftpd_user_conf < local_root=/home/vsftpd < virtual_use_local_privs=YES < < ssl_enable=YES < require_ssl_reuse=NO < #force_local_data_ssl=NO < force_local_logins_ssl=YES < ssl_sslv2=YES < ssl_sslv3=YES < ssl_tlsv1=YES

Premiers tests

Nous avons le choix dans les clients ftp, mais tous ne supportent pas SSL...

Vous pouvez faire rapidement des tests avec les deux clients légers, mais néanmoins puissants, suivants :

  • ftp-ssl
  • lftp

lftp -u essai1,essai1 nas.isalo.org lftp essai1@nas.isalo.org:~> ls drwxrwxrwx 2 0 0 4096 Sep 12 08:22 dossier lftp essai1@nas.isalo.org:/> cd dossier lftp essai1@nas.isalo.org:/dossier> put /home/laurent/test/sgfxi 227286 octets transférés lftp essai1@nas.isalo.org:/dossier> ls -rw------- 1 111 116 227286 Sep 12 08:23 sgfxi lftp essai1@nas.isalo.org:/dossier> cd .. lftp essai1@nas.isalo.org:/> ls drwxrwxrwx 2 0 0 4096 Sep 12 08:22 dossier lftp essai1@nas.isalo.org:/> mkdir dossier1 mkdir ok, « dossier1 » créé lftp essai1@nas.isalo.org:/> cd dossier1 cd ok, cwd=/dossier1 lftp essai1@nas.isalo.org:/dossier1> put yaptitude 3039 octets transférés lftp essai1@nas.isalo.org:/dossier1> ls -rw------- 1 111 116 3039 Sep 12 08:24 yaptitude lftp essai1@nas.isalo.org:/dossier1> quit

ftp-ssl nas.isalo.org 21 Connected to nas.isalo.org. 220 Welcome to blah FTP service. Name (nas.isalo.org:laurent): essai2 234 Proceed with negotiation. [SSL Cipher DES-CBC3-SHA] 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. drwxrwxrwx 2 0 0 4096 Sep 12 08:23 dossier 226 Directory send OK. ftp> cd dossier 250 Directory successfully changed. ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. -rw------- 1 111 116 227286 Sep 12 08:23 sgfxi 226 Directory send OK. ftp> del sgfxi 250 Delete operation successful. ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. 226 Directory send OK. ftp> cd .. 250 Directory successfully changed. ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. drwxrwxrwx 2 0 0 4096 Sep 12 08:26 dossier 226 Directory send OK. ftp> quit 221 Goodbye.

Le man de vsftpd.conf

Lol 12 septembre 2010 à 08:34 (EAT)