Closed 18712886438 closed 5 years ago
同时想请教一下这块c的代码, hook compress的时候,想改变第三四个参数的值,然后调用原方法需要怎么做呢?
import cn.banny.auxiliary.Inspector;
import cn.banny.unidbg.Emulator;
import cn.banny.unidbg.LibraryResolver;
import cn.banny.unidbg.Module;
import cn.banny.unidbg.Symbol;
import cn.banny.unidbg.arm.ARMEmulator;
import cn.banny.unidbg.arm.HookStatus;
import cn.banny.unidbg.arm.context.EditableArm32RegisterContext;
import cn.banny.unidbg.file.FileIO;
import cn.banny.unidbg.file.IOResolver;
import cn.banny.unidbg.hook.ReplaceCallback;
import cn.banny.unidbg.hook.whale.IWhale;
import cn.banny.unidbg.hook.whale.Whale;
import cn.banny.unidbg.linux.android.AndroidARMEmulator;
import cn.banny.unidbg.linux.android.AndroidResolver;
import cn.banny.unidbg.linux.android.dvm.*;
import cn.banny.unidbg.memory.Memory;
import cn.banny.unidbg.pointer.UnicornPointer;
import com.sun.jna.Pointer;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;
public class DemoTest extends AbstractJni implements IOResolver {
private static LibraryResolver createLibraryResolver() {
return new AndroidResolver(23);
}
private static ARMEmulator createARMEmulator() {
return new AndroidARMEmulator("zzz.me.unidbg_d");
}
private final ARMEmulator emulator;
private final VM vm;
private final Module module;
private final DvmClass MainActivity;
private DemoTest() throws IOException {
emulator = createARMEmulator();
emulator.getSyscallHandler().addIOResolver(this);
final Memory memory = emulator.getMemory();
memory.setLibraryResolver(createLibraryResolver());
memory.setCallInitFunction();
vm = emulator.createDalvikVM(new File("src/test/resources/app/app-debug.apk"));
DalvikModule dm = vm.loadLibrary("native-lib", false);
dm.callJNI_OnLoad(emulator);
module = dm.getModule();
MainActivity = vm.resolveClass("zzz.me.unidbg_d.MainActivity".replace(".", "/"));
}
public static void main(String[] args) throws Exception {
DemoTest test = new DemoTest();
test.test();
test.destroy();
}
private void destroy() throws IOException {
emulator.close();
System.out.println("destroy module=" + module);
}
private void test() {
vm.setJni(this);
IWhale whale = Whale.getInstance(emulator);
final Symbol compress;
try {
compress = emulator.getMemory().findModule("libz.so").findSymbolByName("compress");
if (compress != null) {
whale.WInlineHookFunction(compress, new ReplaceCallback() {
@Override
public HookStatus onCall(Emulator emulator, long originFunction) {
EditableArm32RegisterContext context = emulator.getContext();
byte[] fake = new byte[16];
ThreadLocalRandom.current().nextBytes(fake);
UnicornPointer pointer = emulator.getMemory().malloc(fake.length, true).getPointer();
pointer.write(0, fake, 0, fake.length);
context.setR2((int) pointer.toUIntPeer());
context.setR3(fake.length);
Pointer dest = context.getPointerArg(0);
Pointer destLen = context.getPointerArg(1);
Pointer src = context.getPointerArg(2);
long srcLen = context.getLongArg(3);
Inspector.inspect(src.getByteArray(0, (int) srcLen), "Let's compress");
return HookStatus.RET(emulator, originFunction);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
Number ret = MainActivity.callStaticJniMethod(emulator, "stringFromJNI()Ljava/lang/String;");
long hash = ret.intValue() & 0xffffffffL;
StringObject obj = vm.getObject(hash);
vm.deleteLocalRefs();
System.err.println(obj.getValue());
}
@Override
public FileIO resolve(File workDir, String pathname, int oflags) {
return null;
}
}
你再试下,unidbg的代码也有小更新
你再试下,unidbg的代码也有小更新
感谢大神,没问题了
以上这段malloc会有内存泄露
hook compress的时候,想改变第三四个参数的值,如果要改变的值比原来内存小的话,可以直接覆盖原来的数据,再改变第四个参数
hook compress的时候,想改变第三四个参数的值,如果要改变的值比原来内存小的话,可以直接覆盖原来的数据,再改变第四个参数
收到, 我这边可以改变之前值的长度, 增加到大于要修改的值,就可以尽量避免了吧
附上示例
cpp代码:
报错日志:
附上app:
app.zip