如今的移动应用开发,为了保证自己的劳动果实不被窃取,我们常常用到代码混淆、第三方加固 等等手段 。为了防止我们的app运行在虚拟机上被 ‘不良人’ 反编译,我们还需要判断手机是不是处于真机状态,我碰到过的一些用到了 反虚拟机 的软件 。例如 美团 和 饿了么 ,防止用户用虚拟机刷新用户,直接判断手机是否是真机,不是真机就退出程序,这样一方面可以防止用户刷新用户红包 ,也防止那些 xx0xx 反编译我们的软件。 闲话少说,
我就直接 上来一个工具类
1 package com.anyou.craftsman.utils; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.telephony.TelephonyManager; 6 import android.util.Log; 7 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.FileNotFoundException; 11 import java.io.IOException; 12 import java.io.InputStream; 13 14 /** 15 * Created by 梁 on 2017/12/15. 16 */ 17 18 public class EmulatorCheck { 19 20 private static EmulatorCheck emulatorCheck ; 21 22 public static EmulatorCheck getInstance() { 23 if(emulatorCheck == null) 24 { 25 emulatorCheck = new EmulatorCheck() ; 26 } 27 return emulatorCheck ; 28 } 29 30 private static String[]known_pipes = { "/dev/socket/qemud", "/dev/qemu_pipe" }; 31 32 /* 33 * 第一种 检测模拟器上特有的几个文件 34 * */ 35 public boolean checkPipes() 36 { 37 for(int i =0;i <known_pipes.length;i++){ 38 String pipes =known_pipes[i]; 39 File qemu_socket= new File(pipes); 40 if(qemu_socket.exists()){ 41 Log.v("Result:","Find pipes!"); 42 return true; 43 } 44 } 45 Log.v("Result:","Not Find pipes!"); 46 return false; 47 } 48 49 50 /* 51 * 第二种 检测手机号 是不是以下 号码 52 * */ 53 private static String[]known_numbers = {"15555215554","15555215556", 54 "15555215558","15555215560","15555215562","15555215564", 55 "15555215566","15555215568","15555215570","15555215572", 56 "15555215574","15555215576","15555215578","15555215580", 57 "15555215582","15555215584",}; 58 59 public static Boolean CheckPhoneNumber(Context context){ 60 TelephonyManager telephonyManager =(TelephonyManager)context 61 .getSystemService(Context.TELEPHONY_SERVICE); 62 63 @SuppressLint("MissingPermission") String phonenumber =telephonyManager.getLine1Number(); 64 65 for(String number :known_numbers){ 66 if(number.equalsIgnoreCase(phonenumber)){ 67 Log.v("Result:","Find PhoneNumber!"); 68 return true; 69 } 70 } 71 Log.v("Result:","Not Find PhoneNumber!"); 72 return false; 73 } 74 75 76 /* 77 * 第三种 检测设备IDS 是不是 15 个 0 78 * */ 79 80 private static String[]known_device_ids = {"000000000000000" // 默认ID 81 }; 82 83 public static Boolean CheckDeviceIDS(Context context){ 84 TelephonyManager telephonyManager = (TelephonyManager)context 85 .getSystemService(Context.TELEPHONY_SERVICE); 86 87 @SuppressLint("MissingPermission") String device_ids =telephonyManager.getDeviceId(); 88 89 for(String know_deviceid : known_device_ids){ 90 if(know_deviceid.equalsIgnoreCase(device_ids)){ 91 Log.v("Result:","Find ids: 000000000000000!"); 92 return true; 93 } 94 } 95 Log.v("Result:","Not Find ids: 000000000000000!"); 96 return false; 97 } 98 99 100 /* 101 * 102 * 第四种 检测imesi is 是不是 31026 + 10个 0 103 * */ 104 private static String[]known_imsi_ids = {"310260000000000" // 默认的 imsi id 105 }; 106 public static Boolean CheckImsiIDS(Context context){ 107 TelephonyManager telephonyManager =(TelephonyManager) 108 context.getSystemService(Context.TELEPHONY_SERVICE); 109 110 @SuppressLint("MissingPermission") String imsi_ids =telephonyManager.getSubscriberId(); 111 112 for(String know_imsi :known_imsi_ids){ 113 if(know_imsi.equalsIgnoreCase(imsi_ids)){ 114 Log.v("Result:","Find imsi ids: 310260000000000!"); 115 return true; 116 } 117 } 118 Log.v("Result:","Not Find imsi ids: 310260000000000!"); 119 return false; 120 } 121 122 123 124 /* 125 * 第五种 检测设备信息 126 * */ 127 public static Boolean CheckEmulatorBuild(Context context){ 128 String BOARD =android.os.Build.BOARD; 129 String BOOTLOADER =android.os.Build.BOOTLOADER; 130 String BRAND =android.os.Build.BRAND; 131 String DEVICE =android.os.Build.DEVICE; 132 String HARDWARE =android.os.Build.HARDWARE; 133 String MODEL =android.os.Build.MODEL; 134 String PRODUCT =android.os.Build.PRODUCT; 135 if(BOARD== "unknown"|| BOOTLOADER== "unknown" 136 ||BRAND =="generic" ||DEVICE =="generic" 137 ||MODEL =="sdk" ||PRODUCT =="sdk" 138 ||HARDWARE =="goldfish") 139 { 140 Log.v("Result:","Find Emulator by EmulatorBuild!"); 141 return true; 142 } 143 Log.v("Result:","Not Find Emulator by EmulatorBuild!"); 144 return false; 145 } 146 147 148 /* 149 * 第六种 检测运营商 如果 是Android 那么就是 模拟器 150 * */ 151 152 153 public static boolean CheckOperatorNameAndroid(Context context){ 154 @SuppressLint("WrongConstant") String szOperatorName = ((TelephonyManager) 155 context.getSystemService("phone")).getNetworkOperatorName(); 156 157 if(szOperatorName.toLowerCase().equals("android")== true){ 158 Log.v("Result:","Find Emulator by OperatorName!"); 159 return true; 160 } 161 Log.v("Result:","Not Find Emulator by OperatorName!"); 162 return false; 163 } 164 165 166 167 } 168
直接拿来使用,getInstance() 在调用其中的任何一个方法,返回为true 则就是虚拟机 ,false 为 真机 。