FTP service construction (ftpd, pure-ftpd, vsftpd)

1 Introduction:

FTP(File Transfer Protocol)文件传输协议,用于控制网络文件双向

2. Method:

FTP支持两种模式:Standard (PORT方式,主动方式),Passive (PASV,被动方式)。

2.1 Port mode

FTP 客户端首先和服务器的TCP 21端口建立连接,用来发送命令,客户端需要接收数据的时候在这个通道上发送PORT命令。PORT命令包含了客户端用什么端口接收数据。在传送数据的时候,服务器端通过自己的TCP 20端口连接至客户端的指定端口发送数据。FTP server必须和客户端建立一个新的连接用来传送数据。

2.2 Passive Mode

建立控制通道和Standard模式类似,但建立连接后发送Pasv命令。服务器收到Pasv命令后,打开一个临时端口(端口号大于1023小于65535)并且通知客户端在这个端口上传送数据的请求,客户端连接FTP服务器此端口,然后FTP服务器将通过这个端口传送数据。
    很多防火墙在设置的时候都是不允许接受外部发起的连接的,所以许多位于防火墙后或内网的FTP服务器不支持PASV模式,因为客户端无法穿过防火墙打开FTP服务器的高端端口;而许多内网的客户端不能用PORT模式登陆FTP服务器,因为从服务器的TCP 20无法和内部网络的客户端建立一个新的连接,造成无法工作。

The above content is taken from baidu as an understanding, and the code downloaded from ftp written in python will be involved later.

3. Use ftpd to provide ftp service:

3.1 Ubuntu builds ftpd

$ sudo apt update
$ sudo apt install ftpd
$ sudo mkdir -p /home/uftp
$ sudo useradd -d /home/uftp -s /bin/bash uftp
$ sudo passwd uftp
输入新的 UNIX 密码:<输入密码>
重新输入新的 UNIX 密码:<输入密码>
$ sudo chown -R uftp:uftp uftp/

3.2 Test:
3.2.0 Checking Port Status

$ netstat -tnl

It is normal for the following to appear

Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 :::21                   :::*                    LISTEN     

3.2.1. Write a text file aaa.txt under /home/uftp of FTP-Server
3.2.2. Find another host in the LAN, if there is no ftp client, download it.

$ ftp 192.168.10.123<ftp-server的ip地址>
Connected to 192.168.10.123.
220 192.168.10.123 FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.

Name (192.168.10.123:user):uftp <输入ftp user>
331 Password required for uftp.
Password:<输入密码>

230 User uftp logged in.
Remote system type is UNIX.
Using binary mode to transfer files.

ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for '/bin/ls'.
-rw-rw-r-- 1 uftp uftp  28 2月  26 16:03 aaa

测试下载:
ftp> get aaa.txt 
local: aaa.txt remote: aaa.txt
200 PORT command successful.
150 Opening BINARY mode data connection for 'aaa.txt' (28 bytes).
226 Transfer complete.
28 bytes received in 0.00 secs (497.1591 kB/s)

3.3 Restrict user login

通过设置/etc/ftpuser文件可以限制用户或组登录ftp-server。ftpuser用来定义哪些使用者不可以使用 FTP 登入,只要将使用者登入的账号加入这个档案中,该使用者就不能使用 FTP 登入系统了。
例如:
1)在文件/etc/ftpuser中添加一条uftp再尝试用uftp用户登录,uftp用户登录失败。   
user@localhost:~$ ftp 192.168.10.123
Connected to 192.168.10.123.
220 192.168.10.123 FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
Name (192.168.10.123:user): uftp
331 Password required for uftp.
Password:
530 Login incorrect.
Login failed.
ftp> 

There is information that adding @groupname to /etc/ftpuser can restrict group login to ftp, but it is not realized after trying.

3.4 Prohibit switching directories

在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录(包括ftp根目录的上级目录),这样会对系统带来安全隐患。
ftp> cd ..
250 CWD command successful.
通过配置/etc/ftpchroot文件,就是可以让使用者登入后,只看得到自己的家目录。如果在 ftpchroot 这个文件中添加该使用username或以 "@" 为开头的groupname,使用者登入后,根目录就会是自己的家目录,而无法切换目录到非家目录的其它系统目录中.

Try to find that adding username can take effect, adding @groupname does not take effect

3.5 Set up anonymous user access and download

要使匿名用户可以访问ftp-server必须满足两个条件:
1)名为ftp的用户在ftp-server中存在且有家目录。
2)允许ftp用户访问ftp-server。

