vsftpd


FTP(File Transfer Protocol,文件传输协议),是一种应用层协议,可以实现很好的实现跨平台,但是无法实现一些其他的功能,像如文件系统挂载等功能。NFS(Network File System,网路文件系统)是工作在内核模式下的,因此不能很好的实现跨平台,一般只能是Linux主机或者Unix主机之间,但可以实现挂载使用等功能。SMB(Service Message Block,服务消息块协议),能够实现Windows和Linux主机之间的文件共享服务,可实现跨平台,在Linux上实现了CIFS(Common Internet File System)协议。

一、FTP协议

1、FTP原理知识

是一种C/S架构,基于套接字通信,用来在两台机器之间相互传输文件。FTP协议用到2种tcp连接:一是命令连接,用于客户端和服务端之间传递命令,监听在tcp/21端口;另一个是数据传输连接,用来传输数据,监听的端口是随机的。

2、FTP的被动模式、主动模式

主动模式的原理如下图:

wKioL1cR6TfxXuaTAACYV1oo8oo227.jpg


主动模式存在的问题是,在客户端一般都会有防火墙的设置,当服务端与客户端数据进行数据通信时,客户端的防火墙会将服务端的端口挡在外面。此时,通信就会受阻。因此,被动模式就产生了。



被动模式的原理图如下:

wKioL1cR6YnT5YM1AADqIaBXT00789.jpg



被动模式也会存在防火墙的问题,客户端与服务端传输数据时,在服务端也会有防火墙,但在服务端的防火墙有连接追踪的功能,解决了防火墙的问题。因此,一般使用被动模式比较多。


3、FTP的用户认证

FTP支持系统用户,匿名用户,和虚拟用户三种用户认证。

匿名用户:登陆用户名是anonymous,没有密码

系统用户:是FTP服务器端的本地用户和对应的密码,默认访问的是用户家目录

虚拟用户:仅用于访问服务器中特定的资源,常见的虚拟用户认证的方式有使用文件认证或使用数据库进行认证。最终也会将这些虚拟用户同一映射为一个系统用户,访问的默认目录就是这个系统用户的家目录。

4、常见的状态信息码

1**:提示信息

2**:成功执行的状态码

3**:需要进一步提供补充类的信息码,例如在输入用户账号信息后出现此状态,提示继续输入密码

4**:客户端类的错误

5**:服务端错误

5、常见的实现FTP协议的工具

服务端:

Linux端:wu-ftpd,pureftp,vsftpd(Centos 6上默认提供的)

windows端:ServU,FileZilla-Server

客户端工具:

Linux操作系统:ftp,lftp,lftpget,wget,cul,gftp等

windows操作系统:FileZilla

6、vsftpd

在CentOS上默认提供的是vsftpd(Very Secure FTP),以安全著称。

用户认证配置文件:/etc/pam.d/vsftpd      
服务脚本:/etc/rc.d/init.d/vsftpd      
配置文件目录:/etc/vsftpd       
主配置文件:vsftpd.conf       
匿名用户(映射为ftp用户)共享资源位置:/var/ftp       
系统用户通过ftp访问的资源的位置:用户自己的家目录       
虚拟用户通过ftp访问的资源的位置:给虚拟用户指定的映射成为的系统用户的家目录


Vsftpd

[root@centos ~]# rpm -ql vsftpd

/etc/logrotate.d/vsftpd    日志轮训备份配置文件

/etc/pam.d/vsftpd        基于pam.d 用户认证配置文件

/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      主程序

 

匿名用户(映射为ftp用户)共享位置为:/var/ftp

系统用户通过ftp访问时资源位置:用户自己的家目录

虚拟用户ftp访问时资源位置:给虚拟用户指定的映射成为的系统用户的家目录

 

/etc/vsftpd/vsftpd.conf配置文件详解:

 

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).

anonymous_enable=YES  是否允许匿名登录

#

# Uncomment this to allow local users to log in.

local_enable=YES  系统用户登录,访问到自己的家目录,很危险需要锁在自己的家目录

#

# Uncomment this to enable any form of FTP write command.

write_enable=YES 本地用户是否有写权限  

#

# Default umask for local users is 077. You may wish to change this to 022,

# if your users expect that (022 is used by most other ftpd's)

local_umask=022 限制本地用户上传的文件的权限

