search
HomeBackend DevelopmentPython TutorialHow to use libpcap library for packet capture and data processing in Python

This article brings you relevant knowledge about Python, which mainly introduces how to use the libpcap library for packet capture and data processing, including installing libpcap, using the libpcap library, etc. Let’s take a look at the content below. I hope it will be helpful to everyone.

【Related recommendations: Python3 video tutorial

python version: python 3.9

libpcap version: 1.11.0b7

The python libpcap library is a development package that binds the underlying c language libpcap library. It is designed to provide the unix c libpcap library API accessible to python applications (and Npcap and WinPcap provided for win32 systems), directly using the underlying c code, with very good performance.

Here is recorded how to install and use the libpcap library in python3.9 under Windows 10 environment (for Linux and mac systems, please refer to Windows).

pypi address: https://pypi.org/project/libpcap/

github address: https://github.com/karpierz/libpcap

1. Install the libpcap library

1. Online installation

Use pip directly to install:

pip install libpcap

The latest version is installed by default.

2. Offline installation

2.1 Download the offline installation file

You can download it at pypi page source code or whl file.

2.2 Perform offline installation operation

1) You can use the source code for installation

Extract the file to the current directory , and then execute the installation command:

python -m pip install ./libpcap-1.11.0b7

2) You can also use the whl file for offline installation

The installation command is as follows:

python -m pip install libpcap-1.11.0b7-py3-none-any.whl

2. Use libpcap library

1. Import and specify the pcap library

import libpcap
libpcap.config(LIBPCAP="wpcap")

2. Introduction to commonly used APIs

tcpdump is implemented based on libpcap. The documentation for C language libpcap can be found on the tcpdump official website:

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

Here is a description of common Python interfaces.

2.1 Get the network device interface

  • lookupdev(errbuf)

Function: This function uses For finding network devices, the returned value can be called directly by the open_live function.

Parameters:

errbuf is a c language string type, used to obtain error information.

Usage example:

import ctypes as ct
import libpcap as pcap
errbuf = ct.create_string_buffer(pcap.PCAP_ERRBUF_SIZE + 1)
device = pcap.lookupdev(errbuf)
print(errbuf.value)
  • findalldevs(alldevs, errbuf)

Function: This function Used to find all network devices.

Parameters:

alldevs is the pcap_if_t structure pointer, used to store all found network device information.

errbuf is a c language string type, used to obtain error information.

Usage example:

import ctypes as ct
import libpcap as pcap
errbuf = ct.create_string_buffer(pcap.PCAP_ERRBUF_SIZE + 1)
alldevs = ct.POINTER(pcap.pcap_if_t)()    
pcap.findalldevs(ct.byref(alldevs), errbuf)
print(alldevs[0].name)
pcap.freealldevs(alldevs)

2.2 Packet capture interface

  • ##open_live(device:bytes,snaplen:int,promisc:int,to_ms:int, errbuf)

Function: This function is used to open a network device for capturing data

Parameters:

device is the name of the network interface, It can be obtained through the API or specified manually, such as: "eth0"

snaplen is the length of the captured data packet, which cannot be greater than 65535

promise is used to mark whether to enable promiscuous mode, 1 represents promiscuous mode, other values ​​represent non-promiscuous mode

to_ms represents the number of milliseconds to wait. After this time, the function to obtain the data packet will return immediately, 0 means waiting until a data packet arrives

errbuf is a C language string type, used to obtain error information.

Return value: Returns a pcap_t type pointer. This pointer must be used in all subsequent operations.

Usage example:

import ctypes as ct
import libpcap as pcap
device = b'eth0' # linux 
errbuf = ct.create_string_buffer(pcap.PCAP_ERRBUF_SIZE + 1)
handle = pcap.open_live(device,4096,1,1000,errbuf)
if errbuf.value:
    print("hanle error :",errbuf.value)

  • open_offline(fname:bytes,errbuf)

  • ##Function: This function is used to open offline Capture file

Parameters:

fname is the file name, for example: b"/tmp/test1.cap"

errbuf is the c language string type, used to obtain error message.

Return value: Returns a pcap_t type pointer. This pointer must be used in all subsequent operations.

Usage example:

import ctypes as ct
import libpcap as pcap
errbuf = ct.create_string_buffer(pcap.PCAP_ERRBUF_SIZE + 1)
handle = pcap.open_offline(fname,errbuf)
if errbuf.value:
    print("hanle error :",errbuf.value)

2.3 Packet acquisition interface

    next(handle,pheader)
  • Function: This function is used to obtain data packets, only one packet at a time.

