labunix's blog

labunixのラボUnix

Wheezyのvsftpdへのログイン先をchrootディレクトリに変更する。

■Wheezyのvsftpdへのログイン先をchrootディレクトリに変更する。
 シェルが使えるユーザは、上位ディレクトリに移動できる。
 sudoグループの中の許可したユーザに限定しているので、シェルが使えないユーザなど居ない。
 ※rootユーザはsudoグループに所属し、ftpusersから削除したときだけログインできる。

 debian Wheezyにセキュアなvsftpdを導入する
 http://labunix.hateblo.jp/entry/20130609/1370707083

■「chroot_local_user=YES」だとすべてのローカルユーザがホームディレクトリにchrootされるが、
 「警告」の通り問題がある。
 よって、「chroot_list_enable」と「chroot_list_file」で指定することになる。
 「chroot_list_file」はデフォルトの「/etc/vsftpd.chroot_list」を使うことにすると、
 以下の設定だけでよいことになる。

$ echo -e "root\nlabunix" | sudo tee /etc/vsftpd.chroot_list
root
labunix

$ sudo sed -i s/"#\(chroot_list_enable=YES\)"/"\1"/ /etc/vsftpd.conf
$ grep chroot_list_enable=YES /etc/vsftpd.conf
chroot_list_enable=YES

$ sudo /etc/init.d/vsftpd restart
Stopping FTP server: vsftpd.
Starting FTP server: vsftpd.

■chrootを有効にしたことでログイン自体が失敗する。

$ ftp-ssl 192.168.45.11
Connected to 192.168.45.11.
220 (vsFTPd 2.3.5)
Name (192.168.45.11:labunix): labunix
234 Proceed with negotiation.
[SSL Cipher DES-CBC3-SHA]
331 Please specify the password.
Password:
ssl_getc: SSL_read failed -1 = 0
421 Service not available, remote server has closed connection
Login failed.
No control connection for command: Success
ftp> cd ..
Not connected.
ftp> quit

■マニュアルから、chrootの場所を指定する必要があることが分かる。

$ man vsftpd.conf 2>/dev/null | grep -A 5 "local_root\$"
       local_root
              ローカルログイン(すなわち、非 anonymous ログイン)の後に 変更する
              ディレクトリを指定する。  ディレクトリ変更の失敗は、静かに無視す
              る。

              デフォルト: (なし)

■chrootの場所を指定。

$ sudo mkdir -p /var/lib/chroot/vsftpd/home/labunix
$ sudo mkdir -p /var/lib/chroot/vsftpd/home/root

