#40629·cowrie

`ping` without `-c` leaves an orphaned per-second reactor timer running forever after the client disconnects

Author: vbontchevCreated Sep 23, 2026Updated Sep 23, 2026

Describe the bug ping <host> run without -c <count> leaves self.max at 0. Command_ping.showreply() increments self.count before comparing it to self.max (self.count += 1 then if self.count == self.max), so once self.count reaches 1 it can never equal 0 again and the command never reaches its own termination branch. This matches real ping (no -c means run until interrupted), and is not itself the bug.

The bug is that nothing stops this scheduled timer when the underlying connection is lost. showreply() reschedules itself with reactor.callLater(1, self.showreply) every second regardless of whether the session is still alive, and only handle_CTRL_C ever calls self.scheduled.cancel(). HoneyPotBaseProtocol.connectionLost() (src/cowrie/shell/protocol.py) clears self.cmdstack and sets self.terminal = None, but does not touch commands that hold their own pending reactor.callLater handles. So once an attacker runs a bare ping and disconnects (or is disconnected, e.g. by the idle timeout) without pressing Ctrl-C first, the scheduled showreply() call is orphaned: it keeps firing once a second, forever, for the remaining lifetime of the Cowrie process.

Each firing calls self.write(...), which goes through PipeProtocol._write_to_terminal() (src/cowrie/shell/pipe.py). Since self.protocol.terminal is now None, this hits the else branch and logs Connection was probably lost. Could not write to terminal every time, i.e. once a second, forever, from a single dropped connection. On a busy honeypot this produces tens of thousands of duplicate log lines per day and leaves an equal number of stale IDelayedCall objects ticking in the reactor.

To Reproduce

  1. Connect to the honeypot (SSH or Telnet) and log in.
  2. Run ping 9.9.9.9 (no -c option).
  3. Disconnect the client immediately (or wait for the idle timeout), without sending Ctrl-C.
  4. Watch the Cowrie log: starting one second after the disconnect, [cowrie.shell.pipe.PipeProtocol#info] Connection was probably lost. Could not write to terminal is logged once every second, indefinitely (observed for over 8 hours straight in production until the log file rolled over at midnight; the underlying timer is not tied to the log rotation and would keep firing past that).

Expected behavior When the client disconnects while an uncounted ping is still running, the pending reactor.callLater should be cancelled (or at least stop rescheduling itself) instead of continuing to fire once a second for the rest of the process's lifetime and spamming the log with the same message.

Server (please complete the following information):

  • OS: Windows 10 Pro (also reproducible on any platform Cowrie runs on; the bug is in showreply()'s own logic, not OS-specific)
  • Python: 3.14.2
  • Twisted: 26.4.0

Additional context

  • Cowrie Mode: Shell (default)
  • Fix attached: src/cowrie/commands/ping.py. showreply() now checks self.protocol is None or self.protocol.terminal is None before writing or rescheduling, and bails out (setting self.running = False) instead. This stops the orphaned chain at the very next tick after disconnect rather than letting it run forever, while leaving normal (connected) ping behavior, including the no--c "runs until interrupted" case, unchanged.
  • Checked the rest of the code for the same pattern (a command holding its own reactor.callLater/LoopingCall that reschedules itself without checking whether the session is still alive): sleep.py and tftp.py also use reactor.callLater, but both are for a single, bounded, one-shot delay (the requested sleep duration, or a fixed TFTP timeout) and terminate on their own, so they are not subject to this same unbounded-reschedule bug. ping.py is the only command that reschedules itself indefinitely.

ping.py