首先模拟出一个目录下面有三个配置文件,内容都一样!

[root@rhelX64 test]# ll
总用量 12
-rw-r--r-- 1 root root 95 12月  4 12:26 apache.conf
-rw-r--r-- 1 root root 95 12月  4 12:26 mysql.conf
-rw-r--r--
1 root root 95 12月  4 12:25 nginx.conf
[root@rhelX64 test]# cat nginx.conf
this is one line.
this is two line.
this is three line.
this is four line.
this is five line.

  用MD5做标记,写入conf.log文件

[root@rhelX64 test]# cat /mnt/conf.log
105256b35aa4a08e18378b66be607c7f  apache.conf
105256b35aa4a08e18378b66be607c7f  mysql.conf
105256b35aa4a08e18378b66be607c7f  nginx.conf

 没有改动文件,我们来看看测试结果!

[root@rhelX64 test]# md5sum -c /mnt/conf.log
apache.conf: 确定
mysql.conf: 确定
nginx.conf: 确定


 改动nginx文件,看看通过“指纹”文件,是否可以报警?

 

[root@rhelX64 test]# echo "this is six line." >> nginx.conf
[root@rhelX64 test]# md5sum -c /mnt/conf.log               
apache.conf: 确定
mysql.conf: 确定
nginx.conf: 失败
md5sum: 警告:1/3 生成的校验和不匹配

 可以看到,nginx失败,表示已经被篡改文件,下面通过一个脚本来监控配置文件。

 

#!/bin/bash
#created by sanpang
#email:zyjqianfuyu@163.com
#home:lovers.blog.51cto.com
#qq:791880666
#function   This script is used to monitor if the file is a malicious changes
# Source function library.
. /etc/rc.d/init.d/functions
if [ -e "/mnt/conf.log" ]; then
    md5sum -c /mnt/conf.log
  else
    md5sum /test/*.conf > /mnt/conf.log
    md5sum -c /mnt/conf.log
fi