FTP协议采取的是明文传递,其他人很容易通过抓包工具抓取到用户的个人信息。

我们所采取的安全访问主要分为两种,

1.ftps

利用ftp+ssl 的结合,对用户的个人信息进行加密。

2.虚拟账号的映射

虚拟账号FTP看作是一种特殊的匿名FTP,这个特殊的匿名FTP,特殊在哪里呢,它拥有登录FTP的用户名和密码,但是它所使用的用户名又不是本地用户(即它的用户名只能用来登录FTP,而不能用来登录系统),并且所有的虚拟用户名,在登录FTP时,都是在映射为一个真实的账号之后才登录到FTP上的。(需要说明的是这个真实的号是可以登录系统的,即它和本地用户在这一点上性质是一样的。)

[root@localhost ~]# mkdir /mnt/cdrom

[root@localhost ~]# mount /dev/cdrom /mnt/cdrom

[root@localhost ~]# yum install vsftpd –y  

[root@localhost ~]# service vsftpd start

[root@localhost ~]# useradd test
[root@localhost ~]# passwd test

增加本地账户test。以供测试。密码为123.

[root@localhost ~]# tshark -ni eth0 -R "tcp.dstport eq 21"

195.702847 192.168.145.10 -> 192.168.145.200 FTP Request: USER test
195.703455 192.168.145.10 -> 192.168.145.200 FTP Request: PASS 123
195.745117 192.168.145.10 -> 192.168.145.200 FTP Request: opts utf8 on
195.745739 192.168.145.10 -> 192.168.145.200 FTP Request: syst
195.746251 192.168.145.10 -> 192.168.145.200 FTP Request: site help
195.746526 192.168.145.10 -> 192.168.145.200 FTP Request: PWD
195.746808 192.168.145.10 -> 192.168.145.200 FTP Request: CWD /home/test/pub/

可以看到很容易就得到了用户的个人信息

1.FTPS的实现

[root@localhost ~]# vim /etc/pki/tls/openssl.cnf

45 dir = /etc/pki/CA
88 countryName = optional
89 stateOrProvinceName = optional
90 organizationName = optional
136 countryName_default = CN
141 stateOrProvinceName_default = BEIJING
144 localityName_default = BEIJING

[root@localhost ~]# cd /etc/pki/CA/

[root@localhost CA]# mkdir certs crl newcerts

[root@localhost CA]# touch index.txt serial

[root@localhost CA]# echo "01" >serial

[root@localhost CA]# openssl genrsa 1024 >private/cakey.pem

[root@localhost CA]# chmod 600 private/cakey.pem

[root@localhost CA]# openssl req -new -key private/cakey.pem -x509 -out cacert.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) [CN]:
State or Province Name (full name) [BEIJING]:
Locality Name (eg, city) [BEIJING]:
Organization Name (eg, company) [My Company Ltd]:zz.com
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server's hostname) []:www.zz.com
Email Address []:
[root@localhost CA]#

这样CA就搞定了,进行ftp证书的申请

[root@localhost CA]# mkdir -pv /etc/vsftpd/certs

[root@localhost CA]# cd /etc/vsftpd/certs/

[root@localhost certs]# openssl genrsa 1024 >vsftpd.key

[root@localhost certs]# openssl req -new -key vsftpd.key -out vsftpd.csr

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) [CN]:
State or Province Name (full name) [BEIJING]:
Locality Name (eg, city) [BEIJING]:
Organization Name (eg, company) [My Company Ltd]:zz.com
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server's hostname) []:ftp://tec.zz.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

[root@localhost certs]# openssl ca -in vsftpd.csr -out vsftpd.cert

Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jul 26 08:08:07 2012 GMT
            Not After : Jul 26 08:08:07 2013 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BEIJING
            organizationName          = zz.com
            organizationalUnitName    = tec
            commonName                = ftp://tec.zz.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                F1:76:A8:59:10:62:99:68:81:D3:D1:90:83:C5:0D:87:3E:C1:D7:BC
            X509v3 Authority Key Identifier:
                keyid:CC:D2:88:63:AF:D9:DD:0C:60:7F:D7:94:A7:3F:7F:F5:BC:17:CA:DA

