当前位置: 首页 > GNU/Linux, Linux系统, 网络安全 > 正文

FTP服务器的搭建与安全配置

  FTP可以说是Internet上使用非常广泛的一种通讯协议了。它工作在OSI模型的第7层,是TCP/IP的一种具体应用。FTP采用基于TCP的可靠连接:监听21端口来等待控制连接请求,当连接建立后,采用20号端口来建立数据传输通道。

  FTP中使用的一些典型消息:

    125  数据连接打开,传输开始

    200  命令OK

    331  用户名OK,需要输入密码

    425  不能打开数据连接

    452  错误写文件

    500  命令无法识别

 

  Vsftp是Linux系统下的一套开源FTP服务器软件,具有结构简单、性能优良的特点,是一款轻量型稳定安全的FTP。

  1.安装Vsftp。(测试环境:ContOS 6.5)

  查看本机有没有vsftp安装源:

~# rpm -qa | grep svftpd

   如果有,则会显示vsftp的版本,这样就可以直接安装:

~#  yum install vsftpd

  但我的上面貌似没有,只能下载源码编译了。Vsftp官网主页:http://vsftpd.beasts.org ,貌似进不去,网上找了下,可以在这里下载:站长之家 。版本是Vsftp v2.3.2 For Linux。解压编译:

~#  tar xvf vsftpd-2.3.2.tar.gz
~#   cd ./vsftpd-2.3.2

  在此说明一下vsftpd-2.3.2目录下的builddefs.h文件,该文件主要用来设定FTP服务器的一些安全配置:CTP_Wrappers、PAM和SSL。可以按需要选择/取消。  

vim ./builddefs.h
#ifndef VSF_BUILDDEFS_H
#define VSF_BUILDDEFS_H

#undef VSF_BUILD_TCPWRAPPERS    //对TCP包进行解析的工具,用于限制某种服务的访问权限进而达到保护系统的目的。
#define VSF_BUILD_PAM           //一种高效便利的用户级别的认证方式,用来加强服务器的安全性能。
#undef VSF_BUILD_SSL            //利用加密技术,保障在传输过程中数据不会被窃听。

#endif /* VSF_BUILDDEFS_H */

  2.配置vsftpd.conf文件

  vsftpd.conf文件是主配置文件,在/etc/vsftpd/目录下.

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES  //设置是否允许匿名访问
#
# Uncomment this to allow local users to log in.
local_enable=YES  //设置是否允许本地用户登录
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES   //设置是否允许写操作
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022  //本地用户操作权限
#
# 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  //设置是否允许匿名建立目录
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES  //设置是否在改变目录后发送消息
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES  //设置是否激活日志功能
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES  //设置是否使用20端口传输数据(PORT模式)
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
//修改匿名用户上传文件的所有者
#chown_uploads=YES  
#chown_username=whoever
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log
#xferlog_file=/var/log/xferlog  //设置日志文件保存位置
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
xferlog_std_format=YES  //设置是否使用标准文件日志
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600  //设置会话的超时时间
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120  //设置数据传输的超时时间
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure  //设置非特权用户账户
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES  //设置是否允许客户端使用sync等命令
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
//设置是否以ASCII形式传输文件
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES  
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.  //设置登录后的欢迎信息
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES  //设置是否启用禁止指定匿名用户登录
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails  //加入用户列表(前提是上面已经设置为YES)
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_local_user=YES  //与下面配置有关
#chroot_list_enable=YES //设置是否允许本地用户离开其主目录
# (default follows)
#
//如果使用该项,则上面的chroot_local_user=YES需设置为NO。指定不能离开主目录的用户,将用户名一个一行写入指定文件。
#chroot_list_file=/etc/vsftpd/chroot_list  
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES  //
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES  //设置是否开启IPV4监听
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES  //设置是否开启IPV6监听

pam_service_name=vsftpd  //设置访问所使用的PAM模块
userlist_enable=YES  //如果激活该选项,将禁止所指定文件中的用户登录
tcp_wrappers=YES  //设置是否使用TCP_Wrappers作为主机访问控制方式

  3.配置ftpusers文件

   ftpusers文件用于限定系统中那些用户能/不能使用FTP服务。可以根据实际情况添加/删除。