$ sudo chmod 700 /var/lib/chroot/vsftpd/home/*
$ sudo chown labunix:labunix /var/lib/chroot/vsftpd/home/labunix

$ sudo ls -ld /var/lib/chroot/vsftpd/home/* | awk '{print $1,$NF}'
drwx------. /var/lib/chroot/vsftpd/home/labunix
drwx------. /var/lib/chroot/vsftpd/home/root

$ echo 'local_root=/var/lib/chroot/vsftpd/home' | sudo tee -a /etc/vsftpd.conf
local_root=/var/lib/chroot/vsftpd/home

$ sudo /etc/init.d/vsftpd restart
Stopping FTP server: vsftpd.
Starting FTP server: vsftpd.

■ディレクトリ移動は「local_root」まで出来る。
 というより、直接「local_root」のトップディレクトリに移動してしまうので、
 権限の制限で別ユーザのディレクトリには移動も出来ないようにする。

$ ftp-ssl 192.168.45.11
Connected to 192.168.45.11.
220 (vsFTPd 2.3.5)
Name (192.168.45.11:labunix): labunix
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.
drwx------    2 1000     1000         4096 Jun 16 23:09 labunix
drwx------    2 0        0            4096 Jun 16 23:10 root
226 Directory send OK.
ftp> cd root
550 Failed to change directory.
ftp> cd labunix
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-r--r--    1 1000     1000            5 Jun 16 23:09 hello
226 Directory send OK.
ftp> get hello
local: hello remote: hello
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for hello (5 bytes).
226 Transfer complete.
5 bytes received in 0.05 secs (0.1 kB/s)
ftp> quit
221 Goodbye.

■シンボリックリンクを貼るとダウンロード専用のvsftpd-chroot環境と言えるところまで出来る。

$ ln -s /var/lib/chroot/vsftpd/home/labunix/ ftproot
$ ls -ld ftproot
lrwxrwxrwx. 1 labunix labunix 36  616 23:18 ftproot -> /var/lib/chroot/vsftpd/home/labunix/

■他のユーザは使えないプライベートなftprootになる。

$ sudo -u toor touch ftproot/test
touch: `ftproot/test' に touch できません: 許可がありません
$ sudo -u labunix touch ftproot/test
■sudoグループ共有ディレクトリを以下のように作成

$ sudo chown root:sudo /var/lib/chroot/vsftpd/home/sudo
$ sudo chmod 770 /var/lib/chroot/vsftpd/home/sudo

$ ln -s /var/lib/chroot/vsftpd/home/sudo ftpsudo
$ touch ftpsudo/test

■sudoグループ専用のダウンロードが出来る、共有ディレクトリ「ftpsudo」とする。

$ ftp-ssl 192.168.45.11
Connected to 192.168.45.11.
220 (vsFTPd 2.3.5)
Name (192.168.45.11:labunix): labunix
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.
drwx------    2 1000     1000         4096 Jun 16 23:19 labunix
drwx------    2 0        0            4096 Jun 16 23:10 root
drwxrwx---    2 0        27           4096 Jun 16 23:22 sudo
226 Directory send OK.
ftp> cd sudo
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-r--r--    1 1000     1000            0 Jun 16 23:22 test
226 Directory send OK.
ftp> get test
local: test remote: test
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for test (0 bytes).
226 Transfer complete.
ftp> put hello
local: hello remote: hello
200 PORT command successful. Consider using PASV.
550 Permission denied.
ftp> quit
221 Goodbye.

■書き込みも有効にする。

$ grep write_enable /etc/vsftpd.conf
#write_enable=YES
#anon_mkdir_write_enable=YES

$ sudo sed -i s/"#\(write_enable=YES\)"/"\1"/ /etc/vsftpd.conf

$ grep write_enable /etc/vsftpd.conf
write_enable=YES
#anon_mkdir_write_enable=YES

■ログは有効になっているので、そのまま再起動。

$ grep ^xferlog /etc/vsftpd.conf
xferlog_enable=YES

$ sudo /etc/init.d/vsftpd restart
Stopping FTP server: vsftpd.
Starting FTP server: vsftpd.

■確認。

$ ftp-ssl 192.168.45.11
Connected to 192.168.45.11.
220 (vsFTPd 2.3.5)
Name (192.168.45.11:labunix): labunix
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> cd sudo
250 Directory successfully changed.
ftp> put hello
local: hello remote: hello
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
5 bytes sent in 0.00 secs (33.9 kB/s)
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-r--r--    1 1000     1000            5 Jun 16 23:54 hello
-rw-r--r--    1 1000     1000            0 Jun 16 23:22 test
226 Directory send OK.
ftp> quit
221 Goodbye.

$ ls -ld /var/lib/chroot/vsftpd/home/sudo
drwxrwx---. 2 root sudo 4096  616 23:54 /var/lib/chroot/vsftpd/home/sudo

$ ls -l ftpsudo
lrwxrwxrwx. 1 labunix labunix 32  616 23:22 ftpsudo -> /var/lib/chroot/vsftpd/home/sudo

$ ls -l ftpsudo/
合計 4
-rw-r--r--. 1 labunix labunix 5  616 23:54 hello
-rw-r--r--. 1 labunix labunix 0  616 23:22 test

■アップロードのときだけ、クライアントが悪さしているように見える。

$ sudo tail -3 /var/log/vsftpd.log
Sun Jun 16 23:54:29 2013 [pid 1] [labunix] OK LOGIN: Client "192.168.45.11"
Sun Jun 16 23:54:35 2013 [pid 2] [labunix] DEBUG: Client "192.168.45.11", "Connection terminated without SSL shutdown - buggy client?"
Sun Jun 16 23:54:35 2013 [pid 3] [labunix] OK UPLOAD: Client "192.168.45.11", "/sudo/hello", 5 bytes, 0.07Kbyte/sec

■ASCIIモードを有効にしても変わらないので、放置とする。
 「Connection terminated without SSL shutdown - buggy client?」
 一応、変更方法を。

$ grep ascii /etc/vsftpd.conf
#ascii_upload_enable=YES
#ascii_download_enable=YES

$ sudo sed -i s/"#\(ascii_.*\)"/"\1"/ /etc/vsftpd.conf
$ grep ascii /etc/vsftpd.conf
ascii_upload_enable=YES
ascii_download_enable=YES

$ sudo /etc/init.d/vsftpd restart
Stopping FTP server: vsftpd.
Starting FTP server: vsftpd.