详解vsftpd搭建ftp和ftps

来源:互联网 发布:淘宝怎样延长付款时间 编辑:程序博客网 时间:2024/05/18 07:09

一、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(vsFTPd2.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~]# ftp172.16.2.1
Connectedto 172.16.2.1(172.16.2.1).
220(vsFTPd2.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 Jul10 03:18linux-3.10.tar.xz
226Directory send OK.
服务器端查看本地用户信息
[root@stu2~]# finger gentoo
Login:gentoo                      Name:
Directory:/users/gentoo             Shell: /bin/bash
Lastlogin Sat Aug17 09:54(CST) on pts/1 from 172.16.254.54
Nomail.
NoPlan.
Root用户默认没有访问权限
[root@station92~]# ftp172.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~]# ftp172.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 Aug19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul10 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 Aug19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul10 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~]# ftp172.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~]# ftp172.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 typeis UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/"                //锁定用户的家目录
ftp> bye
221Goodbye.
[root@station92~]# ftp172.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 typeis 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~]# ftp172.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~]# ftp172.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 typeis UNIX.
Usingbinary mode to transfer files.
ftp> pwd
257"/users/centos"
ftp> bye
221Goodbye.
[root@station92~]# ftp172.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 typeis 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~]# ftp172.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~]# ftp172.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 typeis 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 Jul10 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 Aug19 00:16fstab
-rw-rw-r--    1 500     500      73175088 Jul10 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 root4096 Aug 1801:58pub
[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~]# lftp172.16.2.1
lftp172.16.2.1:~> ls
drwxr-xr-x    2 0       0            4096 Aug17 17:58pub
drwxrwxr-x    2 0       0            4096 Aug19 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~]# lftp172.16.2.1
lftp172.16.2.1:~> ls
drwxr-xr-x    2 0       0            4096 Aug17 17:58pub
drwxrwxr-x    2 0       0            4096 Aug19 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 Aug19 03:46fstab
-rw-------    1 14      50            103 Aug19 08:16issue
lftp172.16.2.1:/upload> mkdir test
mkdir ok,`test' created
lftp172.16.2.1:/upload> ls
-rw-------    1 14      50            921 Aug19 03:46fstab
-rw-------    1 14      50            103 Aug19 08:16issue
drwx------    2 14      50           4096 Aug19 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 enteris what iscalled a Distinguished Name or a DN.
There arequite a few fields but you can leave some blank
For somefields there will be adefault value,
If youenter '.', the field will be left blank.
-----
CountryName (2letter 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 enteris what iscalled a Distinguished Name or a DN.
There arequite a few fields but you can leave some blank
For somefields there will be adefault value,
If youenter '.', the field will be left blank.
-----
CountryName (2letter 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 []:caadmin@magedu.com
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

原创粉丝点击