Very Secure FTP Daemon

提供: ArchWiki
ナビゲーションに移動 検索に移動

vsftpd (Very Secure FTP Daemon) は UNIX ライクなシステム用の、軽量で安定していてセキュアな FTP サーバーです。

インストール

vsftpd パッケージをインストールしてください。

インストールしたら vsftpd.service デーモンを起動・有効化します。

xinetd で vsftpd を使う方法は #xinetd を使う を見て下さい。

設定

vsftpd の設定は /etc/vsftpd.conf ファイルを編集することでほとんど行えます。ファイルの中に説明がちゃんと書かれているので、このセクションでは重要な変更点を取り上げるにとどめます。利用可能な全てのオプションとドキュメントについては、vsftpd.conf (5) の man ページを見るか オンラインで閲覧 してください。デフォルトのファイルは /srv/ftp にあります。

/etc/hosts.allow で接続を有効化します:

# Allow all connections
vsftpd: ALL
# IP adress range
vsftpd: 10.0.0.0/255.255.255.0

アップロードを有効にする

アップロードなどの、ファイルシステムへの変更を可能にするには、/etc/vsftpd.confWRITE_ENABLE フラグを YES に設定する必要があります:

write_enable=YES

ローカルユーザーログイン

/etc/passwd のユーザーがログインできるようにするには /etc/vsftpd.conf に以下の行を設定する必要があります:

local_enable=YES

匿名ログイン

以下の行で匿名ユーザーがログインできるか制御されています。デフォルトでは、/srv/ftp からのダウンロードのみ匿名ログインが許可されています:

/etc/vsftpd.conf
...
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
...
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
...

以下のオプションも追加することができます (詳しくは vsftpd.conf(5) を参照):

/etc/vsftpd.conf
# No password is required for an anonymous login          
no_anon_password=YES

# Maximum transfer rate for an anonymous client in Bytes/second          
anon_max_rate=30000

# Directory to be used for an anonymous login  
anon_root=/example/directory/

Chroot 監獄

chroot 環境をセットアップすることでユーザーからホームディレクトリを隔離することができます。以下の行を /etc/vsftpd.conf に追加してください:

chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

chroot_list_file 変数には監獄化するユーザーを記述したファイルを指定します。

以下の行を記述することで、環境をさらに制限することができます:

chroot_local_user=YES

ローカルユーザーがデフォルトで監獄化されます。この場合、chroot_list_file で指定されたファイルのユーザーだけが chroot 監獄に入らないことになります。

ユーザーログインの制限

ユーザーが FTP サーバーにログインできないようにするには /etc/vsftpd.conf に以下の2行を追加:

userlist_enable=YES
userlist_file=/etc/vsftpd.user_list

userlist_file にはログインを許可しないユーザーを記載したファイルを指定します。

特定のユーザーだけログインを許可したい場合、以下の行を追加:

userlist_deny=NO

userlist_file で指定されたファイルに記述されているユーザーはログインできるようになります。

接続の制限

データ転送レート (1つの IP から接続できるクライアント数や接続数) は /etc/vsftpd.conf に以下を記述することで制限できます:

local_max_rate=1000000 # Maximum data transfer rate in bytes per second
max_clients=50         # Maximum number of clients that may be connected
max_per_ip=2           # Maximum connections per IP

xinetd を使う

Xinetd は接続の監視と制御について強力な機能を持っています。ただし普通の vsftpd サーバーでは無用の代物です。

vsftpd をインストールすると必要なサービスファイル (/etc/xinetd.d/vsftpd) が追加されます。サービスはデフォルトでは無効化されています。ftp サービスを有効化:

service ftp
{
        socket_type             = stream
        wait                    = no
        user                    = root
        server                  = /usr/bin/vsftpd
        log_on_success  += HOST DURATION
        log_on_failure  += HOST
        disable                 = no
}

vsftpd デーモンをスタンドアロンモードで実行する場合、/etc/vsftpd.conf に以下の変更を加えてください:

listen=NO

上記の設定をしないと接続が出来ません:

500 OOPS: could not bind listening IPv4 socket

vsftpd デーモンを起動する代わりに xinetd.service を起動・有効化してください。

SSL/TLS を使って FTP をセキュア化

