In this post, I'm going to show you how to solve the OWASP crackme challenge by patching the binary using radare2 and debugging it with gdbserver to get the secret string.
To all of you who don't know about patching a binary, I have a post that talks about specifically about the overview of this technique. Link: https://court-of-testing-analysing.blogspot.com/2019/10/patching-binary-with-radare2-day-22.html
Background:
what is gdb? according to access.redhat.com "The GNU Debugger, commonly abbreviated as GDB, is a command-line tool that can be used to debug programs written in various programming languages. It allows you to inspect memory within the code being debugged, control the execution state of the code, detect the execution of particular sections of code, and much more."why should we need to use GDB inside android? although Java or kotlin is a pretty popular programming language used in the android application. But both of the languages are not one go-to tool to create android application because sometimes your application needs to interact with a low-level function of the android such as openGL, SSL and etc. This functionality can only be access by C and C++ programming language and we can embed it inside the application.
This so-called C or C++ library is stored inside the "lib" folder
as you can see from the above picture there are several multiple folders and it is named based on well-known hardware architecture. Why the android application has multiple folder? simple, to provide higher usability among device.
So there are a lot of types in mobile hardware architecture out there, it can be ARM or x86 intel. Because an android developer cannot control the distribution type of the architecture they just put all the libraries to the "lib" folder and when the application is installed inside the device it will choose the most compatible library for the application.
in this post, I used an android emulator with x86 arch specification.
Get your hands dirty:
Patching the binary
- First, lets analyze the binary so we know what we are dealing with.
- ~# r2 -Aw libfoo.so (the -A means let the radare2 flag all important section and w is to open the binary file into write mode)
- (inside the r2 shell) ~# afl (this will list all the function of the binary)
- we notice that there is a "ptrace" function,right ? so remember if you found this function it may that the binary equip itself with anti-debugging feature so you need to patch this function.
- we need to trace this function call by doing xref (cross referencing)
- (inside the r2 shell) ~#axt sym.imp.ptrace
- As you can see the "ptrace" is called in function "sub.fork_720"
- Go to the "sub.fork_720" and try to dump the assembly code. we got our two ptrace
- (inside the r2 shell) ~#s sub.fork_720 (move to the function)
- To patch the function we need to change into visual mode inside radare2
- (inside the r2 shell) ~#Vp (switch to visual mode, your shell will turn like pic below)
- Navigate into the ptrace function by using the arrow keys
- Type "A" to enter append mode for edit the assembly code
- Now try to insert "call 0" code and to the two ptrace (dont forget to save it)
- You may be expect to enter a "nop" code replace the assembly but i dont recommend to use this because it will make the application crash
- Exit the shell by press "q" and type "quit"
Reassemble the application
We got ourselves a new patched library but it is not done yet, since the application code with root and debugging detection we need to circumvent this function.- Use apktool to get the smali code and edit the main activity smali file to be like the below picture.
- Repackage the application and sign it (i use appium-sign, check this link). To make everything faster i create a bash script to automate the process.
- If everything goes according to the plan, there is only one thing to do. You have to put gdbserver into the device (there is an awesome github page that already provide all gdbserver according to its own architecture type github)
- After you download the binary, we need to put it inside the device but you have to put it inside ("/system/bin") folder.
- to do this, connect to the device using adb and type "mount"
- As the picture show that the /system folder is read-only we need to change folder permission using "mount -o rw,remount /dev/block/sda6 /system"

- Next, we can put our gdbserver to the /system/bin folder
Debugging the application:
- Run the gdbserver and you have to supply the uid of the crackme application so it can attach process along with the app. Now the gdbserver is waiting for the connection. Note: in this situation the application cannot be operated because it halt all of the operation to listen for the server i suggest you guys don't do anything to the app or it will be crashed.
- Connect to the gdbserver.
- The last thing we need to know is to where to put the breakpoint so we can get the secret string. Simple ! as you can see when we try to list all the function of the binary there is "strncmp" right ? as you aware the function take two parameter to be compare "eax" and "esi" register. All we have to do is to examine this two reg because it contain either our input or the secret string.
- How to put the breakpoint ? first we need to know the beginning of the address for the library and after we found it we just have to add the address to the "strncmp" address.
- We go the beginning address which is 0xd616f000, add it with 0x0000ffb, we got 0x616fffb.
- (inside gdb shell) ~# b *0xd616fffb
- We need to trigger the application to reach the breakpoint and to do this you need to input a string that woth 23 char because before the application do string comparison, it compare the length first. (0x17 => 23)
- After you input enough char, the application will hit the breakpoint and we can investigate the two register.
Yeayy we got the secret string :)
THANK YOU have nice day
KUDOSS to the this amazing web post to give a very thorough explanation regarding android library debugging:
http://sh3llc0d3r.com/owasp-uncrackable-android-level2/
http://resources.infosecinstitute.com/android-hacking-and-security-part-20-debugging-apps-on-android-emulator-using-gdb/#gref
Comments
Post a Comment