Skip to main content

a low level python implementation of a raw socket interface for sending Ethernet frames using Berkeley Packet Filters (BPF) on BSD based systems.

Project description

RawSocket

Overview

This repository contains a low level python implementation of a raw socket interface for sending Ethernet frames using Berkeley Packet Filters (BPF) on BSD based systems.

Prerequisites

Ensure you are running a Unix-based system (e.g., macOS, freeBSD, openBSD etc) that supports BPF devices (/dev/bpf*).

Installation

No additional dependencies are required. This module relies on Python's built-in os, struct, and fcntl modules.

python3 -m pip install rawsock

Usage

Example Code

from rawsock import RawSocket

# Create a RawSocket instance for network interface 'en0'
sock = RawSocket(b"en0")

# Construct an Ethernet frame with a broadcast destination MAC
frame = RawSocket.frame(
    b'\xff\xff\xff\xff\xff\xff',  # Destination MAC (broadcast)
    b'\x6e\x87\x88\x4d\x99\x5f',  # Source MAC
    ethertype=b"\x88\xB5",
    payload=b"test"  # Custom payload
)

# Send the frame
success = sock.send(frame)

# to send an ARP request:
success = sock.send_arp(
    source_mac="76:c9:1d:f1:27:04",
    source_ip="192.168.178.85",
    target_ip="192.168.178.22"
)

To receive incoming packets while sending:

sock = RawSocket("en0")
with sock.listener(5): # listen for 5 seconds
    success = sock.send_arp(
        source_mac="76:c9:1d:f1:27:04",
        source_ip="192.168.178.85",
        target_ip="192.168.178.22"
    )
print(sock.captured_packets)

Apply custom filters to capture specific packets:

# the following code listens for ARP packets with the specified
# dest mac address and checks if the target ip is available in payload
# which means the device has responded with its mac address if its
# connected to the network
with sock.listener(6, filter_ = {"ethertype": b"\x08\x06", "destination_mac": "76:c9:1d:f1:27:04", "payload": [b"\xc0\xa8\xb2\x16",]}):
    success = sock.send_arp(
        source_mac="76:c9:1d:f1:27:04",
        source_ip="192.168.178.85",
        target_ip="192.168.178.22"
    )
print(sock.captured_packets)

Methods

send(frame: bytes) -> int

Sends an Ethernet frame via the bound BPF device. Returns 1 on success, 0 on failure.

frame(dest_mac: bytes, source_mac: bytes, ethertype: bytes = b'\x88\xB5', payload: str | bytes) -> bytes

Constructs an Ethernet frame with the specified parameters.

send_arp(...)

A public method to send an ARP request.

Notes

  • This code has been tested on macOS with python 3.13.
  • The code assumes that at least one /dev/bpf* device is available and not busy.
  • Packets may require root privileges to send. (on macOS you must run the script as root)
  • Wireshark usually occupies the first found BPF device /dev/bpf0 if it's open and listening, so make sure to use /dev/bpf1 in the script.
  • The system’s network interface must be in promiscuous mode to receive raw packets.

License

This code is licensed under the MIT License.

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

rawsock-0.0.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rawsock-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file rawsock-0.0.3.tar.gz.

File metadata

  • Download URL: rawsock-0.0.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for rawsock-0.0.3.tar.gz
Algorithm Hash digest
SHA256 287c48390d45cacb848e88d16bc9b6935334302f43e42b4489f9c50467630ad7
MD5 cb06a666266ebf4806d78a7ded5a9708
BLAKE2b-256 4549c1ce581f5e4254354d52e8c3da9643db37f45e651052c72d9ae746311f47

See more details on using hashes here.

File details

Details for the file rawsock-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: rawsock-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for rawsock-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b43aeb38f9c51ca9ccf954e122be5e0e1e3d9ee32303b9e10c5a2d7e8e0e8545
MD5 6ca5e6f17640e27f3d16b97fb6f2cba8
BLAKE2b-256 c0045bd3422fe38f2ab7d6cd7d895f441d62ee61eb2a5ad111e5d8be56ff5fa1

See more details on using hashes here.

Supported by

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