winpcapy
========
| A Modern Python wrapper for WinPcap
| Access WinPcap through ctypes.
Based on Massimo Ciani’s WinPcapy (https://code.google.com/p/winpcapy/)
Install
-------
pip install winpcapy
Usage
-----
Quick packet live log printer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
>>> from winpcapy import WinPcapUtils
# run on the first Ethernert interface and print a log for each packet
>>> WinPcapUtils.capture_on_and_print("*Ethernet*")
16:05:49,624258 len:199
16:05:49,685950 len:60
16:05:49,686022 len:54
16:05:49,767311 len:66
16:05:49,819156 len:66
16:05:50,052113 len:92
16:05:50,128862 len:60
Easy Packet live callback
~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
from winpcapy import WinPcapUtils
# Example Callback function to parse IP packets
def packet_callback(win_pcap, param, header, pkt_data):
# Assuming IP (for real parsing use modules like dpkt)
ip_frame = pkt_data[14:]
# Parse ips
src_ip = ".".join([str(ord(b)) for b in ip_frame[0xc:0x10]])
dst_ip = ".".join([str(ord(b)) for b in ip_frame[0x10:0x14]])
print("%s -> %s" % (src_ip, dst_ip))
WinPcapUtils.capture_on("*Ethernet*", packet_callback)
Device/Interface enumeration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
>>> from winpcapy import WinPcapDevices
# Return a list of all the devices detected on the machine
>>> WinPcapDevices.list_devices()
{'\\Device\\NPF_{0A78B7C8-F023-1337-1337-84D448AA5126}': 'Microsoft',
'\\Device\\NPF_{2997B9BB-AA53-1337-1337-B862F874271C}': 'Microsoft',
'\\Device\\NPF_{C2EAA982-F851-1337-1337-B8D2A9BCE406}': 'Intel(R) Ethernet Connection I218-LM',
'\\Device\\NPF_{EAF47DBE-5B49-1337-1337-BD059E02666B}': 'Microsoft'}
# Itearte over devices (in memory), with full details access
>>> with WinPcapDevices() as devices:
... for device in devices:
... print device.name, device.description, device.flags ,device.addresses.contents.netmask.contents.sa_family
...
"\Device\NPF_{0A78B7C8-F023-1337-1337-84D448AA5126} Microsoft 0 0"
"\Device\NPF_{C2EAA982-F851-1337-1337-B8D2A9BCE406} Intel(R) Ethernet Connection I218-LM 0 0"
"\Device\NPF_{EAF47DBE-5B49-1337-1337-BD059E02666B} Microsoft 0 0"
"\Device\NPF_{2997B9BB-AA53-1337-1337-B862F874271C} Microsoft 0 0"
Easy Packet sending
~~~~~~~~~~~~~~~~~~~
.. code:: python
from winpcapy import WinPcapUtils
# Build a packet buffer
# This example-code is built for tutorial purposes, for actual packet crafting use modules like dpkt
arp_request_hex_template = "%(dst_mac)s%(src_mac)s08060001080006040001" \
"%(sender_mac)s%(sender_ip)s%(target_mac)s%(target_ip)s" + "00" * 18
packet = arp_request_hex_template % {
"dst_mac": "aa"*6,
"src_mac": "bb"*6,
"sender_mac": "bb"*6,
"target_mac": "cc"*6,
# 192.168.0.1
"sender_ip": "c0a80001",
# 192.168.0.2
"target_ip": "c0a80002"
}
# Send the packet (ethernet frame with an arp request) on the interface
WinPcapUtils.send_packet("*Ethernet*", packet.decode("hex"))
Raw data
{
"_id": null,
"home_page": "https://github.com/orweis/winpcapy",
"name": "winpcapy",
"maintainer": "Or Weis",
"docs_url": null,
"requires_python": null,
"maintainer_email": "py@bitweis.com",
"keywords": "winpcapy, pcap, winpcap, Packet capture, tcpdump",
"author": "Or Weis",
"author_email": "py@bitweis.com",
"download_url": "https://files.pythonhosted.org/packages/90/0d/648bc8b3efcf3a028f5bff3c1545b6d06138de73a3c3284f03d0643bfccb/winpcapy-1.0.3.tar.gz",
"platform": null,
"description": "winpcapy\n========\n\n| A Modern Python wrapper for WinPcap\n| Access WinPcap through ctypes.\n\nBased on Massimo Ciani\u2019s WinPcapy (https://code.google.com/p/winpcapy/)\n\n\nInstall\n-------\npip install winpcapy\n\nUsage\n-----\n\nQuick packet live log printer\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n >>> from winpcapy import WinPcapUtils\n # run on the first Ethernert interface and print a log for each packet\n >>> WinPcapUtils.capture_on_and_print(\"*Ethernet*\")\n 16:05:49,624258 len:199\n 16:05:49,685950 len:60\n 16:05:49,686022 len:54\n 16:05:49,767311 len:66\n 16:05:49,819156 len:66\n 16:05:50,052113 len:92\n 16:05:50,128862 len:60\n\nEasy Packet live callback\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n from winpcapy import WinPcapUtils\n\n # Example Callback function to parse IP packets\n def packet_callback(win_pcap, param, header, pkt_data):\n # Assuming IP (for real parsing use modules like dpkt)\n ip_frame = pkt_data[14:]\n # Parse ips\n src_ip = \".\".join([str(ord(b)) for b in ip_frame[0xc:0x10]])\n dst_ip = \".\".join([str(ord(b)) for b in ip_frame[0x10:0x14]])\n print(\"%s -> %s\" % (src_ip, dst_ip))\n\n WinPcapUtils.capture_on(\"*Ethernet*\", packet_callback)\n\nDevice/Interface enumeration\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n >>> from winpcapy import WinPcapDevices\n # Return a list of all the devices detected on the machine\n >>> WinPcapDevices.list_devices()\n {'\\\\Device\\\\NPF_{0A78B7C8-F023-1337-1337-84D448AA5126}': 'Microsoft',\n '\\\\Device\\\\NPF_{2997B9BB-AA53-1337-1337-B862F874271C}': 'Microsoft',\n '\\\\Device\\\\NPF_{C2EAA982-F851-1337-1337-B8D2A9BCE406}': 'Intel(R) Ethernet Connection I218-LM',\n '\\\\Device\\\\NPF_{EAF47DBE-5B49-1337-1337-BD059E02666B}': 'Microsoft'}\n \n # Itearte over devices (in memory), with full details access\n >>> with WinPcapDevices() as devices:\n ... for device in devices:\n ... print device.name, device.description, device.flags ,device.addresses.contents.netmask.contents.sa_family\n ... \n \"\\Device\\NPF_{0A78B7C8-F023-1337-1337-84D448AA5126} Microsoft 0 0\"\n \"\\Device\\NPF_{C2EAA982-F851-1337-1337-B8D2A9BCE406} Intel(R) Ethernet Connection I218-LM 0 0\"\n \"\\Device\\NPF_{EAF47DBE-5B49-1337-1337-BD059E02666B} Microsoft 0 0\"\n \"\\Device\\NPF_{2997B9BB-AA53-1337-1337-B862F874271C} Microsoft 0 0\"\n\nEasy Packet sending\n~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n from winpcapy import WinPcapUtils\n # Build a packet buffer\n # This example-code is built for tutorial purposes, for actual packet crafting use modules like dpkt\n arp_request_hex_template = \"%(dst_mac)s%(src_mac)s08060001080006040001\" \\\n \"%(sender_mac)s%(sender_ip)s%(target_mac)s%(target_ip)s\" + \"00\" * 18\n packet = arp_request_hex_template % {\n \"dst_mac\": \"aa\"*6,\n \"src_mac\": \"bb\"*6,\n \"sender_mac\": \"bb\"*6,\n \"target_mac\": \"cc\"*6,\n # 192.168.0.1\n \"sender_ip\": \"c0a80001\",\n # 192.168.0.2\n \"target_ip\": \"c0a80002\"\n }\n # Send the packet (ethernet frame with an arp request) on the interface\n WinPcapUtils.send_packet(\"*Ethernet*\", packet.decode(\"hex\"))\n",
"bugtrack_url": null,
"license": "GPLv2",
"summary": "A Modern Python wrapper for WinPcap",
"version": "1.0.3",
"project_urls": {
"Homepage": "https://github.com/orweis/winpcapy"
},
"split_keywords": [
"winpcapy",
" pcap",
" winpcap",
" packet capture",
" tcpdump"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "900d648bc8b3efcf3a028f5bff3c1545b6d06138de73a3c3284f03d0643bfccb",
"md5": "936e892b3a960952632eff7703a87c55",
"sha256": "f3c00d3449269a42c64ac222ef6b2363598cc39cb22a65f579a7a9142c18d755"
},
"downloads": -1,
"filename": "winpcapy-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "936e892b3a960952632eff7703a87c55",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 19907,
"upload_time": "2024-04-18T12:53:22",
"upload_time_iso_8601": "2024-04-18T12:53:22.643461Z",
"url": "https://files.pythonhosted.org/packages/90/0d/648bc8b3efcf3a028f5bff3c1545b6d06138de73a3c3284f03d0643bfccb/winpcapy-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-18 12:53:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "orweis",
"github_project": "winpcapy",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "winpcapy"
}