YAHFA使用总结

开源项目YAHFA-master在git上面
https://github.com/PAGalaxyLab/YAHFA

-----------------  开源项目介绍  -------------------
1.demoApp      

    1.1:这个为被hook的demo项目,其中引入了yahfa的库library
    1.2:在这个类里面唯一在MainApp(Application)中对hook进行初始化
        

try {
        /*
        这里的dexClassLoader 是设置了配置hook相关的信息的一个配置集合的apk,路径是在sdcard/下,当然可以通过以下的路径进行修改
         */
            ClassLoader classLoader = getClassLoader();

            DexClassLoader dexClassLoader = new DexClassLoader(
                    new File(Environment.getExternalStorageDirectory(), "demoPlugin-debug.apk").getAbsolutePath(),
                    getCodeCacheDir().getAbsolutePath(), null, classLoader);
            HookMain.doHookDefault(dexClassLoader, classLoader);
        } catch (Exception e) {
            e.printStackTrace();
        }


        
    1.3:其他的一些类为被hook的类,包括一个so
    
2.demoPlugin

    2.1:HookInfo 关键类,这里配置了一个hookItemNames 是进行hook的类的集合
    

public class HookInfo {
        public static final String TAG = "HookInfo";
        public static String[] hookItemNames = {
                Hook_Log_e.class.getName(),
                Hook_String_startsWith.class.getName(),
                Hook_ClassWithVirtualMethod_tac.class.getName(),
                Hook_ClassWithStaticMethod_tac.class.getName(),
                Hook_ClassWithJNIMethod_fromJNI.class.getName(),
                Hook_ClassWithCtor.class.getName(),
        };
    }


    2.2:Hook_ClassWithCtor,Hook_ClassWithJNIMethod_fromJNI,Hook_ClassWithStaticMethod_tac,Hook_ClassWithVirtualMethod_tac 等是一些hook的实例,其中包括静态方法,native方法等的hook
    
    2.3 Hook_ClassWithStaticMethod_tac     

public class Hook_ClassWithStaticMethod_tac {
            public static String className = "lab.galaxy.yahfa.demoApp.ClassWithStaticMethod";
            public static String methodName = "tac";
            public static String methodSig =
                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";

            public static String hook(String a, String b, String c, String d) {
                Log.w(TAG, "in ClassWithStaticMethod.tac(): " + a + ", " + b + ", " + c + ", " + d);
                return "test" + a;
            }
            /*
            public static String backup(String a, String b, String c, String d) {
                Log.w(TAG, "ClassWithStaticMethod.tac() should not be here");
                return "";
            }
            */
        }


        hook方法的格式比较简单,类名,方法名,参数的格式(这里的参数格式为smali的语法格式),然后是根据参数和返回值,写hook和backup方法即可
    
3.library : 是yahfa的实现库

4.demo的使用

    4.1编译demoApp进行安装
    4.2编译demoPlugin 将生成的apk放置到sdcard的路径下
    4.3运行demoApp就会从日志中发现以上的方法都被hook了
    
--------------项目中的使用------------------------------------------------------

1.将demoApp 编译后生成的so拷入到项目中

2.拷贝library的lab.galaxy.yahfa.HookMain到项目的相同路径下面

    2.1由于导入的时候 HookMain 中的shouldVisiblyInit 会一直报错,将以下的方法直接return 0,由于我使用的是android7 ,所以不受影响
        

public static int initClass() {
            // do nothing before Android R or on x86 devices
            if(shouldVisiblyInit()) {
                long thread = getThread();
                return visiblyInit(thread);
            }
            else {
                return 0;
            }
        }

3.新建一个项目demoPlugin,按以上的demoPlugin的规范编写自己要hook的方法,生成apk,放到sdcard路径下

4.在项目的初始化时候,要将以下的apk名称改为你后来生成的apk的名称    

try {
        /*
        这里的dexClassLoader 是设置了配置hook相关的信息的一个配置集合的apk,路径是在sdcard/下,当然可以通过以下的路径进行修改
         */
            ClassLoader classLoader = getClassLoader();

            DexClassLoader dexClassLoader = new DexClassLoader(
                    new File(Environment.getExternalStorageDirectory(), "demoPlugin-debug.apk").getAbsolutePath(),
                    getCodeCacheDir().getAbsolutePath(), null, classLoader);
            HookMain.doHookDefault(dexClassLoader, classLoader);
        } catch (Exception e) {
            e.printStackTrace();
        }


        
5.由于YAHFA是hook自己进程的方法,所以需要将以上的so和实现注入到要hook的应用中,比如通过application,加载dex方式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值