Bash腳本實(shí)現(xiàn)實(shí)時(shí)監(jiān)測(cè)登錄
引言
背景介紹:在服務(wù)器的運(yùn)維管理中,及時(shí)監(jiān)控系統(tǒng)的登錄日志對(duì)保障系統(tǒng)的安全至關(guān)重要。通過(guò)實(shí)時(shí)監(jiān)控登錄日志,運(yùn)維人員可以發(fā)現(xiàn)潛在的異常登錄行為,防止系統(tǒng)被非法訪問(wèn)。
問(wèn)題引入:如何實(shí)現(xiàn)實(shí)時(shí)監(jiān)控登錄日志,并及時(shí)響應(yīng)潛在的安全風(fēng)險(xiǎn)?
實(shí)時(shí)監(jiān)控登錄日志的意義
安全性:通過(guò)監(jiān)控登錄日志,可以迅速發(fā)現(xiàn)惡意登錄、暴力 破解等異常行為。
合規(guī)性:確保滿足各種合規(guī)要求,記錄所有用戶的登錄行為。
解決方案概述
監(jiān)控目標(biāo):關(guān)注登錄日志中的關(guān)鍵信息,例如登錄時(shí)間、IP 地址、用戶名、登錄方式等。
技術(shù)選型:通過(guò)編寫(xiě) Bash 腳本,結(jié)合inotify、awk、grep 等工具,來(lái)實(shí)現(xiàn)對(duì)日志文件的實(shí)時(shí)監(jiān)控與分析。
腳本實(shí)現(xiàn)原理
實(shí)時(shí)監(jiān)控:利用 inotify 命令動(dòng)態(tài)監(jiān)控日志文件的變動(dòng),并結(jié)合 sed 命令實(shí)時(shí)提取和輸出新增的登錄日志。
日志篩選:通過(guò) grep 等工具過(guò)濾出登錄失敗、異常登錄等相關(guān)信息。
報(bào)警機(jī)制:腳本可以配置成在監(jiān)控到異常行為時(shí),自動(dòng)發(fā)送通知郵件
腳本示例
#!/bin/bash
# 作者: 阿杰
# 用途: 實(shí)時(shí)檢測(cè)登錄日志,統(tǒng)計(jì)異常登錄
# 腳本名稱: watch_secure.sh
# 用法: bash watch_seacure.sh
# 日志記錄
log_err() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S')]: \033[31mERROR: \033[0m$@\n"
}
log_info() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S')]: \033[32mINFO: \033[0m$@\n"
}
log_warning() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S')]: \033[33mWARNING: \033[0m$@\n"
}
# 初始化Map
declare -A secureMap
init() {
# 行數(shù)記錄文件
line_file_name="conf/line_file.txt"
# inode存儲(chǔ)文件
inode_file="conf/inode.txt"
# 認(rèn)證失敗文件記錄
ssh_auth_failed_file="conf/ssh_auth_failed.csv"
# 文件列表
file_array=("$line_file_name" "$inode_file" "$ssh_auth_failed_file")
# inode 文件狀態(tài)
inode_file_status=0
# 控制是否進(jìn)行寫(xiě)入 0為可寫(xiě),1為不可寫(xiě)
write_status=1
oneSecureKey=""
{
if [ ! -d "conf" ];then
mkdir conf
fi
# 檢查文件是否存在
for file in ${file_array[@]};do
check_file_exists $file
done
line=$(cat $line_file_name)
if [ -z "$line" ];then
line=0
fi
# 認(rèn)證失敗文件第一次創(chuàng)建
if [ $(wc -l < $ssh_auth_failed_file) -eq 0 ];then
# 時(shí)間以月天為單位(None為空賬號(hào)或不存在賬號(hào))
echo "登錄認(rèn)證失敗時(shí)間,源IP地址,登錄賬號(hào),連接認(rèn)證失敗次數(shù)" > $ssh_auth_failed_file
fi
}
file_name="/var/log/secure"
if [ -z "$(rpm -qa | grep 'inotify-tools')" ];then
yum install -y inotify-tools > /dev/null 2>&1
if [ $? -ne 0 ];then
log_err "[init] inotify-tools 安裝失敗!"
fi
fi
}
# 檢查文件是否存在,不存在則創(chuàng)建
check_file_exists() {
local file_name=$1
if [ ! -f "$file_name" ];then
touch $file_name
if [ $? -ne 0 ];then
log_err "[check_file_exists] file: $file_name 文件創(chuàng)建失敗!"
fi
fi
}
# 監(jiān)聽(tīng)文件事件
watch_file() {
inotifywait -mrq --format '%e' --event create,delete,modify $file_name | while read event ;do
case "$event" in
MODIFY)
start_read_file
;;
# 文件被刪除或重新創(chuàng)建
CREATE|DELETE)
# 重置文件行數(shù)
line=0
> $line_file_name
check
;;
*)
log_warning "[watch_file] watch file event: $event"
;;
esac
done
}
# 只讀一行
read_line_file() {
((line++))
echo $line > $line_file_name
# 不是指定數(shù)據(jù)退出
if [ $(sed -n "$line p" $file_name | grep 'pam_unix(sshd:auth): authentication failure;' | wc -l ) -ne 1 ];then
return
fi
# 控制是否進(jìn)行寫(xiě)入
write_status=0
oneSecureKey=$(sed -n "$line p" $file_name |awk -v dateNow=$(date +"%Y") '{
split($0,rhost,"rhost=")
split(rhost[2],rhost," ")
split($0,user," user=")
if (length(user[2])==0) {
user[2]="None"
}
print dateNow":"$1":"$2","rhost[1]","user[2]
}')
log_info "[read_line_file] line: $line data:[$oneSecureKey]"
send_map $oneSecureKey
}
# 往MAP中塞入數(shù)據(jù)
send_map() {
local key=$1
if [ -n ${secureMap[$key]} ];then
secureMap[$key]=`expr ${secureMap[$key]} + 1`
else
secureMap[$key]=1
fi
}
wirte_all_secure() {
for key in ${!secureMap[@]};do
write_one_secure $key
done
}
write_one_secure() {
local key="$@"
local data=$(grep -w -n "$key" $ssh_auth_failed_file)
if [ -n "$data" ];then
local i=$(echo $data | awk -F: '{print $1}')
local a=$(echo $data | awk -F, '{print $NF}')
sed -i "${i} s#$a#${secureMap[$key]}#" $ssh_auth_failed_file
if [ $? -ne 0 ];then
log_err "[write_secure] 寫(xiě) $ssh_auth_failed_file 文件失敗! data:[$key,${secureMap[$key]}]"
fi
else
# 新數(shù)據(jù)
echo "$key,${secureMap[$key]}" >> $ssh_auth_failed_file
if [ $? -ne 0 ];then
log_err "[write_secure] 寫(xiě) $ssh_auth_failed_file 文件失敗! data:[$key,${secureMap[$key]}]"
fi
fi
log_info "[write_secure] line: $line status: $write_status data:[$key,${secureMap[$key]}]"
}
# 啟動(dòng)前應(yīng)先檢查是否讀取過(guò)
check() {
# 檢查預(yù)存Inode是否一致
check_secure_file_inode
}
# 檢查登錄日志Inode是否一致
check_secure_file_inode() {
inode=$(ls -i $file_name | awk '{print $1}')
inode_file_data="$(cat $inode_file)"
if [ -n "$inode_file_data" ]; then
if [ $inode -ne $inode_file_data ];then
log_warning "[check_secure_file_inode] secure file inode is inconsistency"
# inode不一致,重置
echo "$inode" > $inode_file
inode_file_status=1
else
inode_file_status=0
fi
else
# 第一次讀取
echo "$inode" > $inode_file
inode_file_status=1
fi
}
# 開(kāi)始讀取文件
start_read_file() {
# 第一次讀取
if [ $inode_file_status -eq 1 ] ;then
# 使用循環(huán)將歷史內(nèi)容讀取
while true;do
if [ $line -eq $(wc -l < $file_name) ];then
break
fi
read_line_file
done
wirte_all_secure
elif [ $line -ne $(wc -l < $file_name) ];then
# 使用循環(huán)將行數(shù)對(duì)齊
while true;do
if [ $line -eq $(wc -l < $file_name) ];then
break
fi
read_line_file
if [ $write_status -eq 0 ];then
write_one_secure $oneSecureKey
fi
# 狀態(tài)設(shè)置為1
write_status=1
done
# else
# read_line_file
# if [ $write_status -eq 0 ];then
# write_one_secure $oneSecureKey
# fi
# # 狀態(tài)設(shè)置為1
# write_status=1
fi
}
test_main() {
init
check_secure_file_inode
}
main() {
# 初始化
init
# 內(nèi)容檢查
check
start_read_file
log_info "[main] watch secure startd"
watch_file
}
main到此這篇關(guān)于Bash腳本實(shí)現(xiàn)實(shí)時(shí)監(jiān)測(cè)登錄的文章就介紹到這了,更多相關(guān)Bash腳本監(jiān)測(cè)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一天一個(gè)shell命令 linux文本操作系列-wc命令詳解
這篇文章主要介紹了一天一個(gè)shell命令 linux文本操作系列-wc命令詳解,需要的朋友可以參考下2016-06-06
shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn)
本文主要介紹了shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Shell腳本之文件批量創(chuàng)建與修改的簡(jiǎn)單方法
有時(shí)需要將文件內(nèi)容進(jìn)行修改,如果文件數(shù)量不多可以一個(gè)一個(gè)修改,那么如果文件數(shù)量很多一個(gè)一個(gè)修改很麻煩,這篇文章主要給大家介紹了關(guān)于Shell腳本之文件批量創(chuàng)建與修改的相關(guān)資料,需要的朋友可以參考下2021-06-06
查詢上次Ubuntu重啟時(shí)間的方法命令總結(jié)
在大多數(shù)情況下,Linux 系統(tǒng)的關(guān)機(jī)時(shí)間、重啟日期和運(yùn)行時(shí)長(zhǎng)等調(diào)試信息在系統(tǒng)故障排錯(cuò)時(shí)會(huì)顯得比較重要,本文將詳細(xì)介紹多種方法來(lái)查詢上次 Ubuntu 重啟的時(shí)間,并解釋每種方法的背后原理,需要的朋友可以參考下2024-05-05
bash scp command not found的解決方法
今天在一系統(tǒng)上運(yùn)行bash scp提示command not found,經(jīng)過(guò)如下方法解決了,需要的朋友可以參考下2013-03-03

