WinPCAP封包协议分析

对WINPCAP简单封装,直接看代码吧。

头文件:

#pragma once
#include <pcap.h>
#include <string>


class CNetCap
{
public:
CNetCap(void);
virtual ~CNetCap(void);
pcap_if_t * m_dDevice;
pcap_if_t * m_dAllDevice;
pcap_t * m_fCap ;
u_int m_nDriverNum;
char m_bErrorBuf[PCAP_ERRBUF_SIZE];
int m_nRes;
struct pcap_pkthdr * m_sHeader;
u_char * m_cPktData ;
public:
pcap_if_t * GetAllDevice();
pcap_if_t * SelectOneDevice(int num, pcap_if_t * allDevice);
void StartCap(int num, pcap_handler handler, std::string filter);
/* prototype of the packet handler */
// virtual void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) = 0;
};


实现 :

#include "NetCap.h"
#include <iostream>


CNetCap::CNetCap(void)
{
}


CNetCap::~CNetCap(void)
{
}


pcap_if_t * CNetCap::GetAllDevice()
{
pcap_if_t * allDevice ;
if (pcap_findalldevs(&allDevice, m_bErrorBuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", m_bErrorBuf);
return NULL;
}
return allDevice;
}


pcap_if_t * CNetCap::SelectOneDevice(int num, pcap_if_t * allDevice)

int i = 0;
pcap_if_t * selectDevice;
/* Print the list */
for(selectDevice=allDevice; selectDevice; selectDevice=selectDevice->next)
{
printf("%d. %s\n    ", ++i, selectDevice->name);
if (selectDevice->description)
printf(" (%s)\n", selectDevice->description);
else
printf(" (No description available)\n");
}
if(i == 0)
{
std::cout<<"No interfaces found! Make sure WinPcap is installed.\n";
return NULL;
}


if (num < 1 || num > i)
{
std::cout<<"Interface number out of range.\n";
/* Free the device list */
pcap_freealldevs(allDevice);
return NULL;
}


for (selectDevice=allDevice, i=0; i< num-1 ;selectDevice=selectDevice->next, i++);
return selectDevice;
}


void CNetCap::StartCap(int sNum, pcap_handler handler, std::string filter)
{
bpf_u_int32 NetMask;
struct bpf_program fcode;


if((m_dAllDevice = GetAllDevice()) == NULL)
{
std::cout<<"查找设备错误!!"<<std::endl;
return;
}
m_dDevice = SelectOneDevice(sNum, m_dAllDevice);
m_fCap = pcap_open_live(m_dDevice->name, 
65536,
1, 
1000,
m_bErrorBuf);
if(m_fCap == NULL)
{
std::cout<<"打开设备错误!!"<<std::endl;
return;
}
pcap_freealldevs(m_dAllDevice);
NetMask=0xffffff;
if(pcap_compile(m_fCap, &fcode, filter.c_str(), 1, NetMask) < 0)
{
fprintf(stderr,"\nError compiling filter: wrong syntax.\n");


pcap_close(m_fCap);
return;
}
//set the filter
if(pcap_setfilter(m_fCap, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter\n");
pcap_close(m_fCap);
return;
}
pcap_loop(m_fCap, 0, handler, NULL);


pcap_close(m_fCap);  
}


调用 :

#include "NetCap.h"
#include <iostream>
#include <winsock.h> 
using namespace std;


/* 4 bytes IP address */
typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;


/* IPv4 header */
typedef struct ip_header
{
u_char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
u_char tos; // Type of service 
u_short tlen; // Total length 
u_short identification; // Identification
u_short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl; // Time to live
u_char proto; // Protocol
u_short crc; // Header checksum
ip_address saddr; // Source address
ip_address daddr; // Destination address
u_int op_pad; // Option + Padding
}ip_header;
typedef struct tcp_header
{
u_short  sport;//16位源端口
u_short dport;//目的端口
u_int seq;//32位序列号
u_int ack;//32位确认号
u_char lenres;//4位首部长度/6位保留字
u_char flag;//6位标志位
u_short win;//16位窗口大小
u_short sum;//16位检验和
u_short urp;//16位紧急数据偏移量
} tcp_hearder;

实现 PACKET_HANDLER即可

void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinPcap是一个开放源代码的Windows平台上的网络封包捕获工具。它提供了一个用于在网络上抓取数据包的接口和库,以及一系列功能强大的工具和库函数,使用户能够进行网络流量分析协议分析WinPcap的使用非常方便,用户只需安装它的驱动程序和库文件,并调用相应的API函数即可开始网络抓包。用户可以选择使用WinPcap提供的命令行工具来进行抓包,也可以使用自己开发的程序进行网络数据的捕获。 通过使用WinPcap,用户可以捕获到网络中的数据包,包括从底层网络设备(如网卡)接收到的原始数据。用户可以根据自己的需要选择捕获的数据包类型、过滤条件和捕获时机等。捕获的数据包可以保存到文件中,用户可以随后对保存的数据包进行分析和后续处理。 WinPcap支持的网络协议非常广泛,包括以太网、IP、TCP、UDP、ICMP等常见的网络协议,还支持更高层的协议如HTTP、FTP、SMTP等。用户可以根据自己的需求对特定协议的数据包进行分析,从中提取出需要的信息。 通过对网络抓包分析,用户可以了解网络流量的组成以及各个协议的运行机制,进一步发现网络中的问题和安全隐患。网络抓包分析在网络故障排除、性能优化、安全审计等方面都有广泛的应用。同时,网络抓包分析也是网络安全研究的重要手段之一,可以帮助用户发现网络攻击行为并采取相应的防御措施。 总而言之,WinPcap是一个强大的网络抓包分析工具,通过它,用户可以方便地进行网络数据包的捕获和分析,从中获得对网络运行状况和安全性的深入了解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值