基于c语言抓包软件程序,C或者C++写的抓包工具。

满意答案

给你一个winpcap的示例代码,具体的可以根据自己的需求修改

#include

#include

#include

#define LINE_LEN 16

int main(int argc, char **argv)

{

pcap_if_t *alldevs, *d;

pcap_t *fp;

u_int inum, i=0;

char errbuf[PCAP_ERRBUF_SIZE];

int res;

struct pcap_pkthdr *header;

const u_char *pkt_data;

printf(“pktdump_ex: prints the packets of the network using WinPcap.n”);

printf(” Usage: pktdump_ex [-s source]nn”

” Examples:n”

” pktdump_ex -s file.acpn”

” pktdump_ex -s \Device\NPF_{C8736017-F3C3-4373-94AC-9A34B7DAD998}nn”);

if(argc

{

printf(“nNo adapter selected: printing the device list:n”);

/* The user didn’t provide a packet source: Retrieve the local device list */

if(pcap_findalldevs(&alldevs, errbuf) == -1)

{

fprintf(stderr,”Error in pcap_findalldevs_ex: %sn”, errbuf);

exit(1);

}

/* Print the list */

for(d=alldevs; d; d=d->next)

{

printf(“%d. %sn “, ++i, d->name);

if (d->description)

printf(” (%s)n”, d->description);

else

printf(” (No description available)n”);

}

if (i==0)

{

printf(“nNo interfaces found! Make sure WinPcap is installed.n”);

return -1;

}

printf(“Enter the interface number (1-%d):”,i);

scanf(“%d”, &inum);

if (inum i)

{

printf(“nInterface number out of range.n”);

/* Free the device list */

pcap_freealldevs(alldevs);

return -1;

}

/* Jump to the selected adapter */

for (d=alldevs, i=0; inext, i++);

/* Open the adapter */

if ((fp = pcap_open_live(d->name, // 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,”nError opening adaptern”);

return -1;

}

}

else

{

/* Do not check for the switch type (‘-s’) */

if ((fp = pcap_open_live(argv[2], // 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,”nError opening adaptern”);

return -1;

}

}

/* Read the packets */

while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)

{

if(res == 0)

/* Timeout elapsed */

continue;

/* print pkt timestamp and pkt len */

printf(“%ld:%ld (%ld)n”, header->ts.tv_sec, header->ts.tv_usec, header->len);

/* Print the packet */

for (i=1; (i caplen + 1 ) ; i++)

{

printf(“%.2x “, pkt_data[i-1]);

if ( (i % LINE_LEN) == 0) printf(“n”);

}

printf(“nn”);

}

if(res == -1)

{

printf(“Error reading the packets: %sn”, pcap_geterr(fp));

return -1;

}

pcap_close(fp);

return 0;

}

00分享举报

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览113153 人正在系统学习中 相关资源:SAMM软件保证成熟度模型落地工具-网络安全文档类资源-CSDN文库

来源:迟落有渡

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月19日
下一篇 2021年4月19日

相关推荐