zoukankan      html  css  js  c++  java
  • 11 使用Vsftpd服务传输文件

    第11章 使用Vsftpd服务传输文件

    11.1 文件传输协议

    FTP是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20、21号端口,其中端口20(数据端口)用于进行数据传输,端口21(命令端口)用于接受客户端发出的相关FTP命令与参数。FTP服务器普遍部署于内网中,具有容易搭建、方便管理的特点.

    [root@linuxprobe ~]# yum install vsftpd
    
    [root@linuxprobe ~]# iptables -F
    [root@linuxprobe ~]# service iptables save
    iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
    
    [root@linuxprobe ~]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak
    [root@linuxprobe ~]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak > /etc/vsftpd/vsftpd.conf
    [root@linuxprobe ~]# cat /etc/vsftpd/vsftpd.conf
    anonymous_enable=YES
    local_enable=YES
    write_enable=YES
    local_umask=022
    dirmessage_enable=YES
    xferlog_enable=YES
    connect_from_port_20=YES
    xferlog_std_format=YES
    listen=NO
    listen_ipv6=YES
    pam_service_name=vsftpd
    userlist_enable=YES
    tcp_wrappers=YES
    
    11.2 Vsftpd服务程序
    [root@linuxprobe ~]# yum install ftp
    
    11.2.1 匿名访问模式

    表11-2 可以向匿名用户开放的权限参数以及作用

    参数 作用
    anonymous_enable=YES 允许匿名访问模式
    anon_umask=022 匿名用户上传文件的umask值
    anon_upload_enable=YES 允许匿名用户上传文件
    anon_mkdir_write_enable=YES 允许匿名用户创建目录
    anon_other_write_enable=YES 允许匿名用户修改目录名称或删除目录
    [root@linuxprobe ~]# vim /etc/vsftpd/vsftpd.conf
    1 anonymous_enable=YES
    2 anon_umask=022
    3 anon_upload_enable=YES
    4 anon_mkdir_write_enable=YES
    5 anon_other_write_enable=YES
    6 local_enable=YES
    7 write_enable=YES
    8 local_umask=022
    9 dirmessage_enable=YES
    10 xferlog_enable=YES
    11 connect_from_port_20=YES
    12 xferlog_std_format=YES
    13 listen=NO
    14 listen_ipv6=YES
    15 pam_service_name=vsftpd
    16 userlist_enable=YES
    17 tcp_wrappers=YES
    
    [root@linuxprobe ~]# systemctl restart vsftpd
    [root@linuxprobe ~]# systemctl enable vsftpd
     ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service
    
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): anonymous
    331 Please specify the password.
    Password:此处敲击回车即可
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> cd pub
    250 Directory successfully changed.
    ftp> mkdir files
    550 Permission denied.
    
    [root@linuxprobe ~]# ls -ld /var/ftp/pub
    drwxr-xr-x. 3 root root 16 Jul 13 14:38 /var/ftp/pub
    [root@linuxprobe ~]# chown -Rf ftp /var/ftp/pub
    [root@linuxprobe ~]# ls -ld /var/ftp/pub
    drwxr-xr-x. 3 ftp root 16 Jul 13 14:38 /var/ftp/pub
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): anonymous
    331 Please specify the password.
    Password:此处敲击回车即可
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> cd pub
    250 Directory successfully changed.
    ftp> mkdir files
    550 Create directory operation failed.
    
    [root@linuxprobe ~]# getsebool -a | grep ftp
    ftp_home_dir --> off
    ftpd_anon_write --> off
    ftpd_connect_all_unreserved --> off
    ftpd_connect_db --> off
    ftpd_full_access --> off
    ftpd_use_cifs --> off
    ftpd_use_fusefs --> off
    ftpd_use_nfs --> off
    ftpd_use_passive_mode --> off
    httpd_can_connect_ftp --> off
    httpd_enable_ftp_server --> off
    sftpd_anon_write --> off
    sftpd_enable_homedirs --> off
    sftpd_full_access --> off
    sftpd_write_ssh_home --> off
    tftp_anon_write --> off
    tftp_home_dir --> off
    

    我们可以根据经验(需要长期培养,别无它法)和策略的名称判断出是ftpd_full_access--> off策略规则导致了操作失败。接下来修改该策略规则,并且在设置时使用-P参数让修改过的策略永久生效,确保在服务器重启后依然能够顺利写入文件。

    [root@linuxprobe ~]# setsebool -P ftpd_full_access=on
    
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): anonymous
    331 Please specify the password.
    Password:此处敲击回车即可
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> cd pub
    250 Directory successfully changed.
    ftp> mkdir files
    257 "/pub/files" created
    ftp> rename files database
    350 Ready for RNTO.
    250 Rename successful.
    ftp> rmdir database
    250 Remove directory operation successful.
    ftp> exit
    221 Goodbye.
    

    1、匿名公开模式

    2、本地账户模式

    11.2.2 本地用户模式
    参数 作用
    anonymous_enable=NO 禁止匿名访问模式
    local_enable=YES 允许本地用户模式
    write_enable=YES 设置可写权限
    local_umask=022 本地用户模式创建文件的umask值
    userlist_deny=YES 启用“禁止用户名单”,名单文件为ftpusers和user_list
    userlist_enable=YES 开启用户作用名单文件功能
    [root@linuxprobe ~]# vim /etc/vsftpd/vsftpd.conf
    1 anonymous_enable=NO
    2 local_enable=YES
    3 write_enable=YES
    4 local_umask=022
    5 dirmessage_enable=YES
    6 xferlog_enable=YES
    7 connect_from_port_20=YES
    8 xferlog_std_format=YES
    9 listen=NO
    10 listen_ipv6=YES
    11 pam_service_name=vsftpd
    12 userlist_enable=YES
    13 tcp_wrappers=YES
    

    在vsftpd服务程序的主配置文件中正确填写参数,然后保存并退出。还需要重启vsftpd服务程序,让新的配置参数生效。在执行完上一个实验后还原了虚拟机的读者,还需要将配置好的服务添加到开机启动项中,以便在系统重启自后依然可以正常使用vsftpd服务。

    [root@linuxprobe ~]# systemctl restart vsftpd
    [root@linuxprobe ~]# systemctl enable vsftpd
     ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service
    

    按理来讲,现在已经完全可以本地用户的身份登录FTP服务器了。但是在使用root管理员登录后,系统提示如下的错误信息:

    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): root
    530 Permission denied.
    Login failed.
    ftp>
    

    可见,在我们输入root管理员的密码之前,就已经被系统拒绝访问了。这是因为vsftpd服务程序所在的目录中默认存放着两个名为“用户名单”的文件(ftpusers和user_list)。不知道大家是否已看过一部日本电影“死亡笔记”(刘遄老师在上学期间的最爱),里面就提到有一个黑色封皮的小本子,只要将别人的名字写进去,这人就会挂掉。vsftpd服务程序目录中的这两个文件也有类似的功能—只要里面写有某位用户的名字,就不再允许这位用户登录到FTP服务器上。

    [root@linuxprobe ~]# cat /etc/vsftpd/user_list 
    1 # vsftpd userlist
    2 # If userlist_deny=NO, only allow users in this file
    3 # If userlist_deny=YES (default), never allow users in this file, and
    4 # do not even prompt for a password.
    5 # Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
    6 # for users that are denied.
    7 root
    8 bin
    9 daemon
    10 adm
    11 lp
    12 sync
    13 shutdown
    14 halt
    15 mail
    16 news
    17 uucp
    18 operator
    19 games
    20 nobody
    [root@linuxprobe ~]# cat /etc/vsftpd/ftpusers 
    # Users that are not allowed to login via ftp
    1 root
    2 bin
    3 daemon
    4 adm
    5 lp
    6 sync
    7 shutdown
    8 halt
    9 mail
    10 news
    11 uucp
    12 operator
    13 games
    14 nobody
    

    果然如此!vsftpd服务程序为了保证服务器的安全性而默认禁止了root管理员和大多数系统用户的登录行为,这样可以有效地避免黑客通过FTP服务对root管理员密码进行暴力破解。如果您确认在生产环境中使用root管理员不会对系统安全产生影响,只需按照上面的提示删除掉root用户名即可。我们也可以选择ftpusers和user_list文件中没有的一个普通用户尝试登录FTP服务器:

    [root@linuxprobe ~]# ftp 192.168.10.10 
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): linuxprobe
    331 Please specify the password.
    Password:此处输入该用户的密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> mkdir files
    550 Create directory operation failed.
    
    [root@linuxprobe ~]# getsebool -a | grep ftp
    ftp_home_dir --> off
    ftpd_anon_write --> off
    ftpd_connect_all_unreserved --> off
    ftpd_connect_db --> off
    ftpd_full_access --> off
    ftpd_use_cifs --> off
    ftpd_use_fusefs --> off
    ftpd_use_nfs --> off
    ftpd_use_passive_mode --> off
    httpd_can_connect_ftp --> off
    httpd_enable_ftp_server --> off
    sftpd_anon_write --> off
    sftpd_enable_homedirs --> off
    sftpd_full_access --> off
    sftpd_write_ssh_home --> off
    tftp_anon_write --> off
    tftp_home_dir --> off
    [root@linuxprobe ~]# setsebool -P ftpd_full_access=on
    
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): linuxprobe
    331 Please specify the password.
    Password:此处输入该用户的密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> mkdir files
    257 "/home/linuxprobe/files" created
    ftp> rename files database
    350 Ready for RNTO.
    250 Rename successful.
    ftp> rmdir database
    250 Remove directory operation successful.
    ftp> exit
    221 Goodbye.
    
    11.2.3 虚拟用户模式
    [root@linuxprobe ~]# cd /etc/vsftpd/
    [root@linuxprobe vsftpd]# vim vuser.list
    zhangsan
    redhat
    lisi
    redhat
    
    [root@linuxprobe vsftpd]# db_load -T -t hash -f vuser.list vuser.db
    [root@linuxprobe vsftpd]# file vuser.db
    vuser.db: Berkeley DB (Hash, version 9, native byte-order)
    [root@linuxprobe vsftpd]# chmod 600 vuser.db
    [root@linuxprobe vsftpd]# rm -f vuser.list
    
    [root@linuxprobe ~]# useradd -d /var/ftproot -s /sbin/nologin virtual
    [root@linuxprobe ~]# ls -ld /var/ftproot/
    drwx------. 3 virtual virtual 74 Jul 14 17:50 /var/ftproot/
    [root@linuxprobe ~]# chmod -Rf 755 /var/ftproot/
    
    [root@linuxprobe ~]# vim /etc/pam.d/vsftpd.vu
    auth       required     pam_userdb.so db=/etc/vsftpd/vuser
    account    required     pam_userdb.so db=/etc/vsftpd/vuser
    
    [root@linuxprobe ~]# vim /etc/vsftpd/vsftpd.conf
    1 anonymous_enable=NO
    2 local_enable=YES
    3 guest_enable=YES
    4 guest_username=virtual
    5 allow_writeable_chroot=YES
    6 write_enable=YES
    7 local_umask=022
    8 dirmessage_enable=YES
    9 xferlog_enable=YES
    10 connect_from_port_20=YES
    11 xferlog_std_format=YES
    12 listen=NO
    13 listen_ipv6=YES
    14 pam_service_name=vsftpd.vu
    15 userlist_enable=YES
    16 tcp_wrappers=YES
    
    [root@linuxprobe ~]# mkdir /etc/vsftpd/vusers_dir/
    [root@linuxprobe ~]# cd /etc/vsftpd/vusers_dir/
    [root@linuxprobe vusers_dir]# touch lisi
    [root@linuxprobe vusers_dir]# vim zhangsan
    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
    
    [root@linuxprobe ~]# vim /etc/vsftpd/vsftpd.conf
    anonymous_enable=NO
    local_enable=YES
    guest_enable=YES
    guest_username=virtual
    allow_writeable_chroot=YES
    write_enable=YES
    local_umask=022
    dirmessage_enable=YES
    xferlog_enable=YES
    connect_from_port_20=YES
    xferlog_std_format=YES
    listen=NO
    listen_ipv6=YES
    pam_service_name=vsftpd.vu
    userlist_enable=YES
    tcp_wrappers=YES
    user_config_dir=/etc/vsftpd/vusers_dir
    [root@linuxprobe ~]# systemctl restart vsftpd
    [root@linuxprobe ~]# systemctl enable vsftpd
     ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service
    
    [root@linuxprobe ~]# getsebool -a | grep ftp
    ftp_home_dir –> off
    ftpd_anon_write –> off
    ftpd_connect_all_unreserved –> off
    ftpd_connect_db –> off
    ftpd_full_access –> off
    ftpd_use_cifs –> off
    ftpd_use_fusefs –> off
    ftpd_use_nfs –> off
    ftpd_use_passive_mode –> off
    httpd_can_connect_ftp –> off
    httpd_enable_ftp_server –> off
    sftpd_anon_write –> off
    sftpd_enable_homedirs –> off
    sftpd_full_access –> off
    sftpd_write_ssh_home –> off
    tftp_anon_write –> off
    tftp_home_dir –> off
    [root@linuxprobe ~]# setsebool -P ftpd_full_access=on
    
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): lisi
    331 Please specify the password.
    Password:此处输入虚拟用户的密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> mkdir files
    550 Permission denied.
    ftp> exit
    221 Goodbye.
    [root@linuxprobe ~]# ftp 192.168.10.10
    Connected to 192.168.10.10 (192.168.10.10).
    220 (vsFTPd 3.0.2)
    Name (192.168.10.10:root): zhangsan
    331 Please specify the password.
    Password:此处输入虚拟用户的密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> mkdir files
    257 "/files" created
    ftp> rename files database
    350 Ready for RNTO.
    250 Rename successful.
    ftp> rmdir database
    250 Remove directory operation successful.
    ftp> exit
    221 Goodbye.
    
    11.3 TFTP简单文件传输协议
    [root@linuxprobe ~]# yum install tftp-server tftp
    
    [root@linuxprobe ~]# vim /etc/xinetd.d/tftp
    service tftp
    {
            socket_type             = dgram
            protocol                = udp
            wait                    = yes
            user                    = root
            server                  = /usr/sbin/in.tftpd
            server_args             = -s /var/lib/tftpboot
            disable                 = no
            per_source              = 11
            cps                     = 100 2
            flags                   = IPv4
    }
    
    [root@linuxprobe ~]# systemctl restart xinetd
    [root@linuxprobe ~]# systemctl enable xinetd
    [root@linuxprobe ~]# firewall-cmd --permanent --add-port=69/udp
    success
    [root@linuxprobe ~]# firewall-cmd --reload 
    success
    
    [root@linuxprobe ~]# echo "i love linux" > /var/lib/tftpboot/readme.txt
    [root@linuxprobe ~]# tftp 192.168.10.10
    tftp> get readme.txt
    tftp> quit
    [root@linuxprobe ~]# ls
    anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures readme.txt Videos
    Desktop Downloads Music Public Templates
    [root@linuxprobe ~]# cat readme.txt 
    i love linux
    
  • 相关阅读:
    httpclient 5.0 设置超时时间
    httpclient raw请求
    使用EasyUI-datagrid-export.js导出Excel,不导出隐藏列
    ExpressSet 虚拟适配器
    中断模式队列
    autohotkey 线程入门
    读rbtree
    树莓派串口
    MCP3421使用详解
    PMOS开关电路
  • 原文地址:https://www.cnblogs.com/gerenboke/p/12632447.html
Copyright © 2011-2022 走看看