#

# Uncomment this to allow the anonymous FTP user to upload files. This only

# has an effect if the above global write enable is activated. Also, you will

# obviously need to create a directory writable by the FTP user.

#anon_upload_enable=YES  是否允许匿名用户上传。

#

# Uncomment this if you want the anonymous FTP user to be able to create

# new directories.

#anon_mkdir_write_enable=YES  是否允许匿名用户建立目录

#

# Activate directory messages - messages given to remote users when they

# go into a certain directory.

dirmessage_enable=YES         就是警告信息

#

# The target log file can be vsftpd_log_file or xferlog_file.

# This depends on setting xferlog_std_format parameter

xferlog_enable=YES    是否开启日志功能

#

# Make sure PORT transfer connections originate from port 20 (ftp-data).

connect_from_port_20=YES

#

# If you want, you can arrange for uploaded anonymous files to be owned by

# a different user. Note! Using "root" for uploaded files is not

# recommended!

#chown_uploads=YES   是否允许把用户上传的文件属组改变

#chown_username=whoever    把用户上传文件属组改变为指定的文件属组。例如改变成whoever

#

# The name of log file when xferlog_enable=YES and xferlog_std_format=YES

# WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log

#xferlog_file=/var/log/xferlog  日志文件格式存在在那个地方

#

# Switches between logging into vsftpd_log_file and xferlog_file files.

# NO writes to vsftpd_log_file, YES to xferlog_file

xferlog_std_format=YES     日志文件格式

#

# You may change the default value for timing out an idle session.

#idle_session_timeout=600    超时时长

#

# You may change the default value for timing out a data connection.

#data_connection_timeout=120   数据连接时超时时长

#

# It is recommended that you define on your system a unique user which the

# ftp server can use as a totally isolated and unprivileged user.

#nopriv_user=ftpsecure

#

# Enable this and the server will recognise asynchronous ABOR requests. Not

# recommended for security (the code is non-trivial). Not enabling it,

# however, may confuse older FTP clients.

#async_abor_enable=YES

#

# By default the server will pretend to allow ASCII mode but in fact ignore

# the request. Turn on the below options to have the server actually do ASCII

# mangling on files when in ASCII mode.

# Beware that on some FTP servers, ASCII support allows a denial of service

# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd

# predicted this attack and has always been safe, reporting the size of the

# raw file.

# ASCII mangling is a horrible feature of the protocol.

#ascii_upload_enable=YES

#ascii_download_enable=YES

#

# You may fully customise the login banner string:

#ftpd_banner=Welcome to blah FTP service.  欢迎信息,没多大用

#

# You may specify a file of disallowed anonymous e-mail addresses. Apparently

# useful for combatting certain DoS attacks.

#deny_email_enable=YES

# (default follows)0.-**

#banned_email_file=/etc/vsftpd/banned_emails

#

# You may specify an explicit list of local users to chroot() to their home

# directory. If chroot_local_user is YES, then this list becomes a list of

# users to NOT chroot().

#chroot_local_user=YES   所的本地用户锁定在自己的家目录

#chroot_list_enable=YES   锁定指定用户的在自己的家目录 

# (default follows)

#chroot_list_file=/etc/vsftpd/chroot_list

#

# You may activate the "-R" option to the builtin ls. This is disabled by

# default to avoid remote users being able to cause excessive I/O on large

# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume

# the presence of the "-R" option, so there is a strong case for enabling it.

#ls_recurse_enable=YES

#

# When "listen" directive is enabled, vsftpd runs in standalone mode and

# listens on IPv4 sockets. This directive cannot be used in conjunction

# with the listen_ipv6 directive.

listen=YES

#

# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6

# sockets, you must run two copies of vsftpd with two configuration files.

# Make sure, that one of the listen options is commented !!

#listen_ipv6=YES

 

pam_service_name=vsftpd

userlist_enable=YES 是否启用userlist文件

userlist_deny=YES|NO   YES时为黑名单,NO时为白名单

tcp_wrappers=YES

 

 

 

匿名用户配置:

anonymous_enable=YES         是否启用匿名用户访问。默认为打开。

 

anon_root=/var/ftp             匿名访问FTP的路径,默认为/var/ftp

anon_upload_enable=YES        是否打开匿名用户上传。默认关闭。

