Skip to content

Instantly share code, notes, and snippets.

@eybisi
Created February 14, 2022 23:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eybisi/d1a845bab0889ec1c830eb25cd0bebb4 to your computer and use it in GitHub Desktop.
Save eybisi/d1a845bab0889ec1c830eb25cd0bebb4 to your computer and use it in GitHub Desktop.
hook multi dex variant of android packers
// https://cryptax.medium.com/multidex-trick-to-unpack-android-bianlian-ed52eb791e56
// https://android.googlesource.com/platform/frameworks/multidex/+/refs/heads/master/library/src/androidx/multidex/MultiDex.java#716
// https://android.googlesource.com/platform/libcore/+/master/dalvik/src/main/java/dalvik/system/DexPathList.java#397
// copy file works reeeealy slow, sorry for that. Instead you can hook file.delete since multidex will remove temp dexes.
// https://android.googlesource.com/platform/frameworks/multidex/+/refs/heads/master/library/src/androidx/multidex/MultiDexExtractor.java#418
Java.deoptimizeBootImage()
Java.deoptimizeEverything()
function copyFile(destPath,sourcePath){
const File = Java.use('java.io.File');
const FileInputStream = Java.use('java.io.FileInputStream');
const FileOutputStream = Java.use('java.io.FileOutputStream');
const BufferedInputStream = Java.use('java.io.BufferedInputStream');
const BufferedOutputStream = Java.use('java.io.BufferedOutputStream');
var sourceFile = File.$new.overload('java.lang.String').call(File, sourcePath);
if (sourceFile.exists() && sourceFile.canRead()) {
var destinationFile = File.$new.overload('java.lang.String').call(File, destPath);
destinationFile.createNewFile();
var fileInputStream = FileInputStream.$new.overload('java.io.File').call(FileInputStream, sourceFile);
var fileOutputStream = FileOutputStream.$new.overload('java.io.File').call(FileOutputStream, destinationFile);
var bufferedInputStream = BufferedInputStream.$new.overload('java.io.InputStream').call(BufferedInputStream, fileInputStream);
var bufferedOutputStream = BufferedOutputStream.$new.overload('java.io.OutputStream').call(BufferedOutputStream, fileOutputStream);
var data = 0;
while ((data = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(data);
// console.log('buffuredInputStream : ' + data);
}
bufferedInputStream.close();
fileInputStream.close();
bufferedOutputStream.close();
fileOutputStream.close();
}
else {
console.log('Error : File cannot read.')
}
}
Java.perform(function(){
var dexpathlist = Java.use("dalvik.system.DexPathList")
dexpathlist.loadDexFile.implementation = function(file,dir,loader,elements){
// https://android.googlesource.com/platform/libcore/+/master/dalvik/src/main/java/dalvik/system/DexPathList.java#397
console.log("loaddexfile ")
console.log(file.getAbsolutePath())
copyFile("/data/local/tmp/"+file.getName(),file.getAbsolutePath())
return this.loadDexFile(file,dir,loader,elements)
}
dexpathlist.makeDexElements.overload('java.util.List', 'java.io.File', 'java.util.List', 'java.lang.ClassLoader').implementation = function(a,b,c,d){
// https://android.googlesource.com/platform/frameworks/multidex/+/refs/heads/master/library/src/androidx/multidex/MultiDex.java#716
// console.log("called makeDexElements")
// for(var i=0;i<a.size();i++){
// // copyFile("/data/local/tmp/test",a.get(i).toString())
// console.log("adding dex element " + a.get(i))
// }
return this.makeDexElements(a,b,c,d)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment