詳解vsftpd搭建ftp和ftps

一、FTP的作用和工作原理

FTP(FileTransfer Protocol)是文件傳輸協議的簡稱

FTP的作用

FTP的主要作用,就是讓用戶連接上一個遠程計算機(這些計算機上運行着FTP服務器程序)查看遠程計算機有哪些文件,然後把文件從遠程計算機上拷貝到本地計算機,或把本地計算機的文件送到遠程計算機去

FTP服務器的工作原理

簡單地說,支持ftp協議的服務器就是ftp服務器,ftp協議的連接方式有兩種,一種是命令連接,一種是數據連接,而ftp的數據連接方式也有兩種,一種是主動模式,一種是被動模式

其主動模式的工作原理

1.客戶端對服務器發起請求,連接的是服務器的21號端口,客戶端的端口號N是大於1024的隨機端口

2.服務器的21號端口給予客戶端響應數據流

3.服務器打開20號端口去連接客戶端的N+1的端口

4.客戶端給予響應,數據開始傳輸

被動模式的工作原理

1.客戶端對服務器發起的請求連接是服務器的21號端口,客戶端的端口號N是大於1024的隨機端口

2.服務器的21號端口給予客戶端響應

3.服務器打開一個大於1024的隨機端口,客戶端使用N+1端口號去連接服務器打開的端口

4.服務器給予響應,於是數據開始傳輸

需要注意的是:客戶端如何連接服務器端的這個隨機端口的呢?在命令連接階段,服務器會傳輸172.16.2.1.113.26的字符串過去,前四個標識的是服務器的IP地址,而隨機端口是通過後面兩個字符計算得出的,計算法則爲第一個乘以256加上第二個數,即客戶端連接服務器端的這個隨機端口號是113*256+26

二、安裝ftp軟件包:

配置好yum源後通過yum –y install vsftpd進行安裝

[root@stu2~]# rpm -ql vsftpd     //查看軟件包生成了那些文件

列出幾個較爲重要的

1
2
3
4
5
6
7
8
9
10
/etc/logrotate.d/vsftpd       //配置日誌回滾的文件
/etc/pam.d/vsftpd                   //定義vsftpd是如何認證用戶的,默認情況vsftpd支持使用匿名用戶和本地用戶
/etc/rc.d/init.d/vsftpd              //服務啓動腳本
/etc/vsftpd
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list
/etc/vsftpd/vsftpd.conf       //服務的主配置文件
/etc/vsftpd/vsftpd_conf_migrate.sh
/usr/sbin/vsftpd                  //服務器端程序
/var/ftp                //ftp的默認家目錄


ftp協議響應碼

1xx:服務器信息

2xx:正確響應信息

3xx:正常響應,某操作過程尚未完成,需進一步補充完成;

4xx:客戶端錯誤;

5xx:服務器端錯誤;


vsftpd的用戶類型:

需要注意的是,ftp服務器端文件路徑是用戶的家目錄,查看用戶家目錄的相關信息可以通過finger命令來查看

1.匿名用戶:事實上是服務器端自動映射的一個系統用戶

客戶端匿名連接:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@station92~]# ftp172.16.2.1 //連接測試
Connectedto 172.16.2.1 (172.16.2.1).
220(vsFTPd 2.2.2)//220是協議響應碼,表示正常響應
Name(172.16.2.1:root): ftp//表示匿名訪問
331Please specify the password.
Password:                  //匿名用戶登錄密碼爲空
230Login successful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"    //匿名用戶默認被鎖定在“/“目錄下,而本地用戶沒有被鎖定,因此看以看所有的文件
 [root@stu2 ~]# finger ftp
Login:ftp                      Name: FTP User
Directory:/var/ftp                     Shell: /sbin/nologin
Neverlogged in.
Nomail.
NoPlan.
其中Directory: /var/ftp表示用戶家目錄路徑信息

2.本地用戶:即/etc/passwd中的用戶,默認root和id號小於500的用戶都禁止訪問ftp

本地用戶連接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220(vsFTPd 2.2.2)
Name(172.16.2.1:root): gentoo
331Please specify the password.
Password:
230Login successful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp>pwd
257"/users/gentoo"
ftp>ls
227Entering Passive Mode (172,16,2,1,236,228).
150Here comes the directory listing.
-rw-rw-r--    1 500     500      73175088 Jul 10 03:18linux-3.10.tar.xz
226Directory send OK.
服務器端查看本地用戶信息
[root@stu2~]# finger gentoo
Login:gentoo                      Name:
Directory:/users/gentoo             Shell: /bin/bash
Lastlogin Sat Aug 17 09:54 (CST) on pts/1 from 172.16.254.54
Nomail.
NoPlan.
Root用戶默認沒有訪問權限
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): root
530 Permissiondenied.
Loginfailed.

3.虛擬用戶:事實上服務器端自動映射的一個系統用戶,多個虛擬用戶同時被映射爲一個系統用戶,但不同的虛擬用戶可以具有不同的訪問權限

三、vsftpd的工作特性

在/etc/vsftpd/vsftpd.conf中定義了vsftpd的工作特性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@stu2~]# grep -v "^#" /etc/vsftpd/vsftpd.conf | grep -v "^$"
anonymous_enable=YES//允許匿名用戶訪問,若禁止使用NO
local_enable=YES//允許本地用戶訪問,若禁止則使用NO
write_enable=YES//表示是否允許本地用戶有上傳權限的,YES表示可以,NO表示禁止,也取決於客戶端連接時使用的客戶端工具
local_umask=022//設置本地用戶上傳建立文件時的權限掩碼
dirmessage_enable=YES//用戶切換進入目錄時顯示.message(如果存在)文件的內容
xferlog_enable=YES//是否開啓傳輸日誌的
connect_from_port_20=YES//連接控制端口爲20
xferlog_std_format=YES//啓動標準xferlog的日誌格式,若禁用此項,將使用vsftpd自己的日誌格式
listen=YES//是否以獨立運行的方式監聽服務
pam_service_name=vsftpd//設root
userlist_enable=YES//表明啓動本地用戶
tcp_wrappers=YES//是否開啓tcp_wrappers主機訪問控制
除此之外還有註釋掉的,有些可以啓動的具體爲:
#anon_upload_enable=YES#是否匿名用戶上傳文件
#anon_mkdir_write_enable=YES#匿名用戶具有創建目錄的權限
可以加一條
Anon_other_write_enable=YES#其他權限,可是設置文件的屬主屬組,刪除等操作
#xferlog_file=/var/log/xferlog#記錄傳輸日誌內容的
#idle_session_timeout=600#命令連接的超時時間
#data_connection_timeout=120#數據連接的超時時間
#chroot_local_user=YES
#chroot_list_enable=YES
#chroot_list_file=/etc/vsftpd/chroot_list

chroot: 禁錮用戶於其家目錄中,實現方式有兩種:

    1、所有的本地用戶都被鎖定在家目錄下了

    chroot_local_user=YES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): gentoo
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"            //本地用戶被鎖定在家目錄下
ftp> ls
227Entering Passive Mode (172,16,2,1,195,144).
150 Herecomes the directory listing.
-rw-r--r--    1 500     500           921 Aug 19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul 10 03:18linux-3.10.tar.xz
226Directory send OK.
ftp> cd/
250Directory successfully changed.
ftp> ls
227Entering Passive Mode (172,16,2,1,231,75).
150 Herecomes the directory listing.
-rw-r--r--    1 500     500           921 Aug 19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul 10 03:18linux-3.10.tar.xz
226Directory send OK.

2、禁錮/etc/vsftpd/chroot_list中的指定用戶,黑名單

[root@stu2vsftpd]# touch chroot_list

[root@stu2vsftpd]# vim chroot_list

在chroot_list中寫上centos 和gentoo兩個用戶,用戶hailian沒寫入,進行驗證

            chroot_list_enable=YES

            chroot_list_file=/etc/vsftpd/chroot_list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): centos
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"                 //鎖定用戶的家目錄
ftp> bye
221Goodbye.
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): gentoo
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"                 //鎖定用戶的家目錄
ftp> bye
221Goodbye.
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): hailian
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/home/hailian"      //沒有鎖定

3、不禁錮/etc/vsftpd/chroot_list中的指定用戶,白名單

            chroot_local_user=YES            

            chroot_list_enable=YES

            chroot_list_file=/etc/vsftpd/chroot_list

進行驗證:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): gentoo
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/users/gentoo"
ftp> bye
221Goodbye.
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): centos
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/users/centos"
ftp> bye
221Goodbye.
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): hailian
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"

