package com.jniunidbg.part7;import com.github.unidbg.AndroidEmulator;import com.github.unidbg.Module;import com.github.unidbg.linux.android.AndroidEmulatorBuilder;import com.github.unidbg.linux.android.AndroidResolver;import com.github.unidbg.linux.android.dvm.*;import com.github.unidbg.linux.android.dvm.api.SystemService;import com.github.unidbg.memory.Memory;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import java.util.UUID;public class DianPing extends AbstractJni {private final AndroidEmulator emulator;private final VM vm;private final Module module;private DvmObject SIUACollector;public FileInputStream fileInputStream;public InputStreamReader inputStreamReader;public BufferedReader bufferedReader;private SimpleDateFormat simpleDateFormat;DianPing() {emulator = AndroidEmulatorBuilder.for32Bit().setRootDir(new File("target/rootfs")).build();final Memory memory = emulator.getMemory();memory.setLibraryResolver(new AndroidResolver(23));vm = emulator.createDalvikVM(new File("unidbg-android/src/test/resources/lession2/part7/dazhongdianping.apk"));vm.setJni(this);vm.setVerbose(true);DalvikModule dm = vm.loadLibrary("mtguard", true);module = dm.getModule();dm.callJNI_OnLoad(emulator);SIUACollector = vm.resolveClass("com/meituan/android/common/mtguard/NBridge$SIUACollector").newObject(null);}public static void main(String[] args) {DianPing dp = new DianPing();System.out.println("call getHWProperty");dp.getHWProperty();System.out.println("call getHWStatus");dp.getHWStatus();System.out.println("call getHWEquipmentInfo");dp.getHWEquipmentInfo();System.out.println("call getExternalEquipmentInfo");dp.getExternalEquipmentInfo();System.out.println("call getUserAction");dp.getUserAction();System.out.println("call getEnvironmentInfo");dp.getEnvironmentInfo();System.out.println("call getEnvironmentInfoExtra");dp.getEnvironmentInfoExtra();System.out.println("call getPlatformInfo");dp.getPlatformInfo();System.out.println("call getLocationInfo");dp.getLocationInfo();System.out.println("call startCollection");String result = dp.startCollection();System.out.println(result);}public String getHWProperty(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getHWProperty()Ljava/lang/String;").getValue();System.out.println("getHWProperty:"+ret);return ret;}public String getHWStatus(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getHWStatus()Ljava/lang/String;").getValue();System.out.println("getHWStatus:"+ret);return ret;}public String getHWEquipmentInfo(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getHWEquipmentInfo()Ljava/lang/String;").getValue();System.out.println("getHWEquipmentInfo:"+ret);return ret;}public StringgetExternalEquipmentInfo(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getExternalEquipmentInfo()Ljava/lang/String;").getValue();System.out.println("getExternalEquipmentInfo:"+ret);return ret;}public String getUserAction(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getUserAction()Ljava/lang/String;").getValue();System.out.println("getUserAction:"+ret);return ret;}public String getEnvironmentInfo(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getEnvironmentInfo()Ljava/lang/String;").getValue();System.out.println("getEnvironmentInfo:"+ret);emulator.set("getEnvironmentInfo", ret);return ret;}public String getEnvironmentInfoExtra(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getEnvironmentInfoExtra()Ljava/lang/String;").getValue();System.out.println("getEnvironmentInfoExtra:"+ret);return ret;}public String getPlatformInfo(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getPlatformInfo()Ljava/lang/String;").getValue();System.out.println("getPlatformInfo:"+ret);return ret;}public String getLocationInfo(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "getLocationInfo()Ljava/lang/String;").getValue();System.out.println("getLocationInfo:"+ret);return ret;}public String startCollection(){String ret = (String) SIUACollector.callJniMethodObject(emulator, "startCollection()Ljava/lang/String;").getValue();System.out.println("startCollection:"+ret);return ret;}@Overridepublic DvmObject<?> getObjectField(BaseVM vm, DvmObject<?> dvmObject, String signature) {switch (signature){case "com/meituan/android/common/mtguard/NBridge$SIUACollector->mContext:Landroid/content/Context;":{return vm.resolveClass("android/content/Context").newObject(null);}case "android/content/res/Configuration->locale:Ljava/util/Locale;":{return vm.resolveClass("java/util/Locale").newObject(Locale.getDefault());}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->batteryHelper:Lcom/meituan/android/common/dfingerprint/collection/utils/BatteryHelper;":{return vm.resolveClass("com/meituan/android/common/dfingerprint/collection/utils/BatteryHelper").newObject(null);}}return super.getObjectField(vm, dvmObject, signature);}@Overridepublic DvmObject<?> allocObject(BaseVM vm, DvmClass dvmClass, String signature) {switch (signature){case "java/lang/StringBuilder->allocObject":{return dvmClass.newObject(new StringBuilder());}case "java/io/File->allocObject":{return dvmClass.newObject(null);}case "java/io/BufferedReader->allocObject":{return dvmClass.newObject(null);}case "java/io/InputStreamReader->allocObject":{return dvmClass.newObject(null);}case "java/io/FileInputStream->allocObject":{return dvmClass.newObject(null);}case "java/text/SimpleDateFormat->allocObject":{return dvmClass.newObject(null);}case "java/util/Date->allocObject":{return dvmClass.newObject(null);}}return super.allocObject(vm, dvmClass, signature);}@Overridepublic void callVoidMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) {switch (signature){case "java/lang/StringBuilder->()V":return;case "java/io/File->(Ljava/lang/String;)V":{String filePath = vaList.getObjectArg(0).getValue().toString();System.out.println("File init:"+filePath);emulator.set(dvmObject.toString(), filePath);return;}case "java/io/FileInputStream->(Ljava/lang/String;)V":{String input = vaList.getObjectArg(0).getValue().toString();if(input.equals("/proc/cpuinfo")){try {fileInputStream = new FileInputStream("unidbg-android/src/test/resources/lession2/part7/cpuinfo");} catch (FileNotFoundException e) {e.printStackTrace();}return;}System.out.println("FileInputStream init:"+input);}case "java/io/InputStreamReader->(Ljava/io/InputStream;)V":{inputStreamReader = new InputStreamReader(fileInputStream);return;}case "java/io/BufferedReader->(Ljava/io/Reader;)V":{bufferedReader = new BufferedReader(inputStreamReader);return;}case "java/io/BufferedReader->close()V":{try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}return;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->safeClose(Ljava/io/Closeable;)V":{return;}case "java/text/SimpleDateFormat->(Ljava/lang/String;Ljava/util/Locale;)V":{String str = vaList.getObjectArg(0).getValue().toString();Locale locale = (Locale) vaList.getObjectArg(1).getValue();simpleDateFormat = new SimpleDateFormat(str, locale);return;}case "java/util/Date->()V":{return;}}super.callVoidMethodV(vm, dvmObject, signature, vaList);}@Overridepublic DvmObject<?> getStaticObjectField(BaseVM vm, DvmClass dvmClass, String signature) {switch (signature){case "android/os/Build->BOARD:Ljava/lang/String;":{return new StringObject(vm, "sdm845");}case "android/os/Build->MANUFACTURER:Ljava/lang/String;":{return new StringObject(vm, "Xiaomi");}case "android/os/Build->BRAND:Ljava/lang/String;":{return new StringObject(vm, "Xiaomi");}case "android/os/Build->MODEL:Ljava/lang/String;":{return new StringObject(vm, "MIX 2S");}case "android/os/Build->PRODUCT:Ljava/lang/String;":{return new StringObject(vm, "polaris");}case "android/os/Build->HARDWARE:Ljava/lang/String;":{return new StringObject(vm, "qcom");}case "android/os/Build->DEVICE:Ljava/lang/String;":{return new StringObject(vm, "polaris");}case "android/os/Build->HOST:Ljava/lang/String;":{return new StringObject(vm, "c3-miui-ota-bd134.bj");}case "android/os/Build->ID:Ljava/lang/String;":{return new StringObject(vm, "QKQ1.190828.002");}case "android/os/Build$VERSION->RELEASE:Ljava/lang/String;":{return new StringObject(vm, "10");}case "android/os/Build->TAGS:Ljava/lang/String;":{return new StringObject(vm, "release-keys");}case "android/os/Build->FINGERPRINT:Ljava/lang/String;":{return new StringObject(vm, "Xiaomi/polaris/polaris:10/QKQ1.190828.002/V12.0.2.0.QDGCNXM:user/release-keys");}case "android/os/Build->TYPE:Ljava/lang/String;":{return new StringObject(vm, "user");}case "android/os/Build$VERSION->SDK:Ljava/lang/String;":{return new StringObject(vm, "29");}}return super.getStaticObjectField(vm, dvmClass, signature);}@Overridepublic DvmObject<?> callObjectMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) {switch (signature){case "com/meituan/android/common/mtguard/NBridge$SIUACollector->checkBuildAttribute(Ljava/lang/String;)Ljava/lang/String;":{String str = vaList.getObjectArg(0).getValue().toString();return new StringObject(vm, (str.isEmpty() || str.equalsIgnoreCase("unknown")) ? CommonConstant.Symbol.MINUS : str);}case "java/lang/StringBuilder->append(Ljava/lang/String;)Ljava/lang/StringBuilder;":{StringBuilder stringBuilder = (StringBuilder) dvmObject.getValue();String arg1 = vaList.getObjectArg(0).getValue().toString();return vm.resolveClass("java/lang/StringBuilder").newObject(stringBuilder.append(arg1));}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getSysProp(Ljava/lang/String;)Ljava/lang/String;":{String prop = vaList.getObjectArg(0).getValue().toString();System.out.println("getSysProp:"+prop);if(prop.equals("ro.product.cpu.abi")){return new StringObject(vm, "arm64-v8a");}if(prop.equals("ro.product.cpu.abi2")){return new StringObject(vm, "");}if(prop.equals("ro.build.product")){return new StringObject(vm, "polaris");}if(prop.equals("ro.build.description")){return new StringObject(vm, "polaris-user 10 QKQ1.190828.002 V12.0.2.0.QDGCNXM release-keys");}if(prop.equals("ro.secure")){return new StringObject(vm, "1");}if(prop.equals("ro.debuggable")){return new StringObject(vm, "1");}if(prop.equals("persist.sys.usb.config")){return new StringObject(vm, "");}if(prop.equals("sys.usb.config")){return new StringObject(vm, "adb");}if(prop.equals("sys.usb.state")){return new StringObject(vm, "adb");}if(prop.equals("gsm.version.baseband")){return new StringObject(vm, "4.0.c2.6-00335-0914_2350_3c8fca6,4.0.c2.6-00335-0914_2350_3c8fca6");}if(prop.equals("gsm.version.ril-impl")){return new StringObject(vm, "Qualcomm RIL 1.0");}if(prop.equals("gsm.sim.state")){return new StringObject(vm, "ABSENT,ABSENT");}if(prop.equals("gsm.sim.state.2")){return new StringObject(vm, "");}if(prop.equals("wifi.interface")){return new StringObject(vm, "wlan0");}}case "android/content/Context->getResources()Landroid/content/res/Resources;":{return vm.resolveClass("android/content/res/Resources").newObject(null);}case "android/content/res/Resources->getConfiguration()Landroid/content/res/Configuration;":{return vm.resolveClass("android/content/res/Configuration").newObject(null);}case "java/lang/StringBuilder->toString()Ljava/lang/String;":{StringBuilder stringBuilder = (StringBuilder) dvmObject.getValue();return new StringObject(vm, stringBuilder.toString());}case "java/lang/StringBuilder->append(I)Ljava/lang/StringBuilder;":{StringBuilder stringBuilder = (StringBuilder) dvmObject.getValue();int i = vaList.getIntArg(0);return vm.resolveClass("java/lang/StringBuilder").newObject(stringBuilder.append(i));}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getCpuInfoType()Ljava/lang/String;":{return new StringObject(vm, "arm");}case "java/io/BufferedReader->readLine()Ljava/lang/String;":{String oneLine = null;try {oneLine = bufferedReader.readLine();} catch (IOException e) {e.printStackTrace();}System.out.println("bufferread readLine:"+oneLine);if(oneLine==null){return null;}return new StringObject(vm, oneLine);}case "java/lang/String->substring(I)Ljava/lang/String;":{String str = dvmObject.getValue().toString();return new StringObject(vm, str.substring(vaList.getIntArg(0)));}case "android/content/Context->getApplicationContext()Landroid/content/Context;":{return vm.resolveClass("android/content/Context").newObject(null);}case "android/content/Context->getSystemService(Ljava/lang/String;)Ljava/lang/Object;":{StringObject serviceName = vaList.getObjectArg(0);assert serviceName != null;if(serviceName.getValue().equals("window")){return vm.resolveClass("android/view/WindowManager").newObject(null);}return new SystemService(vm, serviceName.getValue());}case "android/hardware/SensorManager->getDefaultSensor(I)Landroid/hardware/Sensor;":{return vm.resolveClass("android/hardware/Sensor").newObject(vaList.getIntArg(0));}case "android/hardware/Sensor->getName()Ljava/lang/String;":{int tag = (int) dvmObject.getValue();System.out.println(tag);if(tag==1){return new StringObject(vm, "ICM20690");}if(tag==9){return new StringObject(vm, "gravityNon-wakeup");}}case "android/hardware/Sensor->getVendor()Ljava/lang/String;":{int tag = (int) dvmObject.getValue();if(tag==1){return new StringObject(vm, "qualcomm");}else if(tag==9){return new StringObject(vm, "qualcomm");}}case "android/view/WindowManager->getDefaultDisplay()Landroid/view/Display;":{return vm.resolveClass("android/view/Display").newObject(signature);}case "java/lang/StringBuilder->append(C)Ljava/lang/StringBuilder;":{StringBuilder stringBuilder = (StringBuilder) dvmObject.getValue();char c = (char) vaList.getIntArg(0);return vm.resolveClass("java/lang/StringBuilder").newObject(stringBuilder.append(c));}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getTotalInternalMemorySize()Ljava/lang/String;":{return new StringObject(vm, "21379328");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getTotalExternalMemorySize()Ljava/lang/String;":{return new StringObject(vm, "124345246");}case "android/telephony/TelephonyManager->getSimOperator()Ljava/lang/String;":{return new StringObject(vm, "46009");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getAccessSubType()Ljava/lang/String;":{return new StringObject(vm, "wifi");}case "java/util/UUID->toString()Ljava/lang/String;":{UUID uuid = (UUID) dvmObject.getValue();return new StringObject(vm, uuid.toString());}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getDataActivity(Landroid/content/Context;)Ljava/lang/String;":{return new StringObject(vm, "");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getEnvironmentInfo()Ljava/lang/String;":{return new StringObject(vm, (String) emulator.get("getEnvironmentInfo"));}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->isVPN()Ljava/lang/String;":{return new StringObject(vm, "0");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->brightness(Landroid/content/Context;)Ljava/lang/String;":{return new StringObject(vm, "22");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->systemVolume(Landroid/content/Context;)Ljava/lang/String;":{return new StringObject(vm, "13");}case "java/text/SimpleDateFormat->format(Ljava/util/Date;)Ljava/lang/String;":{return new StringObject(vm, simpleDateFormat.format(new Date()));}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->appCache(Landroid/content/Context;)Ljava/lang/String;":{return new StringObject(vm, "149045277");}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->availableSystem()Ljava/lang/String;":return new StringObject(vm, "unknown");case "com/meituan/android/common/mtguard/NBridge$SIUACollector->totalMemory()Ljava/lang/String;":return new StringObject(vm, "5905510400");case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getFirstLaunchTime(Landroid/content/Context;)Ljava/lang/String;":{return new StringObject(vm, "1637665296460");}case "android/net/wifi/WifiManager->getConnectionInfo()Landroid/net/wifi/WifiInfo;":{return vm.resolveClass("android/net/wifi/WifiInfo").newObject(signature);}case "android/net/wifi/WifiInfo->getSSID()Ljava/lang/String;":return new StringObject(vm, "CU_TckX");case "android/telephony/TelephonyManager->getNetworkOperator()Ljava/lang/String;":return new StringObject(vm, "");case "java/lang/StringBuilder->append(Ljava/lang/CharSequence;II)Ljava/lang/StringBuilder;":{StringBuilder stringBuilder = (StringBuilder) dvmObject.getValue();CharSequence charSequence = (CharSequence) vaList.getObjectArg(0).getValue();int arg2 = vaList.getIntArg(1);int arg3 = vaList.getIntArg(2);return vm.resolveClass("java/lang/StringBuilder").newObject(stringBuilder.append(charSequence, arg2, arg3));}case "android/net/wifi/WifiInfo->getBSSID()Ljava/lang/String;":{return new StringObject(vm, "");}}return super.callObjectMethodV(vm, dvmObject, signature, vaList);}@Overridepublic int getStaticIntField(BaseVM vm, DvmClass dvmClass, String signature) {switch (signature){case "android/os/Build$VERSION->SDK_INT:I":{return 29;}}return super.getStaticIntField(vm, dvmClass, signature);}@Overridepublic DvmObject<?> callStaticObjectMethodV(BaseVM vm, DvmClass dvmClass, String signature, VaList vaList) {switch (signature){case "java/lang/Integer->toString(I)Ljava/lang/String;":{int i = vaList.getIntArg(0);System.out.println("java/lang/Integer->toString:"+i);return new StringObject(vm, Integer.toString(i));}case "java/util/UUID->randomUUID()Ljava/util/UUID;":{return dvmClass.newObject(UUID.randomUUID());}case "java/lang/String->valueOf(I)Ljava/lang/String;":{return new StringObject(vm, String.valueOf(vaList.getIntArg(0)));}}return super.callStaticObjectMethodV(vm, dvmClass, signature, vaList);}@Overridepublic boolean callBooleanMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) {switch (signature){case "android/content/pm/PackageManager->hasSystemFeature(Ljava/lang/String;)Z":{String feature = vaList.getObjectArg(0).getValue().toString();if(feature.equals("android.hardware.sensor.accelerometer")){return true;}if(feature.equals("android.hardware.sensor.gyroscope")){return true;}if(feature.equals("android.hardware.wifi")){return true;}if(feature.equals("android.hardware.bluetooth")){return true;}if(feature.equals("android.hardware.bluetooth_le")){return true;}if(feature.equals("android.hardware.telephony")){return true;}if(feature.equals("android.hardware.usb.accessory")){return true;}if(feature.equals("android.hardware.location.gps")){return true;}if(feature.equals("android.hardware.nfc")){return true;}System.out.println("hasSystemFeature:"+feature);}case "java/io/File->exists()Z":{String tag = emulator.get(dvmObject.toString());if(tag.equals("/sys/class/power_supply/battery/voltage_now")){return true;}if(tag.equals("/sys/class/power_supply/battery/temp")){return false;}}case "java/lang/String->startsWith(Ljava/lang/String;)Z":{String str = dvmObject.getValue().toString();String arg =vaList.getObjectArg(0).getValue().toString();System.out.println("startswith");System.out.println(str);System.out.println(arg);return str.startsWith(arg);}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->isPermissionGranted(Ljava/lang/String;Landroid/content/Context;)Z":{String permission = vaList.getObjectArg(0).getValue().toString();System.out.println(permission);if(permission.equals("android.permission.READ_PHONE_STATE")){return true;}if(permission.equals("android.permission.ACCESS_WIFI_STATE")){return true;}}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->getBatteryInfo()Z":{return true;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->isAccessibilityEnable()Z":{return false;}case "java/lang/String->equalsIgnoreCase(Ljava/lang/String;)Z":{String str = (String) dvmObject.getValue();String arg = vaList.getObjectArg(0).getValue().toString();return str.equalsIgnoreCase(arg);}case "java/lang/String->equals(Ljava/lang/Object;)Z":{String str = (String) dvmObject.getValue();Object object = vaList.getObjectArg(0).getValue();return str.equals(object);}}return super.callBooleanMethodV(vm, dvmObject, signature, vaList);}@Overridepublic int callIntMethodV(BaseVM vm, DvmObject<?> dvmObject, String signature, VaList vaList) {switch (signature){case "com/meituan/android/common/mtguard/NBridge$SIUACollector->boolean2Integer(Z)I":{return vaList.getIntArg(0);}case "java/lang/String->compareToIgnoreCase(Ljava/lang/String;)I":{String str = dvmObject.getValue().toString();String arg = vaList.getObjectArg(0).getValue().toString();System.out.println("compareToIgnoreCase");System.out.println(str);System.out.println(arg);return str.compareToIgnoreCase(arg);}case "java/lang/String->lastIndexOf(I)I":{String str = dvmObject.getValue().toString();return str.lastIndexOf(vaList.getIntArg(0));}case "android/view/Display->getHeight()I":{return 2030;}case "android/view/Display->getWidth()I":{return 1080;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->uiAutomatorClickCount()I":{return 0;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->androidAppCnt(Landroid/content/Context;)I":{return 0;}case "java/lang/String->length()I":{String str = dvmObject.getValue().toString();return str.length();}case "android/net/wifi/WifiInfo->getRssi()I":{return 0;}}return super.callIntMethodV(vm, dvmObject, signature, vaList);}@Overridepublic boolean callStaticBooleanMethodV(BaseVM vm, DvmClass dvmClass, String signature, VaList vaList) {switch (signature){case "android/text/TextUtils->isEmpty(Ljava/lang/CharSequence;)Z":{String charSequence = (String) vaList.getObjectArg(0).getValue();System.out.println("debug");return charSequence.isEmpty();}}return super.callStaticBooleanMethodV(vm, dvmClass, signature, vaList);}@Overridepublic int getIntField(BaseVM vm, DvmObject<?> dvmObject, String signature) {switch (signature){case "com/meituan/android/common/mtguard/NBridge$SIUACollector->level:I":{return 93;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->scale:I":{return 100;}case "com/meituan/android/common/mtguard/NBridge$SIUACollector->status:I":{return 3;}}return super.getIntField(vm, dvmObject, signature);}@Overridepublic boolean getBooleanField(BaseVM vm, DvmObject<?> dvmObject, String signature) {switch (signature){case "com/meituan/android/common/mtguard/NBridge$SIUACollector->plugged:Z":{return false;}}return super.getBooleanField(vm, dvmObject, signature);}}