import frida
import sys

jscode="""
var moduleFuncName;
var m =  Module.enumerateExportsSync('libart.so');
m.forEach(function(m){
	if(m.name.indexOf("OpenMemory") != -1){
		moduleFuncName = m.name;
		console.log("module function name: "+ m.name);
};

});

var memmethod="_ZN3art7DexFile10OpenMemoryEPKhjRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEjPNS_6MemMapEPKNS_10OatDexFileEPS9_";
var OpenMemory = Module.findExportByName("libart.so",memmethod);
console.log("the opemmemory:"+OpenMemory);
	if(OpenMemory != undefined){
		Interceptor.attach(OpenMemory,{
		onEnter: function(args){
			console.log("base: "+ args[1]);
			console.log("size: "+ args[2].toInt32());
			console.log(hexdump(
				args[1],{
					offset: 0,
					length: 64,
					header: true,
					ansi: true


				}
				));
			var begin = args[1];
			console.log("magic : " + Memory.readUtf8String(begin))
			var address = parseInt(begin,16) + 0x20;
			var dex_size = Memory.readInt(ptr(address));
			console.log("dex_size :" + dex_size);
			var file = new File("/sdcard/" + dex_size + ".dex", "wb");
			file.write(Memory.readByteArray(begin, dex_size));
			file.flush();
			file.close();


		},
		onLeave: function(retval){
			console.log("Finished!!!");
			console.log("=========================================");
		}
	});

}else{
	console.log("Null Point!!!");
}

"""
package_name="com.shixiang"

def on_message(message,data):
    print(message)

session=frida.get_device_manager().enumerate_devices()[-1].attach(package_name)
script=session.create_script(jscode)
script.on("message",on_message)
script.load()
sys.stdin.read()