libxposed / api

Apache License 2.0
127 stars 47 forks source link

About provide apk/dex path from PackageLoadedParam #5

Closed wrlu closed 1 year ago

wrlu commented 1 year ago

Sometimes developers want to know the apk/dex path when loading, like /data/app/xxxxxx/base.apk or /data/user/0/xxxxx/files/dymload.dex, or it is a memory dex. So could you please add some new methods to PackageLoadedParam class in LSPosed new API, such as getDexPath() and isMemoryDex?

P.S. We can get this information from DexFile_openDexFileNative and DexFile_openInMemoryDexFilesNative function in dalvik_system_DexFile.cc.

yujincheng08 commented 1 year ago

PackageLoadedParam.getAppInfo() should have provided enough information?

wrlu commented 1 year ago

OK you are right, but ApplicationInfo does not directly provide the information about is memory dex or not. So please add isMemoryDex() API if possible?

And another thing is the API named isFirstPackage() may be confused, because many applications use split apk or user will hook the system_server itself.

wrlu commented 1 year ago

This is an example to check if a dex is a base apk or a hotfix binary in my hook framework project.

#define FS_DATA_PATH "/data"
#define FS_STORAGE_PATH "/storage"
#define FS_DATA_APP_PATH "/data/app"

bool isHotfixDexPath(const char *sourceName) {
    if (sourceName == nullptr) {
        return false;
    }
    // Dex file in read-only partition should not be a hot patch.
    if (!( strstart(sourceName, FS_DATA_PATH) 
        || strstart(sourceName, FS_STORAGE_PATH) )) {
        return false;
    }
    // Dex file in system default apk path should not be a hot patch.
    // TODO: On some Android system apk can be installed to sdcard.
    if (strstart(sourceName, FS_DATA_APP_PATH)) {
        return false;
    }
    // Otherwise should be a hot patch (like /data/user/0, /data/data, /sdcard or /storage/emulated/0).
    return true;
}
yujincheng08 commented 1 year ago

What is isMemoryDex used for? Could you provide a valid scenario?

When hooking system_server, you won't get isFirstPackage because you will get a SystemServerLoadedParam, which does not have isFirstPackage.

wrlu commented 1 year ago

I think isMemoryDex() or isHotfixDexPath() can be used for app protection hooking.

yujincheng08 commented 1 year ago

Aha, I got you. But unfortunately, those hotfixes won't trigger OnPackageLoaded.

wrlu commented 1 year ago

抱歉没有仔细阅读过Xposed实现的原理,我目前做了一个项目就是通过插桩dalvik_system_DexFile.cc这里去抓取一些动态加载的dex。所以说OnPackageLoaded这个是只针对于主包才会调用的嘛?对于动态加载的dex或者内存dex没有回调?

yujincheng08 commented 1 year ago

Basically, OnPackageLoaded is for a package. Hotfixes are not even a package. Technically, OnPackageLoaded only triggers whenever a LoadedApk creates its ClassLoader.

wrlu commented 1 year ago

Ok I got it.