原始套接字
1、原始套接字的概述
什麼是原始套接字(SOCK_RAW)
1、一種不同於SOCK_STREAM、SOCK_DGRAM的套接字,它實現於系統核心
2、可以接收本機網卡上所有的數據幀(數據包),對於監聽網絡流量和分析網絡數據很有作用
3、開發人員可發送自己組裝的數據包到網絡上
4、廣泛應用於高級網絡編程
5、網絡專家、黑客通常會用此來編寫奇特的網絡程序
流式套接字只能收發
TCP協議的數據
數據報套接字只能收發
UDP協議的數據
原始套接字可以收發
1、內核沒有處理的數據包,因此要訪問其他協議
2、發送的數據需要使用,原始套接字(SOCK_RAW)
ubuntu12.04中描述網絡協議結構的文件如下
在TCP/IP協議棧中的每一層爲了能夠正確解析出上層的數據包,從而使用一些“協議類型”來標記,詳細如下圖
組裝/拆解udp數據包流程
1.1、UDP封包格式
1.2、IP封包格式
1.3、Ethernet封包格式
1.3、TCP封包格式
1.5、ICMP封包格式:ping
2、創建原始套接字(socket)
int socket(PF_PACKET, SOCK_RAW, protocol)
功能:
創建鏈路層的原始套接字
參數:
protocol:指定可以接收或發送的數據包類型
ETH_P_IP:IPV4數據包
ETH_P_ARP:ARP數據包
ETH_P_ALL:任何協議類型的數據包
返回值:
成功(>0):鏈路層套接字
失敗(<0):出錯
頭文件:
#include <sys/socket.h>
#include <netinet/ether.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/ether.h>
int main()
{
//創建一個鏈路層 通信的原始套接字
int fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
printf("fd = %d\n", fd);
close(fd);
return 0;
}
注意運行 時加sudo
案例:數據包的分析
鏈路層數據格式
demo:recvfrom接受鏈路層幀數據
案例:網絡分析器:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/ether.h>
int main()
{
//1、 創建一個原始套接字 ETH_P_ALL收發任何數據類型
int sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sockfd < 0)
{
perror("socket");
return 0;
}
printf("sockfd = %d\n", sockfd);
//2、使用recvfrom接受網絡數據 數據很多
while(1)
{
//定義buf存放幀數據 大小1500 unsigned char
unsigned char buf[1500]="";
int len = recvfrom(sockfd, buf, sizeof(buf),0,NULL,NULL);
printf("len = %d\n", len);
//buf不要用%s遍歷 幀數大多都是不識別的ASCII值 有太多的0x00
//printf("buf=%s\n",buf);
//sleep(1);//別sleep會丟失數據
//解析buf-->mac頭信息-->必須明白mac頭的結構
//1、mac頭部:目的mac(6B) 源mac(6B) 類型(2B)
//[mac][ip][tcp/udp][data] ff:ff:ff:ff:ff:ff
char src_mac[18]="";
char dst_mac[18]="";
sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x",\
buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x",\
buf[0+6],buf[1+6],buf[2+6],buf[3+6],buf[4+6],buf[5+6]);
printf("%s--->%s\n", src_mac, dst_mac);
//判斷mac頭部中協議類型 0x0800 IP 0x0806 ARP 0x8035 RARP
unsigned short mac_type = ntohs(*(unsigned short *)(buf+12));
if( mac_type == 0x0800 )
{
printf("mac_type = %#x IP報文\n",mac_type);
//2、分析IP頭部
unsigned char *ip_addr = buf+14;//+14跳過mac頭
//ip_addr跳到源IP的起始位置
ip_addr += 12;
char src_ip[16]="";
char dst_ip[16]="";
sprintf(src_ip,"%d.%d.%d.%d", \
ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
ip_addr += 4;
sprintf(dst_ip,"%d.%d.%d.%d", \
ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
printf("%s--->%s\n",src_ip,dst_ip);
//判斷完成網路層的上一層協議類型
ip_addr = buf+14;
unsigned char *ip_type = ip_addr +9;
if(*ip_type == 1)
{
printf("ICMP報文\n");
}
else if(*ip_type == 2)
{
printf("IGMP報文\n");
}
else if(*ip_type == 6)
{
printf("TCP報文\n");
ip_addr = buf+14;//ip報文起始位置
int ip_head_len = (*ip_addr&0x0f)*4;//提取ip報文的頭部長度
unsigned char *tcp_addr = buf+14+ip_head_len;
unsigned src_port = ntohs(*(unsigned short *)tcp_addr);
unsigned dst_port = ntohs(*(unsigned short *)(tcp_addr+2));
printf("%hu--->%hu\n", src_port, dst_port);
//調到tcp首部長度的位置
unsigned char *tcp_headLen_addr = tcp_addr+12;
int tcp_head_len = ((*tcp_headLen_addr>>4)&0x0f)*4;
printf("TCP:%s\n", tcp_addr+tcp_head_len);
}
else if(*ip_type == 17)
{
printf("UDP報文\n");
ip_addr = buf+14;//ip報文起始位置
int ip_head_len = (*ip_addr&0x0f)*4;//提取ip報文的頭部長度
unsigned char *udp_addr = buf+14+ip_head_len;
unsigned short src_port = ntohs(*(unsigned short *)udp_addr);
unsigned short dst_port = ntohs(*(unsigned short *)(udp_addr+2));
printf("%hu--->%hu\n", src_port, dst_port);
printf("%s\n", udp_addr+8);//應用層數據
}
}
else if(mac_type == 0x0806)
{
printf("mac_type = %#x ARP報文\n",mac_type);
}
else if(mac_type == 0x8035)
{
printf("mac_type = %#x RARP報文\n",mac_type);
}
}
//關閉套接字
close(sockfd);
return 0;
}
運行結果: