pcap 抓包 Windows

windows 中使用pcap抓包,需要安装npcap,可以在官网(Npcap: Windows Packet Capture Library & Driver)下载安装包和SDK,进行开发。

#include <pcap.h>
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <WS2tcpip.h>

#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"Ws2_32.lib")

#define MAX_PRINT 80
#define MAX_LINE 16

#ifdef _WIN32
#include <tchar.h>
BOOL LoadNpcapDlls()
{
	CHAR npcap_dir[512];
	UINT len;
	len = GetSystemDirectory(npcap_dir, 480);
	if (!len) {
		fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
		return FALSE;
	}
	strcat_s(npcap_dir, 512,"\\Npcap");
	if (SetDllDirectory(npcap_dir) == 0) {
		fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
		return FALSE;
	}
	return TRUE;
}
#endif


/**
* IPv4 结构
*/
typedef struct {
#define IPH_GET_VER(v) (((v) >> 4) & 0x0F)
#define IPH_GET_LEN(v) (((v) & 0x0F) << 2)
	uint8_t version_len;

	uint8_t tos;
	uint16_t tot_len;
	uint16_t id;

#define IP_OFFMASK 0x1fff
	uint16_t frag_off;
	uint8_t ttl;

#define IP_PROTO_UDP  17  /* UDP protocol */
#define IP_PROTO_TCP   6  /* TCP protocol */
#define IP_PROTO_ICMP  1  /* ICMP protocol */
#define IP_PROTO_IGMP  2  /* IGMP protocol */
	uint8_t    protocol;

	uint16_t check_sum;
	uint32_t saddr;
	uint32_t daddr;
	/* The options start here. */
}  IPHDR;

/**
* ICMP 头结构
*/
typedef struct {
	IPHDR ip_hdr;
	uint8_t type;
	uint8_t code;
	uint16_t check_sum;
	/* data start here. */
}ICMPHDR;



void usage();

void packet_handler(unsigned char* param, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
	IPHDR* IpHdr = (IPHDR*)(packet + 14);
	//    printf("pkt len %d,%d\n",pkthdr->len,IpHdr->protocol);
		// 只处理ICMP
	if (IpHdr->protocol != IP_PROTO_ICMP) {
		return;
	}

	// 输出源目的地址
	struct in_addr s;
	struct in_addr d;
	s.s_addr = IpHdr->saddr;
	d.s_addr = IpHdr->daddr;
	char sipstr[30] = { 0 };
	char dipstr[30] = { 0 };
	InetNtop(AF_INET, &s.s_addr, sipstr, sizeof(sipstr));
	InetNtop(AF_INET, &d.s_addr, dipstr, sizeof(dipstr));
	printf("icmp %s --> %s\n", sipstr, dipstr);

}


int main(int argc, char** argv)
{
	pcap_t* fp=NULL;
	char errbuf[PCAP_ERRBUF_SIZE];
	char* netname = NULL;

#ifdef _WIN32
	/* Load Npcap and its functions. */
	if (!LoadNpcapDlls())
	{
		fprintf(stderr, "Couldn't load Npcap\n");
		exit(1);
	}
#endif

	if (argc == 1)
	{
		usage();
		return -1;
	}

	netname = argv[1];

	// open a capture from the network
	if (netname != NULL)
	{
		if ((fp = pcap_open_live(netname,		// name of the device
			65536,								// portion of the packet to capture. 
												// 65536 grants that the whole packet will be captured on all the MACs.
			1,									// promiscuous mode (nonzero means promiscuous)
			1000,								// read timeout
			errbuf								// error buffer
		)) == NULL)
		{
			fprintf(stderr, "\nUnable to open the adapter.\n");
			return -2;
		}

		pcap_loop(fp, 0, packet_handler, NULL);
	}
	else usage();

	

	return 0;
}


void usage()
{
	pcap_if_t* alldevs;
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_if_t* d;
	int i = 0;
	/* Retrieve the device list */
	if (pcap_findalldevs(&alldevs, errbuf) == -1)
	{
		fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
		exit(1);
	}

	/* Print the list */
	for (d = alldevs; d; d = d->next)
	{
		printf("%d. %s", ++i, d->name);
		if (d->description)
			printf(" (%s)\n", d->description);
		else
			printf(" (No description available)\n");
	}

	printf("exec <netname>\n");
	exit(0);
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值