`Passwd.setpwentry()` reseeds the global random module using the attacker's login username, disrupting unrelated randomness in every other concurrent session
Describe the bug
src/cowrie/shell/pwd.py's Passwd.setpwentry() is called (from
src/cowrie/shell/avatar.py) whenever a login username isn't found in
/etc/passwd, to assign that session a synthetic but consistent
uid/gid:
seed_id = crc32(name.encode("utf-8"))
seed(seed_id)
...
e["pw_uid"] = randint(1500, 10000)seed()/randint() here are random.seed/random.randint -- the
module-level functions, which operate on Python's single shared, global
random module state. Calling random.seed(seed_id) doesn't just scope
randomness for this function; it resets the entire process's shared
random state, for every other piece of code anywhere in the same Python
process that calls random.* afterwards, until something reseeds it
again.
name here is the attacker's own login username, entirely attacker
-controlled input. And Cowrie serves many simultaneous SSH/Telnet
sessions from different attackers in a single process. So: any attacker
logging in with a username not already in /etc/passwd (which, given
Cowrie's typically-permissive login handling, is most non-default
usernames) reseeds the global RNG for the whole process, based only on
a CRC32 of their chosen username. Every other concurrently-running
session's unrelated calls to random.* -- e.g. simulated ping latency in
commands/ping.py, simulated package data in commands/apt.py /
commands/yum.py, randomized behavior in commands/base.py /
commands/adduser.py / shell/server.py -- gets whatever sequence
follows from that seed instead of its own expected randomness, for as
long as nothing else happens to reseed it first.
To Reproduce Steps to reproduce the behavior:
- Seed the global
randommodule to some baseline state and record the next few valuesrandom.random()would produce (representing what an unrelated part of the codebase would see). - Reset to the same baseline state, then call
Passwd().setpwentry("some_username")(as happens on an unrecognized-username login) exactly as the current code does it. - Call
random.random()a few more times and compare against step 1's recorded values.
This was reproduced directly: the "unrelated caller" sequence after
setpwentry() runs is completely different from (and fully determined
by) the baseline, confirming the global state is disrupted and is a
deterministic function of the username passed in.
Expected behavior
setpwentry()'s deterministic-per-username uid/gid assignment shouldn't
leak into or disrupt the shared global random state used by the rest of
the process. Using a local random.Random(seed_id) instance scoped to
this function keeps the per-username determinism (verified: the same
username still consistently gets the same uid across calls, and
different usernames still get different uids) without touching global
state (verified: the global random module's sequence is now unaffected
by a setpwentry() call).
Server (please complete the following information):
- OS: This bug is OS-independent (pure Python
randommodule state management, no OS-specific code involved). - Python: Python 3.14.2
- Twisted: Twisted 26.4.0
Additional context
- Cowrie Mode: Shell (default); affects any session whose login
username isn't already present in
/etc/passwd, viasrc/cowrie/shell/avatar.py's call tosetpwentry(). - Patched file:
src/cowrie/shell/pwd.py(Passwd.setpwentry(): now uses a localrandom.Random(seed_id)instance instead of the globalrandom.seed()/random.randint()functions).
Source: cowrie/cowrie