ftp安全性的應用

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 。

image

站點選擇站點管理器,右鍵Flashfxp新建站點,連接類型選爲SSLV3

應用後進行連接

image

抓包可以發現已經抓取不到有價值的信息了

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

測試:

 image

如果我們希望用戶有特殊的權限。我們可以這樣做

例如

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   用戶的配置的目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章