Vsftpd服務搭建

參考資料:html

http://www.9170.org/post-322.html    史上最詳細的vsftpd配置文件講解   做者:王健宇linux


實驗可能會用到的測試工具
vim

flashfxp或filezilla        windows

一、安裝vsftpd(服務器端)、ftp軟件(客戶端)安全

[root@gnnt ~]# yum install vsftpd ftp服務器

#yum源的創建可參見:http://1343138116.blog.51cto.com/6124664/1681861  RedHat 6.6自建yum倉庫tcp

二、查詢vsftpd安裝列表ide

[root@gnnt ~]# rpm -ql vsftpd工具

/etc/vsftpd/vsftpd.conf   #vsftpd服務的主配置文件post

/etc/pam.d/vsftpd       #vsftpd的pam模塊 

/etc/vsftpd/ftpusers     #控制用戶是否可以登陸FTP服務器,默認爲加入此文件的用戶不能登陸

/etc/vsftpd/user_list    #主配置文件常常與此命令連用userlist_deny=YES(黑名單),爲NO表明白名單

以上4個文件常常用到

三、實際用戶

以用戶zj爲例

(1)、建立zj用戶

[root@ftp ~]# useradd zj

[root@ftp ~]# passwd zj

Changing password for user zj.

New password: 

BAD PASSWORD: it is too simplistic/systematic

Retype new password: 

passwd: all authentication tokens updated successfully.

(2)、將用戶禁錮在自身的主目錄中

實驗前最好先關閉iptables與SElinux

[root@ftp ~]#service iptables stop

[root@ftp ~]#setfenforce 0

[root@ftp ~]#getfenforce

Permissive

[root@ftp ~]# vim /etc/vsftpd.conf/vsftpd.conf

chroot_list_enable=YES              #開啓chroot寫入列表的功能

chroot_list_file=/etc/vsftpd/chroot_list  #與上條命令結合使用,在文件裏的用戶將被禁錮

[root@ftp ~]#vim /etc/vsftpd/chroot_list

zj

[root@ftp ~]#

注:若是以上兩個參數與chroot_local_user=YES(將用戶限制在本身的主目錄內,爲了安全最好是YES)連用,將會出現chroot_list中的用戶將成爲白名單,不然爲黑名單,我這裏沒有與它連用

(3)、普通用戶的文件上傳、下載、新建、刪除

[root@ftp ~]# ftp localhost        #登陸FTP服務器,也可自行添加一臺linux客戶端作測試

Connected to localhost (127.0.0.1).

220-welcome to ftp.gnnt.com!!!    

220

Name (localhost:root): zj            #輸入用戶名

331 Please specify the password.

Password:                       #輸入密碼

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> pwd                        

257 "/" 

ftp> cd /home                    #被禁錮在了本身的家目錄中,不能切換目錄

550 Failed to change directory.                     

ftp> ls

227 Entering Passive Mode (127,0,0,1,160,213).

150 Here comes the directory listing.

226 Directory send OK.

ftp> lcd /etc                    #改變本地的工做目錄

Local directory now /etc

ftp> put inittab     

#上傳/etc/inittab文件到/home/zj,上傳文件時最好是在本身的主目錄中(此種狀況發生在沒有被禁錮的用戶中)

local: inittab remote: inittab

227 Entering Passive Mode (127,0,0,1,185,62).

150 Ok to send data.

226 Transfer complete. 

884 bytes sent in 9e-05 secs (9822.22 Kbytes/sec)

ftp> get inittab          #下載inittab文件到當前目錄(意指登陸FTP時是在哪一個目錄進行的)

local: inittab remote: inittab

227 Entering Passive Mode (127,0,0,1,114,111).

150 Opening BINARY mode data connection for inittab (884 bytes).

226 Transfer complete.

884 bytes received in 0.0189 secs (46.85 Kbytes/sec)

ftp> mkdir test                       #新建test目錄

257 "/test" created

ftp> rmdir test                       #刪除test目錄

250 Remove directory operation successful.

ftp> delete inittab                    #刪除inittab文件

250 Delete operation successful.

ftp> ls

227 Entering Passive Mode (127,0,0,1,41,127).

150 Here comes the directory listing.

226 Directory send OK.


同時也可在windows下進行測試(這裏我使用的gif錄製軟件「都叫獸」,能夠百度搜索下載)

這裏個人windows客戶端IP地址爲:172.16.0.255

     linux服務器端IP地址爲:172.16.0.10


wKiom1XlYaqxi2zEAAIuq1XKR68525.gif


四、虛擬用戶(未完待續)

五、匿名用戶上傳、下載、新建、刪除文件的方法(不建議給匿名用戶如此大的權限)

(1)、修改vsftpd.conf配置文件