Certificate is to be certified until Jul 26 08:08:07 2013 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

通过man手册查看vsftpd中与cert,ssl相关参数。添加至vsftpd配置文件中

[root@localhost certs]# man 5 vsftpd.conf

[root@localhost certs]# vim /etc/vsftpd/vsftpd.conf

114 pam_service_name=vsftpd
115 userlist_enable=YES
116 tcp_wrappers=YES
117 rsa_cert_file=/etc/vsftpd/certs/vsftpd.cert       //证书文件存放位置
118 rsa_private_key_file=/etc/vsftpd/certs/vsftpd.key       //私钥存放位置
119 ssl_tlsv1=YES                                            //版本支持
120 ssl_sslv2=YES
121 ssl_sslv3=YES
122 ssl_enable=YES
123 force_local_logins_ssl=YES                         //强制用户进行加密登录
124 force_local_data_ssl=YES                          //强制数据加密

[root@localhost certs]# service vsftpd restart

我们使用FTP客户端工具进行测试。

这里使用的是Flashfxp 。

p_w_picpath

站点选择站点管理器,右键Flashfxp新建站点,连接类型选为SSLV3

应用后进行连接

p_w_picpath

抓包可以发现已经抓取不到有价值的信息了

981.505168 192.168.145.10 -> 192.168.145.200 FTP Request: AUTH SSL
981.516046 192.168.145.10 -> 192.168.145.200 FTP Request: \200\332\001\003\000\000\261\000\000\000 \000\300\024\000\300
981.518291 192.168.145.10 -&gt; 192.168.145.200 FTP Request: \026\003\000\000\204\020\000\000\200\234\2270\177V\306}<\365\230
981.723459 192.168.145.10 -> 192.168.145.200 TCP 55233 &gt; 21 [ACK] Seq=443 Ack=921 Win=63320 Len=0
981.723778 192.168.145.10 -&gt; 192.168.145.200 TCP [TCP Dup ACK 448#1] 55233 &gt; 21 [ACK] Seq=443 Ack=921 Win=63320 Len=0 SLE=846 SRE=921
1036.808819 192.168.145.10 -&gt; 192.168.145.200 FTP Request: \027\003\000\000\030w\253;\a\254z\273%*l\227\003\001\237\307\0335\374\003\253yt\254C\027\003\000\000 \223\033\330|O\254\214\261\317\271ixv&gt;\345\023\362\327\274\357\t\267\375\205t\204\243\314r\354\275%
1036.817167 192.168.145.10 -&gt; 192.168.145.200 FTP Request: \027\003\000\000\030\205\233\237\033\200'V\305\256\330Y\223\034\305\356\037A\234a\206\206\340\354\256\027\003\000\000 \274\303l/\026|\024#H\271f_KL\253\021\0167\271\271\335^\031\245/\355#\230P62\244
1036.818470 192.168.145.10 -&gt; 192.168.145.200 FTP Request: \027\003\000\000\030\356\203\342\3309:\3760\366t\230j\005\2711\377\325\212\206\3707\3040<\027\003\000\000 \037\237*r5\201\242\346\031\322\202\266\023_k\316+]\221\020\371O\000\250\255\255\311m\232\215\251\037
1037.009523 192.168.145.10 -> 192.168.145.200 FTP Request: \027\003\000\000\030\2731\274\025\213\021\203<\223yMd\270\275\245?\031x\240\377\251\022\273\241\027\003\000\000 &lt;\202\206#\3575\224U\247d6\037\346\244N\353\\342\352\365)\252\271\227)\263\230\367\355R\326l
1037.010302 192.168.145.10 -> 192.168.145.200 FTP Request: \027\003\000\000\030\036\006\a\363\267\327^q\242r

2.虚拟账号的实现