まず TLS を使うために X.509 SSL/TLS 証明書が必要です。証明書がない場合、以下のようにして自己署名証明書を簡単に生成できます:

# cd /etc/ssl/certs
# openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout vsftpd.pem -out vsftpd.pem
# chmod 600 vsftpd.pem

会社情報などが要求されます。証明書が信頼できる証明書でない場合、入力する情報はとくに意味はなく、ただ単に暗号化にのみ使われます。信頼できる証明書を使う場合、Let's Encrypt などの認証局から証明書を入手してください。

それから設定ファイルを編集:

/etc/vsftpd.conf
ssl_enable=YES

#choose what you like, if you accept anonymous connections, you may want to enable this
# allow_anon_ssl=NO

#by default all non anonymous logins and forced to use SSL to send and receive password and data, set to NO to allow non secure connections
force_local_logins_ssl=NO
force_local_data_ssl=NO

#you should at least enable TLS v1 if you enable SSL
ssl_tlsv1=YES
#these options will permit or prevent SSL v2 and v3 protocol connections. TLS v1 connections are preferred. 
ssl_sslv2=NO
ssl_sslv3=YES

#give the correct path to your .pem file
rsa_cert_file=/etc/ssl/certs/vsftpd.pem
#the .pem file also contains the private key
rsa_private_key_file=/etc/ssl/certs/vsftpd.pem

パッシブモードでホスト名を解決

vsftpd がパッシブモードで宣伝する IP アドレスを上書きしてサーバーのホスト名を起動時に DNS で解決するようにするには、/etc/vsftpd.conf に以下の行を追加してください:

pasv_addr_resolve=YES
pasv_address=yourdomain.org
ノート:
  • ダイナミック DNS の場合、pasv_address を定期的に更新したりサーバーを再起動する必要はありません。
  • パッシブモードを使っているときに LAN 経由で接続できないときは、LAN クライアントからではなくアクティブモードを使ってみてください。

ポートの設定

デフォルトの FTP 受信ポートやパッシブモードのデータポートを調整する必要があります:

  • FTP サーバーをウェブ上に公開する場合、サーバーが攻撃される危険性を少なくするため、受信ポートを標準ポートの 21 以外に変更すると良いでしょう。
  • パッシブモードで開くポートを制限したい場合、範囲を指定することができます。

ポートは設定ファイルで以下のように定義できます:

/etc/vsftpd.conf
listen_port=2211

pasv_min_port=5000
pasv_max_port=5003

iptables の設定

FTP デーモンを実行するサーバーが iptables ファイアウォールで保護されている可能性があります。FTP サーバーにアクセスできるように、以下のようにして適切なポートを開く必要があります:

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

この記事では iptables を設定する方法に関する説明はしません。次のページを見て下さい: シンプルなステートフルファイアウォール

iptables で適切な FTP 接続を処理するにはカーネルモジュールが必要になります。特に nf_conntrack_ftp は FTP が指定された listen_port (デフォルトでは 21) をコマンドでのみ使うのに必要です。データ転送は全て別のポートで行われます。FTP デーモンが選択するポートはランダムで、セッションごとに変わります (アクティブモードとパッシブモードのどちらを使っているかにもよります)。iptables でポートのパケットを許可するには、nf_conntrack_ftp が必要です。起動時に自動的にロードするには /etc/modules-load.d に以下のように新しいファイルを作成:

# echo nf_conntrack_ftp > /etc/modules-load.d/nf_conntrack_ftp.conf

カーネル 4.7 以上では sysctl で以下のように net.netfilter.nf_conntrack_helper=1 を設定:

# echo net.netfilter.nf_conntrack_helper=1 > /etc/sysctl.d/70-conntrack.conf

あるいは以下を使う必要があります:

# iptables -A PREROUTING -t raw -p tcp --dport 21 -j CT --helper ftp

ヒントとテクニック

PAM と仮想ユーザー

PAMpam_userdb.so を使うことはもうできません。代わりの方法として libpam_pwdfileAUR を使うことができます。多数のユーザーが存在する環境では pam_mysqlAUR[リンク切れ: アーカイブ: aur-mirror] というセクションもあります。ただし、このセクションでは chroot 環境と pam_pwdfile.so による認証を設定する方法だけを説明します。