[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

banner_file=/etc/vsftpd/welcome.txt

listen=YES

xferlog_enable=YES

xferlog_file=/var/log/xferlog

命令逐條解釋

#給anonymous用戶有上傳文件的權限

#給anonymous用戶有建立文件的權限

#給anonymous用戶有建立文件以外的其它權限,例如刪除文件

#登陸ftp服務器時顯示的信息,此文件須要本身手動建立,並添加相應的內容

#設置vsftpd的啓動方式爲stand alone(獨立守護進程),另外一種方式爲super daemon(超級守護進程),可在/etc/xinetd.d/目錄下配置一個腳本文件便可讓其成爲瞬時守護進程(用於訪問量較小的FTP服務器)

#是否開啓傳輸日誌

#傳輸日誌的文件的路徑

[root@ftp ~]#service vsftpd restart

Shutting down vsftpd:                                    [  OK  ]

Starting vsftpd for vsftpd:                                [  OK  ]

#每次修改配置文件或剛安裝完服務後,都不要忘記重啓,使服務可用

(2)、匿名用戶上傳文件方法

1)新建uploads文件夾與「歡迎」文件

[root@ftp ~]#mkdir /var/ftp/uploads

[root@ftp ~]#echo "welcome to ftp.gnnt.com!!!" >/etc/vsftpd/welcome.txt

#新建welcome.txt文件並向其追加內容

2)添加匿名用戶能夠上傳文件權限

anon_upload_enable=YES       #將vsftpd.conf配置文件中的井號去掉

3)設置uploads文件夾權限

[root@ftp ~]#setfacl -m u:ftp:rwx /var/ftp/uploads/

[root@ftp uploads]# getfacl /var/ftp/uploads/

getfacl: Removing leading '/' from absolute path names

# file: var/ftp/uploads/

# owner: root

# group: root

user::rwx

user:ftp:rwx

group::r-x

mask::rwx

other::r-x

4)上傳/etc/inittab文件

[root@ftp ~]# ftp localhost 

Connected to localhost (127.0.0.1).

220-welcome to ftp.gnnt.com!!!    #事先創建好了的「歡迎」文件

220 

Name (localhost:root): anonymous   #用戶名

331 Please specify the password.

Password:                  #密碼能夠不用輸,敲回車鍵便可

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd uploads                        #進入到uploads目錄

250 Directory successfully changed.

ftp> lcd /etc/                         #在客戶端上改變本地的工做目錄

Local directory now /etc

ftp> put inittab                        #上傳inittab到uploads目錄

local: inittab remote: inittab

227 Entering Passive Mode (127,0,0,1,176,207).

150 Ok to send data.

226 Transfer complete.

884 bytes sent in 9.6e-05 secs (9208.33 Kbytes/sec)

ftp> ls

227 Entering Passive Mode (127,0,0,1,119,21).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab

-rw-r--r--    1 0        0           0 Aug 18 11:14 test.txt

226 Directory send OK.

若沒有執行3)這步操做,將出現如下提示:

ftp> put inittab

local: inittab remote: inittab

227 Entering Passive Mode (127,0,0,1,250,230).

550 Permission denied.

(3)、匿名用戶新建、刪除目錄方法

anon_mkdir_write_enable=YES    #將vsftpd.conf配置文件中的井號去掉

ftp> cd uploads            #注下例實驗均在此目錄進行

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode (127,0,0,1,92,36).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab           

226 Directory send OK.

ftp> mkdir test

257 "/uploads/test" created

ftp> rmdir test

250 Remove directory operation successful.

ftp> ls

227 Entering Passive Mode (127,0,0,1,199,206).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab

drwx------    2 14       50           4096 Aug 18 12:04 test

226 Directory send OK.

(4)、匿名用戶刪除文件方法

anon_other_write_enable=YES     #將vsftpd.conf配置文件中的井號去掉

事先在/var/ftp/uploads目錄中新建一個test.txt文件或另外上傳一個文件

ftp> ls

227 Entering Passive Mode (127,0,0,1,173,181).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab

drwx------    2 14       50           4096 Aug 18 12:04 test

-rw-r--r--    1 0        0           0 Aug 18 11:14 test.txt

226 Directory send OK.

ftp> delete test.txt

250 Delete operation successful.

ftp> ls

227 Entering Passive Mode (127,0,0,1,188,242).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab

drwx------    2 14       50           4096 Aug 18 12:04 test

226 Directory send OK.

(5)、匿名用戶下載文件方法

再次在/var/ftp/uploads目錄中新建test.txt文件或本身上傳一個文件進行下載

ftp> ls

227 Entering Passive Mode (127,0,0,1,253,154).

150 Here comes the directory listing.

drwx------    2 14       50           4096 Aug 18 11:18 123

-rw-------    1 14       50           884 Aug 18 11:55 inittab

drwx------    2 14       50           4096 Aug 18 12:04 test

-rw-r--r--    1 0        0           0 Aug 18 12:14 test.txt

226 Directory send OK.

ftp> get test.txt

local: test.txt remote: test.txt

227 Entering Passive Mode (127,0,0,1,221,87).

150 Opening BINARY mode data connection for test.txt (0 bytes).

226 Transfer complete.

ftp> bye

221 Goodbye.

