python-libpcap


Namepython-libpcap JSON
Version 0.5.2 PyPI version JSON
download
home_pagehttps://github.com/caizhengxin/python-libpcap
SummaryCython libpcap
upload_time2024-09-26 06:43:45
maintainerJanKinCai
docs_urlNone
authorJanKinCai
requires_pythonNone
licenseBSD
keywords python-libpcap pylibpcap libpcap pcap pcapngpython libpcap-python python-pcapng
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # python-libpcap

[![pypi.python.org](https://img.shields.io/pypi/v/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)
[![pypi.python.org](https://img.shields.io/pypi/pyversions/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)
[![travis-ci.org](https://api.travis-ci.com/caizhengxin/python-libpcap.svg?branch=master)](https://travis-ci.org/JanKinCai/python-libpcap)
[![pypi.python.org](https://img.shields.io/pypi/dm/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)
[![readthedocs.org](https://readthedocs.org/projects/python-libpcap/badge/?version=latest)](https://python-libpcap.readthedocs.io/en/latest/?badge=latest)
[![img.shields.io](https://img.shields.io/github/languages/code-size/caizhengxin/python-libpcap)](https://pypi.python.org/pypi/python-libpcap)
[![img.shields.io](https://img.shields.io/pypi/l/python-libpcap)](https://github.com/caizhengxin/python-libpcap/blob/master/LICENSE)

This is the Cython encapsulated of the C libpcap library for python.

- Github repo: https://github.com/caizhengxin/python-libpcap
- Documentation: https://python-libpcap.readthedocs.io
- Free software: BSD lincense

## Features

- [x] Read pcap file
- [x] Write pcap file
- [x] Merge pcap file
- [x] Multi-file quick merge
- [x] Get first iface
- [x] Get iface list
- [x] Send raw packet
- [x] Capture data

## Install

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

```bash
$ sudo apt-get install libpcap-dev
$ pip3 install python-libpcap
```

Or

```bash
$ git clone https://github.com/caizhengxin/python-libpcap.git
$ cd python-libpcap
$ pip3 install -e .
```

## Usage

### Command

```bash
# Multi-file quick merge
$ libpcap-merge -i test.pcap -o pcap.pcap port 502
$ libpcap-merge -i pcap/ -o pcap.pcap port 502

# Capture data packet
$ sudo libpcap-capture -i enp0s3 -v -p port 22
$ sudo libpcap-capture -i enp0s3 -o pcap.pcap port 22

# Write packet
$ libpcap-write --output pcap.pcap ac64175ffa41000ec6c9157e08004500004b8a1e400080060000c0a80002c0a80001c794006618e119b56ef0831d5018faf081910000030000231ee00000001d00c1020600c20f53494d415449432d524f4f542d4553c0010a

# Read packet
$ libpcap-read -i test.pcap -v -p port 502
```

### Read pcap file

```python
from pylibpcap.pcap import rpcap


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

### Write pcap file

```python
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")
```

Or

```python
from pylibpcap import OpenPcap


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

### Merge pcap file

```python
from pylibpcap.pcap import mpcap


mpcap("demo.pcap", "demo2.pcap")
mpcap("pcap/", "output.pcap", "port 502")
```

### Get first iface

```python
from pylibpcap import get_first_iface

print(get_first_iface())
```

### Get iface list

```python
from pylibpcap import get_iface_list

print(get_iface_list())
```

### Send raw packet

```python
from pylibpcap import send_packet


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'

send_packet("enp2s0", buf)
```

### Capture packet

```python
from pylibpcap.pcap import sniff


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

Or

```python
from pylibpcap.base import Sniff

sniffobj = None

try:
    sniffobj = Sniff("enp2s0", filters="port 53", count=-1, promisc=1, out_file="pcap.pcap")

    for plen, t, buf in sniffobj.capture():
        print("[+]: Payload len=", plen)
        print("[+]: Time", t)
        print("[+]: Payload", buf)
except KeyboardInterrupt:
    pass
except LibpcapError as e:
    print(e)

if sniffobj is not None:
    stats = sniffobj.stats()
    print(stats.capture_cnt, " packets captured")
    print(stats.ps_recv, " packets received by filter")
    print(stats.ps_drop, "  packets dropped by kernel")
    print(stats.ps_ifdrop, "  packets dropped by iface")
```

## Credits

This package was created with [Cookiecutter](https://github.com/cookiecutter/cookiecutter) and the [caizhengxin/cookiecutter-package](https://github.com/caizhengxin/cookiecutter-package) project template.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/caizhengxin/python-libpcap",
    "name": "python-libpcap",
    "maintainer": "JanKinCai",
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": "jankincai12@gmail.com",
    "keywords": "python-libpcap, pylibpcap, libpcap, pcap, pcapngpython, libpcap-python, python-pcapng",
    "author": "JanKinCai",
    "author_email": "jankincai12@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/15/bf/a2dae0d06e48affe1bc42b949da422a039477c5ca88df7cb3f436b628e7a/python_libpcap-0.5.2.tar.gz",
    "platform": "Linux",
    "description": "# python-libpcap\n\n[![pypi.python.org](https://img.shields.io/pypi/v/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)\n[![pypi.python.org](https://img.shields.io/pypi/pyversions/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)\n[![travis-ci.org](https://api.travis-ci.com/caizhengxin/python-libpcap.svg?branch=master)](https://travis-ci.org/JanKinCai/python-libpcap)\n[![pypi.python.org](https://img.shields.io/pypi/dm/python-libpcap.svg)](https://pypi.python.org/pypi/python-libpcap)\n[![readthedocs.org](https://readthedocs.org/projects/python-libpcap/badge/?version=latest)](https://python-libpcap.readthedocs.io/en/latest/?badge=latest)\n[![img.shields.io](https://img.shields.io/github/languages/code-size/caizhengxin/python-libpcap)](https://pypi.python.org/pypi/python-libpcap)\n[![img.shields.io](https://img.shields.io/pypi/l/python-libpcap)](https://github.com/caizhengxin/python-libpcap/blob/master/LICENSE)\n\nThis is the Cython encapsulated of the C libpcap library for python.\n\n- Github repo: https://github.com/caizhengxin/python-libpcap\n- Documentation: https://python-libpcap.readthedocs.io\n- Free software: BSD lincense\n\n## Features\n\n- [x] Read pcap file\n- [x] Write pcap file\n- [x] Merge pcap file\n- [x] Multi-file quick merge\n- [x] Get first iface\n- [x] Get iface list\n- [x] Send raw packet\n- [x] Capture data\n\n## Install\n\nTo install python-libpcap, run this command in your terminal:\n\n```bash\n$ sudo apt-get install libpcap-dev\n$ pip3 install python-libpcap\n```\n\nOr\n\n```bash\n$ git clone https://github.com/caizhengxin/python-libpcap.git\n$ cd python-libpcap\n$ pip3 install -e .\n```\n\n## Usage\n\n### Command\n\n```bash\n# Multi-file quick merge\n$ libpcap-merge -i test.pcap -o pcap.pcap port 502\n$ libpcap-merge -i pcap/ -o pcap.pcap port 502\n\n# Capture data packet\n$ sudo libpcap-capture -i enp0s3 -v -p port 22\n$ sudo libpcap-capture -i enp0s3 -o pcap.pcap port 22\n\n# Write packet\n$ libpcap-write --output pcap.pcap ac64175ffa41000ec6c9157e08004500004b8a1e400080060000c0a80002c0a80001c794006618e119b56ef0831d5018faf081910000030000231ee00000001d00c1020600c20f53494d415449432d524f4f542d4553c0010a\n\n# Read packet\n$ libpcap-read -i test.pcap -v -p port 502\n```\n\n### Read pcap file\n\n```python\nfrom pylibpcap.pcap import rpcap\n\n\nfor len, t, pkt in rpcap(\"tests/dns.pcap\"):\n    print(\"Time:\", t)\n    print(\"Buf length:\", len)\n    print(\"Buf:\", pkt)\n```\n\n### Write pcap file\n\n```python\nfrom pylibpcap import wpcap\n\n\nbuf = b'\\x00\\xc0\\x9f2A\\x8c\\x00\\xe0\\x18\\xb1\\x0c\\xad\\x08\\x00E\\x00\\x008' \\\n        b'\\x00\\x00@\\x00@\\x11eG\\xc0\\xa8\\xaa\\x08\\xc0\\xa8\\xaa\\x14\\x80\\x1b' \\\n        b'\\x005\\x00$\\x85\\xed\\x102\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00' \\\n        b'\\x00\\x06google\\x03com\\x00\\x00\\x10\\x00\\x01'\n\n\nwpcap(buf, \"pcap.pcap\")\nwpcap([buf, buf], \"pcap.pcap\")\n```\n\nOr\n\n```python\nfrom pylibpcap import OpenPcap\n\n\nwith OpenPcap(\"pcap.pcap\", \"a\") as f:\n    f.write(buf)\n```\n\n### Merge pcap file\n\n```python\nfrom pylibpcap.pcap import mpcap\n\n\nmpcap(\"demo.pcap\", \"demo2.pcap\")\nmpcap(\"pcap/\", \"output.pcap\", \"port 502\")\n```\n\n### Get first iface\n\n```python\nfrom pylibpcap import get_first_iface\n\nprint(get_first_iface())\n```\n\n### Get iface list\n\n```python\nfrom pylibpcap import get_iface_list\n\nprint(get_iface_list())\n```\n\n### Send raw packet\n\n```python\nfrom pylibpcap import send_packet\n\n\nbuf = b'\\x00\\xc0\\x9f2A\\x8c\\x00\\xe0\\x18\\xb1\\x0c\\xad\\x08\\x00E\\x00\\x008' \\\n        b'\\x00\\x00@\\x00@\\x11eG\\xc0\\xa8\\xaa\\x08\\xc0\\xa8\\xaa\\x14\\x80\\x1b' \\\n        b'\\x005\\x00$\\x85\\xed\\x102\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00' \\\n        b'\\x00\\x06google\\x03com\\x00\\x00\\x10\\x00\\x01'\n\nsend_packet(\"enp2s0\", buf)\n```\n\n### Capture packet\n\n```python\nfrom pylibpcap.pcap import sniff\n\n\nfor plen, t, buf in sniff(\"enp2s0\", filters=\"port 53\", count=-1, promisc=1, out_file=\"pcap.pcap\"):\n    print(\"[+]: Payload len=\", plen)\n    print(\"[+]: Time\", t)\n    print(\"[+]: Payload\", buf)\n```\n\nOr\n\n```python\nfrom pylibpcap.base import Sniff\n\nsniffobj = None\n\ntry:\n    sniffobj = Sniff(\"enp2s0\", filters=\"port 53\", count=-1, promisc=1, out_file=\"pcap.pcap\")\n\n    for plen, t, buf in sniffobj.capture():\n        print(\"[+]: Payload len=\", plen)\n        print(\"[+]: Time\", t)\n        print(\"[+]: Payload\", buf)\nexcept KeyboardInterrupt:\n    pass\nexcept LibpcapError as e:\n    print(e)\n\nif sniffobj is not None:\n    stats = sniffobj.stats()\n    print(stats.capture_cnt, \" packets captured\")\n    print(stats.ps_recv, \" packets received by filter\")\n    print(stats.ps_drop, \"  packets dropped by kernel\")\n    print(stats.ps_ifdrop, \"  packets dropped by iface\")\n```\n\n## Credits\n\nThis package was created with [Cookiecutter](https://github.com/cookiecutter/cookiecutter) and the [caizhengxin/cookiecutter-package](https://github.com/caizhengxin/cookiecutter-package) project template.\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Cython libpcap",
    "version": "0.5.2",
    "project_urls": {
        "Documentation": "https://python-libpcap.readthedocs.io",
        "Download": "https://github.com/caizhengxin/python-libpcap.git",
        "Homepage": "https://github.com/caizhengxin/python-libpcap",
        "Source Code": "https://github.com/caizhengxin/python-libpcap"
    },
    "split_keywords": [
        "python-libpcap",
        " pylibpcap",
        " libpcap",
        " pcap",
        " pcapngpython",
        " libpcap-python",
        " python-pcapng"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "15bfa2dae0d06e48affe1bc42b949da422a039477c5ca88df7cb3f436b628e7a",
                "md5": "b4b16a950f694fbaabb0a220f8e2872f",
                "sha256": "c1951ec353c58ff2e7006a1fc9ea2a7fc83fabad3301f4f10b77e37f2dd9fde5"
            },
            "downloads": -1,
            "filename": "python_libpcap-0.5.2.tar.gz",
            "has_sig": false,
            "md5_digest": "b4b16a950f694fbaabb0a220f8e2872f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 122772,
            "upload_time": "2024-09-26T06:43:45",
            "upload_time_iso_8601": "2024-09-26T06:43:45.391998Z",
            "url": "https://files.pythonhosted.org/packages/15/bf/a2dae0d06e48affe1bc42b949da422a039477c5ca88df7cb3f436b628e7a/python_libpcap-0.5.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-26 06:43:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "caizhengxin",
    "github_project": "python-libpcap",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "python-libpcap"
}
        
Elapsed time: 0.30962s