1.可以采用重定向
2.采用虚拟目录文章来源:https://www.toymoban.com/news/detail-507389.html
前两个文件不用补文章来源地址https://www.toymoban.com/news/detail-507389.html
package com.file;
import com.github.unidbg.AndroidEmulator;
import com.github.unidbg.Emulator;
import com.github.unidbg.file.FileResult;
import com.github.unidbg.file.IOResolver;
import com.github.unidbg.linux.android.AndroidEmulatorBuilder;
import com.github.unidbg.linux.android.AndroidResolver;
import com.github.unidbg.linux.android.dvm.DalvikModule;
import com.github.unidbg.linux.android.dvm.DvmClass;
import com.github.unidbg.linux.android.dvm.VM;
import com.github.unidbg.linux.file.SimpleFileIO;
import com.github.unidbg.memory.Memory;
import java.io.File;
import java.io.IOException;
public class FileDemo{
private final AndroidEmulator emulator;
private final DvmClass MainActivity;
private final VM vm;
public FileDemo() {
emulator = AndroidEmulatorBuilder
.for32Bit()
//虚拟目录
.setRootDir(new File("unidbg-android/src/test/resources/FileDemo/VFS"))
.build();
Memory memory = emulator.getMemory();
memory.setLibraryResolver(new AndroidResolver(23));
vm = emulator.createDalvikVM(new File("unidbg-android/src/test/resources/FileDemo/app-debug.apk"));
// 打开日志
vm.setVerbose(true);
// 在SO加载前添加文件重定位器
emulator.getSyscallHandler().addIOResolver(new MyIOResolver());
DalvikModule dm = vm.loadLibrary("fileinunidbg", true);
MainActivity = vm.resolveClass("com/example/fileinunidbg/MainActivity");
dm.callJNI_OnLoad(emulator);
}
public void destroy() throws IOException {
emulator.close();
}
public void call(String pathName) {
String methodSign = "stringFromJNI(Ljava/lang/String;)Ljava/lang/String;";
MainActivity.callStaticJniMethodObject(emulator, methodSign, pathName);
}
public static void main(String[] args) throws IOException {
FileDemo fileDemo = new FileDemo();
String arg = "/data/user/0/com.example.fileinunidbg/files";
fileDemo.call(arg);
// 运行结束后关闭
fileDemo.destroy();
}
}
package com.file;
import com.github.unidbg.Emulator;
import com.github.unidbg.file.FileResult;
import com.github.unidbg.file.IOResolver;
import com.github.unidbg.linux.file.ByteArrayFileIO;
import com.github.unidbg.linux.file.DirectoryFileIO;
import com.github.unidbg.linux.file.SimpleFileIO;
import java.io.File;
import java.nio.charset.StandardCharsets;
public class MyIOResolver implements IOResolver {
@Override
public FileResult resolve(Emulator emulator, String pathname, int oflags) {
System.out.println("lilac open file:"+pathname);
if(pathname.equals("/data/user/0/com.example.fileinunidbg/files/key.txt")){
return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/FileDemo/key.txt"), pathname));
// return FileResult.success(new ByteArrayFileIO(oflags, pathname, String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8)));
// return null;
// return FileResult.failed(13);
}
// // 返回对应的文件夹(目录文件)
// if(pathname.equals("/data/user/0/com.example.fileinunidbg/files/demo2")){
// return FileResult.success(new DirectoryFileIO(oflags, pathname,new File("unidbg-android/src/test/resources/FileDemo/demo2")))
// return FileResult.success(new DirectoryFileIO(oflags, pathname, new DirectoryFileIO.DirectoryEntry(true, String.valueOf(System.currentTimeMillis())), new DirectoryFileIO.DirectoryEntry(true, "1234")));
// }
// if(pathname.equals("/data/user/0/com.example.fileinunidbg/files/demo2/1.txt")){
// return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/FileDemo/demo2/1.txt"), pathname));
// }
// if(pathname.equals("/data/user/0/com.example.fileinunidbg/files/demo2/2.txt")){
// return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/FileDemo/demo2/2.txt"), pathname));
// }
// if(pathname.equals("/data/user/0/com.example.fileinunidbg/files/demo2/3.txt")){
// return FileResult.success(new SimpleFileIO(oflags, new File("unidbg-android/src/test/resources/FileDemo/demo2/3.txt"), pathname));
// }
return null;
}
}
到了这里,关于unidbg-补环境访问文件及目录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!