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

相关文章

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Oracle数据库在windows系统上重启步骤

《Oracle数据库在windows系统上重启步骤》有时候在服务中重启了oracle之后,数据库并不能正常访问,下面:本文主要介绍Oracle数据库在windows系统上重启的相关资料,文中通过代... oracle数据库在Windows上重启的方法我这里是使用oracle自带的sqlplus工具实现的方

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压