技术共享之常见的6中种方法检测手机是否是虚拟机

时间:2022-05-30 16:56:49

如今的移动应用开发,为了保证自己的劳动果实不被窃取,我们常常用到代码混淆、第三方加固 等等手段 。为了防止我们的app运行在虚拟机上被 ‘不良人’ 反编译,我们还需要判断手机是不是处于真机状态,我碰到过的一些用到了 反虚拟机 的软件 。例如 美团 和 饿了么 ,防止用户用虚拟机刷新用户,直接判断手机是否是真机,不是真机就退出程序,这样一方面可以防止用户刷新用户红包 ,也防止那些 xx0xx 反编译我们的软件。 闲话少说,
我就直接 上来一个工具类

package com.anyou.craftsman.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by 梁 on 2017/12/15.
*/


public class EmulatorCheck {

private static EmulatorCheck emulatorCheck ;

public static EmulatorCheck getInstance() {
if(emulatorCheck == null)
{
emulatorCheck = new EmulatorCheck() ;
}
return emulatorCheck ;
}

private static String[]known_pipes = { "/dev/socket/qemud", "/dev/qemu_pipe" };

/*
* 第一种 检测模拟器上特有的几个文件
* */

public boolean checkPipes()
{
for(int i =0;i <known_pipes.length;i++){
String pipes =known_pipes[i];
File qemu_socket= new File(pipes);
if(qemu_socket.exists()){
Log.v("Result:","Find pipes!");
return true;
}
}
Log.v("Result:","Not Find pipes!");
return false;
}


/*
* 第二种 检测手机号 是不是以下 号码
* */

private static String[]known_numbers = {"15555215554","15555215556",
"15555215558","15555215560","15555215562","15555215564",
"15555215566","15555215568","15555215570","15555215572",
"15555215574","15555215576","15555215578","15555215580",
"15555215582","15555215584",};

public static Boolean CheckPhoneNumber(Context context){
TelephonyManager telephonyManager =(TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);

@SuppressLint("MissingPermission") String phonenumber =telephonyManager.getLine1Number();

for(String number :known_numbers){
if(number.equalsIgnoreCase(phonenumber)){
Log.v("Result:","Find PhoneNumber!");
return true;
}
}
Log.v("Result:","Not Find PhoneNumber!");
return false;
}


/*
* 第三种 检测设备IDS 是不是 15 个 0
* */


private static String[]known_device_ids = {"000000000000000" // 默认ID
};

public static Boolean CheckDeviceIDS(Context context){
TelephonyManager telephonyManager = (TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);

@SuppressLint("MissingPermission") String device_ids =telephonyManager.getDeviceId();

for(String know_deviceid : known_device_ids){
if(know_deviceid.equalsIgnoreCase(device_ids)){
Log.v("Result:","Find ids: 000000000000000!");
return true;
}
}
Log.v("Result:","Not Find ids: 000000000000000!");
return false;
}


/*
*
* 第四种 检测imesi is 是不是 31026 + 10个 0
* */

private static String[]known_imsi_ids = {"310260000000000" // 默认的 imsi id
};
public static Boolean CheckImsiIDS(Context context){
TelephonyManager telephonyManager =(TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);

@SuppressLint("MissingPermission") String imsi_ids =telephonyManager.getSubscriberId();

for(String know_imsi :known_imsi_ids){
if(know_imsi.equalsIgnoreCase(imsi_ids)){
Log.v("Result:","Find imsi ids: 310260000000000!");
return true;
}
}
Log.v("Result:","Not Find imsi ids: 310260000000000!");
return false;
}



/*
* 第五种 检测设备信息
* */

public static Boolean CheckEmulatorBuild(Context context){
String BOARD =android.os.Build.BOARD;
String BOOTLOADER =android.os.Build.BOOTLOADER;
String BRAND =android.os.Build.BRAND;
String DEVICE =android.os.Build.DEVICE;
String HARDWARE =android.os.Build.HARDWARE;
String MODEL =android.os.Build.MODEL;
String PRODUCT =android.os.Build.PRODUCT;
if(BOARD== "unknown"|| BOOTLOADER== "unknown"
||BRAND =="generic" ||DEVICE =="generic"
||MODEL =="sdk" ||PRODUCT =="sdk"
||HARDWARE =="goldfish")
{
Log.v("Result:","Find Emulator by EmulatorBuild!");
return true;
}
Log.v("Result:","Not Find Emulator by EmulatorBuild!");
return false;
}


/*
* 第六种 检测运营商 如果 是Android 那么就是 模拟器
* */



public static boolean CheckOperatorNameAndroid(Context context){
@SuppressLint("WrongConstant") String szOperatorName = ((TelephonyManager)
context.getSystemService("phone")).getNetworkOperatorName();

if(szOperatorName.toLowerCase().equals("android")== true){
Log.v("Result:","Find Emulator by OperatorName!");
return true;
}
Log.v("Result:","Not Find Emulator by OperatorName!");
return false;
}



}

直接拿来使用,getInstance() 在调用其中的任何一个方法,返回为true 则就是虚拟机 ,false 为 真机 。

如有疑问 请留言 或者 在下方评论