我想在运行时在 android 应用程序中注入代码。我曾尝试使用 dx 工具在 sdcard 中生成 dexfile,但是当我想实例化时,它失败了。是否有任何工具可以注入代码生成新的 dalvik 字节码?我正在研究一些库、aspect 或 guice for android。使用脚本语言会更好吗?
谢谢人们:)
我想在运行时在 android 应用程序中注入代码。我曾尝试使用 dx 工具在 sdcard 中生成 dexfile,但是当我想实例化时,它失败了。是否有任何工具可以注入代码生成新的 dalvik 字节码?我正在研究一些库、aspect 或 guice for android。使用脚本语言会更好吗?
谢谢人们:)
Dexmaker是新的,专为此而设计。以下是项目网站上的部分示例:
DexMaker dexMaker = new DexMaker();
// Generate a HelloWorld class.
TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
generateHelloMethod(dexMaker, helloWorld);
// Create the dex file and load it.
File outputDir = new File(".");
ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
outputDir, outputDir);
Class<?> helloWorldClass = loader.loadClass("HelloWorld");
// Execute our newly-generated code in-process.
helloWorldClass.getMethod("hello").invoke(null);
您可以使用 DexClassLoader 类指定您自己的 DEX 文件。这被一些需要“插件”行为的应用程序使用。
但是,设备上没有任何东西会生成 DEX 文件。没有动态生成代码并利用它的机制。
使用 ASM 或 BCEL 在设备上运行时生成 Dalvik 字节码
此示例使用 ASM 和 BCEL 在设备上生成两个类。这些类被创建到 SD 卡内存中,然后动态加载到 Android 操作系统中。
以下类是示例的模板:
public class HelloWorld {
public static void hello(){
int a=0xabcd;
int b=0xaaaa;
int c=a-b;
String s=Integer.toHexString(c);
System.out.println(s);
}
}
首先,我使用 BCEL 或 ASM 在 SD 卡中创建了一个新的 ad-hoc 类。其次,我使用 SD 卡中的 Dxclient 实用程序将 Java 类转换为 Dex 类。最后我创建了一个 jar 文件,然后我将这个包从 SD 卡加载到设备中
DXClient 参考
https://github.com/headius/dexclient/blob/master/src/DexClient.java
您可以查看此页面,但您必须使用一些工具,例如 APKTool、SignApk。
http://blackhatcrackers.blogspot.de/2013/05/injecting-custom-code-into-android-apks.html
不,这是不可能的。如果可能,Android 应用程序权限将不起作用。