Frida Tutorial 2 - HackTricks
Frida Tutorial 2 - HackTricks
Frida Tutorial 2
If you are interested in hacking carer and hack the unhackable - we are hiring! (fluent polish written
and spoken required).
Some parts of the original code doesn't work and have been modified here.
Part 2
Here you can see an example of how to hook 2 functions with the same name but different
parameters.
Also, you are going to learn how to call a function with your own parameters.
And finally, there is an example of how to find an instance of a class and make it call a function.
//s2.js
console.log("Script loaded successfully ");
Java.perform(function x() {
console.log("Inside java perform function");
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 1/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
You can see that to create a String first is has referenced the class java.lang.String and then it has
created a $new object of that class with a String as content. This is the correct way to create a new
object of a class. But, in this case, you could just pass to this.fun() any String like:
this.fun("hey there!")
Python
//loader.py
import frida
import time
device = frida.get_usb_device()
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 2/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
pid = device.spawn(["com.example.a11x256.frida_test"])
device.resume(pid)
time.sleep(1) #Without it Java.perform silently fails
session = device.attach(pid)
script = session.create_script(open("s2.js").read())
script.load()
python loader.py
Part 3
Python
Now you are going to see how to send commands to the hooked app via Python to call function:
//loader.py
import time
import frida
device = frida.get_usb_device()
pid = device.spawn(["com.example.a11x256.frida_test"])
device.resume(pid)
time.sleep(1) # Without it Java.perform silently fails
session = device.attach(pid)
with open("s3.js") as f:
script = session.create_script(f.read())
script.on("message", my_message_handler)
script.load()
command = ""
while 1 == 1:
command = raw_input("Enter command:\n1: Exit\n2: Call secret function\n3: Hook
if command == "1":
break
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 3/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
The command "1" will exit, the command "2" will find and instance of the class and call the private
function secret() and command "3" will hook the function secret() so it return a different string.
The, if you call "2" you will get the real secret, but if you call "3" and then "2" you will get the fake
secret.
JS
});
}
else {//else if the array has some values
console.log("Result of secret func: " + instances_array[0].secret());
}
});
}
function hookSecret() {
Java.perform(function () {
var my_class = Java.use("com.example.a11x256.frida_test.my_activity");
var string_class = Java.use("java.lang.String");
my_class.secret.overload().implementation = function(){
var my_string = string_class.$new("TE ENGANNNNEEE");
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 4/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
} return my_string;
});
}
rpc.exports = {
callsecretfunction: callSecretFun,
hooksecretfunction: hookSecret
};
Part 4
Here you will see how to make Python and JS interact using JSONs objects. JS use the send()
function to send data to the python cliente, and Python uses post() functions to send data to ths
JS script. The JS will block the execution until is receives s response from Python.
Python
//loader.py
import time
import frida
device = frida.get_usb_device()
pid = device.spawn(["com.example.a11x256.frida_test"])
device.resume(pid)
time.sleep(1)
session = device.attach(pid)
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 5/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
withscript
open("s4.js") as f:
= session.create_script(f.read())
script.on("message", my_message_handler) # register the message handler
script.load()
raw_input()
JS
There is a part 5 that I am not going to explain because there isn't anything new. But if you want to
read it is here: https://11x256.github.io/Frida-hooking-android-part-5/
If you are interested in hacking carer and hack the unhackable - we are hiring! (fluent polish written
and spoken required).
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 6/7
11/26/22, 8:18 AM Frida Tutorial 2 - HackTricks
Previous
Frida Tutorial 1
Next
Frida Tutorial 3
https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-2 7/7