在项目二中,我们完成了对局域网中数据包的抓取以及对数据包头部信息的解析,接下来我们要从Wireshark已经抓取并保存好的pcapng文件中解析数据包,提取各层的含义,在本例中,我的pcapng文件包含以太网帧、PPPoE头部、PPP头部、IP头部、UDP头部以及RIP头部,最后提取数据可以还原成TS流文件,是一个电视剧片段。
这次我们的任务就是从pcapng文件中抓获一个数据包,然后把它的头部信息解析出来。
之前我们一直在用pcap_opne_live()函数来打开设备,现在我们不需要打开网卡设备去抓取实时的在线数据包了,而是从文件中读取数据包,所以我们使用pcap_open_offline()函数,来打开离线文件,从离线文件中抓取数据包并解析。
函数用法(传送门):https://blog.csdn.net/zjl_1026_2001/article/details/2202436
使用Wireshark观察一下离线文件

数据包中的内容就是我上面分析的内容,我们来看一下PPPoE和PPP协议的PDU头部:

这里我们自定义一个PPPoE的头文件:PPPoE.h
#include <linux/ip.h>
struct PPPoEhdr {
__u8 version:4,
type:4;
__u8 code;
__be16 session_id;
__be16 length;
};
接下来是分析并输出的代码,代码中有较为详细的注释,希望对大家有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include "PPPoE.h"
//输出以太网帧头部信息
void print_ether_header_info(struct ether_header *eptr){
int i;
u_char *ptr;
printf("Enthernet II:\n");
ptr = eptr->ether_dhost;//目的MAC地址
i = ETHER_ADDR_LEN;
printf("Destination MAC addres: ");
do{
printf ("%s%02x", (i == ETHER_ADDR_LEN)?"":":", *ptr++);
}while(--i>0);
printf ("\n");
ptr = eptr->ether_shost;//源MAC地址
i = ETHER_ADDR_LEN;
printf("Sourse MAC address: ");
do{
printf ("%s%02x", (i == ETHER_ADDR_LEN)?"":":", *ptr++);
}while(--i>0);
printf("\n");
printf("Enthernet type: %#04x ",ntohs(eptr->ether_type));//负载类型
switch(ntohs(eptr->ether_type)){
case ETHERTYPE_IP:
printf("(IPv4)\n");
break;
case ETHERTYPE_ARP:
printf("(ARP)\n");
break;
case ETHERTYPE_REVARP:
printf("(RARP)\n");
break;
case 0X8864:
printf("(PPPoE)\n");
break;
default:
printf("(Other)\n");
break;
}
}
//输出PPPoE头部信息
void print_PPPoE_header_info(struct PPPoEhdr *PPPoEptr){
char *c;
c = (char *)(PPPoEptr);
printf("\nPPP-over-Ethernet Session:\n");
printf("Version: %d\n",PPPoEptr->version);//版本
printf("Type: %d\n",PPPoEptr->type);//类型
printf("Code: Session data(0x%#04x)\n",PPPoEptr->code);
//printf("Session id: %d\n",ntohs(PPPoEptr->session_id));
printf("Session id: 0x%02x%02x\n",(*(c + 2)),(*(c + 3)));
printf("Length: %d\n",ntohs(PPPoEptr->length));//长度
}
//输出PPP头部信息
void print_PPP_header_info(int i,const u_char *packet){
printf("\nPoint-to-Point Portocol:\n");
if(packet[i]==0x00 && packet[i+1]==0x21)
printf("Protocol: Internet Portocol version 4(0x0021)\n");
else
printf("Protocol: Unknow(0x%02x%02x)",packet[i],packet[i+1]);
}
//输出IP头部信息
void print_ip_header_info(struct iphdr *ipptr){
struct in_addr addr;
char *c;
c = (char *)(ipptr);
printf("\nInternet Portocol:\n");
printf("Version: %d\n",ipptr->version);//版本号
printf("Header length: %d\n",ipptr->ihl);//头部长度
printf("Total length: %d\n",ntohs(ipptr->tot_len));//总长度
printf("Identification: %#x\n",ipptr->id);//认证
c += 6;
printf("Flags: 0x%02x%02x",(*c),(*(c + 1)));//分片标志位DF,MF
if((*c)==0x40&&(*(c+1))==0x00)
printf(" Don't fragment\n");
else printf(" More fragment\n");
printf("Time to live: %d\n",ipptr->ttl);//TTL
printf("Protocol: ");//传输层协议
switch(ipptr->protocol){
case 1:
printf("ICMP(1)\n");
break;
case 2:
printf("IGMP(2)\n");
break;
case 6:
printf("TCP(6)\n");
break;
case 17:
printf("UDP(17)\n");
break;
default:
printf("Unknow(%d)\n",ipptr->protocol);
break;
}
c += 4;
printf("Header checksum: 0x%02x%02x\n",(*(unsigned char *)c),(*(unsigned char *)(c+1)));//检验和
addr.s_addr = ipptr->saddr;
printf("Source IP address: %s\n",inet_ntoa(addr));//源IP地址
addr.s_addr = ipptr->daddr;
printf("Destination IP address: %s\n",inet_ntoa(addr));//目的IP地址
}
//输出TCP头部信息
void print_tcp_header_info(struct tcphdr *tcpptr){
char *c = (char *)(tcpptr);
printf("\nTransmission Control Protocol(TCP):\n");
printf("Destination port: %d\n",ntohs(tcpptr->dest));//目的端口
printf("Source port: %d\n",ntohs(tcpptr->source));//源端口
printf("Seq: %u\n",ntohs(tcpptr->seq));//确认号
c += 16;
printf("Checksum: 0x%02x%02x\n",(*(unsigned char *)c),(*(unsigned char *)(c+1)));//检验和
}
//输出UDP头部信息
void print_udp_header_info(struct udphdr *udpptr){
char *c = (char *)(udpptr);
printf("\nUser Datagram Protocol(UDP):\n");
printf("Source port: %d\n",ntohs(udpptr->source));//源端口
printf("Destination port: %d\n",ntohs(udpptr->dest));//目的端口
printf("Length: %d\n",ntohs(udpptr->len));//长度
c += 6;
printf("Checksum: 0x%02x%02x\n",(*(unsigned char *)c),(*(unsigned char *)(c+1)));//检验和
}
//输出数据信息
void print_data_info(int j,const struct pcap_pkthdr *pkthdr,const u_char *packet){
int i;
printf("\nData:\n");
for(i=0;j<pkthdr->len;i++,j++){
printf(" %02x",packet[j]);
if((i+1)%16==0)
printf("\n");
}
printf("\n");
}
//回调函数
void print_info(u_char *user,const struct pcap_pkthdr *pkthdr,const u_char *packet){
int j,*id;
struct ether_header *eptr;
struct iphdr *ipptr;
struct tcphdr *tcpptr;
struct udphdr *udpptr;
struct PPPoEhdr *PPPoEptr;
j = 0,id = (int *)user;
printf("id: %d\n",++(*id));
printf("Packet length: %d\n",pkthdr->len);//包的长度
printf("Number of bytes: %d\n",pkthdr->caplen);//包的比特数
printf("Reciverd time: %s\n",ctime((const time_t *)&pkthdr->ts.tv_sec));//收到包的时间
//获取以太网帧头部
eptr = (struct ether_header*)packet;
print_ether_header_info(eptr);
j += sizeof(struct ether_header);
switch(ntohs(eptr->ether_type)){
//获取PPPoE协议头部
case 0x8864:
PPPoEptr = (struct PPPoEhdr *)(packet + sizeof(struct ether_header));
print_PPPoE_header_info(PPPoEptr);
j += sizeof(struct PPPoEhdr);
print_PPP_header_info(j,packet);
j += 2;
//获取IP数据包头部
if(packet[j-2]==0x00 && packet[j-1]==0x21){
ipptr = (struct iphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2);
print_ip_header_info(ipptr);
j += sizeof(struct iphdr);
switch(ipptr->protocol){
//获取tcp数据包
case 6:
tcpptr = (struct tcphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2 + sizeof(struct iphdr));
print_tcp_header_info(tcpptr);
j += sizeof(struct tcphdr);
break;
//获取udp数据包
case 17:
udpptr = (struct udphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2 + sizeof(struct iphdr));
print_udp_header_info(udpptr);
j += sizeof(struct udphdr);
if(packet[j]==0x80)
j += 12;
break;
default:
printf("\nThis packet didn't use TCP/UDP protocol.\n");
break;
}
}
break;
//获取IP数据包头部
case ETHERTYPE_IP:
ipptr = (struct iphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2);
print_ip_header_info(ipptr);
j += sizeof(struct iphdr);
switch(ipptr->protocol){
//获取tcp数据包
case 6:
tcpptr = (struct tcphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2 + sizeof(struct iphdr));
print_tcp_header_info(tcpptr);
j += sizeof(struct tcphdr);
break;
//获取udp数据包
case 17:
udpptr = (struct udphdr *)(packet + sizeof(struct ether_header) + sizeof(struct PPPoEhdr) + 2 + sizeof(struct iphdr));
print_udp_header_info(udpptr);
j += sizeof(struct udphdr);
break;
default:
printf("\nThis packet didn't use TCP/UDP protocol.\n");
break;
}
break;
default:
printf("\nThis packet didn't use IP/PPPoE protocol.\n");
break;
}
//获取数据,如果不是IP,不是UDP/TCP则把其他协议的头部当作数据处理
if((pkthdr->len - j) % 188 == 0)
printf("\nThis packet is a TS frame file.\n");
else
printf("\nThis packet is not TS frame file.\n");
print_data_info(j,pkthdr,packet);
printf("\n\n");
}
//主函数
int main(){
//char *device;
char errBuf[PCAP_ERRBUF_SIZE];
pcap_t *head;
int id;
//device = "ens33";//转换成ens33网卡
//打开设备device,最大字节数65535,不处于混杂模式,不抓到数据包不返回,错误提示消息
//head = pcap_open_live(device,65535,0,0,errBuf);
head = pcap_open_offline("医院镜像流.pcapng",errBuf);
if(head){
printf("Open device success!\n");
}else{
printf("Open device failed. %s\n",errBuf);
return 0;
}
//等待一个包返回
struct pcap_pkthdr packet;
const u_char *packetflag = pcap_next(head,&packet);
if(packetflag){
printf("Get a pcaket success.\n");
id = 0;
pcap_loop(head,1,print_info,(u_char *)&id);
}else{
printf("Get a pcaket failed. %s\n",errBuf);
pcap_close(head);
return 0;
}
//关闭设备,归还资源
pcap_close(head);
return 0;
}
来看一下程序运行结果:


