여기서 동일한 이름을 가진 2개의 함수를 후킹하는 예제를 볼 수 있습니다.
또한, 자체 매개변수로 함수를 호출하는 방법을 배우게 될 것입니다.
마지막으로, 클래스의 인스턴스를 찾아 함수를 호출하도록 만드는 예제가 있습니다.
//s2.jsconsole.log("Script loaded successfully ");Java.perform(functionx() {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 functionconsole.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 functionconsole.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 클래스를 참조하고, 그 클래스의 String 내용을 가진 $new 객체를 생성했다는 것을 볼 수 있습니다. 이것은 클래스의 새 객체를 생성하는 올바른 방법입니다. 그러나 이 경우에는 this.fun("hey there!")와 같이 어떤 String이든 this.fun()에 전달할 수 있습니다.
파이썬
//loader.pyimport fridaimport timedevice = frida.get_usb_device()pid = device.spawn(["com.example.a11x256.frida_test"])device.resume(pid)time.sleep(1)#Without it Java.perform silently failssession = device.attach(pid)script = session.create_script(open("s2.js").read())script.load()#prevent the python script from terminatingraw_input()
다음 명령어인 "1"은 종료하고, "2"는 클래스의 인스턴스를 찾아 비공개 함수인 _secret()_를 호출하며, "3"은 함수 _secret()_를 후킹하여 다른 문자열을 반환하도록 합니다.
그러므로 "2"를 호출하면 진짜 비밀을 얻지만, "3"을 호출한 후 "2"를 호출하면 가짜 비밀을 얻게 됩니다.
JS
console.log("Script loaded successfully ");var instances_array = [];functioncallSecretFun() {Java.perform(function () {if (instances_array.length==0) { // if array is emptyJava.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 valuesconsole.log("Result of secret func: "+ instances_array[0].secret());}});}functionhookSecret() {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};
파트 4
여기서는 JSON 객체를 사용하여 Python과 JS가 상호 작용하는 방법을 살펴볼 것입니다. JS는 데이터를 Python 클라이언트로 보내기 위해 send() 함수를 사용하고, Python은 데이터를 JS 스크립트로 보내기 위해 post() 함수를 사용합니다. JS는 Python으로부터 응답을 받을 때까지 실행이 차단됩니다.