python获取http流量并预警

文章讲述了如何利用Scapy库中的Sniff方法进行网络包嗅探,包括设置过滤规则、回调函数处理数据包,以及通过正则表达式检测SQL注入和XSS攻击。同时,文章还介绍了如何监控IP活动,当特定IP行为异常时触发警告。
摘要由CSDN通过智能技术生成
  • Sniff 方法定义:
    count:抓取报的数量,设置为0时则一直捕获
    store:保存抓取的数据包或者丢弃,1保存,0丢弃
    offline:从pcap文件中读取数据包,而不进行嗅探,默认为None
    prn:为每个数据包定义一个回调函数,通常使用lambda表达式来写回调函数
    filter:过滤规则,可以在里面定义winreshark里面的过滤语法,使用 Berkeley Packet Filter (BPF)语法,具体参考:[http://blog.csdn.net/qwertyupoiuytr/article/details/54670477]
    L2socket:使用给定的L2socket
    timeout:在给定的事件后停止嗅探,默认为None
    opened_socket:对指定的对象使用.recv进行读取
    stop_filter:定义一个函数,决定在抓到指定的数据之后停止
    iface:指定抓包的网卡,不指定则代表所有网卡

  • fileter方法:
    type(定义了类型)
    可选值:host, net, port, portrange
    例如:
    host hostnameA
    net 172.31            //相当于172.31.0.0/16,又例如:192.168.1相当于192.168.1.0/24
    port 80
    portrange 6000-6010
    
    dir(direction,定义了传输方向)
    可选值:src, dst, src or dst, src and dst
    例如:
    src net 172.31
    src or dst port 21
    
    proto(protocol定义了网络协议)
    可选值:ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp, udp, icmp
    (fddi, tr, wlan是ether的别名, 包结构很类似)
    例如:
    ether src hostnameA
    arp net 172.31
    udp portrange 7000-8000
    
    连接词:and, or, not
    例如:
    tcp or udp
    not icmp

  • 进行使用
    from scapy.all import *
    import re
    import time
    
    filters = "tcp and port 80"
    
    def result(pkg):  #回调函数将包传回
        try:
            # print(pkg.show())
            srcip = pkg['IP'].src   #获取源IP
            # print(ip)
            if srcip != '192.168.18.141':   #排除自己
                pay = pkg['Raw'].load.decode().split('\r\n')   #list形式存储数据
                if 'POST' in pay[0]:   #POST检查
                    if get_post(pay[-1]):
                        print(f"IP:{srcip},可能出现攻击,payload:{pay[-1]}")
                if 'GET' in pay[0]:  #GET检查
                    if get_get(pay[0]):
                        print(f"IP:{srcip},可能出现攻击,payload:{pay[0]}")
                blast(srcip)
        except:
            pass
    
    def get_post(payload):
        # print(payload)
        sql_rgex = r"union.+select|select.+\d|updatexml\(|into.+outfile|extractvalue\(|information_schema"
        xss_rgex = r"script|img|embed|frame"
        if re.search(sql_rgex,payload,re.I):   #正则查找,忽略大小写
            return True
        if re.search(xss_rgex,payload,re.I):
            return True
    
    def get_get(payload):
        sql_rgex = r"union.+select|select.+\d|updatexml\(|into.+outfile|extractvalue\(|information_schema"
        xss_rgex = r"script|img|embed|frame"
        if re.search(sql_rgex,payload,re.I):
            return True
        if re.search(xss_rgex,payload,re.I):
            return True
    
    ip_count = {} #设置全局变量
    def blast(ip):
        t = int(time.strftime('%S', time.localtime()))  #规定时间
        global ip_count  #在变量中修改全局变量
        if ip not in ip_count:
            ip_count[ip] = 1
        else:
            ip_count[ip] += 1
        if ip_count[ip] > 5: # 如果IP出现超过5次
            print(f"IP:{ip}出现了{ip_count[ip]}次,可能出现爆破攻击")
        if t / 5 == 0:
            ip_count.clear()
    
    sniff(count=0,filter=filters,prn=result)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值