红蓝演练告警waf_v2

OnionT@KillBoy
OnionT@KillBoy
作者
47
文章
0
粉丝
渗透测试378,2991字数 792阅读2分38秒阅读模式
AI智能摘要
红蓝演练中,如何实时捕捉红队攻击痕迹?本文揭秘一套基于WAF告警日志的自动化监测方案。通过解析网络流量中的特征行为,精准识别SQL注入、目录扫描等典型攻击手段,并结合IP黑白名单与正则匹配实现快速告警。代码级实战演示,助你构建灵敏高效的防守响应机制,特别适用于安全演练中的威胁感知环节。
— AI 生成的文章内容摘要
#!coding:utf-8
from socket import *
import pygame,re,os

dirscan = []
dirscanku = open('dirscan.dict','r')
tmpSip = ''
tmpDip = ''
tmpevent_type = ''
tmpxss1 = ''#xss告警太多,去重
URL1 = '/user/loginid/?id=3'
URL2 = '/user/loginid/?id=-'

#!红队IP
Sip = ['210.72.243.18','114.242.71.226','114.242.71.227','114.242.71.228','114.242.71.229','114.242.71.230','114.242.71.231','114.242.71.232','114.242.71.233','114.242.71.234','114.242.71.235','114.242.71.236','114.242.71.237','114.242.71.238','114.242.71.239','114.242.71.240','114.242.71.241','114.242.71.242','114.242.71.243','114.242.71.244','114.242.71.245','114.242.71.246','114.242.71.247','114.242.71.248','114.242.71.249','114.242.71.250','114.242.71.251','114.242.71.252','114.242.71.253','114.242.71.254','1.50.104.62','14.135.74.35','203.93.167.161']


#读取dirscan特征
for line in dirscanku.readlines():
    line = line.strip('n')
    dirscan.append(line)
    #print dirscan 调试


#接收WAF告警
revlog = socket(AF_INET,SOCK_DGRAM)
addr = ('172.16.26.120',515)
revlog.bind(addr)
while True:
    receive_data = revlog.recvfrom(204800)
    date = receive_data[0]#decode('gbk')# 存储接收的数据
    addr = receive_data[1]
    Dev_ip = addr[0]
    port = addr[1]

    #!匹配目标
    try:
        src_ip = str(re.findall("src_ip:+(d+S+)",date)[0])
        dst_ip = str(re.findall("dst_ip:+(d+S+)",date)[0])
        alertlevel = str(re.findall("alertlevel:+(S+)",date)[0])
        event_type = str(re.findall("event_type:+(S+)",date)[0])
        action = str(re.findall("action:+(S+)",date)[0])
    except:
        pass
    print "=",#日志接收状态

    #!监测红队sql注入
    try:
        uri = str(re.findall("uri:+(S{1,19})",date)[0])
    except:
        pass

    #监测红队dirscan特征
    try:
        dir1 = str(re.findall("uri:+(S+)%[0][A]",date)[0])
        method = str(re.findall("method:+(S+)",date)[0])
    except:
        pass
    #监测红队xss特征
    try:
        URLxss = str(re.findall("uri:+.*(alert(d+))",date)[0])
    except:
        pass

    #监测红队IP
    #!去重
    try:
        if tmpSip == src_ip:
            if tmpDip == dst_ip:
                if tmpevent_type == event_type:
                    continue
    except:
        pass

    try:
        #!告警
        if src_ip in Sip:
            tmpSip = src_ip
            tmpDip = dst_ip
            pygame.mixer.init()
            track = pygame.mixer.music.load(r"/root/work/anquan/script/listing/hw/MP3/222.wav")
            pygame.mixer.music.play()
            #print "==="*10
            print "===== This is WAF =====n"
            print "33[1;35m检测到红队攻击IP: %s 目标服务器IP:%s 安全事件:%s 危险等级:%s 动作:%s33[0mn"%(src_ip,dst_ip,event_type,alertlevel,action)
            print "==="*10
    except:
        pass

    #!sql注入告警
    try:
        #!告警
        if uri == URL1:
            tmpSip = src_ip
            tmpDip = dst_ip
            pygame.mixer.init()
            track = pygame.mixer.music.load(r"/root/work/anquan/script/listing/hw/MP3/222.wav")
            pygame.mixer.music.play()
            #print "==="*10
            print "===== This is WAF =====n"
            print "33[1;32m检测到红队攻击IP: %s 目标服务器IP:%s 安全事件:%s 危险等级:%s 动作:%s33[0mn"%(src_ip,dst_ip,event_type,alertlevel,action)
            print "==="*10
    except:
        pass

    try:
        #!告警
        if uri == URL2:
            tmpSip = src_ip
            tmpDip = dst_ip
            pygame.mixer.init()
            track = pygame.mixer.music.load(r"/root/work/anquan/script/listing/hw/MP3/222.wav")
            pygame.mixer.music.play()
            #print "==="*10
            print "===== This is WAF =====n"
            print "33[1;32m检测到红队攻击IP: %s 目标服务器IP:%s 安全事件:%s 危险等级:%s 动作:%s33[0mn"%(src_ip,dst_ip,event_type,alertlevel,action)
            print "==="*10
    except:
        pass

    #dirscan告警
    try:
        #!告警
        if dirscan.index(dir1):
            if 'GET' in method:
                print '××××××××针对dirscan的监测××××%s×××××'%method
                if 'HTTP_Protocol_Validation' in event_type:
                    tmpSip = src_ip
                    tmpDip = dst_ip
                    pygame.mixer.init()
                    track = pygame.mixer.music.load(r"/root/work/anquan/script/listing/hw/MP3/222.wav")
                    pygame.mixer.music.play()
                    #print "==="*10
                    print "===== This is WAF =====n"
                    print "33[1;36m检测到红队攻击IP: %s 目标服务器IP:%s 安全事件:%s 危险等级:%s 动作:%s33[0mn"%(src_ip,dst_ip,event_type,alertlevel,action)
                    print "==="*10
    except:
        pass

    
    '''wafxss告警太多,暂时用IPS替代

    #xss告警
    try:
        #!告警
        if URLxss:
            if 'Cross_Site_Scripting' in event_type:
                print '××××××针对xss的告警×××××'
                tmpSip = src_ip
                tmpDip = dst_ip
                tmpxss1 = event_type
                pygame.mixer.init()
                track = pygame.mixer.music.load(r"/root/work/anquan/script/listing/hw/MP3/222.wav")
                pygame.mixer.music.play()
                #print "==="*10
                print "===== This is WAF =====n"
                print "检测到红队攻击IP: %s 目标服务器IP:%s 安全事件:%s 危险等级:%s 动作:%sn"%(src_ip,dst_ip,event_type,alertlevel,action)
                print "==="*10
    except:
        pass
    '''

红蓝演练告警waf_v2

 
OnionT@KillBoy
评论  37  访客  37
    • 青栀巷
      青栀巷 1

      这脚本跑起来CPU炸了吧?

      • 七巧板拼图家
        七巧板拼图家 1

        红队IP列表写死的?实战不灵活啊🤔

        • 时光残片
          时光残片 0

          之前搞过类似告警,XSS去重真头疼

          • 大侠霍元乙
            大侠霍元乙 1

            dirscan.dict哪来的?有标准字典推荐吗?

              • 社交牛逼症晚期
                社交牛逼症晚期 0

                @ 大侠霍元乙 字典文件自己写的?

              • 静默小能手
                静默小能手 0

                检测逻辑看着有点硬编码了…

                • 晨跑日记
                  晨跑日记 1

                  吃瓜看红蓝对抗,挺刺激😂

                  • 月照心
                    月照心 0

                    SQL注入就靠两个URL匹配?太简单了吧

                      • 雾中莲华
                        雾中莲华 0

                        @ 月照心 URL匹配太简单了,实战会被绕过吧

                      • 小鹿花花
                        小鹿花花 0

                        pygame放生产环境???hhh

                        • 寂凛
                          寂凛 1

                          感觉告警声音一响整个机房都知道了666

                          • 会跳舞的土豆
                            会跳舞的土豆 1

                            新手问下:这个UDP接收能抗住高并发吗?

                            • 天线宝宝他二舅
                              天线宝宝他二舅 0

                              这个告警逻辑有点死板啊

                              • 铁血风
                                铁血风 0

                                pygame放告警也太秀了😂

                                • 夜梦行客
                                  夜梦行客 1

                                  并发高了UDP会不会丢包?

                                  • 窗边人
                                    窗边人 1

                                    XSS去重确实麻烦,之前搞过类似的

                                    • 幻梦泡沫
                                      幻梦泡沫 1

                                      CPU占用看着就高

                                      • 韵语嫣然
                                        韵语嫣然 1

                                        声音告警有点意思,比邮件直接

                                        • 金源贸易
                                          金源贸易 0

                                          看不懂代码,但感觉挺厉害的

                                          • 熊猫小团子
                                            熊猫小团子 1

                                            这脚本听着就卡,UDP收那么多包不丢才怪

                                            • 碧空澈
                                              碧空澈 1

                                              CPU炸了都不意外,pygame这种玩意儿也敢上

                                              • WebVoyager
                                                WebVoyager 1

                                                这告警音乐不能换吗,有点吵

                                                  • VR旅行家
                                                    VR旅行家 1

                                                    @ WebVoyager 可以关掉,设置里有选项

                                                  • 清韵
                                                    清韵 0

                                                    XSS去重真头疼,搞过一次直接放弃治疗了

                                                    • 青简
                                                      青简 0

                                                      红队IP写死?改个地址不就绕了,太僵了

                                                      • 无尽路途
                                                        无尽路途 0

                                                        告警声音一响,全办公室都跟着紧张😂

                                                        • 小安
                                                          小安 0

                                                          求问dirscan.dict有公开的推荐字典吗?

                                                          • 月光兔
                                                            月光兔 1

                                                            SQL注入检测就靠两个URL?这也能防住?

                                                            • 流浪画
                                                              流浪画 0

                                                              pygame放生产?谁批的啊hhh

                                                              • 应龙翔天
                                                                应龙翔天 0

                                                                代码看不懂,但感觉挺硬核的

                                                                • 微信隐身人
                                                                  微信隐身人 1

                                                                  之前搞告警系统,XSS去重搞到吐血

                                                                  • 量子迷踪
                                                                    量子迷踪 1

                                                                    这逻辑全写在if里,改个规则得重写吧

                                                                    • 甜梦果
                                                                      甜梦果 0

                                                                      UDP高并发肯定丢包啊,没缓冲队列怎么扛

                                                                      • 暴躁的咖啡豆
                                                                        暴躁的咖啡豆 1

                                                                        声音告警确实直接,比邮件强多了

                                                                        • 钟馗随从
                                                                          钟馗随从 1

                                                                          告警还播WAV文件,挺会玩

                                                                          • 噬魂魔刃
                                                                            噬魂魔刃 0

                                                                            能跑起来就不错了,别管那么多细节了666

                                                                          匿名

                                                                          发表评论

                                                                          匿名网友

                                                                          拖动滑块以完成验证