一 部署audit监控文件
1--配置audit审计系统
1)安装软件包,查看配置文件(确定审计日志的位置)
- [root@svr5 ~]# yum -y install audit //安装软件包
- [root@svr5 ~]# cat /etc/audit/auditd.conf //查看配置文件,确定日志位置
- [root@svr5 ~]# systemctl start auditd //启动服务
- [root@svr5 ~]# systemctl enable auditd //设置开机自启
- [root@svr5 ~]# service auditd restart //重启服务,不能使用systemctl只能使用6版本的service
- [root@svr5 ~]# service auditd stop //关闭服务。不能使用systemctl只能使用6版本的service
2)配置审计规则
- [root@svr5 ~]# auditctl -s //查询状态
- [root@svr5 ~]# auditctl -l //查看规则
- [root@svr5 ~]# auditctl -D //删除所有规则
定义临时文件系统规则:
- -w 是指定监控文件或者目录 -p 是指定权限跟属性 -k 是指定方便识别生成的日志项
- [root@svr5 ~]# auditctl -w /etc/passwd -p wa -k passwd_change
- //设置规则所有对passwd文件的写、属性修改操作都会被记录审计日志
- [root@svr5 ~]# auditctl -w /etc/selinux/ -p wa -k selinux_change
- //设置规则,监控/etc/selinux目录
- [root@svr5 ~]# auditctl -w /usr/sbin/fdisk -p x -k disk_partition
- //设置规则,监控fdisk程序
- [root@svr5 ~]# auditclt -w /etc/ssh/sshd_conf -p warx -k sshd_config
- //设置规则,监控sshd_conf文件
如果需要创建永久审计规则,则需要修改规则配置文件:
- [root@svr5 ~]# vim /etc/audit/rules.d/audit.rules
- -w /etc/passwd -p wa -k passwd_changes
- -w /usr/sbin/fdisk -p x -k partition_disks
二 加固常见服务的安全
1--优化Nginx服务的安全配置
1) 修改nginx服务名,并隐藏具体的版本号
vim src/core/nginx.h //在nginx源码包目录下修改src目录后的内容
............
#define NGINX_VERSION "" //13行 隐藏版本信息
#define NGINX_VER "wangdacui/" NGINX_VERSION //14行修改服务名 默认是nginx 可以随意改
............
#define NGINX_VAR "wangdacui" //在22行改成与14一致的名称
2) 删除不需要的模块
- [root@svr5 ~]# tar -xf nginx-1.12.tar.gz
- [root@svr5 ~]# cd nginx-1.12
- [root@svr5 nginx-1.12]# ./configure \
- >--without-http_autoindex_module \ //禁用自动索引文件目录模块
- >--without-http_ssi_module
- [root@svr5 nginx-1.12]# make
- [root@svr5 nginx-1.12]# make install
3) 限制并发量
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- … …
- http{
- … …
- limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- server {
- listen 80;
- server_name localhost;
- limit_req zone=one burst=5;
- }
- }
- //备注说明:
- //limit_req_zone语法格式如下:
- //limit_req_zone key zone=name:size rate=rate;
- //上面案例中是将客户端IP信息存储名称为one的共享内存,内存空间为10M
- //1M可以存储8千个IP信息,10M可以存储8万个主机连接的状态,容量可以根据需要任意调整
- //每秒中仅接受1个请求,多余的放入漏斗
- //漏斗超过5个则报错
客户端使用ab测试软件测试效果:
- [root@client ~]# ab -c 100 -n 100 http://192.168.4.5/
Failed requests: 94 //总共请求100个失败94个
4) 拒绝非法的请求
- [root@client ~]# curl -i -X GET http://192.168.4.5 //正常
- [root@client ~]# curl -i -X HEAD http://192.168.4.5 //正常
- //curl命令选项说明:
- //-i选项:访问服务器页面时,显示HTTP的头部信息
- //-X选项:指定请求服务器的方法
通过如下设置可以让Nginx拒绝非法的请求方法:
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- http{
- server {
- listen 80;
- if ($request_method !~ ^(GET|POST)$ ) {
- return 444;
- }
- }
- }
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload
- [root@client ~]# curl -i -X HEAD http://192.168.4.5 //报错
- curl: (52) Empty reply from server
2--数据库安全
1) 初始化安全脚本
- [root@svr5 ~]# systemctl status mariadb
- //确保服务已启动
- [root@svr5 ~]# mysql_secure_installation
- //执行初始化安全脚本
2)密码安全
手动修改MariaDB或MySQL数据库密码的方法:
- [root@svr5 ~]# mysqladmin -uroot -predhat password 'mysql'
- //修改密码,旧密码为redhat,新密码为mysql
- [root@svr5 ~]# mysql -uroot -pmysql
- MariaDB [(none)]>set password for root@'localhost'=password('redhat');
- //使用账户登录数据库,修改密码
为了安全要把输入密码的命令都清空
[root@svr5 ~]# history -c //要清除历史命令把输入密码的命令都清空
[root@svr5 ~]# cat .bash_history //通过命令行修改的密码,bash会自动记录历史,历史记录中记录了明文密码
[root@svr5~]#cat .mysql_history //通过mysql命令修改的密码,mysql也会有所有操作指令的记录,这里也记录了明文密码 show databases;
select user,host,password from mysql.user;
set password for root@`localhost`=password('redhat');
3)数据备份与还原
首先,备份数据库(注意用户名为root,密码为redhat):
[root@svr5 ~]# mysqldump -uroot -p mysql user > user.sql //备份mysql库user表
Enter password:redhat
[root@svr5 ~]# mysqldump -uroot -p mysql > mysql.sql //备份mysql整个库
[root@svr5 ~]# mysqldump -uroot -p --all-databases > all.sql //备份所有数据库
接下来,还原数据库(注意用户名为root,密码为redhat):
[root@svr5 ~]# mysql -uroot -p mysql < user.sql //还原数据表
[root@svr5 ~]# mysql -uroot -p mysql < mysql.sql //还原mysql库
[root@svr5 ~]# mysql -uroot -p < all.sql //还原所有库
4)数据安全
在服务器上(192.168.4.5),创建一个数据库账户:
[root@svr5 ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> grant all on *.* to tom@'%' identified by '123'; //%是指在任意节点都可以登陆
使用tcpdump抓包(192.168.4.5)
[root@svr5 ~]# tcpdump -w log -i any src or dst port 3306
//-w 是保存到指定文件,-i是指定网卡 src是源目标,dst是目标源 port 端口 3306为mariadb
[root@svr5 ~]# tcpdump -A -r log //-A是转换为ACSII,以方便阅读,-r是指定读取文件
[root@client ~]# mysql -h192.168.4.5 -utom -p123
//在client(192.168.4.100)登陆svr5(192.168.4.5)的数据库,使用tom用户 -h指定数据库的ip
MariaDB [(none)]> select * from mysql.user;
3--Tomcat安全性
1) 隐藏版本信息、修改tomcat主配置文件(隐藏版本信息)
[root@svr5 tomcat]# yum -y install java-1.8.0-openjdk-devel
[root@svr5 tomcat]# cd /usr/local/tomcat/lib/
[root@svr5 lib]# jar -xf catalina.jar
[root@svr5 lib]# vim org/apache/catalina/util/ServerInfo.properties
........................
server.info=wangdacui/1.1.1.1 //服务名跟版本号任意
server.number=1.1.1.1 //版本号跟上面一致就行
.................... //访问头部信息还是无法隐藏版本信息,得改tomcat主配置文件
[root@svr5 lib]# vim /usr/local/tomcat/conf/server.xml
....................
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" server="wangdacui" /> //在配置文件里大概70行左右最后添加server=“xxxxx”
......................
2)降级启动
默认tomcat使用系统高级管理员账户root启动服务,启动服务尽量使用普通用户。
[root@svr5 lib]# useradd tomcat
[root@svr5 lib]# chown -R tomcat:tomcat /usr/local/tomcat/
//修改tomcat目录的权限让tomcat用户有操作权限
[root@svr5 lib]# /usr/local/tomcat/bin/shutdown.sh
[root@svr5 lib]# su - tomcat -c /usr/local/tomcat/bin/startup.sh
//切换到tomcat用户,以tomcat用户启动tomcat
[root@svr5 lib]# vim /etc/rc.local
..........
su - tomcat -c /usr/local/tomcat/bin/startup.sh //添加这条命令开机自动执行
[root@svr5 lib]# chmod +x /etc/rc.d/rc.local //为这个开机自动执行的文件添加执行权限
3)删除默认的测试页面
[root@svr5 lib]# rm -rf /usr/local/tomcat/webapps/*
三 使用diff和patch工具打补丁
1--对比单个文件差异
1) 编写两个版本的脚本,一个为v1版本,一个为v2版本。
- [root@svr5 ~]# cat test1.sh //v1版本脚本
- #!/bin/bash
- echo "hello wrld"
- echo "test"
- [root@svr5 ~]# cat test2.sh //v2版本脚本
- #!/bin/bash
- echo "hello world"
- echo "test file"
2) 使用diff命令语法
使用diff命令查看不同版本文件的差异。
- [root@svr5 ~]# diff test1.sh test2.sh //查看文件差异
- @@ -1,3 +1,3 @@
- #!/bin/bash
- -echo "hello world"
- -echo "test"
- +echo "hello the world"
- +echo "test file"
- [root@svr5 ~]# diff -u test1.sh test2.sh //查看差异,包含头部信息
- --- test1.sh 2018-02-07 22:20:02.723971251 +0800
- +++ test2.sh 2018-02-07 22:20:13.358760687 +0800
- @@ -1,3 +1,3 @@
- #!/bin/bash
- -echo "hello world"
- -echo "test"
- +echo "hello the world"
- +echo "test file
2--使用patch命令对单文件代码打补丁
1) 生成补丁文件
- [root@svr5 demo]# diff -u test1.sh test2.sh > test.patch
3)使用patch命令打补丁
在代码相同目录下为代码打补丁
- [root@svr5 demo]# yum -y install patch
- [root@svr5 demo]# patch -p0 < test.patch //打补丁
- patching file test1.sh
- //patch -pnum(其中num为数字,指定删除补丁文件中多少层路径前缀)
- //如原始路径为/u/howard/src/blurfl/blurfl.c
- //-p0则整个路径不变
- //-p1则修改路径为u/howard/src/blurfl/blurfl.c
- //-p4则修改路径为blurfl/blurfl.c
- //-R(reverse)反向修复,-E修复后如果文件为空,则删除该文件
- [root@svr5 demo]# patch -RE < test.patch //还原旧版本,反向修复
3--对比目录中所有文件的差异
1) 准备实验环境
- [root@svr5 ~]# mkdir demo
- [root@svr5 ~]# cd demo
- [root@svr5 demo]# mkdir {source1,source2}
- [root@svr5 demo]# echo "hello world" > source1/test.sh
- [root@svr5 demo]# cp /bin/find source1/
- [root@svr5 demo]# tree source1/ //source1目录下2个文件
- |-- find
- `-- test.sh
- [root@svr5 demo]# echo "hello the world" > source2/test.sh
- [root@svr5 demo]# echo "test" > source2/tmp.txt
- [root@svr5 demo]# cp /bin/find source2/
- [root@svr5 demo]# echo "1" >> source2/find
- [root@svr5 demo]# tree source2/ //source1目录下3个文件
- |-- find
- |-- test.sh
- `-- tmp.txt
- //注意:两个目录下find和test.sh文件内容不同,source2有tmp.txt而source1没有该文件
2)制作补丁文件
- [root@svr5 demo]# diff -u source1/ source2/
- //仅对比了文本文件test.sh;二进制文件、tmp都没有对比差异,仅提示,因为没有-a和-N选项
- [root@svr5 demo]# diff -Nu source1/ source2/
- //对比了test.sh,并且使用source2目录的tmp.txt与source1的空文件对比差异。
-
- [root@svr5 demo]# diff -Nua source1/ source2/
- //对比了test.sh、tmp.txt、find(程序)。
4--使用patch命令对目录下的所有代码打补丁
1)使用前面创建的source1和source2目录下的代码为素材,生成补丁文件
- [root@svr5 demo]# diff -Nuar source1/ source2/ > source.patch
2)使用patch命令为代码打补丁
[root@svr5 demo]# cd source1
[root@svr5 source1]# patch -p1 < ../source.patch