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 下載

 

 


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