成功解析各层PDU头部并分离出以0x47开头的TS流数据文件!
PS:本系列完整文章:
libpcap抓包分析项目(六):https://blog.csdn.net/NCU_CirclePlan/article/details/95624720
libpcap抓包分析项目(五):https://blog.csdn.net/NCU_CirclePlan/article/details/95601520
libpcap抓包分析项目(四):https://blog.csdn.net/NCU_CirclePlan/article/details/95500092
libpcap抓包分析项目(三):https://blog.csdn.net/NCU_CirclePlan/article/details/95451728
libpcap抓包分析项目(二):https://blog.csdn.net/NCU_CirclePlan/article/details/95353239
libpcap抓包分析项目(一):https://blog.csdn.net/NCU_CirclePlan/article/details/95351331
libpcap的安装:https://blog.csdn.net/NCU_CirclePlan/article/details/95342991
本系列相关的源文件已经上传,有需要的朋友可以自行下载:https://download.csdn.net/download/ncu_circleplan/11340737
本文介绍如何使用libpcap从pcapng文件中解析数据包头部信息,包括以太网帧、PPPoE、PPP、IP、UDP及RIP头部。通过pcap_open_offline()函数读取离线文件,并展示PPPoE和PPP协议PDU头部解析的代码实现,最终成功分离出TS流数据。
&spm=1001.2101.3001.5002&articleId=95451728&d=1&t=3&u=44ecfbd9cd304365ac690191810f0536)
5733

被折叠的 条评论
为什么被折叠?



