Skip to main content

Ret2libc (ノ◕ヮ◕)ノ*:・゚✧ (day 38)


I have a lot of fun playing with protostar machine to pop a shell and getting a root privileges. But all of the executable in the machine is designed to be vulnerable, it doesn't contain NX or ASLR protection which is not depict a real world scenario. Time to go further and learn new things

In level 6 protostar, which goes like this:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

void getpath()
{
  char buffer[64];
  unsigned int ret;

  printf("input path please: "); fflush(stdout);

  gets(buffer);

  ret = __builtin_return_address(0);

  if((ret & 0xbf000000) == 0xbf000000) {
    printf("bzzzt (%p)\n", ret);
    _exit(1);
  }

  printf("got path %s\n", buffer);
}

int main(int argc, char **argv)
{
  getpath();
}

so the objective is still the same, we are expected to pop a shell from this program but if you take a look at the getpath function there is a if condition statement that goes like this:

 (ret & 0xbf000000) == 0xbf000000

the code is do an AND operation to "ret" variable that contain the program return address (__builtin_return_address(0) uses to provide you the return address of the program)

if its equal to 0xbf000000 it will exit the program, ok let's try to dig deep even more about what is the meaning of this if statement.



ok first of all the program still give us a segmentation fault and it need 80 bytes offset to replace the EIP register and if we try again by adding 4 bytes after the padding it will replace the EIP.


so let's analyze where we can put our shellcode in the stack by dumping the ESP register.


I'm going to use the address of 0xbffff6aa as the new EIP register. But something interesting happen to the program when we try to fill the EIP to that address.


It looks like we hit the if check in getpath function and if you try to replicate this program in python and use the value 0xbffff6aa it is actually going to return True.


So basically the if statement is a defense mechanism for the program if the EIP is return to the address of the stack it will exit the program


 This is really cool finally we are able to find some program that have defense mechanism on it.

so how do we beat this mechanism ?

well according to challenge

"This level can be done in a couple of ways, such as finding the duplicate of the payload ( objdump -s will help with this), or ret2libc , or even return orientated programming."

in this post we are going to use ret2libc

so what is ret2libc ?

it is abbreviation from return to libc. To all of you who like to program in C may be already familiar with libc library, this library provide all the fundamental C function (libc is known standard C library) when you create C program.

so the idea behind ret2libc is that why you want to writ shellcode when there is plenty of useful functions such as "system()" residing in C library already ? 

thus all we have to do know is to find the address of this three component:


  1. system() function address to running shell command
  2. exit() function to exit the function
  3. "/bin/sh" to spawn a shell
the two first information can be obtained by using the "p" function inside the GDB. Don't worry about the difference of the address the location inside and outside of the GDB is still the same.




 cool ! we got the first two address really easy. Let's try to find the address of /bin/sh. 

to find the offset of the "/bin/sh" you can use strings utility in linux:


next try to dump the processor mapping in the program:



we can see that the list of address of the lib, I choose the 0xb7e90000 address since it's the start the address of the lib.

we need this two address to find out the location of "/bin/sh"

by gathering all this three components we can create a script like this:





cool we got a shell tho :D

Enjoy i hope you guys like this 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...

Bypassing stack canaries protection :') (day 51)

In my previous blogs, I show you guys how to bypass some common protection usually used in Linux binary such as NX and ASLR but this time we are going to take it to the next level we are going to talk about protection employ in the modern Linux OS which is "The Canaries" and how to bypass it. note: this post was not originally mined it was inspired by the following resources https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary/ (Credit goes to the author) we are going to start this post about what is stack canaries and types of different implementation of it then move to the implementation about how to bypass this protection. We are going to focus on "leak canaries" technique What is stack canary: In layman terms, canaries are just another protection mechanism to prevent stack overflow implemented by appending 4/8 bytes value (depend on the architecture) into the stack when a function is entered. When the function is at the end of its exec...