6步教你封杀恶意登录服务器的ip

简介: 6步教你封杀恶意登录服务器的ip

如何快速将恶意IP 加入防火墙黑名单



image.png


前言


经常我们的服务器在深夜,往往会遭到Nmap 扫描,然后有很多ip 试探登录连接我们的服务器,那么我们该如何面对这种情况呢?


需求描述


分析Linux系统/var/log/secure安全日志文件,将黑客或者恶意登陆次数大于20次的IP地址加入Iptables防火墙黑名单;


实验步骤


  • 首先查看安全日志文件


[root@localhost ~]# cat  /var/log/secure|more
Jun  5 10:25:56 localhost sshd[10165]: Accepted password for root from 192.168.10.1 port 58525 ssh2
Jun  5 10:25:56 localhost sshd[10165]: pam_unix(sshd:session): session opened for user root by (uid=
0)
Jun  5 10:25:59 localhost sshd[10184]: Accepted password for root from 192.168.10.1 port 58528 ssh2
Jun  5 10:25:59 localhost sshd[10184]: pam_unix(sshd:session): session opened for user root by (uid=
0)
Jun  5 12:51:19 localhost sshd[10394]: Accepted password for root from 192.168.10.1 port 64063 ssh2
Jun  5 12:51:19 localhost sshd[10394]: pam_unix(sshd:session): session opened for user root by (uid=
0)
Jun  5 13:03:00 localhost sshd[10428]: pam_unix(sshd:auth): authentication failure; logname= uid=0 e
uid=0 tty=ssh ruser= rhost=192.168.10.1  user=root
Jun  5 13:03:00 localhost sshd[10428]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met 
by user "root"
Jun  5 13:03:02 localhost sshd[10428]: Failed password for root from 192.168.10.1 port 64400 ssh2
Jun  5 13:03:06 localhost sshd[10428]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met 
by user "root"
Jun  5 13:03:08 localhost sshd[10428]: Failed password for root from 192.168.10.1 port 64400 ssh2
Jun  5 13:03:14 localhost sshd[10428]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met 
--More--


  • 过滤其它ip,只看登录失败的ip地址


[root@localhost ~]# grep "Failed password" /var/log/secure
Jun  5 13:03:02 localhost sshd[10428]: Failed password for root from 192.168.10.1 port 64400 ssh2
Jun  5 13:03:08 localhost sshd[10428]: Failed password for root from 192.168.10.1 port 64400 ssh2
Jun  5 13:03:16 localhost sshd[10428]: Failed password for root from 192.168.10.1 port 64400 ssh2
Jun  5 13:03:27 localhost sshd[10431]: Failed password for root from 192.168.10.1 port 64438 ssh2
Jun  5 13:15:33 localhost sshd[10442]: Failed password for root from 192.168.10.10 port 49796 ssh2
Jun  5 13:15:38 localhost sshd[10442]: Failed password for root from 192.168.10.10 port 49796 ssh2
Jun  5 13:15:38 localhost sshd[10442]: Failed password for root from 192.168.10.10 port 49796 ssh2
Jun  5 13:15:46 localhost sshd[10444]: Failed password for root from 192.168.10.10 port 49798 ssh2
Jun  5 13:15:50 localhost sshd[10444]: Failed password for root from 192.168.10.10 port 49798 ssh2
Jun  5 13:15:53 localhost sshd[10444]: Failed password for root from 192.168.10.10 port 49798 ssh2
Jun  5 13:15:59 localhost sshd[10446]: Failed password for root from 192.168.10.10 port 49800 ssh2
Jun  5 13:16:00 localhost sshd[10446]: Failed password for root from 192.168.10.10 port 49800 ssh2
Jun  5 13:16:02 localhost sshd[10446]: Failed password for root from 192.168.10.10 port 49800 ssh2
[root@localhost ~]# 


  • 打印登录失败的ip


[root@localhost ~]# grep "Failed password" /var/log/secure |awk '{print$(NF-3)}'
192.168.10.1
192.168.10.1
192.168.10.1
192.168.10.1
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
192.168.10.10
[root@localhost ~]# 


  • 进行排序,统计次数


[root@localhost ~]# grep "Failed password" /var/log/secure |awk '{print$(NF-3)}'|sort|uniq -c|sort -nr
      9 192.168.10.10
      4 192.168.10.1
[root@localhost ~]# 


  • 匹配恶意登录次数大于5次的ip


[root@localhost ~]# grep "Failed password" /var/log/secure |awk '{print$(NF-3)}'|sort|uniq -c|sort -nr|awk '{if ($1>=5) print $2}'
192.168.10.10
[root@localhost ~]# 


  • 对匹配出来的做一个for循环,然后写入防火墙文件


[root@localhost ~]# for i in $(grep "Failed password" /var/log/secure|awk '{print $(NF-3)}'|sort|uniq -c|sort -nr|awk '{if($1>=5) print $2}');do sed -i "/lo/a -A INPUT -s $i -j DROP" /etc/sysconfig/iptables ;done


总结



运维安全在实际生产环境中有着很重要的地位,我们面对黑客疯狂扫描试探的时候,我就需要见流量封杀IP。如何快速封杀IP角色需要我们掌握数量掌握linux命令。特别是awk,sed。在我们脚本中很常用。一定要掌握好。


相关文章
|
3天前
|
网络协议 物联网 Linux
你不能不知道的:无公网IP,SSH远程连接CentOS服务器【内网穿透】
你不能不知道的:无公网IP,SSH远程连接CentOS服务器【内网穿透】
|
3天前
|
安全 Linux 网络安全
Windows搭建Emby媒体库服务器,无公网IP远程访问本地影音文件
Windows搭建Emby媒体库服务器,无公网IP远程访问本地影音文件
18 0
|
3天前
|
数据安全/隐私保护 Windows
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-2
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
3天前
|
存储 网络协议 文件存储
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-1
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
3天前
|
缓存 网络安全 开发工具
Git服务器报错:host key for (ip地址) has changed and you have requested strict checking
Git服务器报错:host key for (ip地址) has changed and you have requested strict checking
|
3天前
|
弹性计算 Shell Apache
某时间段访问apache 服务器的请求IP
【4月更文挑战第29天】
17 2
|
3天前
|
监控 Linux 网络安全
Linux服务器如何查询连接服务器的IP
【4月更文挑战第17天】Linux服务器如何查询连接服务器的IP
21 1
|
3天前
|
安全 Java Linux
如何实现无公网IP及服务器实现公网环境企业微信网页应用开发调试
如何实现无公网IP及服务器实现公网环境企业微信网页应用开发调试
|
3天前
|
缓存 监控 UED
IP代理如何影响网站的速度?代理ip服务器有哪些作用?
IP代理如何影响网站的速度?代理ip服务器有哪些作用?
|
3天前
|
Java Python
如何设置代理ip服务器地址
如何设置代理ip服务器地址

热门文章

最新文章