user_list:定義ftp用戶白名單和黑名單:

        1、白名單:

            userlist_enable=YES

            userlist_deny=NO

        2、黑名單:

            userlist_enable=YES

            userlist_deny=YES


傳輸速率限定,默認單位爲字節:

        anon_max_rate=10240#定義匿名用戶的最大傳輸速率,單位爲字節

        local_max_rate=10240#限定本地用戶最大的下載速度爲10KB/s

連接數限定:

        max_clients=100

        max_per_ip=2

修改配置文件後要重新加載服務

servicevsftpd reload

注:

 1)由於此配置文件檢查要求比較嚴格,不可隨意出現任意空白字符,例如write_enable=YES之前不可出現任何空白字符;等號兩側不可出現任何空白字符。

  2)當更改這個文件中的這些權限時一定要確保SElinux是關閉的,不然會被阻止的。

驗證:

在服務器端的主配置文件/etc/vsftpd/vsftpd.conf

write_enable=YES//表示是否允許本地用戶有上傳權限的,YES表示可以,NO表示禁止,也取決於客戶端連接時使用的客戶端工具,匿名用戶不可以上傳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): ftp
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> put/etc/fstab
local:/etc/fstab remote: /etc/fstab
227Entering Passive Mode (172,16,2,1,50,195).
550Permission denied.//匿名用戶不允許有上傳權限
本地用戶:
[root@station92~]# ftp 172.16.2.1
Connectedto 172.16.2.1 (172.16.2.1).
220 (vsFTPd2.2.2)
Name(172.16.2.1:root): gentoo
331 Pleasespecify the password.
Password:
230 Loginsuccessful.
Remotesystem type is UNIX.
Usingbinary mode to transfer files.
ftp> put/etc/fstab
local:/etc/fstab remote: /etc/fstab
227Entering Passive Mode (172,16,2,1,90,137).//ftp工作在被動模式下
553 Couldnot create file.//表明上傳功能沒問題,但無法創建文件
ftp> pwd
257"/users/gentoo"

上述無法上傳文件的原因是因爲客戶端工具問題,用lftp既可以實現上傳功能

1
2
3
4
5
6
7
8
9
[root@station92~]# lftp gentoo@172.16.2.1
Password:
lftpgentoo@172.16.2.1:~> ls    
-rw-rw-r--    1 500     500      73175088 Jul 10 03:18linux-3.10.tar.xz
lftpgentoo@172.16.2.1:~> put /etc/fstab
921 bytestransferred     //實現了上傳功能
lftpgentoo@172.16.2.1:~> ls
-rw-r--r--    1 500     500           921 Aug 19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul 10 03:18linux-3.10.tar.xz


啓動匿名用戶可以上傳文件的,在服務器端的主配置文件/etc/vsftpd/vsftpd.conf中啓用anon_upload_enable=YES

# servicevsftpd reload

用匿名用戶連接服務器時,仍不能上傳文件,原因是匿名用戶在/var/ftp/沒用創建目錄的功能,需要在服務器端進行設置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@stu2~]# cd /var/ftp/
[root@stu2ftp]# ll
total 4
drwxr-xr-x2 root root 4096 Aug 18 01:58 pub
[root@stu2ftp]# mkdir upload
[root@stu2ftp]# setfacl -m u:ftp:rwx upload/
[root@stu2ftp]# getfacl upload/
# file: upload/
# owner:root
# group:root
user::rwx
user:ftp:rwx
group::r-x
mask::rwx
other::r-x

然後在客戶端實現上傳功能

1
2
3
4
5
6
7
8
[root@station92~]# lftp 172.16.2.1
lftp172.16.2.1:~> ls
drwxr-xr-x    2 0       0            4096 Aug 17 17:58 pub
drwxrwxr-x    2 0       0            4096 Aug 19 02:54upload
lftp172.16.2.1:/> cd upload/
lftp172.16.2.1:/upload> ls
lftp172.16.2.1:/upload> put /etc/fstab
921 bytestransferred

需要注意的是vsftpd要求對/var/ftp/這個目錄只有root用戶具有寫權限,其他任何用戶都不允許

