[笔记] libpcap编译及使用

文章目录


前言

官网

libpcap库安装

环境:

centos 7.9

安装编译

libpcap 下载

bash 复制代码
tar -zxvf libpcap-1.10.4
cd libpcap-1.10.4 
./configure
make && make install

./configure 时 报错:Neither flex nor lex was found

yum install flex lex

libpcap库使用

https://www.tcpdump.org/pcap.html

此处实现的是抓tcp syn包

cpp 复制代码
#include <iostream>
#include <pcap.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ether.h>

void packetHandler(unsigned char* userData, const struct pcap_pkthdr* pkthdr, const unsigned char* packetData) {
    struct ethhdr* ethHeader = (struct ethhdr*)packetData;

    if (ntohs(ethHeader->h_proto) == ETH_P_IP) {
        struct iphdr* ipHeader = (struct iphdr*)(packetData + sizeof(struct ethhdr));

        if (ipHeader->protocol == IPPROTO_TCP) {
            struct tcphdr* tcpHeader = (struct tcphdr*)(packetData + sizeof(struct ethhdr) + ipHeader->ihl * 4);
            if (tcpHeader->syn) {
                char source_ip[INET_ADDRSTRLEN];
                char dest_ip[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, &ipHeader->saddr, source_ip, INET_ADDRSTRLEN);
                inet_ntop(AF_INET, &ipHeader->daddr, dest_ip, INET_ADDRSTRLEN);
                printf("Received TCP SYN packet from %s:%u to %s:%u\n",
                        source_ip, ntohs(tcpHeader->source),
                        dest_ip, ntohs(tcpHeader->dest));
            }
        }
    }
}

int main2(int argc, char *argv[])
{
	char *dev, errbuf[PCAP_ERRBUF_SIZE];

	dev = pcap_lookupdev(errbuf);
	if (dev == NULL) {
		fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
		return(2);
	}
	printf("Device: %s\n", dev);
	return(0);
}

int main3() {
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_if_t* alldevs;
    pcap_if_t* device;

    // 获取系统上的所有网络设备
    if (pcap_findalldevs(&alldevs, errbuf) == -1) {
        std::cerr << "Error finding devices: " << errbuf << std::endl;
        return 1;
    }

    // 遍历并打印设备列表
    int deviceCount = 0;
    for (device = alldevs; device != nullptr; device = device->next) {
        deviceCount++;
        std::cout << "Device " << deviceCount << ": " << device->name << std::endl;
        if (device->description)
            std::cout << "    Description: " << device->description << std::endl;
        else
            std::cout << "    Description: N/A" << std::endl;
    }

    // 释放设备列表
    pcap_freealldevs(alldevs);

    return 0;
}

int main() {
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* handle;

    // 打开网络设备或捕获文件,这里使用默认网络设备 "eth0",你需要根据实际情况修改
    handle = pcap_open_live("ens33", BUFSIZ, 1, 1000, errbuf);

    if (handle == nullptr) {
        std::cerr << "Error opening device: " << errbuf << std::endl;
        return 1;
    }

    // 开始捕获数据包,packetHandler 是回调函数,每捕获一个数据包都会调用它
    if (pcap_loop(handle, 0, packetHandler, nullptr) < 0) {
        std::cerr << "Error in pcap_loop" << std::endl;
        return 1;
    }

    // 关闭捕获会话
    pcap_close(handle);

    return 0;
}

注意:

使用vscode,c++ debug时 链接pcap库 g++执行加"-lpcap"

并且 加 "-std=gnu++0x" 避免c++11特性不支持报错

总结

以上就是今天要讲的内容,本文仅仅简单介绍了libpcap安装和使用。

参考:
libpcap库使用
libpcap简单使用


关于博主

wx/qq:binary-monster/1113673178

CSDN:https://blog.csdn.net/qq1113673178

码云:https://gitee.com/shiver

Github: https://github.com/ShiverZm

个人博客:www.shiver.fun

相关推荐
半壶清水1 小时前
[软考网规考点笔记]-软件开发、项目管理与知识产权核心知识与真题解析
网络·笔记·压力测试
tq10862 小时前
先探索,后设计
笔记
hnult2 小时前
2026 在线培训考试系统选型指南:核心功能拆解与选型逻辑
人工智能·笔记·课程设计
AI视觉网奇2 小时前
ue 角色驱动衣服 绑定衣服
笔记·学习·ue5
三水不滴3 小时前
计网ping原理
经验分享·笔记·计算机网络
prog_61033 小时前
【笔记】思路分享:各种大模型免费当agent后台
笔记·大语言模型·agent·cursor
凯尔萨厮3 小时前
Maven(Windows下载安装)
笔记·maven
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [drivers][input]serio
linux·笔记·学习
菩提小狗4 小时前
小迪安全2023-2024|第5天:基础入门-反弹SHELL&不回显带外&正反向连接&防火墙出入站&文件下载_笔记|web安全|渗透测试|
笔记·安全·web安全
Wentao Sun5 小时前
致敬软件创业者2026
笔记·程序人生