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 动作:%s 33[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 动作:%s 33[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 动作:%s 33[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 动作:%s 33[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
'''


湖北省武汉市洪山区 1F
这脚本跑起来CPU炸了吧?
印度尼西亚 2F
红队IP列表写死的?实战不灵活啊🤔
湖南省衡阳市 3F
之前搞过类似告警,XSS去重真头疼
上海市 4F
dirscan.dict哪来的?有标准字典推荐吗?
北京市 B1
@ 大侠霍元乙 字典文件自己写的?
北京市 5F
检测逻辑看着有点硬编码了…
韩国 B1
@ 静默小能手 红队IP写死太不灵活了
四川省成都市 6F
吃瓜看红蓝对抗,挺刺激😂
重庆市 7F
SQL注入就靠两个URL匹配?太简单了吧
湖北省黄冈市 B1
@ 月照心 URL匹配太简单了,实战会被绕过吧
广东省佛山市南海区 8F
pygame放生产环境???hhh
浙江省 9F
感觉告警声音一响整个机房都知道了666
天津市 10F
新手问下:这个UDP接收能抗住高并发吗?
福建省厦门市思明区 11F
这个告警逻辑有点死板啊
湖南省长沙市 12F
pygame放告警也太秀了😂
浙江省宁波市 13F
并发高了UDP会不会丢包?
北京市 14F
XSS去重确实麻烦,之前搞过类似的
澳大利亚 15F
CPU占用看着就高
上海市 16F
声音告警有点意思,比邮件直接
北京市 17F
看不懂代码,但感觉挺厉害的
日本 18F
这脚本听着就卡,UDP收那么多包不丢才怪
印度 19F
CPU炸了都不意外,pygame这种玩意儿也敢上
澳大利亚 20F
这告警音乐不能换吗,有点吵
日本 B1
@ WebVoyager 可以关掉,设置里有选项
北京市 21F
XSS去重真头疼,搞过一次直接放弃治疗了
福建省厦门市 22F
红队IP写死?改个地址不就绕了,太僵了
广东省深圳市 23F
告警声音一响,全办公室都跟着紧张😂
日本 24F
求问dirscan.dict有公开的推荐字典吗?
辽宁省锦州市 25F
SQL注入检测就靠两个URL?这也能防住?
重庆市 26F
pygame放生产?谁批的啊hhh
陕西省延安市 27F
代码看不懂,但感觉挺硬核的
辽宁省抚顺市 28F
之前搞告警系统,XSS去重搞到吐血
韩国 29F
这逻辑全写在if里,改个规则得重写吧
陕西省西安市 30F
UDP高并发肯定丢包啊,没缓冲队列怎么扛
重庆市 31F
声音告警确实直接,比邮件强多了
山东省青岛市 32F
告警还播WAV文件,挺会玩
上海市杨浦区 33F
能跑起来就不错了,别管那么多细节了666