啓用#anon_mkdir_write_enable=YES#匿名用戶具有創建目錄的權限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@station92~]# lftp 172.16.2.1
lftp172.16.2.1:~> ls
drwxr-xr-x    2 0       0            4096 Aug 17 17:58 pub
drwxrwxr-x    2 0       0            4096 Aug 19 08:16upload
lftp172.16.2.1:/> cd upload/p
cd: Accessfailed: 550 Failed to change directory. (/upload/p)
lftp172.16.2.1:/> cd upload/
lftp172.16.2.1:/upload> ls
-rw-------    1 14      50            921 Aug 19 03:46fstab
-rw-------    1 14      50            103 Aug 19 08:16issue
lftp172.16.2.1:/upload> mkdir test
mkdir ok,`test' created
lftp172.16.2.1:/upload> ls
-rw-------    1 14      50            921 Aug 19 03:46fstab
-rw-------    1 14      50            103 Aug 19 08:16issue
drwx------    2 14      50           4096 Aug 19 08:21test


ftp的傳輸是明文的,要實現加密功能則需要ftps

四、ftpd+ssl實現安全的ftps

首先要自建CA服務器

其次創建自己的私鑰文件#cd /etc/vsftpd/ssl/

(umask 077;openssl genrsa -out vsftpd.key 2048)

umask 077:用括號括起來,則表示此umsak只對當前子shell有效,如果不用括號,則對當前shell都生效,在此創建文件時屬組和其它用戶將沒有任何權限

      openssl genrsa:生成私鑰的命令關鍵字

      -out:指定文件的路徑

      vsftpd.key:私鑰的名稱,名字可以隨便取,用.key結尾是爲了方便記憶

     2048:指私鑰生成的位,默認是512,必須是2的n次方倍數

利用私鑰文件生成證書籤署請求文件

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

    req:證書請求和證書生成工具的命令關鍵字

   -new:製作證書申請

          -key:指定私鑰文件

          -out:輸出證書請求文件的路徑,以.csr結尾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
You areabout to be asked to enter information that will be incorporated
into yourcertificate request.
What youare about to enter is what is called a Distinguished Name or a DN.
There arequite a few fields but you can leave some blank
For somefields there will be a default value,
If youenter '.', the field will be left blank.
-----
CountryName (2 letter code) [XX]:CN
State orProvince Name (full name) []:henan
LocalityName (eg, city) [Default City]:zhengzhou
OrganizationName (eg, company) [Default Company Ltd]:ftp.magedu.com
OrganizationalUnit Name (eg, section) []:^C
[root@stu2ssl]# openssl req -new -key vsftpd.key -out vsftpd.csr
You areabout to be asked to enter information that will be incorporated
into yourcertificate request.
What youare about to enter is what is called a Distinguished Name or a DN.
There arequite a few fields but you can leave some blank
For somefields there will be a default value,
If youenter '.', the field will be left blank.
-----
CountryName (2 letter code) [XX]:CN
State orProvince Name (full name) []:henan
LocalityName (eg, city) [Default City]:zhengzhou
OrganizationName (eg, company) [Default Company Ltd]:magedu
OrganizationalUnit Name (eg, section) []:tech
Common Name(eg, your name or your server's hostname) []:ftp.magedu.com
EmailAddress []:[email protected]
Pleaseenter the following 'extra' attributes
to be sentwith your certificate request
A challengepassword []:
An optionalcompany name []:
[root@stu2ssl]# ls
vsftpd.csr  vsftpd.key

有ca頒發ftp證書

     opensslca :頒發CA證書的命令關鍵字

     -in:指定證書籤署請求文件

     -out:輸出頒發證書的文件

     -days:限定證書的有效期,3656天


修改vsftpd的配置文件讓其支持ssl功能,即在/etc/vsftpd/vsftpd.conf最後添上如下內容

ssl_enable=YES

ssl_tlsv1=YES

ssl_sslv2=YES

ssl_sslv3=YES

allow_anon_ssl=NO

force_local_data_ssl=YES

force_local_logins_ssl=YES

rsa_cert_file=/etc/vsftpd/ssl/vsftpd.crt

rsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key

客戶端軟件用FlashF軟件測試

總結:ftp的傳輸方式是明文的,因此在服務器上通過抓包工具,可以獲取用戶的密碼。命令爲:# tcpdump -i eth0 -XX port 21這樣對服務器是一個很大的安全隱患,因此基於ftps搭建的服務器是非常有必要的,上述講解不足之處還請大家多多提醒,互相幫助,共同進步!




本文出自 “時光的印記” 博客,請務必保留此出處http://lanlian.blog.51cto.com/6790106/1279455

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