• 08_原始套接字


    知识点1【原始套接字概述】

     1、UDP封包格式

    ​IP封包格式:

    Ethernet封包格式 

    TCP封包格式: 

     ICMP封包格式:ping

    知识点2【数据包的分析】

     案例:网络分析器:

    知识点2【混杂模式】接受数据(了解) 

    知识点3【原始套接字发送数据】sendto 

    1、本机的接口地址结构

    2、获取我们的本地接口

    知识点4【案例:扫描mac地址 ARP】

     ARP协议格式:

    知识点1【原始套接字概述】

    ubuntu12.04中描述网络协议结构的文件如下

     在TCP/IP协议栈中的每一层为了能够正确解析出上层的数据包,从而使用一些“协议类型”来标记,详细如下图

    组装/拆解udp数据包流程

     1、UDP封包格式

    IP封包格式:

    Ethernet封包格式 

    TCP封包格式: 

     ICMP封包格式:ping

    知识点2【数据包的分析】

    链路层数据格式

     demo:recvfrom接受链路层帧数据

     案例:网络分析器:

    1. #include<stdio.h>
    2. #include<sys/socket.h>
    3. #include<netinet/ether.h>
    4. int main()
    5. {
    6. //1、 创建一个原始套接字 ETH_P_ALL收发任何数据类型
    7. int sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    8. if(sockfd < 0)
    9. {
    10. perror("socket");
    11. return 0;
    12. }
    13. printf("sockfd = %d\n", sockfd);
    14. //2、使用recvfrom接受网络数据 数据很多
    15. while(1)
    16. {
    17. //定义buf存放帧数据 大小1500 unsigned char
    18. unsigned char buf[1500]="";
    19. int len = recvfrom(sockfd, buf, sizeof(buf),0,NULL,NULL);
    20. printf("len = %d\n", len);
    21. //buf不要用%s遍历 帧数大多都是不识别的ASCII值 有太多的0x00
    22. //printf("buf=%s\n",buf);
    23. //sleep(1);//别sleep会丢失数据
    24. //解析buf-->mac头信息-->必须明白mac头的结构
    25. //1、mac头部:目的mac(6B) 源mac(6B) 类型(2B)
    26. //[mac][ip][tcp/udp][data] ff:ff:ff:ff:ff:ff
    27. char src_mac[18]="";
    28. char dst_mac[18]="";
    29. sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x",\
    30. buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
    31. sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x",\
    32. buf[0+6],buf[1+6],buf[2+6],buf[3+6],buf[4+6],buf[5+6]);
    33. printf("%s--->%s\n", src_mac, dst_mac);
    34. //判断mac头部中协议类型 0x0800 IP 0x0806 ARP 0x8035 RARP
    35. unsigned short mac_type = ntohs(*(unsigned short *)(buf+12));
    36. if( mac_type == 0x0800 )
    37. {
    38. printf("mac_type = %#x IP报文\n",mac_type);
    39. //2、分析IP头部
    40. unsigned char *ip_addr = buf+14;//+14跳过mac头
    41. //ip_addr跳到源IP的起始位置
    42. ip_addr += 12;
    43. char src_ip[16]="";
    44. char dst_ip[16]="";
    45. sprintf(src_ip,"%d.%d.%d.%d", \
    46. ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
    47. ip_addr += 4;
    48. sprintf(dst_ip,"%d.%d.%d.%d", \
    49. ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
    50. printf("%s--->%s\n",src_ip,dst_ip);
    51. //判断完成网路层的上一层协议类型
    52. ip_addr = buf+14;
    53. unsigned char *ip_type = ip_addr +9;
    54. if(*ip_type == 1)
    55. {
    56. printf("ICMP报文\n");
    57. }
    58. else if(*ip_type == 2)
    59. {
    60. printf("IGMP报文\n");
    61. }
    62. else if(*ip_type == 6)
    63. {
    64. printf("TCP报文\n");
    65. ip_addr = buf+14;//ip报文起始位置
    66. int ip_head_len = (*ip_addr&0x0f)*4;//提取ip报文的头部长度
    67. unsigned char *tcp_addr = buf+14+ip_head_len;
    68. unsigned src_port = ntohs(*(unsigned short *)tcp_addr);
    69. unsigned dst_port = ntohs(*(unsigned short *)(tcp_addr+2));
    70. printf("%hu--->%hu\n", src_port, dst_port);
    71. //调到tcp首部长度的位置
    72. unsigned char *tcp_headLen_addr = tcp_addr+12;
    73. int tcp_head_len = ((*tcp_headLen_addr>>4)&0x0f)*4;
    74. printf("TCP:%s\n", tcp_addr+tcp_head_len);
    75. }
    76. else if(*ip_type == 17)
    77. {
    78. printf("UDP报文\n");
    79. ip_addr = buf+14;//ip报文起始位置
    80. int ip_head_len = (*ip_addr&0x0f)*4;//提取ip报文的头部长度
    81. unsigned char *udp_addr = buf+14+ip_head_len;
    82. unsigned short src_port = ntohs(*(unsigned short *)udp_addr);
    83. unsigned short dst_port = ntohs(*(unsigned short *)(udp_addr+2));
    84. printf("%hu--->%hu\n", src_port, dst_port);
    85. printf("%s\n", udp_addr+8);//应用层数据
    86. }
    87. }
    88. else if(mac_type == 0x0806)
    89. {
    90. printf("mac_type = %#x ARP报文\n",mac_type);
    91. }
    92. else if(mac_type == 0x8035)
    93. {
    94. printf("mac_type = %#x RARP报文\n",mac_type);
    95. }
    96. }
    97. //关闭套接字
    98. close(sockfd);
    99. return 0;
    100. }

     运行结果:

     

    知识点2【混杂模式】接受数据(了解) 

    linux下设置

    1、设置混杂模式:ifconfig eth0 promisc

    2、取消混杂模式:ifconfig eth0 -promisc

    linux下通过程序设置网卡混杂模式: 

    知识点3【原始套接字发送数据】sendto 

    1. sendto(sock_raw_fd, msg, msg_len, 0,(struct sockaddr*)&sll, sizeof(sll));
    2. 注意:
    3. 1、sock_raw_fd:原始套接字
    4. 2、msg:发送的消息(封装好的协议数据)
    5. 3、sll:本机网络接口,指发送的数据应该从本机的哪个网卡出去,而不是以前的目的地址
    6. 想一想:
    7. 如何定义sll?

    原始套接字:组帧数据报文----->从本机的哪块网卡sendto发出去

    1、本机的接口地址结构

    1. #include
    2. struct sockaddr_ll sll;

    只需要对sll.sll_ifindex赋值,就可使用

    1. sll.sll_ifindex=本地接口;//关键就是本地接口如何获得
    2. sendto(sock_raw_fd, msg, msg_len, 0,(struct sockaddr*)&sll, sizeof(sll));

    2、获取我们的本地接口

    通过ioctl来获取网络接口地址

    1. struct ifreq#include
    2. IFNAMSIZ 16

     ioctl参数对照表:

    知识点4【案例:扫描mac地址 ARP】

    ARP概述

    ARP(Address Resolution Protocol,地址解析协议)

    1、是TCP/IP协议族中的一个

    2、主要用于查询指定ip所对应的的MAC

    3、请求方使用广播来发送请求

    4、应答方使用单播来回送数据

    5、为了在发送数据的时候提高效率在计算中会有一个ARP缓存表,用来暂时存放ip所对应的MAC,在linux中使用ARP即可查看,在xp中使用ARP -a

    在linux与xp系统下查看ARP的方式:

    以机器A获取机器B的MAC为例:

     ARP协议格式:

     

     

     

     

  • 相关阅读:
    IDEA gradle新增依赖报Could not resolve symbol “XXX“错误
    【TensorFlow深度学习】张量Broadcasting机制与数学运算实践
    Redis-cluster集群详细部署配置--有手就行
    跳动的爱心源码
    嵌入式系统,典型嵌入式系统基本组成,微处理器,嵌入式微处理器,嵌入式软硬件裁减原则,嵌入式实时操作系统
    rabbitMQ 面试题
    Jetson Nano TensorRT C++加速 YOLOV5,集成进qt项目中
    图像配准之图像重采样
    如何使用chorme版本对应的ChromeDriver(不用更改Chrome版本)
    用ChatGPT+Midjourney 5分钟生成30条爆款小红书图文(内有详细教程)
  • 原文地址:https://blog.csdn.net/buhuidage/article/details/127952882