首页 > 其他 > 详细

第十八周

时间:2020-09-29 10:33:48      阅读:37      评论:0      收藏:0      [点我收藏+]

1、实现基于MYSQL验证的vsftpd虚拟用户访问

  • 配置Mysql服务器

# 安装mariadb数据库
[root@mysql ~]# yum install mariadb-server

[root@mysql ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> create database vsftpd        # 创建vsftpd数据库
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use vsftpd                    # 在vsftpd数据库中,创建表users
Database changed
MariaDB [vsftpd]> create table users(id int auto_increment not null primary key,name char(50) binary not null,password char(48) binary not null);
Query OK, 0 rows affected (0.00 sec)

MariaDB [vsftpd]> desc users                    # 查看表结构
    -> ;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| name     | char(50) | NO   |     | NULL    |                |
| password | char(48) | NO   |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

# 添加用户ftpuser1和ftpuser2,让密码加密显示
MariaDB [vsftpd]> insert users (name,password) value(‘ftpuser1‘,password(‘123456‘));
Query OK, 1 row affected (0.00 sec)

MariaDB [vsftpd]> insert users (name,password) value(‘ftpuser2‘,password(‘654321‘));
Query OK, 1 row affected (0.00 sec)

# 给用户vsftpd授权
MariaDB [vsftpd]> grant select on vsftpd.users to vsftpd@‘192.168.10.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
  • 配置Ftp服务器
# 安装需要的软件包
[root@ftp ~]# yum install gcc gcc-c++ pam-devel mariadb-devel vsftpd

# centos 7由于没有对应的yum包,只能编译安装pam_mysql;如果是centos 6可以直接yum安装
[root@ftp ~]# tar xf pam_mysql-0.7RC1.tar.gz 
[root@ftp ~]# cd pam_mysql-0.7RC1/
[root@ftp pam_mysql-0.7RC1]# ./configure --with-pam-mods-dir=/lib64/security
[root@ftp pam_mysql-0.7RC1]# make && make install

# 创建vsftpd.mysql模块文件,内容如下:
# crypt是加密方式,2表示使用mysql password()函数加密
[root@ftp pam.d]# vim /etc/pam.d/vsftpd.mysql
auth required pam_mysql.so user=vsftpd passwd=123456 host=192.168.10.131 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=123456 host=192.168.10.131 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

# 创建vuser用户,作为虚拟用户的映射,修改家目录权限为555(没有w权限),创建一个upload目录用于上传文件
[root@ftp ~]# useradd -d /data/ftproot/ -s /sbin/nologin vuser
[root@ftp ~]# chmod 555 /data/ftproot/
[root@ftp ~]# mkdir /data/ftproot/upload
[root@ftp ~]# setfacl -m u:vuser:rwx /data/ftproot/upload

# 修改配置文件
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
# 在文件的最后,修改pam_service_name为下面内容
pam_service_name=vsftpd.mysql
# 添加下面两行
guest_enable=YES
guest_username=vuser

# 启动vsftpd服务
[root@ftp ~]# systemctl start vsftpd
  • 登录验证

# 使用ftpuser1登录成功
[root@client ~]#ftp 192.168.10.130
Connected to 192.168.10.130 (192.168.10.130).
220 (vsFTPd 3.0.2)
Name (192.168.10.130:root): ftpuser1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,10,130,68,41).
150 Here comes the directory listing.
drwxrwxr-x    2 0        0               6 Sep 22 16:12 upload
226 Directory send OK.
ftp> quit
221 Goodbye.

# 使用ftpuser2登录成功
[root@client ~]#ftp 192.168.10.130
Connected to 192.168.10.130 (192.168.10.130).
220 (vsFTPd 3.0.2)
Name (192.168.10.130:root): ftpuser2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,10,130,199,247).
150 Here comes the directory listing.
drwxrwxr-x    2 0        0               6 Sep 22 16:12 upload
226 Directory send OK.
ftp> 

2、通过NFS实现服务器/www共享访问。

  • 服务器端配置

# 启动服务,centos 7仅启动nfs即可,centos 6需要先启动rpcbind,再启动nfs
[root@nfs-server ~]# systemctl start nfs

# 设置共享属性,共享/www目录,允许192.168.10.0/24网段访问,权限为rw,所有用户都被压榨为nfsnobody用户
[root@nfs-server ~]# vim /etc/exports
/www    192.168.10.0/24(rw,all_squash)

# 创建/www目录,设置nfsnobody的权限为rwx,这样客户端挂载之后,才有写权限
[root@nfs-server ~]# mkdir /www
[root@nfs-server ~]# setfacl -m u:nfsnobody:rwx /www

  • 客户端配置

# 先查看一下服务器端的共享目录
[root@client ~]#showmount -e 192.168.10.130
Export list for 192.168.10.130:
/www 192.168.10.0/24

