目前,客户只能在发现数据或者虚拟机被恶意侵入或者用户的误操作导致了数据的丢失之后,采取善后的手段,但是并没法做到提前的预警。那么通过 PAM 模块,就可以实现用户登录及获取root 权限时,通过邮件的方式进行通知。以实现预先知道、预先警惕的目标,同时降低受影响的范围。以下是通过 PAM 模块实现的邮件通知用户登录的功能

 

1.创建脚本(/tmp/ssh/login_notify.sh),备注:该脚本可存放在服务器的任意位置,但是需要将后续的路径指定好

[root@hlmcen75n1-gen-um waagent]# cat /tmp/ssh/login_notify.sh 
#!/bin/bash

[ "$PAM_TYPE" = "open_session" ] || exit 0
{
echo "User: $PAM_USER"
echo "Ruser: $PAM_RUSER"
echo "Rhost: $PAM_RHOST"
echo "Service: $PAM_SERVICE"
echo "TTY: $PAM_TTY"
echo "Date: `date`"
echo "Server: `uname -a`"
} | mail -s "`hostname -s` $PAM_SERVICE login: $PAM_USER" user@yourdomain.com

 

2.给脚本(/tmp/ssh/login_notify.sh)添加可执行权限

[root@hlmcen75n1-gen-um ~]# chmod +x /tmp/ssh/login_notify.sh

 

3.编辑文件(/etc/pam.d/sshd),在文件最后追加一行(session optional pam_exec.so debug /bin/bash /tmp/ssh/login_notify.sh)

[root@hlmcen75n1-gen-um waagent]# cat /etc/pam.d/sshd 
#%PAM-1.0
auth       required    pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
# Used with polkit to reauthorize users in remote sessions
-auth      optional     pam_reauthorize.so prepare
account    required     pam_nologin.so
account    include      password-auth
password   include      password-auth
# pam_selinux.so close should be the first session rule
session    required     pam_selinux.so close
session    required     pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session    required     pam_selinux.so open env_params
session    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      password-auth
session    include      postlogin
# Used with polkit to reauthorize users in remote sessions
-session   optional     pam_reauthorize.so prepare
session optional pam_exec.so debug /bin/bash /tmp/ssh/login_notify.sh

 

4.至此,每一个用户登录都会发邮件通知给收件人,内容包括用户名以及登录的 IP 地址信息。如果遇到陌生 IP,那么就要注意。以此来判断该虚拟机是否正在遭受攻击,邮件示例如下:

 

5.同理,你可以在把上述脚本应用到 /etc/pam.d/ 其他模块中,比如 sudo,login 等,来进行监控

 

参考链接:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/virtual-machines/linux/aog-virtual-machines-linux-security-reinforce

 

posted on 2019-05-23 23:22  铭的随记  阅读(1822)  评论(0编辑  收藏  举报