nadzoring.arp package¶
ARP module for cache retrieval and spoofing detection.
- class nadzoring.arp.ARPCache[source]¶
Bases:
objectARP cache retrieval and parsing.
Provides platform-specific methods to retrieve and parse ARP cache entries from Linux, Windows, and macOS systems. Automatically detects the current platform and uses the appropriate command and parser.
- _get_darwin_cache() list[ARPEntry][source]¶
Get ARP cache on macOS using
arp -a.- Returns:
List of
ARPEntryobjects from the macOS ARP cache.- Raises:
ARPCacheRetrievalError – If
arpcommand not found or fails.
- _get_linux_cache() list[ARPEntry][source]¶
Get ARP cache on Linux using
ip neigh.- Returns:
List of
ARPEntryobjects from the Linux ARP cache.- Raises:
ARPCacheRetrievalError – If
ipcommand not found or fails.
- static _get_platform() str[source]¶
Detect the current platform.
- Returns:
"linux","windows", or"darwin".- Return type:
Platform identifier
- Raises:
ARPCacheRetrievalError – If the platform is not supported.
- _get_windows_cache() list[ARPEntry][source]¶
Get ARP cache on Windows using
arp -a.- Returns:
List of
ARPEntryobjects from the Windows ARP cache.- Raises:
ARPCacheRetrievalError – If
arpcommand not found or fails.
- _parse_darwin_arp_output(output: str) list[ARPEntry][source]¶
Parse
arp -aoutput on macOS.- Parameters:
output – Raw output from macOS
arp -a.- Returns:
List of parsed
ARPEntryobjects.
- _parse_ip_neigh_output(output: str) list[ARPEntry][source]¶
Parse
ip neighoutput on Linux.- Parameters:
output – Raw output from
ip neigh show.- Returns:
List of parsed
ARPEntryobjects.
- _parse_windows_arp_output(output: str) list[ARPEntry][source]¶
Parse
arp -aoutput on Windows.- Parameters:
output – Raw output from Windows
arp -a.- Returns:
List of parsed
ARPEntryobjects.
- get_cache() list[ARPEntry][source]¶
Get ARP cache entries for the current platform.
Automatically selects the appropriate method based on the detected platform and returns parsed ARP entries.
- Returns:
List of
ARPEntryobjects representing the current ARP cache.- Raises:
ARPCacheRetrievalError – If cache retrieval fails or platform is unsupported.
- exception nadzoring.arp.ARPCacheRetrievalError[source]¶
Bases:
ARPErrorRaised when reading the ARP cache fails.
The exception message should be one of the strings defined in
nadzoring.arp.errors.
- class nadzoring.arp.ARPEntry(ip_address: str, mac_address: str | None, interface: str, state: ARPEntryState, flags: str | None = None)[source]¶
Bases:
objectRepresents a single ARP cache entry.
- Variables:
ip_address (str) – IP address string (e.g.
"192.168.1.1").mac_address (str | None) – MAC address string (e.g.
"00:11:22:33:44:55"), orNonefor incomplete entries.interface (str) – Network interface name (e.g.
"eth0").state (nadzoring.arp.models.ARPEntryState) – Current state of the ARP entry.
flags (str | None) – Optional platform-specific flags or attributes.
- __init__(ip_address: str, mac_address: str | None, interface: str, state: ARPEntryState, flags: str | None = None) None[source]¶
- flags: str | None = None¶
- property has_mac: bool¶
Check whether the entry has a resolved MAC address.
- Returns:
Trueif entry has a valid MAC address,Falsefor incomplete entries.
- interface: str¶
- ip_address: str¶
- mac_address: str | None¶
- state: ARPEntryState¶
- class nadzoring.arp.ARPEntryState(*values)[source]¶
Bases:
EnumARP entry state enumeration.
Represents the possible states of an ARP cache entry as defined by the Linux kernel neighbor subsystem.
- Variables:
REACHABLE – Entry is valid and reachable.
STALE – Entry is valid but possibly unreachable.
DELAY – Waiting for confirmation before probing.
PROBE – Actively probing the entry.
FAILED – Entry has failed resolution.
PERMANENT – Manually configured permanent entry.
NOARP – Device does not support ARP.
UNKNOWN – State could not be determined.
- DELAY = 'delay'¶
- FAILED = 'failed'¶
- NOARP = 'noarp'¶
- PERMANENT = 'permanent'¶
- PROBE = 'probe'¶
- REACHABLE = 'reachable'¶
- STALE = 'stale'¶
- UNKNOWN = 'unknown'¶
- class nadzoring.arp.ARPRealtimeDetector[source]¶
Bases:
objectReal-time ARP spoofing detector using packet analysis.
Monitors network traffic for ARP packets and detects potential spoofing attacks by tracking IP-to-MAC mappings and identifying inconsistencies.
When a packet arrives, the detector checks whether the source MAC already maps to a different IP — if so, a spoofing alert is generated.
- Variables:
ip_mac_map – Dict mapping MAC addresses to their most recently seen IP.
stats – Monitoring statistics (packets processed, alerts, unique MACs).
Examples
>>> detector = ARPRealtimeDetector() >>> alerts = detector.monitor(interface="eth0", count=100) >>> for alert in alerts: ... print(alert["message"])
- get_stats() dict[str, int][source]¶
Return a copy of the current monitoring statistics.
- Returns:
Dict with
packets_processed,alerts_generated, andunique_macs_seenkeys.
- monitor(interface: str | None = None, count: int = 10, timeout_config: TimeoutConfig | None = None, packet_callback: Callable[[Ether, str | None], None] | None = None) list[dict[str, Any]][source]¶
Monitor network traffic for ARP spoofing attacks.
- Parameters:
interface – Network interface to monitor.
Nonemonitors all interfaces.count – Number of packets to capture.
0captures indefinitely.timeout_config – unified configuration for timeout, if is
Noneset default timeouts.packet_callback – Optional callback receiving
(packet, alert)for each processed packet. WhenNone, detected alerts are collected internally and returned.
- Returns:
List of alert dicts, each with
timestamp,interface,message,src_ip, andsrc_mackeys. Always empty when a custom packet_callback is supplied.- Raises:
RuntimeError – If packet sniffing fails.
- process_packet(packet: Ether) str | None[source]¶
Process a single network packet for ARP spoofing detection.
- Parameters:
packet – Scapy packet object. Must contain both ARP and Ethernet layers; packets missing either layer are silently ignored.
- Returns:
Alert message string when spoofing is detected,
Noneotherwise. Format:"ARP attack detected from machine with IP {old_ip} for {new_ip}".
- class nadzoring.arp.ARPSpoofingDetector(cache: ARPCache)[source]¶
Bases:
objectDetects potential ARP spoofing attacks.
Analyses ARP cache entries to identify spoofing attempts by detecting duplicate MAC addresses across different IPs or duplicate IP addresses across different MACs.
- Variables:
cache –
ARPCacheinstance used to retrieve ARP entries.
- __init__(cache: ARPCache) None[source]¶
Initialise the detector with an ARP cache.
- Parameters:
cache –
ARPCacheinstance for retrieving ARP entries.
- detect() list[SpoofingAlert][source]¶
Detect potential ARP spoofing in the current cache.
Analyses all ARP entries and generates alerts for two suspicious patterns:
duplicate_mac — same MAC address associated with multiple IPs
duplicate_ip — same IP address associated with multiple MACs
- Returns:
List of
SpoofingAlertobjects for each detected anomaly.
- class nadzoring.arp.SpoofingAlert(alert_type: str, ip_address: str, mac_address: str, interfaces: list[str], description: str)[source]¶
Bases:
objectRepresents a potential ARP spoofing alert.
- Variables:
alert_type (str) – Type of alert —
"duplicate_mac"or"duplicate_ip".ip_address (str) – IP address involved in the alert.
mac_address (str) – MAC address involved in the alert.
interfaces (list[str]) – Network interfaces where the issue was detected.
description (str) – Human-readable description of the alert.
- __init__(alert_type: str, ip_address: str, mac_address: str, interfaces: list[str], description: str) None[source]¶
- alert_type: str¶
- description: str¶
- interfaces: list[str]¶
- ip_address: str¶
- mac_address: str¶