Frida Tutorial 2
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
์ด๊ฒ์ ๊ฒ์๋ฌผ์ ์์ฝ์
๋๋ค: https://11x256.github.io/Frida-hooking-android-part-2/ (ํํธ 2, 3 & 4)
APKs ๋ฐ ์์ค ์ฝ๋: 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 () { }
});
});
๋ฌธ์์ด์ ์์ฑํ๊ธฐ ์ํด ๋จผ์ java.lang.String ํด๋์ค๋ฅผ ์ฐธ์กฐํ ๋ค์, ํด๋น ํด๋์ค์ $new ๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด ๋ด์ฉ์ ์ฌ์ฉํ์ฌ ์์ฑํ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ด๊ฒ์ด ํด๋์ค์ ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์
๋๋ค. ๊ทธ๋ฌ๋ ์ด ๊ฒฝ์ฐ this.fun()์ 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()
JS
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)
}
})
5๋ถ๋ ์๋ก์ด ๋ด์ฉ์ด ์๊ธฐ ๋๋ฌธ์ ์ค๋ช ํ์ง ์๊ฒ ์ต๋๋ค. ํ์ง๋ง ์ฝ๊ณ ์ถ๋ค๋ฉด ์ฌ๊ธฐ ์์ต๋๋ค: https://11x256.github.io/Frida-hooking-android-part-5/
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


