最近看到某客户端有一个检测模拟器的方法,我正常手机结果被判断是模拟器了,很好奇,于是找了一下原因。
普遍检测方法
public boolean isemulator() { string url = "tel:" "123456"; intent intent = new intent(); intent.setdata(uri.parse(url)); intent.setaction(intent.action_dial); // 是否可以处理跳转到拨号的 intent boolean canresolveintent = intent.resolveactivity(mcontext.getpackagemanager()) != null; return build.fingerprint.startswith("generic") || build.fingerprint.tolowercase().contains("vbox") || build.fingerprint.tolowercase().contains("test-keys") || build.model.contains("google_sdk") || build.model.contains("emulator") || build.serial.equalsignorecase("unknown") || build.serial.equalsignorecase("android") || build.model.contains("android sdk built for x86") || build.manufacturer.contains("genymotion") || (build.brand.startswith("generic") && build.device.startswith("generic")) || "google_sdk".equals(build.product) || ((telephonymanager) mcontext.getsystemservice(context.telephony_service)) .getnetworkoperatorname().tolowercase().equals("android") || !canresolverintent; }
这个代码检测模拟器有两个问题:
1、拨号检测,android10.0及以上均为false
2、build.serial,android8.0以上均为unknown
这导致8.0以上系统均会被误判
推荐模拟器检测方法
设备信息检测
private static final string[] known_numbers = {"15555215554", "15555215556", "15555215558", "15555215560", "15555215562", "15555215564", "15555215566", "15555215568", "15555215570", "15555215572", "15555215574", "15555215576", "15555215578", "15555215580", "15555215582", "15555215584",}; private boolean detectemulator() { if (build.fingerprint.startswith("generic") || build.fingerprint.startswith("unknown") || build.model.contains("google_sdk") || build.model.contains("emulator") || build.model.contains("android sdk built for x86") || build.manufacturer.contains("genymotion") || (build.brand.startswith("generic") && build.device.startswith("generic")) || "google_sdk".equals(build.product)) { return true; } if (build.product.equals("sdk") || build.product.equals("sdk_x86") || build.product.equals("vbox86p") || build.product.equals("emulator")) { return true; } if (build.board == null) { return true; } if (build.board.equals("unknown") || build.board.contains("android") || build.board.contains("droid")) { return true; } if (build.device == null) { return true; } if (build.device.equals("unknown") || build.device.contains("android") || build.device.contains("droid")) { return true; } if (build.hardware == null) { return true; } if (build.hardware.equals("goldfish") || build.hardware.equals("ranchu") || build.hardware.contains("ranchu")) { return true; } if (build.brand == null) { return true; } if (build.brand.startswith("generic") && build.device.startswith("generic")) { return true; } if (build.manufacturer.equals("unknown")) { return true; } if (build.manufacturer.equals("genymotion")) { return true; } if ((build.brand.startswith("generic") && build.device.startswith("generic")) || "google_sdk".equals(build.product)) { return true; } if (build.product == null) { return true; } if (build.product.equals("sdk") || build.product.equals("sdk_x86") || build.product.equals("vbox86p") || build.product.equals("emulator")) { return true; } if (build.hardware.equals("goldfish") || build.hardware.equals("ranchu")) { return true; } if (build.fingerprint.startswith("generic") || build.fingerprint.startswith("unknown") || build.model.contains("google_sdk") || build.model.contains("emulator") || build.model.contains("android sdk built for x86") || build.manufacturer.contains("genymotion") || (build.brand.startswith("generic") && build.device.startswith("generic")) || "google_sdk".equals(build.product)) { return true; } if (build.product == null) { return true; } if (build.product.equals("sdk") || build.product.equals("sdk_x86") || build.product.equals("vbox86p") || build.product.equals("emulator")) { return true; } if (build.hardware.equals("goldfish") || build.hardware.equals("ranchu")) { return true; } if (new file("/dev/socket/qemud").exists() || new file("/dev/qemu_pipe").exists()) { return true; } try { telephonymanager telephonymanager = (telephonymanager) getsystemservice(telephony_service); if (telephonymanager != null) { string deviceid = telephonymanager.getdeviceid(); listknownnumbers = arrays.aslist(known_numbers); if (knownnumbers.contains(deviceid)) { return true; } } } catch (exception e) { } return false; }
上述代码使用了多种方法来检测设备是否为模拟器,这些方法包括:
- 检测 build.fingerprint 是否以 “generic” 或 “unknown” 开头
- 检测 build.model 是否包含 “google_sdk”、“emulator” 或 “android sdk built for x86”
- 检测 build.manufacturer 是否为 “genymotion”
- 检测 build.product 是否为 “sdk”、“sdk_x86”、“vbox86p” 或 “emulator”
- 检测 build.board 是否为 “unknown” 或包含 “android” 或 “droid”
- 检测 build.device 是否为 “unknown” 或包含 “android” 或 “droid”
- 检测 build.hardware 是否为 “goldfish”、“ranchu” 或包含 “ranchu”
- 检测 build.brand 是否以 “generic” 开头,且 build.device 以 “generic” 开头
- 检测 build.product 是否为 “google_sdk”
- 检测是否存在文件 “/dev/socket/qemud” 或 “/dev/qemu_pipe”
- 检测设备的电话号码是否为已知的模拟器电话号码
上述方法都是基于固件信息的判断,通过测试发现很多模拟器都失效,参考网上的教程,还有蓝牙、光线传感器、cpu检测,配合上面的固件信息,基本可以搞定大部分模拟器。
蓝牙检测方法
public boolean nothasbluetooth() { bluetoothadapter ba = bluetoothadapter.getdefaultadapter(); if (ba == null) { return true; } else { // 如果有蓝牙不一定是有效的。获取蓝牙名称,若为null 则默认为模拟器 string name = ba.getname(); if (textutils.isempty(name)) { return true; } else { return false; } } }
光传感器检测方法
public static boolean nothaslightsensormanager(context context) { sensormanager sensormanager = (sensormanager) context.getsystemservice(sensor_service); sensor sensor8 = sensormanager.getdefaultsensor(sensor.type_light); //光 if (null == sensor8) { return true; } else { return false; } }
cpu检测方法
public static boolean checkisnotrealphone() { string cpuinfo = readcpuinfo(); if ((cpuinfo.contains("intel") || cpuinfo.contains("amd"))) { return true; } return false; } public static string readcpuinfo() { string result = ""; try { string[] args = {"/system/bin/cat", "/proc/cpuinfo"}; processbuilder cmd = new processbuilder(args); process process = cmd.start(); stringbuffer sb = new stringbuffer(); string readline = ""; bufferedreader responsereader = new bufferedreader(new inputstreamreader(process.getinputstream(), "utf-8")); while ((readline = responsereader.readline()) != null) { sb.append(readline); } responsereader.close(); result = sb.tostring().tolowercase(); } catch (ioexception ex) { } return result; }
以上检测方法也不是完全可行,随着android系统的更新,模拟器的增多,我们需要具体研究对应的一些变动来更新上述代码。
我们检测要注意一个问题,不一定能检测出所有的模拟器,但是一定不能误杀真机。
总结
建议 首先使用传感器进行可疑性判断
蓝牙, wifi, 电池 可以作为 辅助数据进行监听。
附加传感器类型代码
public string getsensortypename(int type) { switch (type) { case sensor.type_accelerometer: return "加速度传感器"; case sensor.type_gyroscope: return "陀螺仪传感器"; case sensor.type_light: return "环境光线传感器"; case sensor.type_magnetic_field: return "电磁场传感器"; case sensor.type_orientation: return "方向传感器"; case sensor.type_pressure: return "压力传感器"; case sensor.type_proximity: return "距离传感器"; case sensor.type_temperature: return "温度传感器"; case sensor.type_gravity: return "重场传感器"; case sensor.type_linear_acceleration: return "线性加速度传感器"; case sensor.type_rotation_vector: return "旋转矢量传感器"; case sensor.type_relative_humidity: return "湿度传感器"; case sensor.type_ambient_temperature: return "温度传感器"; case sensor.type_game_rotation_vector: return "游戏旋转矢量传感器"; case sensor.type_step_counter: return "计步器"; case sensor.type_geomagnetic_rotation_vector: return "地磁旋转矢量传感器"; case sensor.type_significant_motion: return "特殊动作触发传感器"; default: return "未知传感器"; } }