[root@localhost ~]# mkdir /mnt/cdrom

[root@localhost ~]# mount /dev/cdrom /mnt/cdrom

[root@localhost ~]# yum install vsftpd –y

[root@localhost ~]# service vsftpd start

[root@localhost ~]# cd /etc/vsftpd/

建立三个虚拟账号,奇数行为用户名,偶数行为密码。

[root@localhost vsftpd]# vim viruser.txt

1 user1
  2 123
  3 user2
  4 123
  5 user3
  6 123         

然后我们需要生成hash加密的数据库文件以便让后续的pam.userdb 这个模块来读取我们的用户名密码进行验证.我们需要确认系统已经安装了db4_utils的rpm包,接下来执行:

[root@localhost vsftpd]# yum install db4-utils -y

[root@localhost vsftpd]# db_load -T -t hash -f viruser.txt viruser.db

生成数据库文件后,vsftpd要启用虚拟账号需要跟PAM认证模块配合.我们用rpm安装好vsftp套件后.在/etc/pam.d/下就会有一个vsftpd的文件,这个是vsftpd利用pam这个认证模块来认证用户登陆的,

[root@localhost vsftpd]# vim /etc/pam.d/vsftpd

auth required   pam_userdb.so  db=/etc/vsftpd/viruser

account required  pam_userdb.so  db=/etc/vsftpd/viruser

这里的db=后面指的就是我们生成好的hash数据文件.文件后缀的.db是不需要打的,pam_userdb.so这个模块会自动帮我们识别.

因为虚拟账号要登陆系统必须要影射到我们系统的真实用户上.不可能一个无缘无故的用户跑到我们系统上来.所以需要建立一个虚拟用户影射的系统账号:useradd test 不需要给这个用户密码,那么他也永远不能登陆系统.

[root@localhost vsftpd]# useradd test

[root@localhost vsftpd]# vim /etc/vsftpd/vsftpd.conf

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES       
guest_username=test               //guest账号影射为test

[root@localhost vsftpd]# service vsftpd restart

[root@localhost vsftpd]# chmod a+rx /home/test

[root@localhost vsftpd]# cd /home/test/

[root@localhost test]# touch f1

测试:

 p_w_picpath

如果我们希望用户有特殊的权限。我们可以这样做

例如

user1具有浏览文件,目录和下载的权限.

user2具有下载,浏览,和建立目录的权限.

user3具有下载,上传,浏览,建立并删除目录的权限.

[root@localhost vsftpd]# vim /etc/vsftpd/vsftpd.conf

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES
guest_username=test //guest账号影射为test

user_config_dir=/etc/vsftpd/viruser-conf (用户个性化配置目录)

[root@localhost ~]# mkdir /etc/vsftpd/viruser-conf

[root@localhost ~]# cd /etc/vsftpd/viruser-conf

分别建立user1,user2,user3 这三个文件,文件名必须跟虚拟用户名一致,然后我们就可以在这些文件里面添加用户所对应的权限喽.....
在user1里添加 anon_world_readable_only=NO (这个选项是说用户只可以读取自己有权限读取的文件,这项为YES的话那么其他用户的文件就看不到了,这里我们设置为NO)
然后user2的配置文件:
anon_world_readable_only=NO
download_enable=YES
write_enbale=YES
anon_write_mkdir_enable=YES
anon_max_rate=200000 (限速为200K)
user3的配置
anon_world_readable_only=NO
download_enable=YES
write_enbale=YES
anon_write_mkdir_enable=YES
anon_other_write_enabel=YES
local_root=/db/ftp (默认虚拟用户会登陆到自己影射的系统用户主目录,如果你希望他登陆到其它目录就用这个选项)

重启下vsftpd服务,配置就生效了

参数还有很多:

anon_max_rate   匿名账号的速率
local_max_rate  本地帐号的速率
max_clients      同时在线的最大用户数量
max_per_ip      每用户的最大连接数
限定用户空间    磁盘配额
user_config_dir   用户的配置的目录