3.5.1 First create a user named ftp:

$ sudo mkdir -p /home/ftp
$ sudo useradd -d /home/ftp -s /bin/bash ftp
$ sudo passwd ftp
$ sudo chown -R ftp:ftp ftp/
或:
$ sudo adduser ftp

3.5.2 Then annotate ftp users and anonymous users in the blacklist

$ sudo vim /etc/ftpuser
#ftp
#anonymous

3.5.3 After saving and exiting, try to log in anonymously with another host:

user@localhost:~$ ftp 192.168.10.123

Connected to 192.168.10.123.
220 192.168.10.123 FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
Name (192.168.10.123:user): anonymous <The anonymous user logs in with this username>
331 Guest login ok, send your complete e-mail address as password.

Password:<无密码,直接回车>

230 Guest login ok, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> get ccc
local: ccc remote: ccc
200 PORT command successful.
150 Opening BINARY mode data connection for 'ccc' (8 bytes ).
226 Transfer complete.
8 bytes received in 0.00 secs (289.3518 kB/s)
ftp> 221 Goodbye. But ftp users set chroot by default, and can't ls, and it does not display when opening ftp://192.168.10.123
through browser The content in the directory
man 8 ftpd found the reason as follows:

If the user name is “anonymous” or “ftp”, an anonymous ftp account must be present in the password file (user “ftp”).  In this case the user is allowed to log in by specifying any password (by convention an email address for the user should be used as the password).
In the last case, ftpd takes special measures to restrict the client's access privileges.  The server performs a chroot(2) to the home directory of the “ftp” user.  In order that system security is not breached, it is recommended that the “ftp” subtree be constructed with care, following these rules:...

FTP defaults to the chroot setting for anonymous users, and the chroot setting of the ftpd software also limits the ls function. Use pure-ftpd instead to avoid

4. Use vsftpd to provide ftp services

4.1 Ubuntu configure vsftpd
4.1.1 Install software

$ sudo apt-get install vsftpd
$ netstat -tnl|grep :21
tcp6       0      0 0.0.0.0:21              0.0.0.0:*               LISTEN
##这里现在看是ipv6的在监控,修改配置文件时要把ipv6监听的关了

4.1.2 Modify the configuration file

$ sudo vim /etc/vsftpd.conf
# 监听设置:
listen=YES            #服务器监听 如果使用xinetd来控制ftp选择NO,否则YES
listen_ipv6=NO        #YES-->NO 关闭ipv的监听不然重启会报错

# 本地用户配置:
local_enable=YES      #YES,则允许本地系统用户访问;NO,拒绝本地用户帐号访问
write_enable=YES      #允许上传文件,不开启会报 550 permission denied
local_umask=022       #创建文件和目录的权限:777-022=755

#用户访问路径限制:
#chroot_local_user=YES    # 用于指定chroot_list_file中的用户能否切换到上级目录。默认值为NO。  
#chroot_list_enable=YES   # 设置是否启用chroot_list_file配置项指定的用户列表文件。默认值为NO。  
#chroot_list_file=/etc/vsftpd.chroot_list
#禁用的列表名单,格式为一行一个用户,用于指定用户列表文件,该文件用于控制哪些用户可以切换到用户家目录的上级目录。
#以下是几种组合:
#1)chroot_local_user=YES、chroot_list_enable=YES、chroot_list_file=/etc/vsftpd.chroot_list
#  在/etc/vsftpd.chroot_list文件中列出的用户,可以切换到其他目录;未列出的用户,不能切换到其他目录
#2)chroot_local_user=NO、chroot_list_enable=YES、chroot_list_file=/etc/vsftpd.chroot_list
#  在/etc/vsftpd.chroot_list文件中列出的用户,不能切换到其他目录;未列出的用户,可以切换到其他目录。
#3)chroot_local_user=YES、chroot_list_enable=NO
#  所有的用户均不能切换到其他目录
#4)chroot_local_user=NO、chroot_list_enable=NO
#  所有的用户均能切换到其他目录

#匿名用户设置:
anonymous_enable=NO            # 匿名访问许可:YES,允许;NO:拒绝
#anon_upload_enable=YES        # 匿名上传许可,默认:NO  
#anon_mkdir_write_enable=YES   # 匿名创建目录许可:NO