#User that are not allowed to login via ftp
root
...
...
...

 4.配置user_list文件

 user_list文件指定的用户能否访问FTP服务器取决于userlist_deny选项的设置。默认为YES,即禁止user_list文件的用户访问FTP服务器,这与ftpusers文件相似。但是如果设置为NO,则完全相反,即只允许该文件列表里的用户访问该FTP服务器。

# vsftpd userlist
# If userlist_deny=NO,only allow users in this file
# If userlist_deny=YES(default),never allow users in this file,and do not even prompt for a password.
# ...
# ...
root
...

如果要限制指定的本地用户(即user_list文件的用户)不能访问FTP服务器,可以相应地修改vsftpd.conf文件:

userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/user_list

 同样,如果要限制指定的本地用户(即user_list文件的用户)能够访问FTP服务器,而其他的本地用户不能访问,可以相应地修改vsftpd.conf文件:

userlist_enable=YES
userlist_deny=NO 
userlist_file=/etc/vsftpd/user_list

5.配置允许匿名用户使用FTP服务器

创建用户ftp-anon和目录/var/ftp-pub:

mkdir /var/ftp-pub
useradd -d /var/ftp-pub ftp-anon  //-d 指定用户的主文件目录,vsftpd默认登录成功后的目录是该用户的home目录。

作为匿名访问,/var/ftp-pub不应该属于用户ftp-anon,也不应该有写权限,所以可以用以下方法修改其权限:

~# chown root.root /var/ftp-pub
~# og-w -d /var/ftp-pub

 修改vsftpd.conf文件:

anonymous_enable=YES  //设置允许匿名访问(默认开启)
//设置允许匿名上传和创建目录(非必须,慎用!)
anon_upload_enable=YES
anon_mkdir_write_enable=YES

 6.虚拟用户使用Vsftp服务器

上面的(包括一般的)ftp访问都是通过建立系统帐号来访问服务器的,这样很不安全,如果权限配置错误将会使服务器受到威胁。但是,通过建立虚拟FTP账户(与系统帐号分离),就可以大大增强系统的安全性。虚拟FTP账户只能用于文件传输,也叫guest用户。它是把用户名/密码保存起来,再验证的。所以Vsftp需要一个系统用户的身份来读取数据(用户名/密码)文件,即guest用户,用以映射虚拟用户。

具体配置如下:

(1)生成虚拟用户口令库文件。以下为例:

~# vim login.txt   
zhangsan    //username1  
hehe        //passwd1
lisi        //username2
mimanicai   //passwd2
......

(2)配置生成Vsftp的认证文件

保存并退出,使用db_load命令生成口令库文件:

~#  db_load -T -t hash -f login.txt /etc/vsftpd/vsftpd_login.db

修改口令库文件的权限:

chmod 600 /etc/vsftpd/vsftpd_login.db

 编辑虚拟用户所需的PAM配置文件

~# vim /etc/pam.d/vsftpd
//加入以下两行:
auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db
account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db

 (3)建立虚拟用户访问的目录,并设置相应的访问权限

~# useradd -d /home/ftp virtual
~# chmod 700 /home/ftp

 (4)建立配置文件

~# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
~# vim /etc/vsftpd/vsftpd.conf
//配置如下:
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
listen=YES
guest_enable=YES
guest_username=virtual

(5)重启Vsftp服务器

~# service vsftpd restart

 至此,虚拟用户使用Vsftp服务器配置将完成了。可以使用login.txt里的用户帐号登录FTP服务器了。

7.改变Vsftp的端口号

在vsftpd.conf文件里加入如下,并重启vsftp。

~# listen_port=2121

 8.配置Vstfp服务器的chroot

chroot就是为登录用户指定一个固定的目录,这个目录一般是用户的主目录,用户被限制在该目录下,类似与WEB服务器的虚拟目录,从而保护了系统安全。

设置指定的用户执行chroot:

chroot_local_user=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

相关参考文章: http://wiki.ubuntu.org.cn/Vsftpd



这篇博文由 s0nnet 于2014年10月06日发表在 GNU/Linux, Linux系统, 网络安全 分类下, 欢迎你在下面发表评论
如无特别说明,计算机技术分享发表的文章均为原创,欢迎大家转载,转载请注明: FTP服务器的搭建与安全配置 | 计算机技术分享
关键字:

FTP服务器的搭建与安全配置:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter