pcap 抓包 Windows

2023-10-14 05:59
文章标签 windows 抓包 pcap

本文主要是介绍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 0x1fffuint16_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);// 只处理ICMPif (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);}
#endifif (argc == 1){usage();return -1;}netname = argv[1];// open a capture from the networkif (netname != NULL){if ((fp = pcap_open_live(netname,		// name of the device65536,								// 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 timeouterrbuf								// 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);elseprintf(" (No description available)\n");}printf("exec <netname>\n");exit(0);
}

 

这篇关于pcap 抓包 Windows的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/208619

相关文章

Windows Server服务器上配置FileZilla后,FTP连接不上?

《WindowsServer服务器上配置FileZilla后,FTP连接不上?》WindowsServer服务器上配置FileZilla后,FTP连接错误和操作超时的问题,应该如何解决?首先,通过... 目录在Windohttp://www.chinasem.cnws防火墙开启的情况下,遇到的错误如下:无法与

Python解析器安装指南分享(Mac/Windows/Linux)

《Python解析器安装指南分享(Mac/Windows/Linux)》:本文主要介绍Python解析器安装指南(Mac/Windows/Linux),具有很好的参考价值,希望对大家有所帮助,如有... 目NMNkN录1js. 安装包下载1.1 python 下载官网2.核心安装方式3. MACOS 系统安

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Windows命令之tasklist命令用法详解(Windows查看进程)

《Windows命令之tasklist命令用法详解(Windows查看进程)》tasklist命令显示本地计算机或远程计算机上当前正在运行的进程列表,命令结合筛选器一起使用,可以按照我们的需求进行过滤... 目录命令帮助1、基本使用2、执行原理2.1、tasklist命令无法使用3、筛选器3.1、根据PID

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

Windows环境下安装达梦数据库的完整步骤

《Windows环境下安装达梦数据库的完整步骤》达梦数据库的安装大致分为Windows和Linux版本,本文将以dm8企业版Windows_64位环境为例,为大家介绍一下达梦数据库的具体安装步骤吧... 目录环境介绍1 下载解压安装包2 根据安装手册安装2.1 选择语言 时区2.2 安装向导2.3 接受协议

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

在 Windows 上安装 DeepSeek 的完整指南(最新推荐)

《在Windows上安装DeepSeek的完整指南(最新推荐)》在Windows上安装DeepSeek的完整指南,包括下载和安装Ollama、下载DeepSeekRXNUMX模型、运行Deep... 目录在www.chinasem.cn Windows 上安装 DeepSeek 的完整指南步骤 1:下载并安装