zoukankan      html  css  js  c++  java
  • 在selinux和防火墙开启的情况下安装和配置vsftpd,并且限定IP访问

    操作系统是Centos 7.6。

    常规操作:
    安装软件: yum install -y vsftpd
    查看是否安装:rpm -qa | grep vsftpd, 或者 ls /etc/vsftpd/vsftpd.conf 或执行 vsftpd -v
    启动停止等操作: systemctl {start|stop|restart|status|enable|disable} vsftpd.service

    配置
    配置文件一般是 /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

    关于一些安全加固的配置的修改(配置项等号两边无空格或其他字符):
    - 关于vsftpd的安全加固
        - ftp_banner # 改成自定义的信息
        - chroot_local_user=YES  #有的客户端尤其是程序,可以需要使用文件绝对路径在OS中绝对路径,那么值要改为NO
        - ls_recurse_enable=YES
        - local_umask=022
        - anon_umask=022
    - 其他配置修改(在默认配置文件上的修改)
        - anonymous_enable=NO  #禁用匿名登录
        - listen=YES  #独立进程运行
        - listen_port=2431 # 避免使用默认端口,增强安全性
        - 注释掉 listen_ipv6=YES
        - 确认(默认是开启的) xferlog_enable=YES
          - 需要同时注释掉 xferlog_std_format=YES # 该项的默认值是NO,如果是YES,那么日志会以wu-ftp的格式写入 /var/log/xferlog
          - 有了这些设置,vsftpd会将日志写入/var/log/vsftpd.log文件中;或者也可以设置 syslog_enable=YES 将日志写入 系统日志
        - 确认 userlist_enable=YES
        - 设置 userlist_deny=NO  #设置为NO则只允许user_list文件指定的登录ftp
          - 编辑 /etc/vsftpd/user_list # 建议备份原来 user_list文件,并且保留user_list文件头的注释部分
            - 仅添加下面创建的 test 用户到user_list中(格式是每行一个用户名)  
        - 设置passive模式的端口范围(需要在防火墙上允许)
          pasv_min_port=30000
          pasv_max_port=30020

    创建用户(比如test)(使用本地用户即操作系统用户登录ftp)
    - 最好一开始就指定该用户的home目录,最好是放在保存ftp文件的分区上
    - useradd  -d <homedir> -s /sbin/nologin test # <homedir>需要保证路径中的父目录存在,比如/a/b/c为homedir,那么确保/a/b是存在的
    - passwd test #给新建用户设置密码
    - chmod u-w <homedir> # test用户创建后,homedir的owner一般就是test,同时也属于test组
    - 也可以简单地创建用户(useradd test),然后后续通过usermod等命令修改用户属性
    - 前面修改homedir的权限,使得即使对于其owner也是不可读的,所以需要在homedir创建子目录,作为读写文件的地方。比如下面创建uploads目录用于读写
      - mkdir <homedir>/uploads
      - chown test:test <homedir>/uploads

    和安全机制相关的修改
    - pam认证
      - 从默认配置来看,pam认证是开启的
      - /etc/shells 中添加 /sbin/nologin, 以便 /etc/pam.d/vsftpd 中的 auth       required     pam_shells.so 能够通过- selinux
          - 如果不添加这个可能在/var/log/audit/audit.log会出现一条审计信息:
            type=USER_AUTH msg=audit(1593499214.152:694): pid=13945 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="UserName" exe="/usr/sbin/vsftpd" hostname=192.168.164.1 addr=192.168.164.1 terminal=ftp res=failed'
      - 使用 getsebool -a | grep ftp 命令查看相关的设置项
      - setsebool -P ftpd_full_access on
        - 如果不设置,在/var/log/messages会出现下面信息
            Jun 30 14:51:31 cc1 dbus[6765]: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)
            Jun 30 14:51:31 cc1 dbus[6765]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
            Jun 30 14:51:32 cc1 setroubleshoot: failed to retrieve rpm info for <home-dir>     
            Jun 30 14:47:31 cc1 setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from read access on the directory <home-dir>. For complete SELinux messages run: sealert -l 2fa42966-fd58-448e-9931-8b8c24166f2b
            Jun 30 14:47:31 cc1 python: SELinux is preventing /usr/sbin/vsftpd from read access on the directory <home-dir>.#012#012*****  Plugin catchall_boolean (57.6 confidence) suggests   ******************#012#012If you want to allow ftpd to full access#012Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.#012#012Do#012setsebool -P ftpd_full_access 1#012#012*****  Plugin catchall_labels (36.2 confidence) suggests   *******************#012#012If you want to allow vsftpd to have read access on the <home-dir>. directory#012Then you need to change the label on <home-dir>#012Do#012# semanage fcontext -a -t FILE_TYPE '<home-dir>'#012where FILE_TYPE is one of the following: admin_home_t, bin_t, boot_t, cache_home_t, cert_t, config_home_t, data_home_t, dbusd_etc_t, device_t, devpts_t, etc_runtime_t, etc_t, faillog_t, file_context_t, fonts_cache_t, fonts_t, ftpd_tmp_t, ftpd_tmpfs_t, ftpd_var_run_t, gconf_home_t, gnome_home_t, httpd_user_content_t, httpd_user_script_exec_t, krb5_conf_t, krb5_host_rcache_t, lib_t, locale_t, man_cache_t, man_t, mozilla_plugin_rw_t, net_conf_t, nscd_var_run_t, proc_t, public_content_rw_t, public_content_t, root_t, samba_etc_t, security_t, shell_exec_t, slapd_cert_t, src_t, sssd_public_t, sysfs_t, system_conf_t, system_db_t, systemd_logind_sessions_t, telepathy_cache_home_t, telepathy_data_home_t, textrel_shlib_t, tmp_t, tmpfs_t, udev_var_run_t, user_fonts_t, user_home_dir_t, user_home_t, user_tmp_t, usr_t, var_auth_t, var_lib_t, var_lock_t, var_log_t, var_run_t, virt_home_t, virt_var_lib_t, xferlog_t.#012Then execute:#012restorecon -v '<home-dir>'#012#012#012*****  Plugin catchall (7.64 confidence) suggests   **************************#012#012If you believe that vsftpd should be allowed read access on the <home-dir>. directory by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'vsftpd' --raw | audit2allow -M my-vsftpd#012# semodule -i my-vsftpd.pp#012
         - 上面信息格式连成一行不好阅读,将#012替换为 (我怀疑#012是8进制,算下来就是10,即 的ascii码)
            - 或者在上一行找到 sealert -l 2fa42966-fd58-448e-9931-8b8c24166f2b 这个命令并执行,也可以获得格式更美观的消息。
      - setsebool -P ftpd_connect_all_unreserved 1
    - firewalld的防火墙规则设置
      - 开放自定义的监听端口: firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 192.168.164.1 -p tcp --dport 2431 -j ACCEPT
      - 开放上面配置文件中定义的passive模式用到端口: firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 192.168.164.1 -p tcp --dport 30000:30020 -j ACCEPT
      - 上面两条规则都是direct规则,并通过 -s <ip> 限定了只能从<ip>这个地址访问ftp服务器,如果不需要限制,那么将它从规则中去掉
      - 添加完之后执行 firewall-cmd --reload使得规则生效。但是需要注意如果和其他规则配置一起,那么需要检查/etc/firewalld/direct.xml文件,查看是否之前设置了保底规则(比如全部拒绝之类的),如果设置了保底规则需要将新添加的规则放在保底规则之前再reload才能起作用,否则规则也没有用的。
    - tcp_wrappers
      - 注意到默认配置里面还是开启了tcp_wrappers的,那么可以通过修改/etc/hosts.allow来限定IP,比如在/etc/hosts.allow中添加vsftpd:192.168.164.1一行,那么就限定了能访问vsftpd服务的IP(匹配的需要在/etc/hosts.deny中添加all:all)
      - 因为防火墙规则已经设置限制IP了,所以这里也可以不配置。

    最后重启vsftpd服务, systemctl restart vsftpd。

  • 相关阅读:
    内存中的线程
    python高级线程、进程和进程池
    生产者以及消费者模式(进程池)
    生产者以及消费者模式(多进程)
    生产者以及消费者模式
    全局解释器锁(GIL)
    JoinableQueue队列实现消费之生产者模型
    什么是Sikuli?
    协程基础
    装饰器
  • 原文地址:https://www.cnblogs.com/csmountains/p/13212248.html
Copyright © 2011-2022 走看看