Hijacking Tmux Sessions
Terminal multiplexers such as tmux can be used to allow multiple terminal sessions to be accessed within a single console session.
For many reasons, a user may leave a tmux
process running as a privileged user, such as root set up with weak permissions, and can be hijacked. This may be done with the following commands to create a new shared session and modify the ownership.
🛠️ Step-by-Step Breakdown of the Attack
The first two steps are not the attack, but the conditions that posibilitates the attack.
1️⃣ tmux
is Started as Root with a Shared Socket
- This command creates a new tmux session named
debugsess
. - The
-S /shareds
option sets up a custom Unix domain socket at/shareds
instead of the default/tmp/tmux-<uid>/
. - Since the user running the command is root, the session runs with root privileges.
2️⃣ Changing Ownership of the tmux Socket File
- This changes the ownership of the tmux socket (
/shareds
) to:- User:
root
- Group:
devs
- User:
- The group
devs
now has access to attach to the session.
3️⃣ Attacker Checks for Running tmux
Sessions
- This command lists all running processes that contain "tmux."
- The attacker sees:
- Key takeaways:
- The
tmux
session is running as root. - The socket file is located at
/shareds
.
- The
4️⃣ Checking Permissions on the tmux Socket
- Output:
- Breakdown of permissions (
srw-rw----
):s
→ Socket file.rw- rw- ---
→ Owner (root
) and group (devs
) can read/write to it.---
→ Other users cannot access it.
🔴 Dangerous Misconfiguration!
- Any user in the
devs
group can attach to this session. - Since the session is running as root, attaching grants full root access.
5️⃣ Attacker Confirms Group Membership
- Output:
- The attacker (
htb
) is part of thedevs
group, meaning they have access to the/shareds
socket.
6️⃣ Attacker Hijacks the Root tmux
Session
- The attacker attaches to the existing root session.
Once inside, they run:
- Output:
✅ The attacker now has a full root shell! 🎉
Summary
- Flaw: Root starts a shared tmux session with weak permissions (
root:devs
). - Attack: Attacker, part of
devs
, attaches to the session and gets a root shell. - Fix: Restrict access to the tmux socket file (
chmod 600 /shareds
) and avoid assigning sensitive groups.