日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

发送自定义IP包(测试中:第二版)

發(fā)布時(shí)間:2023/11/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 发送自定义IP包(测试中:第二版) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?發(fā)送自定義IP包:
??????? public struct ip_hdr?? //IP頭
??????? {
?????????? public byte h_lenver; //4位首部長度+4位IP版本號
?????????? public byte tos; //8位服務(wù)類型TOS
?????????? public ushort total_len; //16位總長度(字節(jié))
?????????? public ushort ident; //16位標(biāo)識(shí)
?????????? public ushort frag_and_flags; //3位標(biāo)志位+13報(bào)片偏移
?????????? public byte ttl; //8位生存時(shí)間 TTL
?????????? public byte proto; //8位協(xié)議 (TCP, UDP 或其他)
?????????? public ushort checksum; //16位IP首部校驗(yàn)和
?????????? public uint sourceIP; //32位源IP地址
?????????? public uint destIP; //32位目的IP地址?
??????? }
??????? public struct tcp_hdr? //TCP頭
??????? {
??????????? public ushort th_sport; //16位源端口
??????????? public ushort th_dport; //16位目的端口
??????????? public uint th_seq; //32位序列號
??????????? public uint th_ack; //32位確認(rèn)號
??????????? public byte th_lenres; //4位首部長度/6位保留字
??????????? public byte th_flag; //6位標(biāo)志位
??????????? public ushort th_win; //16位窗口大小
??????????? public ushort th_sum; //16位校驗(yàn)和
??????????? public ushort th_urp; //16位緊急數(shù)據(jù)偏移量
??????? }
??????? public struct psd_hdr??? //TCP偽首部,用來計(jì)算校驗(yàn)和,無意義
??????? {
??????????? public long saddr; //源地址
??????????? public long daddr; //目的地址
??????????? public byte mbz;
??????????? public byte ptcl; //協(xié)議類型
??????????? public ushort tcpl; //TCP長度
??????? }
//開始
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? //檢測是否填寫完整
??????????? if (t_locIP.Text == "" || t_locPort.Text == "" || t_remoIP.Text == "" || t_remoPort.Text == "")
??????????? { MessageBox.Show("本地IP,端口,遠(yuǎn)程IP,端口必填!"); return; }
??????????? if (radioButton2.Checked && t_count.Text == "")
??????????????? return;
??????????? //創(chuàng)建原始套接字
??????????? Socket s;
??????????? try
??????????? {
??????????????? s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
??????????????? s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, false);
??????????? }
??????????? catch (SocketException ee)
??????????? { MessageBox.Show("創(chuàng)建原始套接字失敗!\r\n"+ee.Message.ToString()); return; }
???????????
??????????? //創(chuàng)建IP頭
??????????? ip_hdr myip_hdr = new ip_hdr();
??????????? tcp_hdr mytcp_hdr = new tcp_hdr();
??????????? myip_hdr.h_lenver = (byte)(4 << 4 | Marshal.SizeOf(myip_hdr) / sizeof(uint));
??????????? myip_hdr.total_len = (ushort)(Marshal.SizeOf(myip_hdr) + Marshal.SizeOf(mytcp_hdr));
??????????? myip_hdr.ident = 1;
??????????? myip_hdr.frag_and_flags = 0;
??????????? myip_hdr.ttl = 128;
??????????? myip_hdr.proto = 6;
??????????? myip_hdr.checksum = 0;
??????????? myip_hdr.sourceIP=(uint)IPAddress.Parse(t_locIP.Text).Address;
??????????? myip_hdr.destIP = (uint)IPAddress.Parse(t_remoIP.Text).Address;

??????????? //創(chuàng)建TCP頭
???????????
??????????? mytcp_hdr.th_sport = Convert.ToUInt16(t_locPort.Text);
??????????? mytcp_hdr.th_dport = Convert.ToUInt16(t_remoPort.Text);
??????????? mytcp_hdr.th_seq=0x12345678;//32位序列號
??????????? mytcp_hdr.th_ack = 0;
??????????? mytcp_hdr.th_lenres = (byte)(Marshal.SizeOf(mytcp_hdr) / 4 << 4 | 0);
??????????? mytcp_hdr.th_flag = 2;//修改這里來實(shí)現(xiàn)不同的標(biāo)志位探測,2是SYN,1是FIN,16是ACK探測 等等
??????????? mytcp_hdr.th_win = 512;
??????????? mytcp_hdr.th_urp = 0;
??????????? mytcp_hdr.th_sum = 0;

