Android Penetration Testing
Android Penetration Testing
Abstract ..............................................................................3
Frida ....................................................................................4
Introduction ......................................................................4
Root Detection Bypass ........................................................4
Hooking different methods in java ......................................9
Explanation: .................................................................... 11
Hooking a defined method .............................................. 12
Hooking exit() method .................................................... 15
Hooking return value ...................................................... 17
SSLPinning Bypass ............................................................. 21
Hooking in Python ............................................................. 25
Let’s Play a Game! ............................................................. 26
www.hackingarticles.in Page | 2
Abstract
In this publication, we’ll explain the basics of Frida, how to create your own Frida script, hook it into
processes and perform various functions. Needless to say, there is no end to what a program can do,
therefore, there is no limit on frida’s applications, hence, this publication is only restricted to basics. If you
want an advanced look into Frida and reverse engineering,
www.hackingarticles.in Page | 3
Frida
Introduction
Frida is a dynamic instrumentation toolkit that is used by researchers to perform android hooking
(intercepting IPC and modifying it to make a function perform the desired function). Frida uses javascript
to perform hooking since Android’s native code and javascript both run on JIT compilation techniques, it
can intercept its inter-process communication, add the code specified in a script and completely change
the function’s implementation. Some of its use cases in real life are:
Spy on Crypto APIs
Modify function’s output
Bypass AES encryption
Bypass SSLPinning and Root detection
Trace private application code
Bypass various software sided locks (like applock)
www.hackingarticles.in Page | 4
Now, we’ll try and remove this restriction using Frida. First, it is recommended you install a Frida server
in the device (Follow steps here). Next, we’ll launch the server onto the device.
After a successful install, we can see all the running process in the device on which frida server is running
by the command:
frida-ps -U
www.hackingarticles.in Page | 5
As you can see that our app is running here. We have to bypass root detection here. We can either try
and reverse engineer the jar files, create our own javascript code and bypass root detection or we can rely
on code already created by a large community of developers on codeshare frida repo.
Weblink to the site is: https://codeshare.frida.re/browse
www.hackingarticles.in Page | 6
Here, we can see an antiroot script by dzonerzy. We’ll run it with the following command:
www.hackingarticles.in Page | 7
Now, press y to trust the project.
Now, all that’s left to do is press “%resume” to resume the execution with our hooked code!
And just like that, we can see that root detection has been successfully bypassed!
www.hackingarticles.in Page | 8
Hooking different methods in java
Now, a class might have multiple methods and each of these methods have a specific purpose. For
example, the onCreate() method defines the implementation of activity as soon as the activity is created
(or launched). So, what is, we can hook this function and change the behavior of the activity when it is
created. For the demonstration purpose, I’ll just print some custom text in my console as soon as the
activity is called but the possibilities are limitless. Typically, you won’t have access to the source code,
hence, what we’ll do is extract the apk first and then decompile it to view source code. To pull the apk
we’ll first know it’s the path and then pull it.
www.hackingarticles.in Page | 9
Now, as explained in part 1 of this series (refer para 3 of the article here), we’ll decompile it using apktool
and then use dex2jar to convert it in jar format, and finally use jd-gui to view the decompiled source code
like below. Here is the MainActivity class decompiled.
console.log("Script loaded!");
Java.perform(function(){
mainapp.onCreate.implementation = function(){
var ret =
this.onCreate.overload("android.os.Bundle").call(this);
};
send("Hooks installed");
});
www.hackingarticles.in Page | 10
Explanation:
www.hackingarticles.in Page | 11
To launch this script we type in the following command:
As you can see now, the hook is successfully installed, activity launches and our custom output is now
displayed and the hook is successfully installed
www.hackingarticles.in Page | 12
console.log("Hooked startChallenge() function");
Java.perform(function(){
var newstart = Java.use("jakhar.aseem.diva.MainActivity");
newstart.startChallenge.overload("android.view.View").implementation
= function(v){
var ret =
this.startChallenge.overload("android.view.View").call(this);
};
});
To call this script, without having to input %resume this time, we can type in the command with –no-
pause filter:
And sure enough, every time a button is pressed, our custom input is displayed.
www.hackingarticles.in Page | 13
www.hackingarticles.in Page | 14
Hooking exit() method
We can also tamper the exit method in android just like we tampered onCreate method. Here, I’m using
a demonstration application that I custom coded (link here). It has a button that is performing an exit
function. You can see a sample screenshot below:
www.hackingarticles.in Page | 15
Now, here we see the exit button. As the name states, on pressing it, application exits.
We create a hook down below that will stop the exit. Here, “java.lang.System” is the package that has exit
function and so we’ll overload it using “sysexit.exit.overload().implementation.” Now, whenever a user
clicks on exit, our send method will be called and exit will be stopped.
sysexit.exit.overload("int").implementation = function(var_0) {
};
});
www.hackingarticles.in Page | 16
Let’s fire this script up and sure enough, we can see that the process is not terminated when the exit
button is clicked. If it had been terminated frida must have thrown a process terminated error and closed
the console.
www.hackingarticles.in Page | 17
console.log(“Hook for implementation of method”);
Java.perform(function myFunc() {
var myClass =
Java.use(“com.example.harshitrajpal.MainActivity”);
myClass.returnValue.implementation = function(){
//we will manipulate the return value here
var ret = 100;
return ret;
}
});
Let’s first run the program without loading our hook. We can see that the program outputs 60 which is
the correct answer.
www.hackingarticles.in Page | 18
www.hackingarticles.in Page | 19
Now, we’ll fire up our script and see what changes happen in the application now.
And sure enough, the output gets tampered and 100 is returned now!
www.hackingarticles.in Page | 20
SSLPinning Bypass
Frida is most commonly used to bypass SSLPinning in android so that researchers and pen testers can
intercept its network calls and conduct a traffic analysis. For the demo of this attack, I downloaded an
application named “Certificate Pinning Demo”. For the demonstration of this attack, you must have your
burp suite configured with your device (follow point 3 of the article here). Now, when I pin the client and
send an HTTPS request, it throws an SSL error.
www.hackingarticles.in Page | 21
Now, on the codeshare repository here, akabe1 has put a great script to perform SSLPinning bypass. We’ll
use this script to perform the attack. Note that applications might have different code of pinning, so these
codes need to be modified as and when required.
www.hackingarticles.in Page | 22
www.hackingarticles.in Page | 23
And finally, when we now send a request to sslabs.com in pinned mode, we are able to get an HTTP 200
response code!
www.hackingarticles.in Page | 24
Hooking in Python
Python coders can customize a whole fridascript to run in python environment using the python’s frida
package and API. This would make performing multiple processes in hooks easier. Here, I’ll create a hook
on startChallenge function as above.
jscode = """
console.log("Hooked startChallenge() function");
Java.perform(function(){
var newstart = Java.use("jakhar.aseem.diva.MainActivity");
newstart.startChallenge.overload("android.view.View").implementation
= function(v){
//enter any implementation of startChallenge you want
//for demo I'm just sending an alert on console
send("MainActivity.startChallenge() is now started");
console.log("You clicked...but in vain!");
var ret =
this.startChallenge.overload("android.view.View").call(this);
};
});
"""
import frida,sys
process = frida.get_usb_device().attach("jakhar.aseem.diva")
script = process.create_script(jscode)
print("*** Running Hook on startChallenge() now!")
script.load()
sys.stdin.read()
Now, every time user clicks on any button to start the challenge, the execution stops and our custom
output is printed instead
.
www.hackingarticles.in Page | 25
We, run this script using the command below:
python3 startChallenge.py
All the examples demonstrated till now are very basic. There are advanced hooking techniques to perform
various different functions whose references I’ll mention at the end. One such challenge I found was on
11×256’s blog. In example #1, we have to intercept the APK, see what’s happening behind the white
screen, change its implementation and modify its behaviour. finally, we’ll check logcat to see if our hook
worked and the sum of our custom defined integers is thrown or not.
Follow the link here and download the sample apk.
www.hackingarticles.in Page | 26
www.hackingarticles.in Page | 27
First, after running the application in the emulator we saw just a plain white screen. That means something
must probably be happening in the background.
As you can see, my_activity is present. This means this is the activity responsible for the full white front
screen.
Now, we’ll use objection to watch what this class is actually doing. (Full objection tutorial here.)
www.hackingarticles.in Page | 28
Here, observe that fun() is being called. This has two int parameters, so, presumably, these two integers
are getting performed a mathematical operation on.
Now, we write a code in javascript:
www.hackingarticles.in Page | 29
console.log("Script loaded successfully ");
Java.perform(function x() { //Silently fails without the sleep from the python
code
console.log("Inside java perform function");
//get a wrapper for our class
var my_class = Java.use("com.example.a11x256.frida_test.my_activity");
//replace the original implmenetation of the function `fun` with our custom
function
my_class.fun.implementation = function (x, y) {
//print the original arguments
console.log("original call: fun(" + x + ", " + y + ")");
//call the original implementation of `fun` with args (2,5)
var ret_value = this.fun(2, 5);
return ret_value;
}
});
This code does nothing but defines fun() function and specifies 2 and 5 as our own integers on which some
mathematical function will be performed. but before that, the script also intercepts and displays the
original call and obviously the original integers!
As we can see, the original call had two integers namely, 50 and 30.
www.hackingarticles.in Page | 30
Let’s quickly check logcat and see what is happening in the background.
As we can see in the screenshot down below, a mathematical Sum of type Double is being repeatedly
called. This is similar to the behaviour of the app we just installed that was calling a method called fun
after every second. Hence, it is safe to conclude that fun() is adding two integers. Original numbers to be
added were 50 and 30, which we not only intercepted and dumped but also changed to 2 and 5 and the
sum of 2 and 5 is now being called as evident in logcat.
www.hackingarticles.in Page | 31
If you want an advanced look into Frida and reverse engineering,In this publication, we’ll explain the
basics of Frida, how to create your own Frida script, hook it into processes and perform various
functions. Needless to say, there is no end to what a program can do, therefore, there is no limit on
frida’s applications, hence, this publication is only restricted to basics.
www.hackingarticles.in Page | 32