pycapng 0.3__tar.gz → 0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycapng-0.4/PKG-INFO +269 -0
- pycapng-0.4/README-pypi.md +249 -0
- pycapng-0.4/README.md +86 -0
- {pycapng-0.3 → pycapng-0.4}/pyproject.toml +1 -1
- pycapng-0.3/PKG-INFO +0 -79
- pycapng-0.3/README.md +0 -59
- {pycapng-0.3 → pycapng-0.4}/.github/workflows/build.yml +0 -0
- {pycapng-0.3 → pycapng-0.4}/.github/workflows/release.yml +0 -0
- {pycapng-0.3 → pycapng-0.4}/.gitignore +0 -0
- {pycapng-0.3 → pycapng-0.4}/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/LICENSE +0 -0
- {pycapng-0.3 → pycapng-0.4}/Tutorial.md +0 -0
- {pycapng-0.3 → pycapng-0.4}/Tutorial2.md +0 -0
- {pycapng-0.3 → pycapng-0.4}/VERSION +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/linenoise/linenoise.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/linenoise/linenoise.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh.md +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh_eval.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh_io.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh_layer.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh_main.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/pcapsh_posa.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ah.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/amqp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/bacnet.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/bfd.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/bgp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/cdp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/coap.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/dccp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/dhcpv6.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/dnp3.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/dot1q.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/eap.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/eapol.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/eapol_key.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/eigrp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/esp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/hsrp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/icmpv6.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/igmp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ikev2.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ipv6.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/isakmp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/kerberos.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/l2tp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/lacp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/llc.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/modbus_tcp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/mpls.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/mqtt.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/nbns.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/netflow_v5.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/openflow.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ospf.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/pim.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ppp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/pppoe.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ptpv2.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/rdp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/ripv2.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/rsvp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/rtp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/sctp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/snap.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/socks5.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/stp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/stun.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/vrrp.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/protos/wol.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/run_tests.sh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_cstring_security.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_dns.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_forloop.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_inline_protocol.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_posa_custom.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_protocols.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_raw_inspect.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_tcp_session.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_tftp.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/test_wireshark_samples.pcapsh +0 -0
- {pycapng-0.3 → pycapng-0.4}/bin/tests/tftp_protos.posa +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/embed_protos.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/capture_tui.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/live_capture.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/pcapsh_callback.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/pcapsh_custom_protocol.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/pcapsh_inline.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/pcapsh_run_script.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/pcapsh_tls_https.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/read.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/write_rdp.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/examples/write_tcp.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/pycapng.cpp +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/pycapng.hpp +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/pycapture.cpp +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/pypcapsh.cpp +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/tests/__init__.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/bindings/python/tests/test_pcapsh.py +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/blocks.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/capture.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/dissect.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/easyapi.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/blocks.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/capture.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/dissect.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/easyapi.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/io.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/libpcapng.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/linktypes.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/objects.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/posa.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/asn1.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/bootp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/dhcp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/dns.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/ethernet.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/flow.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/http2.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/http2_hpack.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/http2_stream.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/icmp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/ipv4.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/ntp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/rdp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/ssh.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/ssl.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/tcp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/tcp_mss.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/tls_stream.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/protocols/udp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/reassembly.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/include/libpcapng/reassembly_tcp.h +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/io.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/objects.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/posa.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/asn1.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/dhcp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/dns.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/ethernet.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/flow.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/http2.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/http2_hpack.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/http2_stream.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/icmp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/ipv4.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/ntp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/rdp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/ssh.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/ssl.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/tcp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/tcp_mss.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/tls_stream.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/protocols/udp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/reassembly.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/reassembly_tcp.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/CMakeLists.txt +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/dhcp-write.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/dns-write.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/ntp-write.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/pcapng-spec.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/rdp-write.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/read.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/reasm.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/ssl-http2-write.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/tcp-sequence.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/lib/tests/writeall.c +0 -0
- {pycapng-0.3 → pycapng-0.4}/libpcapng.pc.cmake +0 -0
pycapng-0.4/PKG-INFO
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: pycapng
|
|
3
|
+
Version: 0.4
|
|
4
|
+
Summary: Python bindings for libpcapng — read/write pcapng files and live packet capture
|
|
5
|
+
Keywords: pcapng,pcap,packet-capture,network,packet,wireshark
|
|
6
|
+
Author-Email: Seb Tricaud <sebastien.tricaud@proton.me>
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: C++
|
|
14
|
+
Classifier: Topic :: System :: Networking :: Monitoring
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Project-URL: Homepage, https://github.com/stricaud/libpcapng
|
|
17
|
+
Project-URL: Repository, https://github.com/stricaud/libpcapng
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# pycapng
|
|
22
|
+
|
|
23
|
+
Python library for reading, writing, and capturing network traffic in the
|
|
24
|
+
[pcapng](https://github.com/pcapng/pcapng) file format.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
pip install pycapng
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- **Read** pcapng files packet by packet
|
|
31
|
+
- **Write** pcapng files with hand-crafted Ethernet/IP/TCP/UDP/ICMP/DNS/DHCP/NTP/TLS packets
|
|
32
|
+
- **Capture** live traffic with a Wireshark-style display filter (Linux / macOS)
|
|
33
|
+
- **Reassemble** fragmented IP packets
|
|
34
|
+
- **Script** packet flows with the built-in pcapsh engine
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Reading a pcapng file
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import pycapng
|
|
42
|
+
|
|
43
|
+
p = pycapng.PcapNG()
|
|
44
|
+
p.OpenFile("capture.pcapng", "r")
|
|
45
|
+
|
|
46
|
+
def on_packet(data: bytes, ts: int) -> None:
|
|
47
|
+
print(f"packet {len(data)} bytes ts={ts}")
|
|
48
|
+
|
|
49
|
+
p.ForeachPacket(on_packet)
|
|
50
|
+
p.CloseFile()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Writing packets
|
|
56
|
+
|
|
57
|
+
### Raw bytes
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import pycapng
|
|
61
|
+
|
|
62
|
+
p = pycapng.PcapNG()
|
|
63
|
+
p.OpenFile("out.pcapng", "w")
|
|
64
|
+
|
|
65
|
+
raw = bytes([0x45, 0x00, 0x00, 0x28]) # any bytes
|
|
66
|
+
p.WritePacket(raw)
|
|
67
|
+
|
|
68
|
+
p.CloseFile()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### TCP
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
p.WriteTcpPacket(
|
|
75
|
+
src_ip="192.168.1.10", dst_ip="93.184.216.34",
|
|
76
|
+
src_port=54321, dst_port=80,
|
|
77
|
+
payload=b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n",
|
|
78
|
+
flags=0x02, # SYN
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Build a packet without writing it
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
syn = p.BuildTcpPacket(
|
|
86
|
+
src_ip="10.0.0.1", dst_ip="10.0.0.2",
|
|
87
|
+
src_port=1234, dst_port=443,
|
|
88
|
+
payload=b"", flags=0x02,
|
|
89
|
+
)
|
|
90
|
+
# syn is bytes — inspect it, modify it, write it later
|
|
91
|
+
p.WritePacket(syn)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### UDP / ICMP
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
pkt = p.BuildUdpPacket(
|
|
98
|
+
src_ip="10.0.0.1", dst_ip="8.8.8.8",
|
|
99
|
+
src_port=12345, dst_port=53,
|
|
100
|
+
payload=b"\x00\x01...", # your DNS query bytes
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
icmp = p.BuildIcmpPacket(
|
|
104
|
+
src_ip="10.0.0.1", dst_ip="10.0.0.2",
|
|
105
|
+
icmp_type=8, icmp_code=0, # Echo Request
|
|
106
|
+
payload=b"hello",
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Application-layer helpers
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
# DNS
|
|
114
|
+
query = p.BuildDnsQuery("example.com")
|
|
115
|
+
response = p.BuildDNSResponse("example.com", "93.184.216.34")
|
|
116
|
+
|
|
117
|
+
# DHCP
|
|
118
|
+
discover = p.BuildDhcpDiscover()
|
|
119
|
+
offer = p.BuildDhcpOffer(client_mac="aa:bb:cc:dd:ee:ff",
|
|
120
|
+
offered_ip="192.168.1.100")
|
|
121
|
+
|
|
122
|
+
# NTP
|
|
123
|
+
req = p.BuildNtpRequest()
|
|
124
|
+
resp = p.BuildNtpReply()
|
|
125
|
+
|
|
126
|
+
# TLS handshake (produces realistic-looking bytes for testing)
|
|
127
|
+
ch = p.BuildTlsClientHello()
|
|
128
|
+
sh = p.BuildTlsServerHello()
|
|
129
|
+
ct = p.BuildTlsCertificate()
|
|
130
|
+
fin = p.BuildTlsFinished()
|
|
131
|
+
app = p.BuildTlsApplicationData(b"GET / HTTP/1.1\r\n")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Write with a timestamp
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
import time
|
|
138
|
+
|
|
139
|
+
p.WritePacketTime(raw_bytes, int(time.time() * 1e9)) # nanoseconds
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Live capture
|
|
145
|
+
|
|
146
|
+
Requires **root** or `CAP_NET_RAW` on Linux, **root** or the network entitlement
|
|
147
|
+
on macOS.
|
|
148
|
+
|
|
149
|
+
### Simple callback loop
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
import pycapng
|
|
153
|
+
|
|
154
|
+
with pycapng.Capture("eth0") as cap:
|
|
155
|
+
cap.set_filter("tcp.dstport == 443")
|
|
156
|
+
cap.loop(0, lambda pkt: print(f"{pkt.captured_len} bytes"))
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Full example
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
import pycapng, signal
|
|
163
|
+
|
|
164
|
+
cap = pycapng.Capture(pycapng.capture_default_device())
|
|
165
|
+
cap.set_snaplen(65535)
|
|
166
|
+
cap.set_promisc(True)
|
|
167
|
+
cap.set_timeout(100) # ms between ring deliveries
|
|
168
|
+
cap.set_filter("ip and not arp")
|
|
169
|
+
|
|
170
|
+
signal.signal(signal.SIGINT, lambda *_: cap.break_loop())
|
|
171
|
+
|
|
172
|
+
def on_packet(pkt: pycapng.PacketInfo) -> None:
|
|
173
|
+
ts = pkt.timestamp_ns // 1_000_000_000
|
|
174
|
+
print(f"[{ts}] {pkt.direction} {pkt.captured_len}/{pkt.original_len} bytes")
|
|
175
|
+
|
|
176
|
+
cap.loop(0, on_packet)
|
|
177
|
+
|
|
178
|
+
stats = cap.get_stats()
|
|
179
|
+
print(f"received={stats.received} dropped={stats.dropped}")
|
|
180
|
+
cap.close()
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Display filter syntax
|
|
184
|
+
|
|
185
|
+
The filter language is a subset of Wireshark's display filter syntax.
|
|
186
|
+
|
|
187
|
+
| Example | Meaning |
|
|
188
|
+
|---|---|
|
|
189
|
+
| `tcp` | any TCP packet |
|
|
190
|
+
| `tcp.dstport == 443` | TCP to port 443 |
|
|
191
|
+
| `ip.src == 192.168.0.0/16` | source in subnet |
|
|
192
|
+
| `tcp.flags.syn and not tcp.flags.ack` | SYN-only |
|
|
193
|
+
| `udp.port == 53` | DNS (src or dst) |
|
|
194
|
+
| `ip.addr == 10.0.0.1` | any direction to/from IP |
|
|
195
|
+
| `tcp and not (tcp.dstport == 80 or tcp.dstport == 443)` | non-web TCP |
|
|
196
|
+
| `eth.addr == aa:bb:cc:dd:ee:ff` | by MAC (src or dst) |
|
|
197
|
+
|
|
198
|
+
Built-in fields: `eth.{src,dst,type,addr}` · `ip.{src,dst,proto,ttl,len,addr}` ·
|
|
199
|
+
`ip6.{src,dst}` · `tcp.{srcport,dstport,flags,flags.syn/ack/rst/fin,port}` ·
|
|
200
|
+
`udp.{srcport,dstport,port}` · `icmp.{type,code}`
|
|
201
|
+
|
|
202
|
+
### Capture directly to a pcapng file
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
# one-liner: capture 500 TLS packets and save to a file
|
|
206
|
+
pycapng.capture_to_file("eth0", "tls.pcapng",
|
|
207
|
+
filter="tcp.dstport == 443", count=500)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### List available interfaces
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
for dev in pycapng.capture_list_devices():
|
|
214
|
+
print(dev["name"], "(loopback)" if dev["loopback"] else "")
|
|
215
|
+
|
|
216
|
+
iface = pycapng.capture_default_device()
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## IP fragment reassembly
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
import pycapng
|
|
225
|
+
|
|
226
|
+
r = pycapng.Reassembler()
|
|
227
|
+
p = pycapng.PcapNG()
|
|
228
|
+
p.OpenFile("fragments.pcapng", "r")
|
|
229
|
+
|
|
230
|
+
def on_packet(data: bytes, ts: int) -> None:
|
|
231
|
+
complete = r.add(data)
|
|
232
|
+
if complete:
|
|
233
|
+
print(f"reassembled datagram: {len(complete)} bytes")
|
|
234
|
+
|
|
235
|
+
p.ForeachPacket(on_packet)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## pcapsh script engine
|
|
241
|
+
|
|
242
|
+
The `pycapng.pcapsh` submodule exposes a scripting engine for building and
|
|
243
|
+
replaying packet flows declaratively.
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
from pycapng import pcapsh
|
|
247
|
+
|
|
248
|
+
engine = pcapsh.PcapSH()
|
|
249
|
+
engine.RunScript("my_flow.pcapsh", "out.pcapng")
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
TLS record builders are also available as standalone functions:
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
from pycapng.pcapsh import (
|
|
256
|
+
tls_client_hello, tls_server_hello,
|
|
257
|
+
tls_certificate, tls_finished,
|
|
258
|
+
tls_application_data,
|
|
259
|
+
)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## C library
|
|
265
|
+
|
|
266
|
+
pycapng is the Python interface to [libpcapng](https://github.com/stricaud/libpcapng),
|
|
267
|
+
a C library that also ships a standalone command-line tool (`pcapsh`) and a
|
|
268
|
+
full C API. See the repository for C usage, build instructions, and the
|
|
269
|
+
[capture API reference](docs/capture.md).
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# pycapng
|
|
2
|
+
|
|
3
|
+
Python library for reading, writing, and capturing network traffic in the
|
|
4
|
+
[pcapng](https://github.com/pcapng/pcapng) file format.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
pip install pycapng
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- **Read** pcapng files packet by packet
|
|
11
|
+
- **Write** pcapng files with hand-crafted Ethernet/IP/TCP/UDP/ICMP/DNS/DHCP/NTP/TLS packets
|
|
12
|
+
- **Capture** live traffic with a Wireshark-style display filter (Linux / macOS)
|
|
13
|
+
- **Reassemble** fragmented IP packets
|
|
14
|
+
- **Script** packet flows with the built-in pcapsh engine
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Reading a pcapng file
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import pycapng
|
|
22
|
+
|
|
23
|
+
p = pycapng.PcapNG()
|
|
24
|
+
p.OpenFile("capture.pcapng", "r")
|
|
25
|
+
|
|
26
|
+
def on_packet(data: bytes, ts: int) -> None:
|
|
27
|
+
print(f"packet {len(data)} bytes ts={ts}")
|
|
28
|
+
|
|
29
|
+
p.ForeachPacket(on_packet)
|
|
30
|
+
p.CloseFile()
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Writing packets
|
|
36
|
+
|
|
37
|
+
### Raw bytes
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import pycapng
|
|
41
|
+
|
|
42
|
+
p = pycapng.PcapNG()
|
|
43
|
+
p.OpenFile("out.pcapng", "w")
|
|
44
|
+
|
|
45
|
+
raw = bytes([0x45, 0x00, 0x00, 0x28]) # any bytes
|
|
46
|
+
p.WritePacket(raw)
|
|
47
|
+
|
|
48
|
+
p.CloseFile()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### TCP
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
p.WriteTcpPacket(
|
|
55
|
+
src_ip="192.168.1.10", dst_ip="93.184.216.34",
|
|
56
|
+
src_port=54321, dst_port=80,
|
|
57
|
+
payload=b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n",
|
|
58
|
+
flags=0x02, # SYN
|
|
59
|
+
)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Build a packet without writing it
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
syn = p.BuildTcpPacket(
|
|
66
|
+
src_ip="10.0.0.1", dst_ip="10.0.0.2",
|
|
67
|
+
src_port=1234, dst_port=443,
|
|
68
|
+
payload=b"", flags=0x02,
|
|
69
|
+
)
|
|
70
|
+
# syn is bytes — inspect it, modify it, write it later
|
|
71
|
+
p.WritePacket(syn)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### UDP / ICMP
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
pkt = p.BuildUdpPacket(
|
|
78
|
+
src_ip="10.0.0.1", dst_ip="8.8.8.8",
|
|
79
|
+
src_port=12345, dst_port=53,
|
|
80
|
+
payload=b"\x00\x01...", # your DNS query bytes
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
icmp = p.BuildIcmpPacket(
|
|
84
|
+
src_ip="10.0.0.1", dst_ip="10.0.0.2",
|
|
85
|
+
icmp_type=8, icmp_code=0, # Echo Request
|
|
86
|
+
payload=b"hello",
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Application-layer helpers
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
# DNS
|
|
94
|
+
query = p.BuildDnsQuery("example.com")
|
|
95
|
+
response = p.BuildDNSResponse("example.com", "93.184.216.34")
|
|
96
|
+
|
|
97
|
+
# DHCP
|
|
98
|
+
discover = p.BuildDhcpDiscover()
|
|
99
|
+
offer = p.BuildDhcpOffer(client_mac="aa:bb:cc:dd:ee:ff",
|
|
100
|
+
offered_ip="192.168.1.100")
|
|
101
|
+
|
|
102
|
+
# NTP
|
|
103
|
+
req = p.BuildNtpRequest()
|
|
104
|
+
resp = p.BuildNtpReply()
|
|
105
|
+
|
|
106
|
+
# TLS handshake (produces realistic-looking bytes for testing)
|
|
107
|
+
ch = p.BuildTlsClientHello()
|
|
108
|
+
sh = p.BuildTlsServerHello()
|
|
109
|
+
ct = p.BuildTlsCertificate()
|
|
110
|
+
fin = p.BuildTlsFinished()
|
|
111
|
+
app = p.BuildTlsApplicationData(b"GET / HTTP/1.1\r\n")
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Write with a timestamp
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import time
|
|
118
|
+
|
|
119
|
+
p.WritePacketTime(raw_bytes, int(time.time() * 1e9)) # nanoseconds
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Live capture
|
|
125
|
+
|
|
126
|
+
Requires **root** or `CAP_NET_RAW` on Linux, **root** or the network entitlement
|
|
127
|
+
on macOS.
|
|
128
|
+
|
|
129
|
+
### Simple callback loop
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
import pycapng
|
|
133
|
+
|
|
134
|
+
with pycapng.Capture("eth0") as cap:
|
|
135
|
+
cap.set_filter("tcp.dstport == 443")
|
|
136
|
+
cap.loop(0, lambda pkt: print(f"{pkt.captured_len} bytes"))
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Full example
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
import pycapng, signal
|
|
143
|
+
|
|
144
|
+
cap = pycapng.Capture(pycapng.capture_default_device())
|
|
145
|
+
cap.set_snaplen(65535)
|
|
146
|
+
cap.set_promisc(True)
|
|
147
|
+
cap.set_timeout(100) # ms between ring deliveries
|
|
148
|
+
cap.set_filter("ip and not arp")
|
|
149
|
+
|
|
150
|
+
signal.signal(signal.SIGINT, lambda *_: cap.break_loop())
|
|
151
|
+
|
|
152
|
+
def on_packet(pkt: pycapng.PacketInfo) -> None:
|
|
153
|
+
ts = pkt.timestamp_ns // 1_000_000_000
|
|
154
|
+
print(f"[{ts}] {pkt.direction} {pkt.captured_len}/{pkt.original_len} bytes")
|
|
155
|
+
|
|
156
|
+
cap.loop(0, on_packet)
|
|
157
|
+
|
|
158
|
+
stats = cap.get_stats()
|
|
159
|
+
print(f"received={stats.received} dropped={stats.dropped}")
|
|
160
|
+
cap.close()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Display filter syntax
|
|
164
|
+
|
|
165
|
+
The filter language is a subset of Wireshark's display filter syntax.
|
|
166
|
+
|
|
167
|
+
| Example | Meaning |
|
|
168
|
+
|---|---|
|
|
169
|
+
| `tcp` | any TCP packet |
|
|
170
|
+
| `tcp.dstport == 443` | TCP to port 443 |
|
|
171
|
+
| `ip.src == 192.168.0.0/16` | source in subnet |
|
|
172
|
+
| `tcp.flags.syn and not tcp.flags.ack` | SYN-only |
|
|
173
|
+
| `udp.port == 53` | DNS (src or dst) |
|
|
174
|
+
| `ip.addr == 10.0.0.1` | any direction to/from IP |
|
|
175
|
+
| `tcp and not (tcp.dstport == 80 or tcp.dstport == 443)` | non-web TCP |
|
|
176
|
+
| `eth.addr == aa:bb:cc:dd:ee:ff` | by MAC (src or dst) |
|
|
177
|
+
|
|
178
|
+
Built-in fields: `eth.{src,dst,type,addr}` · `ip.{src,dst,proto,ttl,len,addr}` ·
|
|
179
|
+
`ip6.{src,dst}` · `tcp.{srcport,dstport,flags,flags.syn/ack/rst/fin,port}` ·
|
|
180
|
+
`udp.{srcport,dstport,port}` · `icmp.{type,code}`
|
|
181
|
+
|
|
182
|
+
### Capture directly to a pcapng file
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
# one-liner: capture 500 TLS packets and save to a file
|
|
186
|
+
pycapng.capture_to_file("eth0", "tls.pcapng",
|
|
187
|
+
filter="tcp.dstport == 443", count=500)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### List available interfaces
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
for dev in pycapng.capture_list_devices():
|
|
194
|
+
print(dev["name"], "(loopback)" if dev["loopback"] else "")
|
|
195
|
+
|
|
196
|
+
iface = pycapng.capture_default_device()
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## IP fragment reassembly
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
import pycapng
|
|
205
|
+
|
|
206
|
+
r = pycapng.Reassembler()
|
|
207
|
+
p = pycapng.PcapNG()
|
|
208
|
+
p.OpenFile("fragments.pcapng", "r")
|
|
209
|
+
|
|
210
|
+
def on_packet(data: bytes, ts: int) -> None:
|
|
211
|
+
complete = r.add(data)
|
|
212
|
+
if complete:
|
|
213
|
+
print(f"reassembled datagram: {len(complete)} bytes")
|
|
214
|
+
|
|
215
|
+
p.ForeachPacket(on_packet)
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## pcapsh script engine
|
|
221
|
+
|
|
222
|
+
The `pycapng.pcapsh` submodule exposes a scripting engine for building and
|
|
223
|
+
replaying packet flows declaratively.
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
from pycapng import pcapsh
|
|
227
|
+
|
|
228
|
+
engine = pcapsh.PcapSH()
|
|
229
|
+
engine.RunScript("my_flow.pcapsh", "out.pcapng")
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
TLS record builders are also available as standalone functions:
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
from pycapng.pcapsh import (
|
|
236
|
+
tls_client_hello, tls_server_hello,
|
|
237
|
+
tls_certificate, tls_finished,
|
|
238
|
+
tls_application_data,
|
|
239
|
+
)
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## C library
|
|
245
|
+
|
|
246
|
+
pycapng is the Python interface to [libpcapng](https://github.com/stricaud/libpcapng),
|
|
247
|
+
a C library that also ships a standalone command-line tool (`pcapsh`) and a
|
|
248
|
+
full C API. See the repository for C usage, build instructions, and the
|
|
249
|
+
[capture API reference](docs/capture.md).
|
pycapng-0.4/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# libpcapng
|
|
2
|
+
|
|
3
|
+
C library (with Python bindings) for reading, writing, and capturing network
|
|
4
|
+
traffic in the [pcapng](https://github.com/pcapng/pcapng) file format.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- Full pcapng block support: SHB, IDB, EPB, SPB, NRB, ISB, DSB
|
|
9
|
+
- TLV options on every block type
|
|
10
|
+
- Zero-copy live capture — Linux `TPACKET_V3` ring buffer, macOS BPF
|
|
11
|
+
- Wireshark-style display filter engine operating on decoded packet fields
|
|
12
|
+
- POSA protocol extension hook for custom field providers
|
|
13
|
+
- IP fragment reassembly
|
|
14
|
+
- pcapsh scripting engine for building and replaying packet flows
|
|
15
|
+
- Python bindings (`pip install pycapng`) — see [README-pypi.md](README-pypi.md)
|
|
16
|
+
|
|
17
|
+
## Building
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
mkdir build && cd build
|
|
21
|
+
cmake .. -DCMAKE_BUILD_TYPE=Release
|
|
22
|
+
make -j$(nproc)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Python bindings
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install pycapng
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
See [README-pypi.md](README-pypi.md) for the full Python API reference.
|
|
32
|
+
|
|
33
|
+
## C quick-start
|
|
34
|
+
|
|
35
|
+
Write a TCP SYN packet to a pcapng file:
|
|
36
|
+
|
|
37
|
+
```c
|
|
38
|
+
#include <libpcapng/libpcapng.h>
|
|
39
|
+
|
|
40
|
+
FILE *fp = fopen("out.pcapng", "wb");
|
|
41
|
+
libpcapng_write_header_to_file(fp);
|
|
42
|
+
|
|
43
|
+
/* raw Ethernet frame bytes */
|
|
44
|
+
uint8_t frame[...];
|
|
45
|
+
libpcapng_write_enhanced_packet_to_file(fp, frame, sizeof(frame));
|
|
46
|
+
|
|
47
|
+
fclose(fp);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Compile:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
gcc main.c -o main $(pkg-config libpcapng --libs --cflags)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Live capture (C)
|
|
57
|
+
|
|
58
|
+
```c
|
|
59
|
+
#include <libpcapng/capture.h>
|
|
60
|
+
|
|
61
|
+
char errbuf[PCAPNG_CAPTURE_ERRBUF_SIZE];
|
|
62
|
+
pcapng_capture_t *cap = pcapng_capture_open("eth0", errbuf);
|
|
63
|
+
pcapng_capture_set_filter(cap, "tcp.dstport == 443", errbuf);
|
|
64
|
+
|
|
65
|
+
void on_packet(const pcapng_packet_info_t *pkt, void *ud) {
|
|
66
|
+
printf("%u bytes\n", pkt->captured_len);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
pcapng_capture_loop(cap, 0, on_packet, NULL);
|
|
70
|
+
pcapng_capture_close(cap);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
See [docs/capture.md](docs/capture.md) for the full capture API.
|
|
74
|
+
|
|
75
|
+
## pcapsh
|
|
76
|
+
|
|
77
|
+
An interactive shell and scripting engine for building packet flows:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
./pcapsh
|
|
81
|
+
pcapsh> tcp src 10.0.0.1:1234 dst 10.0.0.2:80 payload "hello"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT
|
|
@@ -20,7 +20,7 @@ build-backend = "scikit_build_core.build"
|
|
|
20
20
|
name = "pycapng"
|
|
21
21
|
dynamic = ["version"]
|
|
22
22
|
description = "Python bindings for libpcapng — read/write pcapng files and live packet capture"
|
|
23
|
-
readme = "README.md"
|
|
23
|
+
readme = "README-pypi.md"
|
|
24
24
|
requires-python = ">=3.9"
|
|
25
25
|
license = { text = "MIT" }
|
|
26
26
|
authors = [{ name = "Seb Tricaud", email = "sebastien.tricaud@proton.me" }]
|
pycapng-0.3/PKG-INFO
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.2
|
|
2
|
-
Name: pycapng
|
|
3
|
-
Version: 0.3
|
|
4
|
-
Summary: Python bindings for libpcapng — read/write pcapng files and live packet capture
|
|
5
|
-
Keywords: pcapng,pcap,packet-capture,network,packet,wireshark
|
|
6
|
-
Author-Email: Seb Tricaud <sebastien.tricaud@proton.me>
|
|
7
|
-
License: MIT
|
|
8
|
-
Classifier: Development Status :: 3 - Alpha
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
-
Classifier: Operating System :: MacOS :: MacOS X
|
|
12
|
-
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: C++
|
|
14
|
-
Classifier: Topic :: System :: Networking :: Monitoring
|
|
15
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
-
Project-URL: Homepage, https://github.com/stricaud/libpcapng
|
|
17
|
-
Project-URL: Repository, https://github.com/stricaud/libpcapng
|
|
18
|
-
Requires-Python: >=3.9
|
|
19
|
-
Description-Content-Type: text/markdown
|
|
20
|
-
|
|
21
|
-
# libpcapng
|
|
22
|
-
|
|
23
|
-
C library with Python bindings to read and write pcapng buffers. It follows the pcapng standard from https://github.com/pcapng/pcapng
|
|
24
|
-
|
|
25
|
-
## Writing your first packet
|
|
26
|
-
|
|
27
|
-
Let's write a TCP SYN using libdumbnet and push the buffer into a pcapng file:
|
|
28
|
-
|
|
29
|
-
```c
|
|
30
|
-
#include <stdio.h>
|
|
31
|
-
#include <stdlib.h>
|
|
32
|
-
#include <string.h>
|
|
33
|
-
|
|
34
|
-
#include <dumbnet.h>
|
|
35
|
-
#include <libpcapng/libpcapng.h>
|
|
36
|
-
|
|
37
|
-
int main(int argc, char **argv)
|
|
38
|
-
{
|
|
39
|
-
FILE *pcapout;
|
|
40
|
-
|
|
41
|
-
char *pkt;
|
|
42
|
-
size_t pktsize = 40;
|
|
43
|
-
|
|
44
|
-
pkt = (char *)malloc(pktsize);
|
|
45
|
-
if (!pkt) {
|
|
46
|
-
fprintf(stderr, "Cannot allocate pkt!\n");
|
|
47
|
-
return -1;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
pcapout = fopen("pkts.pcap", "wb");
|
|
51
|
-
|
|
52
|
-
libpcapng_write_header_to_file(pcapout);
|
|
53
|
-
|
|
54
|
-
uint32_t saddr = 0x0100a8c0;
|
|
55
|
-
uint32_t daddr = 0x561c1268;
|
|
56
|
-
|
|
57
|
-
ip_pack_hdr(pkt, 0, pktsize, 1, 0, 128, IP_PROTO_TCP, saddr, daddr);
|
|
58
|
-
tcp_pack_hdr(pkt + 20, 4096, 80, 1, 0, TH_SYN, 14, 0);
|
|
59
|
-
ip_checksum(pkt, pktsize);
|
|
60
|
-
|
|
61
|
-
libpcapng_write_enhanced_packet_to_file(pcapout, pkt, pktsize);
|
|
62
|
-
|
|
63
|
-
fflush(pcapout);
|
|
64
|
-
fclose(pcapout);
|
|
65
|
-
|
|
66
|
-
free(pkt);
|
|
67
|
-
|
|
68
|
-
return 0;
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Compile using:
|
|
73
|
-
```
|
|
74
|
-
gcc test.c -o test -ldumbnet $(pkg-config libpcapng --libs --cflags)
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
And after running, open the created pcap with wireshark:
|
|
78
|
-
|
|
79
|
-
<p align="center"><img src="doc/firstpkt.png" size="30%"/></p>
|