IP header的结构由RFC 791定义,如图(第一行每个数字代表一个bit位):0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|Version|  IHL  |Type of Service|          Total Length         |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|         Identification        |Flags|      Fragment Offset    |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|  Time to Live |    Protocol   |         Header Checksum       |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                       Source Address                          |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                    Destination Address                        |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                    Options                    |    Padding    |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
然后TCP header的结构是由RFC 793定义的:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|          Source Port          |       Destination Port        |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                        Sequence Number                        |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                    Acknowledgment Number                      |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|  Data |           |U|A|P|R|S|F|                               || Offset| Reserved  |R|C|S|S|Y|I|            Window             ||       |           |G|K|H|T|N|N|                               |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|           Checksum            |         Urgent Pointer        |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|                    Options                    |    Padding    |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

摘自:

  1. https://www.binarytides.com/raw-sockets-c-code-linux/

  2. https://www.binarytides.com/raw-socket-programming-in-python-linux/

  3. https://wordpress.youran.me/python-raw-socket-programming/

/*Raw TCP packetsSilver Moon (m00n.silv3r@gmail.com)
*/
#include<stdio.h>   //for printf
#include<string.h> //memset
#include<sys/socket.h>  //for socket ofcourse
#include<stdlib.h> //for exit(0);
#include<errno.h> //For errno - the error number
#include<netinet/tcp.h> //Provides declarations for tcp header
#include<netinet/ip.h>  //Provides declarations for ip header/* 96 bit (12 bytes) pseudo header needed for tcp header checksum calculation
*/
struct pseudo_header
{u_int32_t source_address;u_int32_t dest_address;u_int8_t placeholder;u_int8_t protocol;u_int16_t tcp_length;
};/*Generic checksum calculation function
*/
unsigned short csum(unsigned short *ptr,int nbytes)
{register long sum;unsigned short oddbyte;register short answer;sum=0;while(nbytes>1) {sum+=*ptr++;nbytes-=2;}if(nbytes==1) {oddbyte=0;*((u_char*)&amp;oddbyte)=*(u_char*)ptr;sum+=oddbyte;}sum = (sum>>16)+(sum &amp; 0xffff);sum = sum + (sum>>16);answer=(short)~sum;return(answer);
}int main (void)
{//Create a raw socketint s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);if(s == -1){//socket creation failed, may be because of non-root privilegesperror("Failed to create socket");exit(1);}//Datagram to represent the packetchar datagram[4096] , source_ip[32] , *data , *pseudogram;//zero out the packet buffermemset (datagram, 0, 4096);//IP headerstruct iphdr *iph = (struct iphdr *) datagram;//TCP headerstruct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));struct sockaddr_in sin;struct pseudo_header psh;//Data partdata = datagram + sizeof(struct iphdr) + sizeof(struct tcphdr);strcpy(data , "ABCDEFGHIJKLMNOPQRSTUVWXYZ");//some address resolutionstrcpy(source_ip , "192.168.1.2");sin.sin_family = AF_INET;sin.sin_port = htons(80);sin.sin_addr.s_addr = inet_addr ("1.2.3.4");//Fill in the IP Headeriph->ihl = 5;iph->version = 4;iph->tos = 0;iph->tot_len = sizeof (struct iphdr) + sizeof (struct tcphdr) + strlen(data);iph->id = htonl (54321);    //Id of this packetiph->frag_off = 0;iph->ttl = 255;iph->protocol = IPPROTO_TCP;iph->check = 0;     //Set to 0 before calculating checksumiph->saddr = inet_addr ( source_ip );   //Spoof the source ip addressiph->daddr = sin.sin_addr.s_addr;//Ip checksumiph->check = csum ((unsigned short *) datagram, iph->tot_len);//TCP Headertcph->source = htons (1234);tcph->dest = htons (80);tcph->seq = 0;tcph->ack_seq = 0;tcph->doff = 5; //tcp header sizetcph->fin=0;tcph->syn=1;tcph->rst=0;tcph->psh=0;tcph->ack=0;tcph->urg=0;tcph->window = htons (5840);    /* maximum allowed window size */tcph->check = 0;    //leave checksum 0 now, filled later by pseudo headertcph->urg_ptr = 0;//Now the TCP checksumpsh.source_address = inet_addr( source_ip );psh.dest_address = sin.sin_addr.s_addr;psh.placeholder = 0;psh.protocol = IPPROTO_TCP;psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data) );int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + strlen(data);pseudogram = malloc(psize);memcpy(pseudogram , (char*) &amp;psh , sizeof (struct pseudo_header));memcpy(pseudogram + sizeof(struct pseudo_header) , tcph , sizeof(struct tcphdr) + strlen(data));tcph->check = csum( (unsigned short*) pseudogram , psize);//IP_HDRINCL to tell the kernel that headers are included in the packetint one = 1;const int *val = &amp;one;if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0){perror("Error setting IP_HDRINCL");exit(0);}//loop if you want to flood :)while (1){//Send the packetif (sendto (s, datagram, iph->tot_len , 0, (struct sockaddr *) &amp;sin, sizeof (sin)) < 0){perror("sendto failed");}//Data send successfullyelse{printf ("Packet Send. Length : %d \n" , iph->tot_len);}}return 0;
}//Complete