#其他设置:
dirmessage_enable=YES         # 进入目录时,是否显示目录说明文件, 需要手工创建.message文件
# message_file=.message         # 设置访问一个目录时获得的目录信息文件的文件名,默认是.message
xferlog_enable=YES            # 是否记录ftp传输过程
xferlog_file=/var/log/vsftpd.log # ftp传输日志的路径和名字
xferlog_std_format=YES        # 是否使用标准的ftp xferlog模式
connect_from_port_20=YES      # 启用20号端口作为数据传送的端口
ftpd_banner=Welcome to FTP Server.    #FTP用户登入时显示的信息
secure_chroot_dir=/var/run/vsftpd/empty  #该选项必须指定一个空的资料夹且任何登入者不能有写权限
pam_service_name=vsftpd                  #这个字符串是PAM服务vsftpd将使用的名称。必须启用
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem  
#此选项指定用于SSL的RSA证书的位置 加密连接。必须开启
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO                 #禁止SSL
use_localtime=YES            #如果启用,vsftpd将显示时间的目录清单。在当地时区。默认是显示GMT。

#网络设置:
#idle_session_timeout=600          # 设置session超时时间   
#data_connection_timeout=120       # 设置数据传输超时时间   
#max_clients=50                    # 用户最大连接数 默认是0不限止   
#max_per_ip=5                      # 每个IP地址最大连接数   
#anon_max_rate=102400              # 匿名的下载速度 KB   
#local_max_rate=102400             # 普通用户的下载速度 KB   

More references: http://www.cnblogs.com/mrcln/p/6189665.html

The simplest configuration file (no anonymous, no directory restriction):

cat /etc/vsftpd.conf |grep -v ^# |grep -v ^$
listen=YES
listen_ipv6=NO
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/vsftpd.log
ftpd_banner=Welcome to TS FTP service.
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO

4.1.3 Restart the service

$ sudo vim /etc/init.d/vsftp restart

4.2 Set user access path restrictions:

在上文中已经详细介绍了,根据上文描述,我们设置为:允许列表中用户切出,禁止其他用户跳出到上级目录中:
$ sudo vim /etc/vsftpd.conf
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list  

$ sudo vim /etc/vsftpd.chroot_list
写入:user,并保存。

$ sudo /etc/init.d/vsftp restart
# 这时除user外的其他用户都不能跳出自己的家目录。

Try to find that the chrooted user cannot directly access ftp, the display is as follows:

500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
ftp >

The reason is:

从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。
要修复这个错误,可以用命令chmod a-w /home/user去除用户主目录的写权限,注意把目录替换成你自己的。或者你可以在vsftpd的配置文件/etc/vsftp.conf中增加下列两项中的一项再重启服务。
allow_writeable_chroot=YES    #根目录可写

4.3 Allow Anonymity

$ sudo vim /etc/vsftp.conf
anonymous_enable=YES         #允许匿名访问
anon_upload_enable=NO        #禁止匿名上传  
anon_mkdir_write_enable=NO   #禁止匿名创建目录
修改完重启服务4.2的问题一样需要设置ftp的目录没有写权限:
sudo chmod a-w /home/ftp/
## 前提是已经有名为ftp的系统用户,且有家目录。没有需要手动创建。

5. Use pure-ftpd to provide ftp services

5.1 ubuntu deployment pure-ftpd

$ sudo apt-get install pure-ftpd
安装软件后即可使用系统内的用户和密码登录(除/etc/ftpuser中涉及的用户外)

5.2 Configure anonymous user to log in to ftp
5.2.1 Create ftp user (must be named ftp)

$ sudo mkdir -p /home/ftp
$ sudo useradd -d /home/ftp -s /bin/bash ftp
$ sudo passwd ftp
$ sudo chown -R ftp:ftp ftp/

5.2.2 Modify the configuration file and restart pure-ftpd

$ sudo vim /etc/pure-ftpd/conf/NoAnonymous
将yes改为no
保存并退出
$ sudo /etc/init.d/pure-ftpd restart

Pure-ftpd anonymous access will also perform chroot settings, it is not allowed to cut out the ftp user's home directory, but ls
can be used: the terminal can ls to view the files in the directory, and the page can display the files in the path.
5.3 Restricting the path (chroot) that users can access

$ sudo vim /etc/pure-ftpd/conf/ChrootEveryone
写入:yes并保存
$ sudo /etc/init.d/pure-ftpd restart

5.4 For the command line parameters and configuration methods of pure-ftpd, refer to the following link:
http://www.jb51.net/os/Ubuntu/347282.html
and: man pure-ftpd, man pure-ftpd-wrapper
CentOS configuration can be Reference:
https://www.cnblogs.com/gleaners/p/5725446.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325810598&siteId=291194637