python当作Suricata主动响应脚本

  • 封锁IP威胁等级为1的情况下
    import os
    import re
    import time
    
    
    def read():
        files = open('/var/log/suricata/fast.log')  #控制指针
        file=('/var/log/suricata/fast.log')  #阅读
        count=files.seek(0,2)  #指针直接到最后
        agex = r"Priority:\s\d"  #获取等级正则
        pcre = r"(?<=\s)\d+.\d+.\d+.\d+"  #获取IP正则
        my = ('127.0.0.1','192.168.18.141')  #白名单IP
        # print(count)
        while True:
            with open(file, mode='r', encoding='utf8') as f:
                # print(f.read())
                if count != 0:
                    # print(count)
                    f.seek(count,0) #移动指针到count位置
                    book = f.readline().strip()
                    # print(book)
                    level=re.findall(agex,book)
                    for leve in level:
                        if '1' in leve:  #等级为1
                            read=re.findall(pcre,book)
                            # print(read)
                            for r in read:
                                if r not in my:
                                    # print(r)
                                    fire_walld(r)
    
    
    def fire_walld(i):
        print(i)
        cmd = f"firewall-cmd --add-rich-rule='rule family=ipv4 source address={i} reject' --timeout=30"
        res = os.popen(cmd).read()
        if 'success' in res:
            print(f'已经将{i}封禁30s')
            read()
        else:
            print('失败')
            exit()
    
    if __name__ == "__main__":
        read()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值