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下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. 解压

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件