Frida Tutorial 2

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Bug bounty tip: sign up for Intigriti, a premium bug bounty platform created by hackers, for hackers! Join us at https://go.intigriti.com/hacktricks today, and start earning bounties up to $100,000!

This is a summary of the post: https://11x256.github.io/Frida-hooking-android-part-2/ (Parts 2, 3 & 4) APKs and Source code: https://github.com/11x256/frida-android-examples

भाग 1 बहुत आसान है।

मूल कोड के कुछ भाग काम नहीं करते हैं और यहाँ संशोधित किए गए हैं।

Part 2

यहाँ आप देख सकते हैं कि कैसे एक ही नाम के 2 फ़ंक्शंस को हुक करें लेकिन अलग-अलग पैरामीटर के साथ। इसके अलावा, आप सीखेंगे कि कैसे अपने खुद के पैरामीटर के साथ एक फ़ंक्शन को कॉल करें। और अंत में, एक उदाहरण है कि कैसे क्लास के एक उदाहरण को खोजें और उसे एक फ़ंक्शन कॉल करने के लिए बनाएं

//s2.js
console.log("Script loaded successfully ");
Java.perform(function x() {
console.log("Inside java perform function");
var my_class = Java.use("com.example.a11x256.frida_test.my_activity");
//Hook "fun" with parameters (int, int)
my_class.fun.overload("int", "int").implementation = function (x, y) { //hooking the old function
console.log("original call: fun(" + x + ", " + y + ")");
var ret_value = this.fun(2, 5);
return ret_value;
};
//Hook "fun" with paramater(String)
var string_class = Java.use("java.lang.String");
my_class.fun.overload("java.lang.String").implementation = function (x) { //hooking the new function
console.log("*")
//Create a new String and call the function with your input.
var my_string = string_class.$new("My TeSt String#####");
console.log("Original arg: " + x);
var ret = this.fun(my_string);
console.log("Return value: " + ret);
console.log("*")
return ret;
};
//Find an instance of the class and call "secret" function.
Java.choose("com.example.a11x256.frida_test.my_activity", {
onMatch: function (instance) {
console.log(tring, and the it has"Found instance: " + instance);
console.log("Result of secret func: " + instance.secret());
},
onComplete: function () { }
});
});

आप देख सकते हैं कि एक String बनाने के लिए पहले java.lang.String क्लास को संदर्भित किया गया है और फिर उस क्लास का एक $new ऑब्जेक्ट एक String के रूप में सामग्री के साथ बनाया गया है। यह किसी क्लास का नया ऑब्जेक्ट बनाने का सही तरीका है। लेकिन, इस मामले में, आप बस this.fun() को कोई भी String पास कर सकते हैं जैसे: this.fun("hey there!")

Python

//loader.py
import frida
import time

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)
script = session.create_script(open("s2.js").read())
script.load()

#prevent the python script from terminating
raw_input()
python loader.py

Part 3

Python

अब आप देखेंगे कि कैसे Python के माध्यम से हुक की गई ऐप को कमांड भेजें ताकि फ़ंक्शन को कॉल किया जा सके:

//loader.py
import time
import frida

def my_message_handler(message, payload):
print message
print payload


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 Secret\nchoice:")
if command == "1":
break
elif command == "2":
script.exports.callsecretfunction()
elif command == "3":
script.exports.hooksecretfunction()

कमांड "1" बाहर निकल जाएगा, कमांड "2" कक्षा का एक उदाहरण खोजेगा और निजी फ़ंक्शन secret() को कॉल करेगा और कमांड "3" फ़ंक्शन secret() को हुक करेगा ताकि यह विभिन्न स्ट्रिंग वापस करे।

तो, यदि आप "2" को कॉल करते हैं, तो आपको वास्तविक रहस्य मिलेगा, लेकिन यदि आप "3" को कॉल करते हैं और फिर "2" को कॉल करते हैं, तो आपको नकली रहस्य मिलेगा।

JS

console.log("Script loaded successfully ");
var instances_array = [];
function callSecretFun() {
Java.perform(function () {
if (instances_array.length == 0) { // if array is empty
Java.choose("com.example.a11x256.frida_test.my_activity", {
onMatch: function (instance) {
console.log("Found instance: " + instance);
instances_array.push(instance)
console.log("Result of secret func: " + instance.secret());
},
onComplete: function () { }

});
}
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");
return my_string;
}
});
}
rpc.exports = {
callsecretfunction: callSecretFun,
hooksecretfunction: hookSecret
};

Part 4

यहाँ आप देखेंगे कि Python और JS कैसे इंटरैक्ट करते हैं JSON ऑब्जेक्ट्स का उपयोग करके। JS send() फ़ंक्शन का उपयोग करके डेटा को Python क्लाइंट को भेजता है, और Python post() फ़ंक्शन का उपयोग करके डेटा को JS स्क्रिप्ट को भेजता है। JS निष्पादन को ब्लॉक करेगा जब तक कि उसे Python से प्रतिक्रिया नहीं मिलती।

Python

//loader.py
import time
import frida

def my_message_handler(message, payload):
print message
print payload
if message["type"] == "send":
print message["payload"]
data = message["payload"].split(":")[1].strip()
print 'message:', message
data = data.decode("base64")
user, pw = data.split(":")
data = ("admin" + ":" + pw).encode("base64")
print "encoded data:", data
script.post({"my_data": data})  # send JSON object
print "Modified data sent"


device = frida.get_usb_device()
pid = device.spawn(["com.example.a11x256.frida_test"])
device.resume(pid)
time.sleep(1)
session = device.attach(pid)
with open("s4.js") as f:
script = session.create_script(f.read())
script.on("message", my_message_handler)  # register the message handler
script.load()
raw_input()

जेएस

console.log("Script loaded successfully ");
Java.perform(function () {
var tv_class = Java.use("android.widget.TextView");
tv_class.setText.overload('java.lang.CharSequence').implementation = function (x) {
var string_to_send = x.toString();
var string_to_recv = "";
send(string_to_send); // send data to python code
recv(function (received_json_object) {
string_to_recv = received_json_object.my_data;
}).wait(); //block execution till the message is received
console.log("Final string_to_recv: "+ string_to_recv)
return this.setText(string_to_recv);
}
});

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/

बग बाउंटी टिप: साइन अप करें Intigriti के लिए, एक प्रीमियम बग बाउंटी प्लेटफॉर्म जो हैकर्स द्वारा, हैकर्स के लिए बनाया गया है! आज ही https://go.intigriti.com/hacktricks पर हमारे साथ जुड़ें, और $100,000 तक की बाउंटी कमाना शुरू करें!

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Last updated