Sudo Token Reuse – Hijacking Active Sudo Sessions
When a user runs sudo, the session token is cached in /var/run/sudo/ts/ for a configurable window (default 15 minutes). If another user's sudo token is still valid and you can access their TTY device, you can inject a command into their terminal that runs sudo without re-authentication. This requires no password and no exploit: only access to the TTY device file.
Requirements for sudo token reuse:
- Another user has an active sudo session (token not expired)
- Write access to their TTY device or
/proc/[pid]/fd/0 - Kernel below 6.2 or
dev.tty.legacy_tiocsti=1for TIOCSTI injection - Alternatively, access to their screen or tmux socket for session hijacking
Prerequisites Check
Check if any user currently has an active sudo session:
A timestamp_timeout=0 means no caching: this technique does not apply. A value of -1 means the token never expires: even better for this technique.
Find users with active terminal sessions:
Finding the Target TTY
Identify the terminal device of the user with the active sudo token:
Check if you can write to their TTY device:
Technique 1: TTY Injection via /proc/[pid]/fd
If you have write access to the TTY device or the user's /proc/[pid]/fd/0 (stdin), inject a command string:
# Get the target PID (their shell)
TARGET_PID=$(ps aux | grep alice | grep -E "bash|sh" | awk '{print $2}' | head -1)
# Check if their stdin fd is accessible
ls -la /proc/$TARGET_PID/fd/0 2>/dev/null
# Inject sudo command into their terminal
# This types the command as if they typed it
python3 -c "
import fcntl, termios, sys
target_pid = $TARGET_PID
fd = open(f'/proc/{target_pid}/fd/0', 'w')
for char in 'sudo cp /bin/bash /tmp/.rootbash; sudo chmod u+s /tmp/.rootbash\n':
fcntl.ioctl(fd, termios.TIOCSTI, char.encode())
fd.close()
print('Injected')
"
TIOCSTI pushes characters into the terminal input queue as if the user typed them. The command executes in the context of the user's shell which still has a valid sudo token.
After injection:
Technique 2: nsenter + /proc/[pid]/fd/0
If nsenter is available and you can enter the target's namespace:
This drops you into a shell within the same namespace as the target user. Their sudo token applies to your sudo call.
Technique 3: screen Session Hijacking
If the user is running a screen session:
If the multiuser flag is set on the screen session or permissions allow, you attach and share their interactive session. Their sudo token is available. Type your command and sudo works without a password.
Technique 4: tmux Session Hijacking
Checking sudo Token Timeout
Understand how long you have before the token expires:
Default is 15 minutes from the last sudo command. If the user just ran sudo and you act within that window, the token is valid.
Why This Works
sudo caches tokens per-TTY. When TIOCSTI injects characters into a terminal, the kernel processes them as real keystrokes: the sudo process authenticating them sees the same TTY as the token was issued for. The sudo daemon validates the TTY match and grants access without re-prompting.
TIOCSTI is controlled by the dev.tty.legacy_tiocsti sysctl (introduced in Linux 6.2, default 0 on 6.2+). On kernels before 6.2, or when legacy_tiocsti is set to 1, any process that can open the TTY device file can inject without any special capability.
Check kernel version:
References
-
Timestamp timeout configuration and token caching behavior.
-
Manual TIOCSTI ioctl documentation and kernel version restrictions.