天天看點

伺服器使用者、密碼變動檢測腳本

#!/bin/bash

#說明:
#報警計數器位置:
#dircount/fileint.txt,dircount/dirint.txt
#腳本日志位置:
#/tmp/diff.log
#腳本主目錄:
#/tools/passwdmd5/
#腳本使用:
#初始化安裝
#./diff_user_passwd.sh install
#解除報警,初始化部署
#./diff_user_passwd.sh init
#正常執行定時任務,或正常檢測:
#./diff_user_passwd.sh start

get_ip()  {

    #cmd_get_ip = "/sbin/ifconfig |grep 'inet addr'|awk -F\: '{print $2}'|awk '{print $1}' | grep -v '^127' | grep -v '192'"
    #cmd_get_ip = "ifconfig |grep 'inet addr'|awk -F\: '{print $2}'|awk '{print $1}' | grep -v '^127'"
    #return cmd_get_ip
    #cmd_get_ip=$(/sbin/ifconfig |grep 'inet 192.168.136'|awk -F ' ' '{print $2}'|awk '{print $1}')
    cmd_get_ip=$(/sbin/ifconfig |grep "192.168.1\." |awk -F ' ' '{print $2}'|tr -d "位址:"|tr -d "addr:")
    echo $cmd_get_ip
}
#生成md5檔案目錄
filepath="/tools/passwdmd5"
dirpath="/tools/dircount"
#格式化日期輸出
date=`date +%Y-%m-%d@%H:%M:%S`
#擷取ip
ip=$(get_ip)
#設定報警最大次數,并将此次異常後重置
maxint=3
#給循環次數初始化
varint=0
#郵件接收人
receiver=你的收件箱

#檢視變動
if [ -f /tools/passwdmd5/passwd.bak -a -f /tools/passwdmd5/shadow.bak ];then
        dfuser=`diff /etc/passwd /tools/passwdmd5/passwd.bak`
        dfpswd=`diff /etc/shadow /tools/passwdmd5/shadow.bak`
else
        dfuser=''
        dfpswd=''
fi



#建立部署
exec_install() {

    rm -fr $filepath
    mkdir -p $filepath
    rm -fr $dirpath
    mkdir -p $dirpath
    cp -f /etc/passwd $filepath/passwd.bak
    cp -f /etc/shadow $filepath/shadow.bak
    md5sum /etc/passwd > $filepath/passwdmd5old.log
    md5sum /etc/shadow >> $filepath/passwdmd5old.log

}

#正常添加使用者或修改密碼後初始化
exec_init() {

    rm -fr $filepath/passwd.bak $filepath/shadow.bak $filepath/passwdmd5old.log
    cp -f /etc/passwd $filepath/passwd.bak
    cp -f /etc/shadow $filepath/shadow.bak
    md5sum /etc/passwd > $filepath/passwdmd5old.log
    md5sum /etc/shadow >> $filepath/passwdmd5old.log
    echo 0 > $dirpath/fileint.txt
}

#正常郵件報警
users_alarm() {
    echo -e "本機使用者有異常!"\\n"使用者變化:"\\n$dfuser\\n"密碼變化:"\\n$dfpswd | mail -s $ip"伺服器上使用者有異常,請檢視!"  $receiver
}
#重置郵件報警
reset_alarm() {
    echo -e "本機使用者異常報警超過$maxint次,執行重置指令!"\\n"使用者變化:"\\n$dfuser\\n"密碼變化:"\\n$dfpswd | mail -s $ip"重置計劃:部署已重置,請檢視重置前使用者異常情況!" $receiver
    echo 0 > $dirpath/fileint.txt
}

#檔案被删除報警
file_alarm() {
    echo -e "$filepath檔案被删除報警!"\\n"使用者變化:"\\n$dfuser\\n"密碼變化:"\\n$dfpswd | mail -s $ip"$filepath檔案可能被删除,請檢查檔案情況!" $receiver
}
#目錄被删除報警
dir_alarm() {

    echo -e "$filepath目錄被删除報警!" | mail -s $ip"$filepath目錄被删除,請檢查目錄情況!" $receiver

}

#對比使用者和密碼函數
diff_user_passwd() {

if [ -f $filepath/passwdmd5old.log -a -f $filepath/passwd.bak -a -f $filepath/shadow.bak ];then
    md5sum /etc/passwd > $filepath/passwdmd5new.log
    md5sum /etc/shadow >> $filepath/passwdmd5new.log
    a=`diff -c $filepath/passwdmd5new.log $filepath/passwdmd5old.log`
    if [ "$a" = "" ];then
        if [ ! -d $dirpath ];then
            mkdir -p $dirpath
            echo 0 > $dirpath/fileint.txt
        fi
        echo "==========================================" >> /tmp/diff.log
        echo $date":使用者正常。" >> /tmp/diff.log
        echo "==========================================" >> /tmp/diff.log
    else
        echo "==========================================" >> /tmp/diff.log
        echo $date":" >> /tmp/diff.log
        echo "$a" >> /tmp/diff.log
        echo "==========================================" >> /tmp/diff.log
        #擷取已執行次數
        if [ ! -d $dirpath ];then
            mkdir -p $dirpath
            echo 0 > $dirpath/fileint.txt
        fi


        if [ ! -f $dirpath/fileint.txt ];then
            echo 0 > $dirpath/fileint.txt
        fi

        varint=`cat $dirpath/fileint.txt`
        if [ $varint -lt $maxint ] ;then
            #擷取詳細變動
            users_alarm
            ((varint++))
            echo $varint > $dirpath/fileint.txt
        else
            #進行異常重置及報警
            reset_alarm
            if [ -f $filepath/passwd.bak -a -f $filepath/shadow.bak ];then
            
                rm -rf $filepath/passwd.bak $filepath/shadow.bak
                echo $date":已進行重置,删除passwd.bak,shadow.bak以及passwdmd5old.log三個檔案!!!" >> /tmp/diff.log
                exec_init
                diff_user_passwd
                
            else
                exec_init
                diff_user_passwd
            fi

        fi
    fi
else
    #擷取已執行次數
    if [ ! -d $dirpath ];then
        mkdir -p $dirpath
        echo 0 > $dirpath/fileint.txt
    fi


    if [ ! -f $dirpath/fileint.txt ];then
        echo 0 > $dirpath/fileint.txt
    fi

    varint=`cat $dirpath/fileint.txt`
    if [ $varint -lt $maxint ] ;then
        file_alarm
        ((varint++))
        echo $varint > $dirpath/fileint.txt
    else
        exec_init
        diff_user_passwd
    fi
fi
}

#開始執行腳本
if [ "$1" = "install" ];then
    exec_install
    diff_user_passwd

elif [ "$1" = "start" ];then
    if [ -d $filepath ];then
        diff_user_passwd
    else
        if [ ! -d $dirpath ];then
                mkdir -p $dirpath
                echo 0 > $dirpath/dirint.txt
        fi
        #循環目錄報警次數
        if [ ! -f $dirpath/dirint.txt ];then
            echo 0 > $dirpath/dirint.txt
        fi
        varint=`cat $dirpath/dirint.txt`
        if [ $varint -lt $maxint ] ;then
            dir_alarm
            ((varint++))
            echo $varint > $dirpath/dirint.txt
        else
            exec_install
            diff_user_passwd
        fi
    fi
elif [ "$1" = "init" ];then
    exec_init
    diff_user_passwd
else
    echo "您輸入的參數有誤,若是新部署請加參數install來部署或者start來啟動"
    exit 0

fi
      

繼續閱讀