[root@gnnt ~]# ls    #能夠看出是直接下載在當前目錄中的

anaconda-ks.cfg  Desktop  Documents  Downloads  install.log.syslog s  test.txt 

六、查看xferlog

[root@ftp ~]# tail /var/log/xferlog 

Tue Aug 18 19:14:52 2015 1 127.0.0.1 0 /uploads/test.txt b _ o a ? ftp 0 * c

Tue Aug 18 19:27:54 2015 1 127.0.0.1 884 /uploads/inittab b _ i a ? ftp 0 * c

Tue Aug 18 19:55:19 2015 1 127.0.0.1 884 /uploads/inittab b _ i a ? ftp 0 * c

Tue Aug 18 20:12:49 2015 1 127.0.0.1 0 /uploads/inittab b _ o a ? ftp 0 * i

Tue Aug 18 20:13:58 2015 1 127.0.0.1 0 /uploads/inittab b _ o a ? ftp 0 * i

Tue Aug 18 20:14:26 2015 1 127.0.0.1 0 /uploads/test.txt b _ o a ? ftp 0 * c

七、用awk命令找出知足條件的日誌

若遇到日誌較多的狀況可經過以下命令解決,暫不深究如下命令,會用即可

[root@ftp ~]# awk '{if(substr($4,1,2)*10000+substr($4,4,2)*100+substr($4,7,2)>=191452&&substr($4,1,2)*10000+substr($4,4,2)*100+substr($4,7,2)<195519)print $0}' /var/log/xferlog


Tue Aug 18 19:14:52 2015 1 127.0.0.1 0 /uploads/test.txt b _ o a ? ftp 0 * c

Tue Aug 18 19:27:54 2015 1 127.0.0.1 884 /uploads/inittab b _ i a ? ftp 0 * c


#此爲一行命令,目的是找出19:14:52—19:55:19的日誌行,注意文中的不等式,要靈活運用

八、vsftpd的安全性

1)使用tcpdump命令監測

[root@ftp ~]# tcpdump -i eth0 -nn -X -vv tcp port 21 and ip host 172.16.0.11

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

2)在windows中的命令提示符進行以下操做

C:\Users\k>ftp 172.16.0.11

鏈接到 172.16.0.11。

220-welcome to ftp.gnnt.com!!!

220

用戶(172.16.0.11:(none)): zj

331 Please specify the password.

密碼:

230 Login successful.

3)監測結果

[root@ftp ~]# tcpdump -i eth0 -nn -X -vv tcp port 21 and ip host 172.16.0.11

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

...........................................................................................

20:54:53.100571 IP (tos 0x0, ttl 128, id 2779, offset 0, flags [DF], proto TCP (6), length 53)

    172.16.0.255.55633 > 172.16.0.11.21: Flags [P.], cksum 0x80ab (correct), seq 10:23, ack 73, win 2030, length 13

0x0000:  4500 0035 0adb 4000 8006 96bd ac10 00ff  E..5..@.........

0x0010:  ac10 000b d951 0015 1f50 ac5e d1db ecce  .....Q...P.^....

0x0020:  5018 07ee 80ab 0000 5041 5353 2031 3233  P.......PASS.123

0x0030:  3435 360d 0a                             456..


...........................................................................................

其中紅色加粗部分爲捕捉到的密碼

九、增強vsftpd的安全性(未完待續)

十、配置文件參數彙總

-------------------------------------------------------------------------------------------

與服務器環境相關的設置值:

banner_file=/etc/vsftpd/welcome.txt

listen=YES

#登陸ftp服務器時顯示的信息,此文件須要本身手動建立,並添加相應的內容

#設置vsftpd的啓動方式爲stand alone(獨立守護進程),另外一種方式爲super daemon(超級守護進程),可在/etc/xinetd.d/目錄下配置一個腳本文件便可讓其成爲瞬時守護進程(用於訪問量較小的FTP服務器)

-------------------------------------------------------------------------------------------

與實體用戶較相關的設置值

chroot_local_user=YES              

chroot_list_enable=YES              

chroot_list_file=/etc/vsftpd/chroot_list  

#將用戶限制在本身的主目錄內,爲了安全最好是YES

#開啓chroot寫入列表的功能

#與上條命令結合使用,在文件裏的用戶將被禁錮

-------------------------------------------------------------------------------------------

與匿名用戶較相關的設置值

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

#給anonymous用戶有上傳文件的權限

#給anonymous用戶有建立文件的權限

#給anonymous用戶有建立文件以外的其它權限,例如刪除文件

-------------------------------------------------------------------------------------------

與系統安全方面的設置值

xferlog_enable=YES

xferlog_file=/var/log/xferlog

#是否開啓傳輸日誌

#傳輸日誌的文件的路徑

-------------------------------------------------------------------------------------------




至此,本文檔已經結束,文檔中不免有不盡人意之處,但願各位可以給出寶貴的建議,謝謝!



紫禁之巔

2015年8月18日 21:12

相關文章
相關標籤/搜索