问题描述
今天刚刚连接上我刚刚买的腾讯云服务器(25岁以下可使用学生优惠价购买,可以点击这里瞅瞅),发现了这么一幕:
上次成功登录至今也不到一天的时间,竟然有12737条登录失败的请求,然后我再使用lastb命令进行查看:
很明显服务器正在被人暴力破解,于是我就打算写一个脚本来自动将这些暴力破解的IP拉到黑名单中,以防止他们继续占用我的服务器资源。
思路
思路很简单,首先通过lastb命令获取所有的登录失败的信息,然后提取出这些信息中的IP地址并对所有的IP进行计数,如果计数大于5次,则视为暴力破解者并加入黑名单。并且定时重复以上操作。
实现
实现的方法有很多,可以使用shell脚本或者其他的编程语言,这里我们采用Python进行编写。
获取所有的登录失败信息
这里,我们使用python中的os.popen模块进行读取Linux命令输出,并使用readlines()函数将结果保存至数组:
import os
infos = os.popen('lastb').readlines()
此时的输出为:
...... 前面还有很多
flw ssh:notty 8.129.166.240 Fri Mar 5 14:37 - 14:37 (00:00)
root ssh:notty 8.129.166.240 Fri Mar 5 14:29 - 14:29 (00:00)
root ssh:notty 8.129.166.240 Fri Mar 5 14:21 - 14:21 (00:00)
wasadmin ssh:notty 8.129.166.240 Fri Mar 5 14:06 - 14:06 (00:00)
zhangdw ssh:notty 8.129.166.240 Fri Mar 5 13:51 - 13:51 (00:00)
btmp begins Fri Mar 5 13:51:03 2021
可以看到,前面都是信息,最后两行信息对我们没用,我们可以使用切片只保留前面的内容,优化后的代码为:
import os
infos = os.popen('lastb').readlines()[:-2]
提取IP地址
我们需要将列表中每个IP地址提取出来,所以我们需要用到正则表达式(这里注意,re.findall()返回的是一个列表,因为每个列表中的元素仅含有一个IP,所以我们直接取第一个值[0]将IP地址取出即可):
import re
ips = [re.findall(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", i.strip())[0] for i in infos]
对IP进行计数并拉黑
到此为止,我们已经拿到了所有的IP地址,接下来我们要对所有的IP进行计数操作,如果次数大于5,便加入黑名单’/etc/hosts.deny’
# 计数
ipCount = {}
for i in ips:
if i not in ipCount:
ipCount[i] = 1
else:
ipCount[i] += 1
# 写入黑名单
msm = ""
for i in ipCount:
if ipCount[i] >= 5:
msm += f"sshd:{i}:deny\n"
with open('/etc/hosts.deny', 'w') as f:
f.write(msm)
至此,就已经完成了整个拉黑的操作。
完整源码如下
import os
import re
def main():
# 读取登录失败信息
infos = os.popen('lastb').readlines()[:-2]
# 提取IP地址
ips = [re.findall(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", i.strip())[0] for i in infos]
# IP计数
ipCount = {}
for i in ips:
if i not in ipCount:
ipCount[i] = 1
else:
ipCount[i] += 1
# 写入黑名单
msm = """#
# hosts.deny This file contains access rules which are used to
# deny connections to network services that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
"""
for i in ipCount:
if ipCount[i] >= 5:
msm += f"sshd:{i}:deny\n"
with open('/etc/hosts.deny', 'w') as f:
f.write(msm)
if __name__ == "__main__":
main()
下面是我自己运营的微信小程序“但行趣事”和公众号“微电脑”,更多的技术文章以及视频我会放到小程序和公众号当中,有志同道合的小伙伴也可以在小程序(联系客服按钮)或者公众号(直接留言)当中联系我们
|
|