这篇文章用来记录Frida在Hook各种方法时的脚本编写。
 
测试源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package  com.example.lesson4one;import  androidx.appcompat.app.AppCompatActivity;import  android.os.Bundle;import  android.util.Log;public  class  MainActivity  extends  AppCompatActivity  {    @Override      protected  void  onCreate (Bundle savedInstanceState)  {         super .onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         while  (true ){             try  {                 Thread.sleep(1000 );             } catch  (InterruptedException e) {                 throw  new  RuntimeException (e);             }             int  m;             m = fun(50 , 80 );         }     }     int  fun (int  x, int  y) {         Log.d("e1ectr0nlc" , String.valueOf(x + y));         return  x + y;     } } 
 
控制台运行
1 frida -U -l xxx.js com.example.lesson4one 
 
Hook调用的方法  使用Frida获取到调用的fun()函数。
1 2 3 4 5 6 7 8 9 10 function  main ( ){    Java .perform (function ( ){         Java .use ("com.example.lesson4one.MainActivity" ).fun .implementation  = function (arg1,arg2 ){             var  result = this .fun (arg1,arg2);             console .log ("arg1, arg2, result" , arg1, arg2, result);             return  result;         }     }) } setImmediate (main)
 
Hook重载的方法 更改源码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package  com.example.lesson4one;import  androidx.appcompat.app.AppCompatActivity;import  android.os.Bundle;import  android.util.Log;public  class  MainActivity  extends  AppCompatActivity  {	private  static  String  total  =  "@@@@####@@@@" ;     @Override      protected  void  onCreate (Bundle savedInstanceState)  {         super .onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         while  (true ){             try  {                 Thread.sleep(1000 );             } catch  (InterruptedException e) {                 throw  new  RuntimeException (e);             }             int  m  =  fun(50 ,80 );             Log.d("e1ectr0nlc m = " , String.valueOf(m));             Log.d("e1ectr0nlc tolowercase" , fun("LOWERCASEME!" )); 	         }     }           	String fun (String x ) {     total +=x ;     return  x.toLowerCase();     }               int  fun (int  x, int  y) {         Log.d("e1ectr0nlc" , String.valueOf(x + y));         return  x + y;     } } 
 
JS脚本如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function  main ( ){    Java .perform (function ( ){      Java .use ("com.example.lesson4one.MainActivity" ).fun .overload ('int' , 'int' ).implementation  = function (arg1, arg2 ){             var  result = this .fun (100 , 200 );     console .log (Java .use ("android.util.Log" ).getStackTraceString (Java .use ("java.lang.Throwable" ).$new()));	            console .log ("arg1, arg2, result" , arg1, arg2, result);             return  result;         }         					 	  Java .use ("com.example.lesson4one.MainActivity" ).fun .overload ('java.lang.String' ).implementation  = function  (arg1  ){             var  result = this .fun (Java .use ('java.lang.String' ).$new("NIHAOJAVA" ));             console .log ("arg1,result" ,arg1,result)             return  result;                      }     }) } setImmediate (main)
 
打印结果如下
同时App内的日志信息也会改变
Hook未调用的方法 在源码中添加一个没有调用过的函数
1 2 3 4 String secret () {         return  total;     } 
 
脚本代码
1 2 3 4 5 6 7 8 9 10 11 function  main ( ){    Java .perform (function ( ){         Java .choose ("com.example.lesson4one.MainActivity" ,{             onMatch :function (instance ){                 console .log ("found instance :" ,instance)                 console .log ("found instance :" ,instance.secret ())             },onComplete :function ( ){}         })     }) } setImmediate (main)
 
Hook未调用的静态方法 1 2 3 4 public  static  String secret2 () {        return  total;     } 
 
Hook脚本
1 2 3 4 5 6 7 function  main ( ){    Java .perform (function ( ){         var  result = Java .use ("com.example.lesson4one.MainActivity" ).secret2 ();        console .log (result);     }) } setImmediate (main)
 
总的测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package  com.example.lesson4one;import  androidx.appcompat.app.AppCompatActivity;import  android.os.Bundle;import  android.util.Log;public  class  MainActivity  extends  AppCompatActivity  {    private  static  String  total  =  "@@@@####@@@@" ;          @Override      protected  void  onCreate (Bundle savedInstanceState)  {         super .onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         while (true ){             try  {                 Thread.sleep(1000 );             } catch  (InterruptedException e) {                 e.printStackTrace();             }             int  m  =  fun(50 ,80 );             Log.d("e1ectr0nlc m = " , String.valueOf(m));             Log.d("e1ectr0nlc tolowercase" , fun("LOWERCASEME!" ));         }     }     String fun (String x ) {         total +=x ;         return  x.toLowerCase();     }     int  fun (int  x ,int  y) {         Log.d("e1ectr0nlc" , String.valueOf((x+y)));         return  x+y;     }     String secret () {         return  total;     }     public  static  String secret2 () {         return  total;     } }