parameter:

handle为pcap_t类型指针

pheader为pcap_pkthdr结构体指针,可通过pkthdr函数创建

返回值:返回u_char类型指针,代表包数据,可使用struct.unpack函数解析

使用示例:

import libpcap as pcap
pheader = pcap.pkthdr()
packet = pcap.next(handle,pheader)

2.4 写文件接口

  • dump_open(handle,fname:bytes)

功能:该函数用于打开文件,存储获取到的数据包。

参数:

handle为pcap_t类型指针

fname为文件名称

返回值:返回pcap_dumper_t 类型指针,后面的所有操作都要使用这个指针。

使用示例:

import libpcap as pcap

fname = b"realtime1.cap"
fPcap = pcap.dump_open(handle,fname)
  • dump(handle,pheader,packet)

功能:该函数用于存储获取到的数据包。

参数:

handle为pcap_dumper_t类型指针

pheader为pcap_pkthdr结构体指针

packet是数据包

返回值:无返回值

使用示例:

fPcapUbyte = ct.cast(fPcap,ct.POINTER(ct.c_ubyte))
pcap.dump(fPcapUbyte,pheader,packet)
  • dump_flush(handle)

功能:该函数用于将缓存的数据刷到磁盘

参数:

handle为pcap_dumper_t类型指针

返回值:错误码,0代表成功,-1代表出错

2.5 资源释放接口

  • close(handle)

功能:释放pcap_t类型指针

参数:

handle为pcap_t类型指针

返回值:无返回值

  • dump_close(handle)

功能:释放pcap_dumper_t类型指针

参数:

handle为pcap_dumper_t类型指针

返回值:无返回值

3、典型使用场景

3.1、网卡实时抓包

可以使用libpcap库进行网卡实时数据抓包,这里进行简单的示例:

1)首先需要获取或指定抓包设备

方法1 :指定网卡接口名称

device = b'\Device\NPF_{BFDBF91E-9848-417D-B8AB-D3ED19990717}' # windows

device = b'eth0' # linux

Windows网卡接口名称可在wireshark的捕获界面看到,具体如下:

linux网卡名称获取:ifconfig

方法2 :使用lookupdev获取网卡接口名称

device = pcap.lookupdev(errbuf)

方法3 :使用findalldevs获取网卡接口名称

alldevs = ct.POINTER(pcap.pcap_if_t)()

pcap.findalldevs(ct.byref(alldevs), errbuf)

device =alldevs[0].name

2)使用open_live函数进行网卡抓包;

3)使用pkthdr函数创建header,获取包头信息(时间戳、包大小);

4)使用next函数循环读取数据包,需要注意的是,获取的packet对象的contents是C语言类型,需要使用它ctypes的pointer函数进行转换;

5)数据包(比如IP头)的解析可使用struct的unpack函数;

6)如果要将抓包数据存盘,可使用dump_open、dump、dump_flush系列函数进行操作,需要注意的是,dump_open函数的第二个参数必须是byte类型;

示例代码及运行效果:

3.2、离线数据解析

可以使用libpcap库进行离线抓包文件的解析,这里进行简单的示例:

1)首先需要使用open_offline函数打开pcap文件,需要注意的是,函数的第一个参数必须是byte类型;

2)使用pkthdr函数创建header,获取包头信息(时间戳、包大小);

3)使用next函数循环读取数据包,需要注意的是,获取的packet对象的contents是C语言类型,需要使用它ctypes的pointer函数进行转换;

4)数据包(比如IP头)的解析可使用struct的unpack函数;

示例代码及运行效果:

3.3、使用过滤条件抓包

网卡实时抓包和离线数据解析时,可以设置过滤条件,避免数据量过大。

过滤条件示例:

1) 过滤IP

  • host 过滤某个ip的所有包

host 8.8.8.8

  • src 过滤源ip

src 8.8.8.8

  • dst过滤目的ip

dst 8.8.8.8

2)过滤端口

  • port进行单个端口过滤

port 22

  • portange进行多个端口过滤

portange 1-1024

  • 可使用src或dst指定端口方向

src port 22

dst port 22

3)指定协议

tcp

udp

icmp

4)使用组合条件

  • and 进行与逻辑

src localhost and dst port 22

src localhost && dst port 22

  • or 进行或逻辑

port 80 or 22

port 80 || 22

Sample code and running effect:

##[Related recommendations:

Python3 video tutorial]

The above is the detailed content of How to use libpcap library for packet capture and data processing in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft