現在的移動應用開發,爲了保證本身的勞動果實不被竊取,咱們經常用到代碼混淆、第三方加固 等等手段 。爲了防止咱們的app運行在虛擬機上被 ‘不良人’ 反編譯,咱們還須要判斷手機是否是處於真機狀態,我碰到過的一些用到了 反虛擬機 的軟件 。例如 美團 和 餓了麼 ,防止用戶用虛擬機刷新用戶,直接判斷手機是不是真機,不是真機就退出程序,這樣一方面能夠防止用戶刷新用戶紅包 ,也防止那些 xx0xx 反編譯咱們的軟件。 閒話少說,
我就直接 上來一個工具類java
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 爲 真機 。android