工作环境: ubuntu 16.04 桌面版
使用libpcap库的应用
http://www.stearns.org/doc/pcap-apps.html
https://eecs.wsu.edu/~sshaikot/docs/lbpcap/libpcap-tutorial.pdf
libpcap使用
https://blog.csdn.net/htttw/article/details/7521053
网络编程工程实训_实验五 使用Libpcap库实时抓包提取数据保存为TS文件
aa
python 解析库
scapy和 dpkt ,dpkt 应该是快一些
https://dpkt.readthedocs.io/en/latest/print_packets.html
dpkt和scapy对处理大pcap包分析对比
https://blog.csdn.net/qq_38231051/article/details/82019782
import socket
# import dpkt
from cap_str import fun_cap_str2,write_cap
# pip install scapy
from scapy.all import *
from scapy.layers.l2 import Ether
from scapy.packet import Packet
from scapy import sessions
import scapy
from scapy.all import *
from scapy.utils import PcapReader
packets=rdpcap("./wireshark_http.pcapng")
for data in packets:
if 'TCP' in data:
s = repr(data)
print(s)
print(data['UDP'].sport)
break
各类攻击 单一协议 pcap数据包 下载网站
http://packetlife.net/captures/
https://wiki.wireshark.org/SampleCaptures/
https://www.netresec.com/?page=PcapFiles
首先是因为接到一个任务:需要对工控常见协议的识别流量进行收集。
项目见:https://github.com/hi-KK/ICS-Protocol-identify (含nse脚本和识别pcap流量)
libpcap抓包分析项目(一)
https://blog.csdn.net/NCU_CirclePlan/article/details/95351331
安装 libpcap库
sudo apt-get install libpcap0.8
sudo apt-get install libpcap-dev
运行第一个测试程序
#include <pcap.h>
#include <stdio.h>
int main()
{
char errBuf[PCAP_ERRBUF_SIZE], * device;
device = pcap_lookupdev(errBuf); //返回第一个合适的网络接口的字符串指针
if(device)
{
printf("success: device: %s\n", device);
}
else
{
printf("error: %s\n", errBuf);
}
return 0;
}
编译方法:
gcc test1.c -lpcap -o test1
运行结果:
success: device: enp4s0
ifconfig 验证一下
运行第二个测试程序
ldev.c
/* ldev.c
Martin Casado
To compile:
>gcc ldev.c -lpcap
Looks for an interface, and lists the network ip
and mask associated with that interface.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h> /* GIMME a libpcap plz! */
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
char *dev; /* name of the device to use */
char *net; /* dot notation of the network address */
char *mask;/* dot notation of the network mask */
int ret; /* return code */
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netp; /* ip */
bpf_u_int32 maskp;/* subnet mask */
struct in_addr addr;
/* ask pcap to find a valid device for use to sniff on */
dev = pcap_lookupdev(errbuf);
/* error checking */
if(dev == NULL)
{
printf("%s\n",errbuf);
exit(1);
}
/* print out device name */
printf("DEV: %s\n",dev);
/* ask pcap for the network address and mask of the device */
ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);
if(ret == -1)
{
printf("%s\n",errbuf);
exit(1);
}
/* get the network address in a human readable form */
addr.s_addr = netp;
net = inet_ntoa(addr);
if(net == NULL)/* thanks Scott :-P */
{
perror("inet_ntoa");
exit(1);
}
printf("NET: %s\n",net);
/* do the same as above for the device's mask */
addr.s_addr = maskp;
mask = inet_ntoa(addr);
if(mask == NULL)
{
perror("inet_ntoa");
exit(1);
}
printf("MASK: %s\n",mask);
return 0;
}
编译方法:
gcc ldev.c -lpcap -o ldev
执行程序: ./ldev
运行结果:
DEV: enp4s0
NET: 192.168.99.0
MASK: 255.255.255.0
论文研究-基于libpcap和PF_RING的网络流量采集查询系统的设计与实现 .pdf
Libpcap是linux下用来捕获数据包的抓包库,它主要是基于socket的,和winpcap的本质的不同是,winpcap是和tcp/ip协议同层的,而libpcap是应用层的库,在tcp/ip层上对socket的又一次封装,所以从网卡得到的数据包需要经过多次拷贝才能达到应用程序,在千兆网的条件下,捕获包的性能较差,为了提高libpcap的包捕获性能,采用PF_RING对libpcap进行改进,改进后的libpcap采用环状缓冲区从网卡接收数据包,然后通过mmap映射到应用程序,减少内存拷贝的次数。为了更好的理解libpcap,pfring,libpfring等库函数,所以对这些源码进行分析,其中pfring是内核的源码,而libpfring是对pfring的封装,供应用程序调用,其实不采用libpcap,直接采用libpring也能捕获数据包,因为目前大部分的sniff工具都是建立在libpcap之上的,所以还是采用libpcap的接口,在底层采用pfring修改socket的实现过程。
Winpcap和libpcap捕获数据包的不同之处在于winpcap是与tcp/ip同层的协议,而libpcap是应用层的开发包,libpcap+pf_ring补丁后,和winpcap就有点类似了,都是采用环状的内核缓冲区,内核缓冲区的大小都可以设置。而winpcap和libpcap另外一个不同之处在于,它可以设定mintocopysize,即当内核缓冲区有这么多数据的时候,就将数据拷贝到应用程序缓冲区,而libpcap是没有这种功能的。Libpcap主要是基于网卡中断或轮询往上层传替数据的。