2019-12-07 IP header的结构由RFC 791定义相关推荐

  1. 2019.12.07日常总结

    二分答案 在昨天的日记里,我们粗略的谈了谈二分.今天,我们来讲讲二分的一个分支--二分答案. 从宏观的角度来讲,二分答案分为整数二分和实数二分.二者的区别就是二分的区间是整数还是实数而已. 整数二分是 ...

  2. JAVA网络编程:TCP/IP数据包结构

    2019独角兽企业重金招聘Python工程师标准>>> 一般来说,网络编程我们仅仅须要调用一些封装好的函数或者组件就能完毕大部分的工作,可是一些特殊的情况下,就须要深入的理解网络数据 ...

  3. 2019.01.07|区块链技术头条

    2019.01.07|区块链技术头条 1.科普 | 深处的蚁穴:与 Gas 相关的三种安全问题 2.科普 | OmiseGo 将如何把 Plasma 带入寻常百姓家 3.干货 | 详解 MimbleW ...

  4. TCP/IP数据包结构分解

    一般来说,网络编程我们只需要调用一些封装好的函数或者组件就能完成大部分的工作,但是一些特殊的情况下,就需要深入的理解 网络数据包的结构,以及协议分析.如:网络监控,故障排查等-- IP包是不安全的,但 ...

  5. TCP/IP数据包结构详解

    一般来说,网络编程我们只需要调用一些封装好的函数或者组件就能完成大部分的工作,但是一些特殊的情况下,就需要深入的理解 网络数据包的结构,以及协议分析.如:网络监控,故障排查等-- IP包是不安全的,但 ...

  6. TCP/IP数据包结构具体解释

    [关键词] TCP IP 数据包 结构 具体解释 网络 协议 一般来说,网络编程我们仅仅须要调用一些封装好的函数或者组件就能完毕大部分的工作,可是一些特殊的情况下,就须要深入的理解 网络数据包的结构, ...

  7. TCP/IP协议头部结构与解析

    参考  http://blog.sina.com.cn/s/blog_634d74310102vmfn.html IP协议 IP协议(Internet Protocol)是网络层协议,用在因特网上,T ...

  8. 2019.12.31大一练习赛

    2019.12.31大一练习赛 寒假培训第一天,老师认为我们题做得太快了,于是晚上搞了一场练习赛. 第1题 QWQ和QAQ Description QWQ的朋友QAQ开了一个A工厂,但QAQ不是一个很 ...

  9. 主流RGBD数据集简介 2019.12.15

    RGBD 数据集简介,2019.12.15 NYU Depth Dataset V2(3D分割任务) 数据集地址: https://cs.nyu.edu/~silberman/datasets/nyu ...

最新文章

  1. 搭建App主流框架_纯代码搭建(OC)
  2. linux c++ 程序运行时间,总结UNIX/LINUX下C++程序计时的方法
  3. 【SQL】SQL(基础查询)、SQL(关联查询)
  4. spring boot实现导出数据到excel
  5. 可变大小区(Variable-Size Extents)
  6. mysql use index用法_MySQL中USE INDEX 和 FORCE INDEX
  7. c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3
  8. 负数在计算机中如何表示?
  9. 【折腾的一个小玩意】基于jquery+百度音乐的音乐外链小工具
  10. java 抽象类和接口2--什么时候用接口什么时候用抽象类
  11. 将多个集合合并成没有交集的集合
  12. Windows2003 安装MVC4 环境的步骤
  13. 21模块-orientation【管理设备的方向信息】
  14. C/C++面试宝典2022版(最新版)
  15. 华为设备配置备份,配置文件导入导出,设备配置恢复,设备镜像上传
  16. jJava基础篇--IO流
  17. 微信小程序实现word,excell等文件下载
  18. OCCT教程二:在visual studio上创建一个occt工程
  19. AP AUTOSAR ——Diagnostic Management
  20. java必备英语单词

热门文章

  1. 多组input文件,每组 multiple选择多张图片上传可增删其中任意一张图片,用formData对象实现(ajax,sync: false同步)
  2. 仿土豆网显示隐藏遮罩案例(HTML、CSS)
  3. 微信小程序自动检测更新新版本
  4. 解决docker push镜像到docker hub报没有权限
  5. 自适应采样次数的Ransac算法
  6. python使用rpa需要什么插件_使用Python制作ArcGIS插件基础篇——工具介绍
  7. springboot testcontext @sql_SpringBoot图文教程11—从此不写mapper文件「集成MybatisPlus」...
  8. Haclon 一些关于显示的基本算子(1)
  9. 不敢下水游泳,莫慌!VR带你畅游水底世界
  10. markdown的基础语言