ZjDroid原理分析

首先入口在这

com.android.reverse.mod.ReverseXposedModule

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

首先initModuleContext中,



hook了onCreate方法


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

而另一个方法 跟脱壳相关

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


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

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

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

接着看看脱壳过程


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层写。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章