FTP简介以及主被动模式介绍

FTP协议简介

文件传输协议(英文:File Transfer Protocol,缩写:FTP)是用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式。它属于网络传输协议的应用层。文件传送(file transfer)和文件访问(file access)之间的区别在于:前者由FTP提供,后者由如NFS等应用系统提供

FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样。但是,FTP有着极高的延时,这意味着,从开始请求到第一次接收需求数据之间的时间,会非常长;并且不时的必须执行一些冗长的登录进程。

优点

  • 促进文件的共享(计算机程序或数据)
  • 鼓励间接或者隐式的使用远程计算机
  • 向用户屏蔽不同主机中各种文件存储系统(File system)的细节
  • 可靠和高效的传输数据

缺点

  • 密码和文件内容都使用明文传输,可能发生窃听。
  • 因为必须开放一个随机的端口以创建连接,当防火墙存在时,客户端- 很难过滤处于主动模式下的FTP流量。这个问题,通过使用被动模式的FTP,得到了很大解决。
  • 服务器可能会被告知连接一个第三方计算机的保留端口。
  • 此方式在需要传输文件数量很多的小文件时,性能不好

主动模式和被动模式

主动模式
  1. 客户端打开一个随机的端口(端口号大于1024,在这里,我们称它为x),同时一个FTP进程连接至服务器的21号命令端口。此时,该tcp连接的来源地端口为客户端指定的随机端口x,目的地端口(远程端口)为服务器上的21号端口。
  2. 客户端开始监听端口(x+1),同时向服务器发送一个端口命令(通过服务器的21号命令端口),此命令告诉服务器客户端正在监听的端口号并且已准备好从此端口接收数据。这个端口就是我们所知的数据端口。
  3. 服务器打开20号源端口并且创建和客户端数据端口的连接。此时,来源地的端口为20,远程数据(目的地)端口为(x+1)。
  4. 客户端通过本地的数据端口创建一个和服务器20号端口的连接,然后向服务器发送一个应答,告诉服务器它已经创建好了一个连接
    示意图
    这里写图片描述
被动模式
  1. 客户端打开一个随机的端口(端口号大于1024,在这里,我们称它为x),同时一个FTP进程连接至服务器的21号命令端口。此时,该tcp连接的来源地端口为客户端指定的随机端口x,目的地端口(远程端口)为服务器上的21号端口。
  2. 客户端开始监听端口(x+1),同时向服务器发送一个PASV命令。服务端则发送一个大于1024号端口的一个随机端口Y。告知客户端可以用这个端口进行数据传输
  3. 客户端初始化一个从自己的数据端口到服务器端指定的数据端口的数据连接
  4. 服务端通过本地的数据端口创建一个和客户端的连接,然后向客户端发送一个应答,告诉客户端它已经创建好了一个连接
    示意图
    这里写图片描述

安装

ftp是一个协议,有许多的具体的实现,在linux比较常用的一个就是vsftpd
安装也比较简单

yum install -y vsftpd

看一下目录结构

[root@localhost vsftpd]# tree /etc/vsftpd/
/etc/vsftpd/
├── ftpusers   ##列在此文件中的用户 均禁止使用ftp服务
├── user_list  ##访问控制名单 可以做白名单或者黑名单
├── vsftpd.conf ##配置文件
└── vsftpd_conf_migrate.sh

用户类别:

  • 匿名用户:anonymous --> ftp, 家目录为/var/ftp,不需要密码
  • 系统用户: 因为ftp协议是明文传输的,所以账号密码很容易被抓包得到。为了安全,至少要禁止系统用户访问ftp服务
  • 虚拟用户:非系统用户,用户账号非为可登录操作系统的用户账号(非/etc/passwd);

配置文件常用的一些配置项

  • 匿名用户相关:
    • anonymous_enable=YES ##是否允许匿名用户登陆
    • anon_upload_enable=YES ##是否允许匿名用户上传文件
    • anon_mkdir_write_enable=YES##是否允许匿名用户拥有写权限
    • anon_other_write_enable=YES##如果设置为YES,则允许匿名用户执行除上载和创建目录之外的写操作,例如删除和重命名。 通常不建议这样做,但为了完整性而包括在内。
    • anon_umask=077
  • 系统用户相关:
    • local_enable=YES #是否允许本地用户登录
    • write_enable=YES#是否允许本地用户对ftp服务器文件拥有写权限
    • local_umask=022#本地用户 掩码默认077
    • chroot_local_user=YES #禁锢所有本地用户 于其家目录;需要事先去除用户对家目录的写权限;
    • chroot_list_enable=YES#用户登录FTP服务器后是否具有访问自己目录以外的其他文件的权限,设置为YES时,用户被锁定在自己的home目录中,
    • chroot_list_file=/etc/vsftpd/chroot_list #被列入此文件的用户,在登录后将不能切换到自己目录以外的其他目录,从而有利于FTP服务器的安全管理和隐私保护。此文件需自己建立需要事先去除用户对家目录的写权限;
  • 传输日志:
    • xferlog_enable=YES
    • xferlog_file=/var/log/xferlog
    • xferlog_std_format=YES
  • 控制可登录vsftpd服务的用户列表:
    • userlist_enable=YES #启用/etc/vsftpd/user_list文件来控制可登录用户;
    • userlist_deny=
      • YES:意味着此为黑名单;
      • NO:白名单;
  • 上传下载速率:
    • anon_max_rate=0 #匿名用户上传下载的最大速率
    • local_max_rate=0 #本地用户上传下载的最大速率
  • 并发连接数限制:
    • max_clients=2000 #最多允许多少客户端连接
    • max_per_ip=50 #每个ip最多可以建立多少链接

