SSH安全告警微信消息提醒
1.1SSH安全告警微信消息提醒
1.1.1前言
由于公司网络服务器资源限制,无法使用监控及堡垒机(防止被攻击所有编写此文章)
1.1.2服务器环境及配置说明
名称 | 性能 | 说明 |
---|---|---|
服务器 | 腾讯云 | 扩展性强 |
内存 | 4G | 前期不需要太大,所以定4G |
硬盘 | 固态 100G | 提高了硬盘的读写性能 |
CPU | 2 核 4G | 2 核 4G |
ssh_huanji.sh | 配置环境的脚本 | |
weixin.sh | 微信接口脚本 | |
Login92fuge.sh | 配置登录主脚本 | |
ip-where.sh | 归属地的来源IP的脚本 | |
ip-sswhere.sh | 定位地理位置信息 | |
sshrc | 重点的配置文件 |
1.1.3环境配置的脚本ssh_huanji.sh (次脚本在系统CentOS运行,如果需要在Ubuntu系统运行可以联系博主修改)
代码语言:javascript复制#!/bin/bash
##############################################################
# File Name: ss_huanji.sh
# Version: V1.0
# Author: guoxiangfu
# EMail: guoxiangfu@92fuge.com
# Created Time : 2022-03-31 15:10:19
# Description:
##############################################################
sh_home="/web_www/jiaoben/sh"
sh_smtp_mail="smtp.guoxiangfu.com:465"
form_smtp_mail="guoxiangfu@guoxiangfu.com"
smtp_user="guoxiangfu@92fuge.com"
smtp_password="$(cat /home/smtp_password.txt)"
if [ ! -d ${sh_home} ]; then
mkdir -p ${sh_home}
fi
yum install screen jq mailx -y
mkdir -p /root/.certs/
echo -n|openssl s_client -connect $sh_smtp_mail |sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/guoxiangfu.crt
/usr/bin/certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/guoxiangfu.crt
/usr/bin/certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/guoxiangfu.crt
cd /root/.certs/
/usr/bin/certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i ./guoxiangfu.crt
/usr/bin/certutil -L -d /root/.certs
echo "set from=$form_smtp_mail" >>/etc/mail.rc
echo "set smtp=:smtps://$sh_smtp_mail" >>/etc/mail.rc
echo "set smtp-auth-user=$smtp_user" >>/etc/mail.rc
echo "set smtp-auth-password=$smtp_password" >>/etc/mail.rc
echo 'set smtp-auth=login' >>/etc/mail.rc
echo 'set ssl-verify=ignore ' >>/etc/mail.rc
echo 'set nss-config-dir=/root/.certs ' >>/etc/mail.rc
echo -e '#!/bin/bashnbash /web_www/jiaoben/sh/Login92fuge.sh' > /etc/ssh/sshrc
脚本说明:
sh_home 脚本工作目录
/etc/mail.rc 邮件配置地址(注意请填写自己的邮箱)
1.1.4weixin.sh配置微信接口的脚本
代码语言:javascript复制#!/bin/bash
##############################################################
# File Name: weixin.sh
# Version: V5.0
# Author: guoxiangfu
# EMail: guoxiangfu@92fuge.com
# Created Time : 2022-03-31 15:15:49
# Description:
##############################################################
#set -x
CorpID="$(cat /app/userhome/wexin.txt|grep CorpID|awk -F "[ =]" '{print $2}')" #企业ID
Secret="$(cat /app/userhome/wexin.txt|grep Secret|awk -F "[ =]" '{print $2}')" #管理组凭证密钥
GETURL=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret
Token=$(/usr/bin/curl -s -G $GETURL |awk -F": '{print $4}'|awk -F" '{print $2}')
#echo $Token
POSTURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Token"
UserID=$1
#PartyID=1
agentid=1000004 #企业号中的应用id
Msg=`echo "$@" | cut -f 3`
message(){
printf '{n'
printf 't"touser": "'"$UserID""",n"
printf 't"toparty": "'"$PartyID""",n"
printf 't"msgtype": "text",n'
printf 't"agentid": "'"$agentid""",n"
printf 't"text": {n'
printf 'tt"content": "'"$Msg"""n"
printf 't},n'
printf 't"safe":"0"n'
printf '}n'
}
/usr/bin/curl --data-ascii "$(message $1 $2 $3)" $POSTURL
1.1.5归属地源IP的脚本ip-where.sh
1.1.6定位地理位置的脚本ip-sswhere.sh
1.1.7配置登录的主脚本
1.1.8配置ssh的重点配置文件sshrc
代码语言:javascript复制cat > /etc/ssh/sshrc << EOF
#!/bin/bash
bash /web_www/jiaoben/sh/Login92fuge.sh
EOF
chmod 644 /etc/ssh/sshrc