anon_mkdir_write_enable=YES    是否打开匿名用户建立目录,默认关闭

anon_other_write_enable=YES     是否允许匿名用的其他操作。比如说删出。

anon_max_rate=0               匿名用户最大传输速度,0时为无限制

anon_umask=077              匿名上传权限掩码 

 

   

本地用户配置:

 

local_enable=YES  系统用户登录,访问到自己的家目录,很危险需要锁在自己的家目录

local_umask=022 限制本地用户上传的文件的权限

write_enable=YES 本地用户是否有写权限

 

chroot_local_user=YES   所有本地用户锁定在自己的家目录

 

chroot_list_enable=YES   锁定指定用户的在自己的家目录 

chroot_list_file=/etc/vsftpd/chroot_list   锁定指定用户的在自己的家目录的具体配置文件。

local_max_rate=0  本地用户数据传输率

 

 

全局配置:

listen=YES                   是否监听端口

listen_port=21                监听入站的FTP请求的端口号

download-enable=YES         如果设置为NO,将拒绝所有的下载请求

dirmessage_enable=YES         用户进入目录是否显示消息

connetct_from_port_20=YES      使用主动模式连接,启用20端口

 

pasv_enable=YES  是否启用被动模式连接,默认为被动

pasv_max_port=24600   被动模式连接的最大端口号

pasv_min_port=24500   被动模式的最小端口号

 

userlist_enable=YES 是否启用userlist文件

userlist_deny=YES|NO   YES时为黑名单,NO时为白名单

 

 

xferlog_enable=YES        是否开启xferlog 日志功能

xferlog_std_format=YES     日志文件格式

xferlog_file=/var/log/xferlog   日志存在在那个地方

 

 

max_clients=0 最大连接并发数,等0时无限制,

max_per_ip: 每个IP可以同时发起的并发请求数

write_enable=YES 本地用户是否有写权限

 

tcp_wrappers=YES    是否启用tcp_wrappers

 

guest_enable=YES      如果为YES,则所有的非匿名登录都映射为guest_username指定的账户

guest_username=ftp    上面选项为YES时,的账户名

user_config_dir=/etc/vsftd/conf   指定目录,在该目录下可以为用户设置独立的配置文件和选项

 

dual_log_enable=NO     是否开启双日志功能

anonymous_enable=YES   是否开启匿名登录 默认为开启

 

chown_uploads=YES         是否允许把用户上传的文件属组改变

chown_username=whoever    把用户上传文件属组改变为指定的文件属组。例如改变成whoever

 

idle_session_timeout=600       会话时长

data_connection_timeout=120   数据连接操时时长

 

pam_service_name=vsftpd        pam 认证

 

主动连接:

       命令:client: 5000----->server: 21/tcp

       数据:server: 20 /tcp-------> client:5000+

 

被动连接:

       命令:client: 5000------->server21 tcp

                            服务器告诉客户端口自己的端口。121*256+23.客户端计算后连接这个端口进行数据传输。

       数据:client: 5000+ ------>server:随机端口

 

yum -y install vsftpd mysql-server mysql-devel pam_mysql

 

# cd /lib64/security/   验证模块位置


Vsftpd+mysql+pam.d(单台服务器)

1、事先安装好开发环境和mysql数据库;

# yum -y install mysql-server mysql-devel vsftpd pam_mysql

# yum -y groupinstall "Development Tools" "Development Libraries"

 

2.安装pam_mysql-0.7RC1

# tar zxvf  pam_mysql-0.7RC1.tar.gz

# cd  pam_mysql-0.7RC1

# ./configure --with-mysql=/usr --with-openssl

# make

# make install

 

二、创建虚拟用户账号

 

1.准备数据库及相关表

 

首先请确保mysql服务已经正常启动。而后,按需要建立存储虚拟用户的数据库即可,这里将其创建为vsftpd数据库。

 

mysql> create database vsftpd;

 

mysql> grant select on vsftpd.* to vsftpd@localhost identified by '123456';

mysql> grant select on vsftpd.* to [email protected] identified by '123456';

mysql> flush privileges;

 

mysql> use vsftpd;

mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));

 

2、添加测试的虚拟用户

 

根据需要添加所需要的用户,需要说明的是,这里将其密码采用明文格式存储,原因是pam_mysqlpassword()函数与MySQLpassword()函数可能会有所不同。

 

