Skip to main content

Cython libpcap

Project description

https://img.shields.io/pypi/v/python-libpcap.svg https://img.shields.io/pypi/pyversions/python-libpcap.svg https://api.travis-ci.com/caizhengxin/python-libpcap.svg?branch=master https://img.shields.io/pypi/dm/python-libpcap.svg Documentation Status https://img.shields.io/github/languages/code-size/caizhengxin/python-libpcap https://img.shields.io/pypi/l/python-libpcap

Cython libpcap, read/write/merge/capture

Features

  • Read pcap file

  • Write pcap file

  • Merge pcap file

  • Multi-file quick merge

  • Get first iface

  • Get iface list

  • Send raw packet

  • Capture data

Installation

To install python-libpcap, run this command in your terminal:

$ sudo apt-get install libpcap-dev
$ pip3 install Cython
$ pip3 install python-libpcap

Demo

Console:

# Multi-file quick merge
$ mpcap -i pcap/ -o pcap.pcap port 502

# Capture data packet
$ sudo capture -i enp2s0 -v

Read pcap:

from pylibpcap.pcap import rpcap


for len, t, pkt in rpcap("tests/dns.pcap"):
    print("Buf length:", len)
    print("Time:", t)
    print("Buf:", pkt)

Write pcap:

from pylibpcap import wpcap


buf = b'\x00\xc0\x9f2A\x8c\x00\xe0\x18\xb1\x0c\xad\x08\x00E\x00\x008' \
      b'\x00\x00@\x00@\x11eG\xc0\xa8\xaa\x08\xc0\xa8\xaa\x14\x80\x1b' \
      b'\x005\x00$\x85\xed\x102\x01\x00\x00\x01\x00\x00\x00\x00\x00' \
      b'\x00\x06google\x03com\x00\x00\x10\x00\x01'


wpcap(buf, "pcap.pcap")
wpcap([buf, buf], "pcap.pcap)
from pylibpcap import OpenPcap


with OpenPcap("pcap.pcap", "a") as f:
    f.write(buf)

Merge pcap:

from pylibpcap.pcap import mpcap


mpcap("demo.pcap", "demo2.pcap")

mpcap("pcap/", "output.pcap", "port 502")

Get first iface:

from pylibpcap.pcap import get_first_iface

print(get_first_iface())

Get iface list:

from pylibpcap import get_iface_list

print(get_iface_list())

Send raw packet:

from pylibpcap import send_packet

send_packet("enp2s0", b"")

Capture data:

from pylibpcap.pcap import sniff


for plen, t, buf in sniff("enp2s0", filters="port 53", count=3, promisc=1, out_file="pcap.pcap"):
    print("[+]: Payload len=", plen)
    print("[+]: Time", t)
    print("[+]: Payload", buf)

Credits

This package was created with Cookiecutter and the caizhengxin/cookiecutter-package project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-libpcap-0.2.1.tar.gz (62.5 kB view details)

Uploaded Source

File details

Details for the file python-libpcap-0.2.1.tar.gz.

File metadata

  • Download URL: python-libpcap-0.2.1.tar.gz
  • Upload date:
  • Size: 62.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9

File hashes

Hashes for python-libpcap-0.2.1.tar.gz
Algorithm Hash digest
SHA256 08b30fed8a01634d2cac48ad37849a532ce652650039f27f911c0fc20cc4aa31
MD5 6f4d7c6b4335dc548846bfe954c8a775
BLAKE2b-256 fcc26744e6a2a05d596a10e5bd55a80635a606d06faebd3f5cdffed37bcd510d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page