1.关闭匿名用户登录
ftp支持匿名登录是不安全,所以要禁止匿名ftp登录
在/etc/vsftpd/vsftpd.conf修改以下三项
anonymous_enable=NO
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
[root@red-hat-5 ~]#
service vsftpd restart
关闭vsftpd: [确定]
为vsftpd启动vsftpd: [确定]
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root):
anonymous
331 Please specify the password.------>提示登录失败,说明已经禁
用匿名登录
Password:
530 Login incorrect.
Login failed.
2.创建一个系统用户来登录ftp
[root@red-hat-5 ~]# useradd -s /sbin/nologin viong
建设一个不能登录系统用户. 只用来登录ftp服务
,这里如果没设置用户目录。默认是在home下
[root@red-hat-5 ~]# passwd viong
Changing password for user viong.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root):
viong
331 Please specify the password.------>提示登录成功
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
接下来测试权限
ftp> put viong.txt上传viong.txt文件成功
local: viong.txt remote: viong.txt
227 Entering Passive Mode (127,0,0,1,98,115)
150 Ok to send data.
226 File receive OK.
ftp> mkdir hong创建hong目录文件成功
257 "/home/viong/hong" created
ftp> rmdir hong删除hong目录文件成功
250 Remove directory operation successful.
ftp> pwd 显示家目录
257 "/home/viong"
ftp> cd /etc切换/etc成功
250 Directory successfully changed.
ftp>
mget passwd下载passwd
mget passwd?
227 Entering Passive Mode (127,0,0,1,220,77)
150 Opening BINARY mode data connection for passwd (1429 bytes).
226 File send OK.
1429 bytes received in 5.9e-05 seconds (2.4e+04 Kbytes/s)
ftp> !ls
显示下载passwd成功
anaconda-ks.cfg install.log install.log.syslog passwd viong.txt
3.加强vsftp安全设置
从以上可以看出ftp家目录存在安全漏洞,所以要修改以下设置:
(1)限制系统用户锁定在家目录
Vi /etc/vsftpd/vsftpd.conf
去掉前面#号
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list限制更多系统用户
然后把所有用户加入/etc/vsftpd/chroot_list即可
[root@red-hat-5 ~]#
ls /etc/vsftpd/ chroot_list
默认是不存在,需要我们手动建立
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh
[root@red-hat-5 ~]# touch /etc/vsftpd/chroot_list
[root@red-hat-5 ~]# cut -d : -f 1 /etc/passwd>>/etc/vsftpd/chroot_list
把本地用户都加入到chroot_list
cut命令是切去某一列,-d是每列的分隔符,-f是切取第几列,然后重定向到chroot文件
[root@red-hat-5 ~]# ll /etc/vsftpd/
总计 24
-rw-r--r-- 1 root root 197 12-25 19:57 chroot_list
-rw--------1 root root 125 2007-12-13 ftpusers
ftpusers指的是阻止这个文件中的用户登陆
-rw------- 1 root root 361 2007-12-13 user_list
-rw------- 1 root root 4396 12-25 19:19 vsftpd.conf
-rwxr--r-- 1 root root 338 2007-12-13 vsftpd_conf_migrate.sh
(2)限制重要系统用户不能登录ftp权限
[root@red-hat-5 ~]# cat /etc/vsftpd/ftpusers
默认会加入一些比较重要系统用户
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@red-hat-5 ~]#
echo viong >> /etc/vsftpd/ftpusers
把之前建的viong账号加进去测试
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root):
viong
331 Please specify the password. ----->提示登录失败,说明生效了
Password:
530 Login incorrect.
Login failed.
(3)利用ftp用户策略允许登录ftp的系统用户
系统添加一个用户也默认有ftp的登陆权限,是不安全,要一个个设置,有点繁琐。利用ftp用户策略解决这个问题,即user_list文件设置,只有user_list中存在的用户才能登录系统
修改配置文件:vi /etc/vsftpd/vsftpd.conf
在userlist_enable=YES文件后面添加
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list
[root@red-hat-5 ~]#
service vsftpd restart
关闭vsftpd: [确定]
为vsftpd启动vsftpd: [确定]
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root):
viong
530 Permission denied. ------------->提示登录失败
Login failed.
[root@red-hat-5 ~]#
echo viong >> /etc/vsftpd/user_list
把viong账号加入到 user_list
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): viong
331 Please specify the password. ------------->提示登录成功
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
(4)设置登录ftp目标ip地址
为了让ftp更安全,我们设置ftp目标ip地址访问
C:\Users\Administrator>ipconfig查看本地ip
...................................
以太网适配器 VMware Network Adapter VMnet1:6
连接特定的 DNS后缀 . . . . . . . :
IPv4 地址 . . . . . . . . . . . . : 192.168.184.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
只允许这个ip地址访问ftp ssh,可以写条iptable做限制.
如下:
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 22 -j ACCEPT
允许192.168.184.1访问本地
22端口
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 21 -j ACCEPT
允许192.168.184.1访问本地
21端口
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 65341:65351 -j ACCEPT
允许192.168.184.1访问本地
PASV端口
[root@red-hat-5 ~]# iptables -P INPUT DROP 禁止任何输入的数据包
[root@red-hat-5 ~]# service iptables save保存iptables设置
将当前规则保存到 /etc/sysconfig/iptables: [确定]
[root@red-hat-5 ~]#service iptables status
检查iptables的设置
表格:filter
Chain INPUT (policy DROP)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT tcp -- 192.168.184.1 0.0.0.0/0 tcp dpt:22
3 ACCEPT tcp -- 192.168.184.1 0.0.0.0/0 tcp dpt:21
4 ACCEPT tcp -192.168.184.1 0.0.0.0/0 tcp dpts:65341:65351
安装完系统后,默认的iptables的规则只允许状态为RELATED、ESTABILISHED的包和SSHD的数据包进入服务器,当安装好vsftpd后,通常需要在iptables上面放开21端口,完整的iptables配置如下:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
在这样的规则下面,客户端能否正常登录到FTP服务器上进行文件的上传和下载呢,测试过程如下:
此例中,FTP服务器IP地址为192.18.2.75,iptables配置如下:

客户端使用Filezilla Client进行连接,效果如下图:

上图中,服务器要求使用被动模式(PASV)连接,从图上可以看出,服务器监听端口44892(175*256+92)等待客户端的连接,但iptables的规则阻止访问该端口,所以连接失败。
将Filezilla配置为主动模式,如下图:

连接服务器:

可以看出连接成功。
因此,在上面的iptables规则下,只能使用主动模式连接。
如何允许使用被动模式呢?
方法一:使用nf_conntrack_ftp模块
插曲:在kernel2.6.19前,这个模块叫ip_conntrack.ko,位于
/lib/modules/`uname -r`/kernel/net/ipv4/netfilter/ip_conntrack_ftp.ko。之后,这个模块改名为nf_conntrack_ftp.ko,位于
/lib/modules/`uname -r` /kernel/net/netfilter/nf_conntrack_ftp.ko
这个模块是Netfilter专门用来处理FTP这个复杂协议的模块,它能够将FTP的数据包标识为RELATED状态,从而允许使用被动模式访问。
使用命令: modprobe nf_conntrack_ftp 加载该模块,之后客户端就能以被动模式连接至FTP服务器了。


上图所示,使用被动模式,服务器监听端口号为159*256+8=40712,在FTP服务器上查看TCP连接状态,如下图:

的确是使用的40712号端口。
方法二:在vsftp的配置文件中使用如下两个选项强制将被动模式时使用的端口号限定在一个范围,然后在iptables上运行对这个范围内端口的访问,选项如下:
Pasv_min_port=10000
Pasv_max_port=15000