In this example we create the directory vsftpd:

# mkdir /etc/vsftpd

One option to create and store user names and passwords is to use the Apache generator htpasswd:

# htpasswd -c /etc/vsftpd/.passwd

A problem with the above command is that vsftpd might not be able to read the generated MD5 hashed password. If running the same command with the -d switch, crypt() encryption, password become readable by vsftpd, but the downside of this is less security and a password limited to 8 characters. Openssl could be used to produce a MD5 based BSDvsftpd-ext password with algorithm 1:

# openssl passwd -1

Whatever solution the produced /etc/vsftpd/.passwd should look like this:

username1:hashed_password1
username2:hashed_password2
...

Next you need to create a PAM service using pam_pwdfile.so and the generated /etc/vsftpd/.passwd file. In this example we create a PAM policy for vsftpd with the following content:

/etc/pam.d/vsftpd
auth required pam_pwdfile.so pwdfile /etc/vsftpd/.passwd
account required pam_permit.so

Now it is time to create a home for the virtual users. In the example /srv/ftp is decided to host data for virtual users, which also reflects the default directory structure of Arch. First create the general user virtual and make /srv/ftp its home:

# useradd -d /srv/ftp virtual

Make virtual the owner:

# chown virtual:virtual /srv/ftp

A basic /etc/vsftpd.conf with no private folders configured, which will default to the home folder of the virtual user:

# pointing to the correct PAM service file
pam_service_name=vsftpd
write_enable=YES
hide_ids=YES
listen=YES
connect_from_port_20=YES
anonymous_enable=NO
local_enable=YES
dirmessage_enable=YES
xferlog_enable=YES
chroot_local_user=YES
guest_enable=YES
guest_username=virtual
virtual_use_local_privs=YES

Some parameters might not be necessary for your own setup. If you want the chroot environment to be writable you will need to add the following to the configuration file:

allow_writeable_chroot=YES

Otherwise vsftpd because of default security settings will complain if it detects that chroot is writable.

最後に vsftpd.service起動してください。

You should now be able to login from a ftp-client with any of the users and passwords stored in /etc/vsftpd/.passwd.

仮想ユーザーにプライベートフォルダを追加

まずユーザーのディレクトリを作成:

# mkdir /srv/ftp/user1
# mkdir /srv/ftp/user2
# chown virtual:virtual /srv/ftp/user?/

次に、/etc/vsftpd.conf に以下の行を追加:

local_root=/srv/ftp/$USER
user_sub_token=$USER

トラブルシューティング

vsftpd: 最新のカーネル (3.5 以上) と .service で接続できない (Error 500)

/etc/vsftpd.conf に以下を追加してください:

seccomp_sandbox=NO

vsftpd: refusing to run with writable root inside chroot()

vsftpd 2.3.5 現在、ユーザーがロックされた chroot ディレクトリに書き込むことはできません。セキュリティ上の脆弱性を防ぐためです。

chroot を有効化して FTP ディレクトリを設定することでアップロードを安全に許可できます:

local_root=/srv/ftp/user
# mkdir -p /srv/ftp/user/upload
#
# chmod 550 /srv/ftp/user
# chmod 750 /srv/ftp/user/upload

/etc/vsftpd.conf に以下を記述することでセキュリティを迂回できます (vsftpd 3.0.0 以上。ソース: Fixing 500 OOPS: vsftpd: refusing to run with writable root inside chroot ()):

allow_writeable_chroot=YES

or alternative:

もしくは AUR から vsftpd-extAUR[リンク切れ: アーカイブ: aur-mirror] をインストールして設定ファイルに allow_writable_root=YES と設定してください。

FileZilla Client: GnuTLS error -8 -15 -110 when connecting via SSL

vsftpd は SSL セッションで平文のエラーメッセージを表示します。デバッグするには、一時的に暗号化を解除しないとエラーメッセージを確認できません [1]

vsftpd.service がブート時に起動しない

vsftpd を有効にしてもブート時に起動しない場合、サービスファイルが network.target の後にロードされるように設定されているか確認してください:

/usr/lib/systemd/system/vsftpd.service
[Unit]
Description=vsftpd daemon
After=network.target

参照