unidbg教程 使用案例代读

本文使用unidbg对snh48 app进行模拟测试,首先给大家呈现完整代码,然后逐行分析代码内容下面是主要代码


EncryptlibUtils(boolean logging) {
this.logging = logging;
////改for64BIT,setProcessName("com.maihan.tredian")
emulator = AndroidEmulatorBuilder.for64Bit()
.setProcessName("com.pocket.snh48.activity")
.addBackendFactory(new Unicorn2Factory(true))
.build(); // 创建模拟器实例,要模拟32位或者64位,在这里区分
final Memory memory = emulator.getMemory(); // 模拟器的内存操作接口
memory.setLibraryResolver(new AndroidResolver(23)); // 设置系统类库解析



    vm = emulator.createDalvikVM(); // 创建Android虚拟机
    vm.setVerbose(logging); // 设置是否打印Jni调用细节
    vm.setJni(this);
    ////改o
    DalvikModule dm = vm.loadLibrary(new File("unidbg-android/src/test/resources/example_binaries/libencryptlib.so"), false); // 加载libNativeHelper.so到unicorn虚拟内存,加载成功以后会默认调用init_array等函数
    dm.callJNI_OnLoad(emulator); // 手动执行JNI_OnLoad函数
    module = dm.getModule(); // 加载好的libNativeHelper.so对应为一个模块
    TreUtilUtils = vm.resolveClass("com/pocket/snh48/base/net/utils/EncryptlibUtils");
}

void destroy() {
    IOUtils.close(emulator);
    if (logging) {
        System.out.println("destroy");
    }
}

public static void main(String[] args) throws Exception {
    EncryptlibUtils test = new EncryptlibUtils(true);
    String sign_str="1636221462621";
    test.call_sign();
   
    test.destroy();
}

////在java中被混淆了,只能通过c++层调用
void call_sign(){

    UnidbgPointer md5CTX=emulator.getMemory().malloc(200, false).getPointer();
    module.callFunction(emulator,0X1EAD0,md5CTX);
    UnidbgPointer inputAddr=emulator.getMemory().malloc(200, false).getPointer();
    byte[] inputBytes="12345678".getBytes();
    inputAddr.write(inputBytes);
    UnidbgPointer outputAddr=emulator.getMemory().malloc(200, false).getPointer();
    module.callFunction(emulator,0x1FA38,md5CTX,inputAddr,inputBytes.length,outputAddr);
    Inspector.inspect(outputAddr.getByteArray(0,32),"result");


}

这段 Java 代码使用了 Unidbg 框架(一个基于 Unicorn 的 Android native 代码模拟执行框架),目的是在 非 Android 环境(如 PC) 中模拟执行一个名为 libencryptlib.so 的 native 动态库中的函数,从而调用其加密逻辑(如 MD5 或其他签名算法)。

下面逐部分详细解释:


1. 类定义与字段

public class EncryptlibUtils extends AbstractJni {
   
   
    private final AndroidEmulator emulator;
    private final VM vm;
    private final Module module;
    private final DvmClass TreUtilUtils;
    private final boolean logging;
  • EncryptlibUtils:自定义类,用于封装对 native 库的调用。
  • 继承 AbstractJni:这是 Unidbg 提供的基类,用于实现 JNI(Java Native Interface)回调,即当 native 代码尝试调用 Java 方法时,可通过此机制模拟。
  • 字段说明
    • emulator:Android 模拟器实例(基于 Unicorn CPU 模拟器)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值