配置文件详解

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=no  #####是否允许匿名用户登录
#
# 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   ###是否允许本地用户对ftp服务器文件拥有写权限,默认允许
#
# 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 ########本地用户 掩码默认077
#
# 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  ####是否允许匿名用户上传文件 需要打开write_enable=yes  默认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     ####是否激活目录欢迎信息功能,当用户首次访问服务器的时候ftp显示欢迎功能  欢迎信息是通过该目录下的.message文件获得的 此文件保存自定义欢迎信息,由用户自己建立
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES  ####是否让系统自动维护和下载日志文件  默认情况该日志文件为/var/log/vsftpd.log也可以通过xferlog_file对日志文件路径进行设定
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES #####是否设定ftp服务器将启用ftp数据端口的连接请求
#
# 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 #####设想要改变的上传文件属主,如果需要输入一个系统用户名可以吧上传的文件都改成root  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#########设定系统维护记录的ftp服务器上传和下载情况的日志文件
#
# 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##是否以表真的xferlog格式书写传输日志文件默为/var/log/xferlog,也可以通过xferlog_file选项对其进行设定   默认no
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600####设置数据传输中断间隔时间,此语句表示空闲的用户会话中断时间为600秒,即当数据传输结束后,用户连接FTP服务器的时间不应超过600秒。可以根据实际情况对该值进行修改
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120####设置数据连接超时时间,该语句表示数据连接超时时间为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####运行vsftpd需要的非特权系统用户,缺省是nobody
#
# 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####是否识别异步ABOR请求。,如果FTP client会下达“async ABOR”这个指令时,这个设定才需要启用,而一般此设定并不安全,所以通常将其取消
#
# 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方式传输数据。默认情况下,服务器会忽略ASCII方式的请求。启用此选项将允许服务器以ASCII方式传输数据不过,这样可能会导致由"SIZE /big/file"方式引起的DoS攻击
#ascii_download_enable=YES
# 此文件需用户自己创建,一行一个email address即可
#
# 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###### 黑名单设置。如果很讨厌某些email address就可以使用此设定来取消他的登录权限,可以将某些特殊的email address抵挡住。
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails# 当上面的deny_email_enable=YES时,可以利用这个设定项来规定哪些邮件地址不可登录vsftpd服务器
#
# 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###### 用户登录FTP服务器后是否具有访问自己目录以外的其他文件的权限,设置为YES时,用户被锁定在自己的home目录中,vsftpd将在下面chroot_list_file选项值的位置寻找chroot_list文件,必须与下面的设置项配合
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list# 被列入此文件的用户,在登录后将不能切换到自己目录以外的其他目录,从而有利于FTP服务器的安全管理和隐私保护。此文件需自己建立
#
# 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#### 是否允许递归查询。默认为关闭,以防止远程用户造成过量的I/O
#
# 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# 是否允许监听。 如果设置为YES,则vsftpd将以独立模式运行,由vsftpd自己监听和处理IPv4端口的连接请求
#
# 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##### 设定是否支持IPV6。如要同时监听IPv4和IPv6端口,则必须运行两套vsftpd,采用两套配置文件,同时确保其中有一个监听选项是被注释掉的

pam_service_name=vsftpd设置PAM外挂模块提供的认证服务所使用的配置文件名,即/etc/pam.d/vsftpd文件
# 此文件中file=/etc/vsftpd/ftpusers字段,说明了PAM模块能抵挡的帐号内容来自文件/etc/vsftpd/ftpusers中
userlist_enable=YES### 是否允许ftpusers文件中的用户登录FTP服务器,默认为NO
# 若此项设为YES,则user_list文件中的用户允许登录FTP服务器
# 而如果同时设置了userlist_deny=YES,则user_list文件为黑名单
tcp_wrappers=YES# 是否使用tcp_wrappers作为主机访问控制方式。
# tcp_wrappers可以实现linux系统中网络服务的基于主机地址的访问控制
# 在/etc目录中的hosts.allow和hosts.deny两个文件用于设置tcp_wrappers的访问控制
# 前者设置允许访问记录,后者设置拒绝访问记录。
# 如想限制某些主机对FTP服务器192.168.57.2的匿名访问,编缉/etc/hosts.allow文件,如在下面增加两行命令:
# vsftpd:192.168.57.1:DENY 和vsftpd:192.168.57.9:DENY
# 表明限制IP为192.168.57.1/192.168.57.9主机访问IP为192.168.57.2的FTP服务器
# 此时FTP服务器虽可以PING通,但无法连接
userlist_deny=NO#### 设置是否阻扯user_list文件中的用户登录FTP服务器,默认为YES
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值