??????????? //偽tcp頭
??????????? psd_hdr mypsd_hdr = new psd_hdr();
??????????? mypsd_hdr.saddr = myip_hdr.sourceIP;
??????????? mypsd_hdr.daddr = myip_hdr.destIP;
??????????? mypsd_hdr.mbz = 0;
??????????? mypsd_hdr.ptcl = 6;
??????????? mypsd_hdr.tcpl =(ushort) Marshal.SizeOf(mytcp_hdr);

??????????? //計(jì)算校驗(yàn)和
??????????? byte[] psdbytes = StructToBytes(mypsd_hdr);
??????????? byte[] tcpbytes = StructToBytes(mytcp_hdr);
??????????? byte[] buffer=new byte[psdbytes.Length+tcpbytes.Length];
??????????? psdbytes.CopyTo(buffer, 0);
??????????? tcpbytes.CopyTo(buffer, psdbytes.Length);
??????????? UInt16[] myarray1 = byteToUint16(buffer);
??????????? mytcp_hdr.th_sum = checksum(myarray1, myarray1.Length);

??????????? byte[] ipbytes = StructToBytes(myip_hdr);
??????????? buffer=new byte[ipbytes.Length+tcpbytes.Length];
??????????? ipbytes.CopyTo(buffer,0);
??????????? tcpbytes.CopyTo(buffer, ipbytes.Length);
??????????? UInt16[] myarray2 = byteToUint16(buffer);
??????????? myip_hdr.checksum = checksum(myarray2,myarray2.Length);
??????????? ipbytes = StructToBytes(myip_hdr);
??????????? ipbytes.CopyTo(buffer, 0);??? //buffer即為要發(fā)送的偽IP包
???????????
??????????? //發(fā)送ip包
??????????? IPEndPoint remoEnd = new IPEndPoint(IPAddress.Parse(t_remoIP.Text), Convert.ToInt16(t_remoPort.Text));
??????????? try
??????????? {
??????????????? s.SendTo(buffer, remoEnd);
??????????????? MessageBox.Show("發(fā)送成功!發(fā)送的數(shù)據(jù)包為:\r\n"+DisplayByte(buffer));
??????????? }
??????????? catch (SocketException ex)
??????????? {
??????????????? MessageBox.Show("發(fā)送數(shù)據(jù)過程中出錯(cuò):\r\n" + ex.Message.ToString());
??????????? }
??????? }
//計(jì)算校驗(yàn)和
??????? 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);
??????? }

//struct類型轉(zhuǎn)換成byte[]
??????? public static byte[] StructToBytes(object structObj)
??????? {
??????????? int size = Marshal.SizeOf(structObj);
??????????? IntPtr buffer = Marshal.AllocHGlobal(size);
??????????? try
??????????? {
??????????????? Marshal.StructureToPtr(structObj, buffer, false);
??????????????? byte[] bytes = new byte[size];
??????????????? Marshal.Copy(buffer, bytes, 0, size);
??????????????? return bytes;
??????????? }
??????????? finally
??????????? {
??????????????? Marshal.FreeHGlobal(buffer);
??????????? }

??????? }??
//byte[]轉(zhuǎn)換成uint16[]
??????? public static UInt16[] byteToUint16(byte[] putin)
??????? {
??????????? double dlong = Convert.ToDouble(putin.Length);
??????????? double dtemp = Math.Ceiling(dlong/2);
??????????? int intlong = Convert.ToInt16(dtemp);
??????????? UInt16[] retArray=new UInt16[intlong];
??????????? int flag = 0;
??????????? for (int i = 0; i < intlong; i++)
??????????? {
??????????????? retArray[i] = BitConverter.ToUInt16(putin, flag);
??????????????? flag += 2;
??????????? }
??????????? return retArray;
??????? }
//顯示byte[]的內(nèi)容
??????? public static string DisplayByte(byte[] putin)
??????? {
??????????? string retstr = "";
??????????? for (int i = 0; i < putin.Length; i++)
??????????????? retstr += putin[i].ToString("X2")+" ";
??????????? return retstr;
??????? }

轉(zhuǎn)載于:https://www.cnblogs.com/tuyile006/archive/2006/12/05/583400.html

總結(jié)

以上是生活随笔為你收集整理的发送自定义IP包(测试中:第二版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。