解析pcap抓包文件,包含pcap头、frame头、eth层、ip层、tcp/udp层等。 C语言+python部分解析...

  参考http://blog.csdn.net/koudaidai/article/details/7673082

首先看下最常见的包的层次:frame层是不是实际传输的数据,是tcpdump添加的一层。eth层如果显示linuxcooked的话是没有指定网卡抓包的原因需要tcpdump -i 指定网卡。

可能会有其他协议层,自行添加即可。例如下图2

                          

1、几个结构体

//pacp文件头结构体
struct pcap_file_header
{
    bpf_u_int32 magic;       /* 0xa1b2c3d4 */
    u_short version_major;   /* magjor Version 2 */
    u_short version_minor;   /* magjor Version 4 */
    bpf_int32 thiszone;      /* gmt to local correction */
    bpf_u_int32 sigfigs;     /* accuracy of timestamps */
    bpf_u_int32 snaplen;     /* max length saved portion of each pkt */
    bpf_u_int32 linktype;    /* data link type (LINKTYPE_*) */
};

//时间戳
struct time_val
{
    int tv_sec;         /* seconds 含义同 time_t 对象的值 */
    int tv_usec;        /* and microseconds */
};

//pcap数据包头结构体
struct pcap_pkthdr
{
    struct time_val ts;  /* time stamp */  
    bpf_u_int32 caplen; /* length of portion present */  
    bpf_u_int32 len;    /* length this packet (off wire) */ 
};

//数据帧头
typedef struct FramHeader_t
{ //Pcap捕获的数据帧头
    u_int8 DstMAC[6]; //目的MAC地址
    u_int8 SrcMAC[6]; //源MAC地址
    u_short FrameType;    //帧类型
} FramHeader_t;

//IP数据报头
typedef struct IPHeader_t
{ //IP数据报头
    u_int8 Ver_HLen;       //版本+报头长度
    u_int8 TOS;            //服务类型
    u_int16 TotalLen;       //总长度
    u_int16 ID; //标识
    u_int16 Flag_Segment;   //标志+片偏移
    u_int8 TTL;            //生存周期
    u_int8 Protocol;       //协议类型
    u_int16 Checksum;       //头部校验和
    u_int32 SrcIP; //源IP地址
    u_int32 DstIP; //目的IP地址
} IPHeader_t;

//TCP数据报头
typedef struct TCPHeader_t
{ //TCP数据报头
    u_int16 SrcPort;//源端口
    u_int16 DstPort;//目的端口
    u_int32 SeqNO;//序号
    u_int32 AckNO; //确认号
    u_int8 HeaderLen; //数据报头的长度(4 bit) + 保留(4 bit)
    u_int8 Flags; //标识TCP不同的控制消息
    u_int16 Window; //窗口大小
    u_int16 Checksum; //校验和
    u_int16 UrgentPointer;  //紧急指针
}TCPHeader_t;

//UDP数据
typedef struct UDPHeader_s
{
    u_int16_t SrcPort;     // 源端口号16bit
    u_int16_t DstPort;    // 目的端口号16bit
    u_int16_t len;        // 数据包长度16bit
    u_int16_t checkSum;   // 校验和16bit
}UDPHeader_t;
View Code

 

2、代码只是解析源ip 包大小以及时间输出到文件中,其他字段自行添加。简单的将就是read协议长度数据到对应协议结构体中输出即可。可以认为都是小端序的。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<time.h>
#include <unistd.h>
#include <sys/stat.h>

#define BUFSIZE 10240
#define STRSIZE 1024

typedef int32_t bpf_int32;
typedef u_int32_t bpf_u_int32;
typedef u_int16_t u_short;
typedef u_int32_t u_int32;
typedef u_int16_t u_int16;
typedef u_int8_t u_int8;

//pacp文件头结构体
struct pcap_file_header
{
    bpf_u_int32 magic;       /* 0xa1b2c3d4 */
    u_short version_major;   /* magjor Version 2 */
    u_short version_minor;   /* magjor Version 4 */
    bpf_int32 thiszone;      /* gmt to local correction */
    bpf_u_int32 si
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的使用libpcap库实现指定网卡、指定端口、指定抓包数量并存储到pcap文件C语言程序: ```c #include <pcap.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #define SNAP_LEN 65535 #define SIZE_ETHERNET 14 #define SIZE_TCP 20 void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet); int main(int argc, char *argv[]) { char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *handle; char *dev = argv[1]; // 网卡名 uint16_t port = atoi(argv[2]); // 端口号 int packet_count = atoi(argv[3]); // 抓包数量 char *pcap_file = argv[4]; // pcap文件名 bpf_u_int32 net = 0; bpf_u_int32 mask = 0; struct bpf_program fp; char filter_exp[100]; sprintf(filter_exp, "tcp port %d", port); // 获取网络号和掩码 if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) { fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf); net = 0; mask = 0; } // 打开网络接口 handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf); if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); exit(EXIT_FAILURE); } // 编译过滤器 if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) { fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); exit(EXIT_FAILURE); } // 设置过滤器 if (pcap_setfilter(handle, &fp) == -1) { fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle)); exit(EXIT_FAILURE); } printf("Capturing packets on %s, port %d...\n", dev, port); // 打开pcap文件 pcap_dumper_t *dumper = pcap_dump_open(handle, pcap_file); if (dumper == NULL) { fprintf(stderr, "Couldn't open pcap file %s: %s\n", pcap_file, pcap_geterr(handle)); exit(EXIT_FAILURE); } // 开始抓包 pcap_loop(handle, packet_count, packet_handler, (u_char *)dumper); // 关闭pcap文件 pcap_dump_close(dumper); // 关闭网络接口 pcap_close(handle); printf("Done.\n"); return 0; } void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { static int packet_num = 0; pcap_dump(args, header, packet); printf("Packet %d captured.\n", ++packet_num); } ``` 程序使用方法:在命令行中输入`./program eth0 80 100 packets.pcap`,其中`eth0`为网卡名,`80`为端口号,`100`为抓包数量,`packets.pcap`为pcap文件名。程序将会抓取指定网卡上指定端口的指定数量的数据包,并将其存储到指定的pcap文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值