发送自定义IP包:
        public struct ip_hdr   //IP头
        {
           public byte h_lenver; //4位首部长度+4位IP版本号
           public byte tos; //8位服务类型TOS
           public ushort total_len; //16位总长度(字节)
           public ushort ident; //16位标识
           public ushort frag_and_flags; //3位标志位+13报片偏移
           public byte ttl; //8位生存时间 TTL
           public byte proto; //8位协议 (TCP, UDP 或其他)
           public ushort checksum; //16位IP首部校验和
           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位确认号
            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位紧急数据偏移量
        }
        public struct psd_hdr    //TCP伪首部,用来计算校验和,无意义
        {
            public long saddr; //源地址
            public long daddr; //目的地址
            public byte mbz;
            public byte ptcl; //协议类型
            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,端口,远程IP,端口必填!"); return; }
            if (radioButton2.Checked && t_count.Text == "")
                return;
            //创建原始套接字
            Socket s;
            try
            {
                s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
                s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, false);
            }
            catch (SocketException ee)
            { MessageBox.Show("创建原始套接字失败!\r\n"+ee.Message.ToString()); return; }
           
            //创建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;

//创建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;//修改这里来实现不同的标志位探测,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);

//计算校验和
            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即为要发送的伪IP包
           
            //发送ip包
            IPEndPoint remoEnd = new IPEndPoint(IPAddress.Parse(t_remoIP.Text), Convert.ToInt16(t_remoPort.Text));
            try
            {
                s.SendTo(buffer, remoEnd);
                MessageBox.Show("发送成功!发送的数据包为:\r\n"+DisplayByte(buffer));
            }
            catch (SocketException ex)
            {
                MessageBox.Show("发送数据过程中出错:\r\n" + ex.Message.ToString());
            }
        }
//计算校验和
        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类型转换成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[]转换成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[]的内容
        public static string DisplayByte(byte[] putin)
        {
            string retstr = "";
            for (int i = 0; i < putin.Length; i++)
                retstr += putin[i].ToString("X2")+" ";
            return retstr;
        }

转载于:https://www.cnblogs.com/tuyile006/archive/2006/12/05/583400.html

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

  1. Teardrop攻击——发送虚假IP包信息

    Teardrop攻击--发送虚假IP包信息 一.原始套接字概述 二.Teardrop攻击原理阐述 三.编写Teardrop程序(伪造一个虚假地址的IP包) 四.参考链接 一.原始套接字概述 原始套接字 ...

  2. Kali Linux Web渗透测试手册(第二版) - 1.3 - 靶机的安装

    Kali Linux Web渗透测试手册(第二版) - 1.3 - 靶机的安装  一.配置KALI Linux和渗透测试环境 在这一章,我们将覆盖以下内容: 在Windows和Linux上安装Virt ...

  3. python构造自定义数据包_pytorch中的自定义数据处理详解

    pytorch在数据中采用Dataset的数据保存方式,需要继承data.Dataset类,如果需要自己处理数据的话,需要实现两个基本方法. :.getitem:返回一条数据或者一个样本,obj[in ...

  4. IP包的生成和发送接口(1)

    http://blog.sina.com.cn/s/indexlist_1657348185_2.html   IP包的生成和发送接口 ==================== (1) Linux内核 ...

  5. TCP/IP协议簇中ARP协议

    目录 1.ARP协议简介 2.ARP协议结构 3.wireshark抓包分析 4.ARP协议分类 5.ARP协议应用 6.ARP攻击 1.ARP协议简介 在<IP协议>中我们讲解了IP地址 ...

  6. qq自定义diy名片代码复制_「正点原子FPGA连载」第六章自定义IP核-呼吸灯实验

    1)摘自[正点原子]领航者 ZYNQ 之嵌入式开发指南 2)实验平台:正点原子领航者ZYNQ开发板 3)平台购买地址:https://item.taobao.com/item.htm?&id= ...

  7. Maltab在数学建模中的应用(第二版)——读书笔记上

    Maltab在数学建模中的应用(第二版)--读书笔记上 1.MATLAB与数据文件的交互 1.1数据拟合 1.2数据拟合实例 1.3数据可视化 1.4层次分析法 2.规划问题的MATLAB求解(多约束 ...

  8. 黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第三章 网络工程-原始套接字与嗅探(1)主机发现工具与包嗅探

    黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第三章 网络工程-原始套接字与嗅探(1)主机发现工具 文章目录 黑帽python第二版(Black Ha ...

  9. win10中Charles从下载安装到证书设置和雷电模拟器或浏览器中抓包测试

    一.下载安装及证书设置 1.在Charles官网https://www.charlesproxy.com/download/下载,我这边下载的是免费体验版的. 体验版用一段时间就会退出,这里另外提供给 ...

最新文章

  1. 英伟达的雄心:成为AI时代的计算平台
  2. 计算机2级access,计算机二级-Access-窗体的设计视图
  3. 多商户商城源码_如何利用多商户B2B2C多商户商城系统后台组件玩转商城?
  4. android 能自动选择的listview,Android ListView多选模式
  5. python双循环_双for循环到Python列表理解
  6. PDF文件实现在线盖章
  7. 从零开始手写 VIO
  8. Tapestry(二):Tapestry基本知识
  9. c语言初步实验报告,c语言实验报告(大一c语言实验报告答案)
  10. VC++ 绘制线条 OnLButtonDown函数(DrawView.cpp) 利用SDK全局函数实现画线功能 利用MFC的CDC类实现画线功能 利用MFC的CClientDC类实现画线功能
  11. 莫比乌斯进阶:bzoj 3994 约数个数和(Mobius)
  12. 学数据结构堆襸_gbk编码 - osc_6pogm9r5的个人空间 - OSCHINA - 中文开源技术交流社区...
  13. AlphaFold2源码解析(3)--数据预处理
  14. 【转】cpu降频问题
  15. 笔记本连接手机热点通过网线给其他电脑联网
  16. 210. 课程表 II(拓扑排序,Kahn 算法)
  17. C#设计模式系列 9 ----Facade外观模式之--天河城购物
  18. latex三线表格绘制
  19. Mc1.16forge官混教程/教补-#5 官混方块写法差异与为方块赋予常见属性
  20. 中西医结合内科学考试宝典 太平洋软件

热门文章

  1. 定义并调用函数输出 fibonacci 序列_科学网—Zmn-0351 薛问天:再谈数学概念的定义,评新华先生《0345》...
  2. 160 - 5 ajj.2
  3. C++ 内存基本构件new [] /delete []的意义、内存泄漏原因、VC下cookie的基本布局
  4. leetcode 42. 接雨水 思考分析(暴力、动态规划、双指针、单调栈)
  5. php 邮件验证_PHP程序来验证电子邮件地址
  6. web安全----xss工具使用3
  7. 两数的最大公约数算法基础及优化
  8. hystrix 单独使用_Hystrix学习
  9. Educational Codeforces Round 73 (Rated for Div. 2)
  10. 贪心算法——选择不相交区间问题