Python武器库开发-Linux系统ssh暴力破解密码自动断阻器(二十)

Linux系统ssh暴力破解密码自动断阻器

关于Linux系统遭遇ssh暴力破解时,我们需要对相关进行暴力破解的非法IP进行封禁,提供以下的脚本,我们只需要在遭遇ssh暴力破解行为的Linux系统上运行该脚本,该脚本便会自动封禁进行ssh远程登陆暴力破解行为的IP,脚本的详细内容如下:

python 复制代码
#!/usr/bin/env python3

import re
import subprocess
import time

#安全日志
logFile = '/var/log/secure'
#黑名单
hostDeny = '/etc/hosts.deny'

#封禁阈值
password_wrong_num = 5


#获取已经加入黑名单的IP,转换为字典
def getDenies():
    deniedDict = {}
    list = open(hostDeny).readlines()
    for ip in list:
        group = re.search(r'(\d+\.\d+\.\d+\.\d+)', ip)
        if group:
            deniedDict[group[1]] = '1'
    return deniedDict


#监控方法
def monitorLog(Logfile):
    #统计密码错误的次数
    tempIp = {}
    #已经拉黑的IP
    deniedDict = getDenies()
    #读取安全日志
    popen = subprocess.Popen('tail -f' + logFile,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    #开始监控
    while True:
        time.sleep(0.1)
        line = popen.stdout.readline().strip()
        if line:
            group = re.search('Invalid user \w+ from (\d+\.\d+\.d+\.\d+)',str(line))
            #提示不存在用户直接封IP
            if group and not deniedDict.get(group[1]):
                subprocess.getoutput('echo \' sshd:{} >> {}'.format(group[1],hostDeny))
                deniedDict[group[1]] = '1'
                time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                print('{} --- add ip:{} to hosts.deny for invalid usr'.format(time_str, group[1]))
                continue

            #用户存在,但密码错误
            group = re.search('Failed password for \w+ from (\d+\.\d+\.d+\.\d+)',str(line))
            if group:
                ip = group[1]
                #统计IP登陆密码错误的次数
                if not tempIp.get(ip):
                    tempIp[1] = 1
                else:
                    tempIp[ip] = tempIp[ip] + 1

                #如果错误次数大于阈值的时候,直接封禁该IP
                if tempIp[ip] > password_wrong_num and not deniedDict.get(ip):
                    del tempIp[ip]
                    subprocess.getoutput('echo \' sshd:{} >> {}'.format(ip,hostDeny))
                    deniedDict[ip] = '1'
                    time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                    print('{} --- add ip:{} to hosts.deny for invalid password'.format(time_str, group[1]))


if __name__ == '__main__':
    monitorLog(logFile)
相关推荐
进击的荆棘2 小时前
Linux系统——线程同步与互斥
linux·运维·服务器·策略模式·同步·互斥·posix
特创数字科技2 小时前
写了个 Windows 下载器:一键拉 GitHub / Gitee 源码,带 SHA256 校验
python·github
橙橙笔记2 小时前
QT的学习第四部分
linux·qt·学习·音视频
.道阻且长.2 小时前
Linux基本开发工具:gdb和cgdb的使用
linux·运维·服务器·开发语言
叼烟扛炮3 小时前
Linux 学习笔记(五十四):内网穿透实战——用 frp 让外网访问你的私人电脑
linux·服务器·网络·笔记·学习
Android系统攻城狮3 小时前
Linux FFmpeg深度解析之av_guess_format调用流程与实战(三十六)
linux·运维·服务器·ffmpeg·ffmpeg音视频进阶
郝学胜-神的一滴3 小时前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
进击的荆棘3 小时前
Linux系统——线程概念与控制之线程封装
linux·运维·服务器·线程
天天被压力3 小时前
【A股分钟级高频数据完全手册 #09】A股历史分时交易数据接口:普通分时怎么批量下
java·人工智能·python
叼烟扛炮3 小时前
[LangChain] 学习笔记(05):提示词模板(Prompt)与少样本注入(Few-Shot)实战
linux·人工智能·笔记·学习·langchain·prompt