美文网首页逆向专栏
ZjDroid原理分析

ZjDroid原理分析

作者: 超威蓝猫l | 来源:发表于2020-07-09 12:18 被阅读0次

    首先入口在这

    com.android.reverse.mod.ReverseXposedModule

    image.png

    因为github没有跳转看起来不方便,所以只是粗略分析一下

    首先initModuleContext中,


    image.png

    hook了onCreate方法

    image.png

    往下翻可以看到其实是注册了一个广播,所以可以通过发送广播的形式,进行命令传递。

    而另一个方法 跟脱壳相关

    public void start() throws Throwable {
    
            pathClassLoader = (PathClassLoader) ModuleContext.getInstance().getBaseClassLoader();
    
            Method openDexFileNativeMethod = RefInvoke.findMethodExact("dalvik.system.DexFile", ClassLoader.getSystemClassLoader(), "openDexFileNative",
                    String.class, String.class, int.class);
            hookhelper.hookMethod(openDexFileNativeMethod, new MethodHookCallBack() {
    
                @Override
                public void beforeHookedMethod(HookParam param) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterHookedMethod(HookParam param) {
                    // TODO Auto-generated method stub
                    String dexPath = (String) param.args[0];
                    int mCookie = (Integer) param.getResult();
                    if (mCookie != 0) {
                        dynLoadedDexInfo.put(dexPath, new DexFileInfo(dexPath,mCookie));
                    }
                }
            });
            
            Method defineClassNativeMethod = RefInvoke.findMethodExact("dalvik.system.DexFile", ClassLoader.getSystemClassLoader(), "defineClassNative",
                    String.class, ClassLoader.class,int.class);
            hookhelper.hookMethod(defineClassNativeMethod, new MethodHookCallBack() {
    
                @Override
                public void beforeHookedMethod(HookParam param) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterHookedMethod(HookParam param) {
                    // TODO Auto-generated method stub
                   if(!param.hasThrowable()){
                       int mCookie = (Integer) param.args[2];
                       setDefineClassLoader(mCookie,(ClassLoader) param.args[1]);
                   }
                }
            });
            
            Method findLibraryMethod = RefInvoke.findMethodExact("dalvik.system.BaseDexClassLoader", ClassLoader.getSystemClassLoader(), "findLibrary",
                    String.class);
            hookhelper.hookMethod(findLibraryMethod, new MethodHookCallBack() {
    
                @Override
                public void beforeHookedMethod(HookParam param) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterHookedMethod(HookParam param) {
                    Logger.log((String) param.args[0]);
                    if (DVMLIB_LIB.equals(param.args[0]) && param.getResult() == null) {
                        param.setResult("/data/data/com.android.reverse/lib/libdvmnative.so");
                    }
                }
            });
        }
    

    可以看到都是hook了一些类,以及最后加载自己的so

    image.png

    第一个hook可以发现,获取了dexpath以及返回值mcookie的指针

    image.png

    第二个方法可以看到hook 获取了mcook的值以及对应的classloader

    第三个方法可以看到如果加载的so 名字是自己的以及找不到的话,就把路径换成自己的so路径,其实就是一个路径查找过程,如果系统找不到自己的so,就直接告诉系统我的so在哪里。

    接着看看脱壳过程


    image.png
    public void dumpDexFile(String filename, String dexPath) {
            File file = new File(filename);
            try {
                if (!file.exists())
                    file.createNewFile();
                int mCookie = this.getCookie(dexPath);
                if (mCookie != 0) {
                    FileOutputStream out = new FileOutputStream(file);
                    ByteBuffer data = NativeFunction.dumpDexFileByCookie(mCookie, ModuleContext.getInstance().getApiLevel());
                    data.order(ByteOrder.LITTLE_ENDIAN);
                    byte[] buffer = new byte[8192];
                    data.clear();
                    while (data.hasRemaining()) {
                        int count = Math.min(buffer.length, data.remaining());
                        data.get(buffer, 0, count);
                        try {
                            out.write(buffer, 0, count);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                } else {
                    Logger.log("the cookie is not right");
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    

    可以看到拿到mcookie的值,然后用native层去读数据,写到文件中。

    https://bbs.pediy.com/thread-252284.htm

    而参考上面链接的说法的话,可以看出,里面的指针指向的是dexfile,因为native的操作无非就是拿到dexfile的base 和size 然后读出来交给java层写。

    相关文章

      网友评论

        本文标题:ZjDroid原理分析

        本文链接:https://www.haomeiwen.com/subject/cimjcktx.html