Advertisement
Open Source Projects by Phil Schwartz

Making DenyHosts Dependable Across Changing Networks

DenyHosts was designed for a practical Linux security problem: repeated SSH login attempts can consume administrator time and expose services to persistent brute-force attacks. Its core task is straightforward—inspect authentication failures, identify abusive addresses, and update deny rules—but reliability becomes more complicated when a system changes networks frequently.

Laptops, cloud instances, virtual machines, and small servers may move between wired connections, wireless networks, VPNs, and dynamically assigned public addresses. A dependable SSH attack blocker must preserve useful security state while adapting to changing interfaces, routes, hostnames, and address families. The goal is to avoid treating network mobility as evidence of either trust or malicious activity.

Separate Host Identity From Network Location

A changing IP address should not redefine the machine running DenyHosts. Network location is an operational detail, while host identity is better represented by local configuration, a persistent data directory, and carefully managed state files. Keeping these concepts separate allows block history and reporting data to survive DHCP renewals or movement between networks.

The distinction also matters when a device receives an address previously used by another subscriber. DenyHosts should not assume that every address change represents continuity, and it should not blindly carry local-network assumptions into a new environment. A stable local identity can preserve useful observations without treating the current address as permanent.

Configuration paths, log locations, and ownership permissions should therefore be explicit. Relative paths, temporary directories, and interface-specific assumptions create avoidable failures during boot or after a network restart. Persistent storage gives the daemon a reliable reference point even when the network is unavailable for part of the startup process.

Observe Authentication Events Without Network Assumptions

The most dependable design begins with the authentication log rather than the network interface. SSH failure records contain the evidence DenyHosts needs: source address, timestamp, username, and the nature of the failed attempt. Monitoring those events remains possible even while routing changes, provided the log watcher handles rotation and temporary file replacement correctly.

A robust parser should accept IPv4 and IPv6 formats, compressed IPv6 notation, varied SSH message layouts, and distributions that place secure-shell events in different files. It should also avoid confusing local service messages with remote authentication failures. Parsing should produce normalized records before the blocking policy evaluates them.

This approach keeps detection independent from whether the host is connected through Ethernet, cellular service, a tunnel, or a temporary hotspot. When the network disappears, DenyHosts can continue reading and recording events. When connectivity returns, it can synchronize or apply policy without reconstructing its entire history from interface state.

Apply Blocks Through a Reconciled Firewall Layer

Writing an address to a deny file is useful only if the SSH service or firewall actually enforces it. On systems with changing connectivity, the enforcement layer should be reconciled rather than updated through fragile one-time commands. A reconciliation pass compares the desired blocked set with the rules currently installed and adds or removes only the necessary entries.

This model works better across firewall reloads, interface recreation, and service restarts. It also reduces duplicate rules, which can become common when a mobile system repeatedly invokes network hooks. The integration should support the platform’s active firewall framework—such as iptables, nftables, or a host access-control mechanism—without assuming that one interface or chain will always exist.

IPv6 deserves equal treatment. Blocking only IPv4 addresses leaves a reachable SSH service exposed if the host advertises an IPv6 route. Address-family-aware storage and rule generation prevent a network transition from silently reducing protection. If a firewall backend is unavailable, DenyHosts should record the pending action and retry rather than marking the address as successfully blocked.

Operating condition Main reliability risk Safer DenyHosts behavior
DHCP address renewal Treating a new address as a new host Preserve local state and reapply policy
VPN connection added Applying rules to the wrong interface Reconcile rules independently of interface names
IPv4/IPv6 transition Leaving one address family unprotected Track and enforce both address types
Firewall reload Losing previously installed blocks Rebuild rules from the persistent blocked set
Offline period Failed updates or incomplete synchronization Queue changes and retry with backoff

Handle Shared State Carefully

DenyHosts deployments may exchange blocked-address information with other hosts. That collaboration can improve detection, but frequent network changes make synchronization timing important. A machine that is offline should retain locally observed failures and transmit them when a trusted connection becomes available.

Shared state should be merged idempotently. Receiving the same address twice must not inflate counters or create duplicate firewall entries. Timestamps, source classification, and schema versions help distinguish fresh observations from old data. An append-only event record or transactional update also reduces corruption if connectivity fails during a write.

Trust boundaries require similar care. A laptop moving between networks should not accept arbitrary blocklists simply because a synchronization endpoint is reachable. Authentication, integrity checks, transport encryption, and explicit peer configuration are necessary, especially when a false block could deny access to legitimate administrators.

Prevent Lockouts During Mobility

Changing networks can produce false positives in subtle ways. A developer may connect from a new office address, a mobile carrier may place many users behind one public IP, or a VPN gateway may represent a large group of legitimate clients. A threshold designed for a fixed server can become too aggressive on a roaming system.

Allowlisting must be conservative and precise. Local management addresses, trusted VPN exit points, and emergency administration paths can be exempted, but broad private-network ranges should not automatically be trusted in every environment. Rules should be reviewed whenever the machine joins a new network, particularly if SSH is exposed through a gateway or port-forward.

A safe system also provides recovery controls. Administrators need a documented way to inspect the current deny set, remove an accidental block, and pause automated enforcement without deleting evidence. Dry-run logging and escalating thresholds can help verify behavior before a new installation begins modifying firewall rules.

For related development work, the same emphasis on transparent diagnostics appears in Phil Schwartz’s regex tester project, where useful tooling depends on making program behavior visible rather than hiding it behind opaque automation.

Test Failure Recovery as a Normal Feature

Reliability should be tested under the conditions that cause network-aware software to fail. Automated tests can simulate DHCP changes, interface renaming, DNS outages, VPN activation, firewall flushes, log rotation, clock adjustments, and repeated daemon restarts. Each scenario should verify both the persistent state and the effective SSH protection.

Observability is essential during these tests and in production. Structured logs should distinguish detected failures, policy decisions, synchronization attempts, and firewall operations. Metrics such as blocked addresses, queued updates, parser errors, and last successful reconciliation give administrators a way to identify drift before it becomes an incident.

A practical operating checklist includes:

Keep Security State Portable but Deliberate

The strongest design treats network changes as routine environmental events rather than exceptional failures. DenyHosts can continue detecting SSH abuse if log processing, persistent state, and enforcement are loosely coupled. Each component should tolerate temporary unavailability and report what it could not complete.

Portability does not mean copying every rule everywhere. It means preserving the evidence and policy needed to make a deliberate decision after the host moves. Local observations, shared intelligence, firewall state, and administrator exceptions should remain distinguishable so that recovery does not erase context.

For Linux administrators and developers maintaining open-source security tools, these principles offer a durable path: monitor the service logs, preserve state, reconcile enforcement, protect synchronization, and test every transition. Explore DenyHosts and its surrounding utilities as a practical foundation for building SSH defenses that remain dependable wherever the system connects.