mysql> INSERT INTO users (name,password) VALUES ('tom',tompassword);

mysql> INSERT INTO users (name,password) VALUES ('jack','jackpassword');

 

三、配置vsftpd

1.建立pam认证所需文件

 

#vi /etc/pam.d/vsftpd.mysql

添加如下两行

auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0

 

account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0

2.修改vsftpd的配置文件,使其适应mysql认证

 

建立虚拟用户映射的系统用户及对应的目录

#useradd -s /sbin/nologin -d /var/ftproot vuser

#chmod go+rx /var/ftproot

 

请确保/etc/vsftpd.conf中已经启用了以下选项

anonymous_enable=YES

local_enable=YES

write_enable=YES

anon_upload_enable=NO

anon_mkdir_write_enable=NO

chroot_local_user=YES

 

而后添加以下选项

guest_enable=YES

guest_username=vuser

 

并确保pam_service_name选项的值如下所示

pam_service_name=vsftpd.mysql

 

 

四、启动vsftpd服务

 

# service vsftpd start

# chkconfig vsftpd on

 

查看端口开启情况

 

# netstat -tnlp |grep :21

tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      23286/vsftpd 

 

spacer.gif 

基于mysql控制vsftp的用户认证机制(两台服务器)

第一mysql准备工作:假如:172.16.10.109mysql 数据库:

 

1建立vsftp库:

mysql> create database vsftpd;

 

2给来访vsftp电脑授权

mysql> grant select on vsftpd.* to vsftpd@172.16.10.107 identified by '123456';

MariaDB [vsftpd]> FLUSH PRIVILEGES;

 

3:创建用户表:

mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));

 

4:添加测试的虚拟用户

 

mysql> INSERT INTO users (name,password) VALUES ('tom',password('tompassword'));

mysql> INSERT INTO users (name,password) VALUES ('jack',password('jackpassword'));

 

ftp服务连接mysql数据库服务器,看连接的上不,注意防火墙和SElinux

# mysql -uvsftpd -h172.16.10.109 -p 

 

第二配置vsftpd,服务器IP172.16.10.107

1:建立pam认证的文件#vi /etc/pam.d/vsftpd.mysql

添加如下两行

auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

 

account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

 

2.修改vsftpd的配置文件,使其适应mysql认证

 

建立虚拟用户映射的系统用户及对应的目录

#useradd -s /sbin/nologin -d /var/ftproot vuser  

#chmod go+rx /var/ftproot

 

请确保/etc/vsftpd.conf中已经启用了以下选项

anonymous_enable=YES

local_enable=YES

write_enable=YES

anon_upload_enable=NO

anon_mkdir_write_enable=NO

chroot_local_user=YES

 

而后添加以下选项

guest_enable=YES

guest_username=vuser

 

并确保pam_service_name选项的值如下所示

pam_service_name=vsftpd.mysql  

 

 

四、启动vsftpd服务

 

# service vsftpd start

# chkconfig vsftpd on

 

查看端口开启情况

 

# netstat -tnlp |grep :21

tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      23286/vsftpd 

 

使用虚拟用户登录,验正配置结果,默认为只读访问。

spacer.gif 

 

五、配置虚拟用户具有不同的访问权限 

vsftpd可以在配置文件目录中为每个用户提供单独的配置文件以定义其ftp服务访问权限,每个虚拟用户的配置文件名同虚拟用户的用户名。配置文件目录可以是任意未使用目录,只需要在vsftpd.conf指定其路径及名称即可。

 

1、配置vsftpd为虚拟用户使用配置文件目录

#  vim vsftpd.conf

添加如下选项

user_config_dir=/etc/vsftpd/vusers

 

2、创建所需要目录,并为虚拟用户提供配置文件

# mkdir /etc/vsftpd/vusers

# cd /etc/vsftpd/vusers

# touch tom jerry

 

3、配置虚拟用户tom的访问权限:

anon_upload_enable=YES  可上传

anon_mkdir_write_enable=YES 可以建立目录

anon_other_write_enable=YES 其他所有权限

   

   jack只能上传

   anon_upload_enable=YES

 

ftp连接后的操作命令:

  mkdir 建立目录  

  rmdir 删除目录

  del  删除文件

  put 将本地文件上传到主机

  get 下载

 

 


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