# 通过mount命令把共享目录挂载到/mnt目录下
[root@client ~]#mount 192.168.10.130:/www /mnt
[root@client ~]#df /mnt
Filesystem          1K-blocks    Used Available Use% Mounted on
192.168.10.130:/www 104806400 4684032 100122368   5% /mnt

# 可以看到新建文件f1成功,并且属主和属组都是nfsnobody
[root@client ~]#cd /mnt
[root@client mnt]#touch f1
[root@client mnt]#ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Sep 23 14:42 f1

3、配置samba共享,实现/www目录共享

# 安装软件包
[root@samba-server ~]# yum install samba -y

# 编辑配置文件,在最后添加下面的内容
[root@samba-server ~]# vim /etc/samba/smb.conf
[www]
path=/www
writeable=yes
valid users=scott

# 创建scott用户,然后添加到samba用户数据库
[root@samba-server ~]# useradd -s /sbin/nologin scott
[root@samba-server ~]# smbpasswd scott
New SMB password:
Retype new SMB password:

# 使用pdbedit -L查看添加的用户
[root@samba-server ~]# pdbedit -L
scott:1000:scott

# 重启服务
[root@samba-server ~]# systemctl restart smb


# 客户端测试访问成功
[root@client ~]#smbclient //192.168.10.130/www -U scott%123456
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Thu Sep 24 19:05:56 2020
  ..                                  D        0  Wed Sep 23 14:04:23 2020
  f1                                  N        0  Wed Sep 23 14:42:02 2020
  centos6                             N        0  Thu Sep 24 18:43:01 2020
  anaconda-ks.cfg                     A     1888  Thu Sep 24 19:05:56 2020

		104806400 blocks of size 1024. 100166084 blocks available
smb: \> 

4、使用rsync+inotify实现/www目录实时同步

  • 服务器端配置

# 安装rsync服务软件包
yum install rsync -y

# 编辑配置文件,内容如下
vi /etc/rsyncd.conf
uid = root					# 使用root用户运行服务
gid = root					# 使用root用户组运行服务
use chroot = no				# 不允许切根
max connections = 0			# 最大连接数,0为不限制
ignore errors				# 如果有错误,就忽略错误
exclude = lost+found/		# 设置不检查的目录,这里忽略了lost+found
log file = /var/log/rsyncd.log			# 日志文件
pid file = /var/run/rsyncd.pid			# pid文件
lock file = /var/run/rsyncd.lock		# 锁文件
reverse lookup = no						# 是否反向解析,不解析速度更快
hosts allow = 192.168.10.0/24			# 允许访问的网段
[backup]
path = /backup/							# 备份文件存放的路径
comment = backup						# 注释信息
read only = no							# 是否只读
auth users = rsyncuser					# 客户端同步时使用的同步账户为rsyncuser
secrets file = /etc/rsync.pass			# 服务器端存放用户名密码的文件


#创建验证文件
[root@rsync-server ~]#echo "rsyncuser:magedu" > /etc/rsync.pass
[root@rsync-server ~]#chmod 600 /etc/rsync.pass

#创建备份目录
[root@rsync-server ~]#mkdir /backup

# 启动服务
systemctl start rsyncd
  • 客户端配置通过脚本实现实时备份

# 创建密码文件,在执行同步命令时可以指定密码文件,否则会是交互式输入密码
[root@client ~]#echo "magedu" > /etc/rsync.pass
[root@client ~]#chmod 600 /etc/rsync.pass

# 安装inotify-tools软件包
yum install inotify-tools -y

# 编写脚本如下
[root@client ~]#cat rsync_inotify.sh 
#!/bin/bash

SRC=‘/www/‘
DEST=‘rsyncuser@192.168.10.128::backup‘
inotifywait -mrq --timefmt ‘%Y-%m-%d %H:%M‘ --format ‘%T %w %f‘ -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
    FILEPATH=${DIR}${FILE}
    rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done

# 执行脚本
# 默认是前台执行,可以加&后台执行
[root@client ~]#sh rsync_inotify.sh

5、使用iptable实现: 放行telnet, ftp, web服务,放行samba服务,其他端口服务全部拒绝

# 加载nf_conntrack_ftp模块用于ftp被动模式
[root@iptables ~]# modprobe nf_conntrack_ftp

# 放行端口
[root@iptables ~]# iptables -A INPUT -p tcp -m multiport --dports 20,21,23,80,139,445 -j ACCEPT

# 放行已经建立连接的会话,主要是放行ftp的被动模式
[root@iptables ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 最后拒绝所有访问
[root@iptables ~]# iptables -A INPUT -j REJECT

第十八周

原文:https://www.cnblogs.com/kfscott/p/13748096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!