Skip to main content

Patching binary with radare2 ( ͠° ͟ʖ ͡°) (day 22)


This blog is meant to be the prerequisite of "OWASP crackme write up version 2 level 2" post since it will talk about how to solve the challenge using debugging and patching techniques. I figured out that it is better to split up these two techniques into two blog posts so it will not become boring and long.

Background:

Over the year, radare2 has become a prominent tool for reverse engineering in Linux and utilizes a sweet command-line interface as opposed to a graphical one. Personally, I like to do RE in radare2 because of it already pre-installed in my kali Linux and it can be piped into python script making the whole process go a lot easier.


I found a really good Github page and it's also used as the reference for this post that teaches you the essentials of the tools to get the grasp the basic operation.

Getting your hands dirty:

  • First, you need to "make" the binary in order to get the executable
  • After we got the executable binary now its time to do some reverse engineering (but it is best to create a copy of the original file). Load the executable to radare2 shell using "r2 -Aw patchme". The following command means that radare2 will automatically analyze to any relevant data that make our analysis so much easier and also it will open the file in write mode so web able to patch the binary

  •  Once we got the shell, type "afl" to show all the functions of the program. It is shown that there is the main function, let's try to go to that function type "s main" to move to the "main" section.

  • The "pdf" command will list the assembly command of the function. The result is pretty straight forward that the function only prints the string "hello there can you patch me up to call my function" and exit the program. But from the printed value we can deduce that there is another function that we must call in order to finish this challenge. But what is it?

  • Try to dump the string inside the binary may be we can get some more hint about this hidden function. We can see that there are two strings, one is from the main function and one that we never saw before (i guess that this is the secret function). type "axt 0x00004052" to do some xref so we know the source of the string (tips: s `axt 0x00004052~[1] help you to move to the xref address), unfortunately, it seems that we cannot dump the function assembly code.
  •  type "Vp" to enter visual mode so we can see the assembly code that contains inside the memory address.  If you try to navigate up with arrow key we can see some prologue and epilogue code and it seems we ended up inside an unknown function that radare2 wasn't able to map it after all it is not used by other functions. 
  • We need to define the function so that we can put it inside the main function. type "d" and "f" it will automatically assign radare2 to define the function and named it according to its format.

  •  To make this easier let's try to rename the function into something more memorable, type "d" and "r" put the new name, I'm gonna call this function "fcn.callme"

  • Next, it is time to put the function callme to the main function, type "A" and it will prompt you "[VA:0]>" navigate your prompt to that many nop in the main function that we saw earlier. Type "call fcn.callme" and enter to save the newly edited program. type "q" to exit the shell and "quit" again from radare2.

  • WALLAA! we succesfully patch the binary.
Thank you have a nice day :)

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...