package com.weikou.beibeivideo.util;
|
|
import android.content.Context;
|
import android.content.pm.ApplicationInfo;
|
import android.os.Debug;
|
import android.util.Log;
|
import android.widget.Toast;
|
|
import com.weikou.beibeivideo.BeibeiVideoApplication;
|
import com.weikou.beibeivideo.BuildConfig;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileReader;
|
import java.util.Locale;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
public class DebuggerUtils {
|
|
|
/**
|
* 判断当前应用是否是debug状态
|
*/
|
public static boolean isDebuggable(Context context) {
|
try {
|
ApplicationInfo info = context.getApplicationInfo();
|
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
/**
|
* 检测是否在非Debug编译模式下,进行了调试操作,以防动态调试
|
*
|
* @param context
|
* @return
|
*/
|
private static Timer timer;
|
private static int tempCount=0;
|
public static void checkDebuggableInNotDebugModel(Context context) {
|
//非Debug 编译,反调试检测
|
if (!BuildConfig.DEBUG) {
|
if (isDebuggable(context)) {
|
ToastUtils.show("已被动态调试");
|
exitApp();
|
}
|
|
timer=new Timer();
|
timer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
// 测试
|
// tempCount++;
|
// if(tempCount==5*10){
|
// exitApp();
|
// }
|
// ToastUtils.show("循环判断");
|
try {
|
//判断是否有调试器连接,是就退出
|
if (Debug.isDebuggerConnected()) {
|
//Toast.makeText(context, "已被动态调试", Toast.LENGTH_SHORT).show();
|
ToastUtils.show("已被动态调试");
|
exitApp();
|
}
|
|
//判断是否被其他进程跟踪,是就退出
|
if (isUnderTraced()) {
|
//Toast.makeText(context, "已被其他恶意进程跟踪", Toast.LENGTH_SHORT).show();
|
ToastUtils.show("已被其他恶意进程跟踪");
|
exitApp();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
},0,200);
|
}
|
if (isUnderTraced()) {
|
ToastUtils.show("已被其他恶意进程跟踪");
|
exitApp();
|
}
|
|
}
|
|
/**
|
* 当我们使用Ptrace方式跟踪一个进程时,目标进程会记录自己被谁跟踪,可以查看/proc/pid/status看到这个信息,而没有被调试的时候TracerPid为0
|
*
|
* @return
|
*/
|
private static boolean isUnderTraced() {
|
String processStatusFilePath = String.format(Locale.US, "/proc/%d/status", android.os.Process.myPid());
|
File procInfoFile = new File(processStatusFilePath);
|
try {
|
BufferedReader b = new BufferedReader(new FileReader(procInfoFile));
|
String readLine;
|
while ((readLine = b.readLine()) != null) {
|
if (readLine.contains("TracerPid")) {
|
String[] arrays = readLine.split(":");
|
if (arrays.length == 2) {
|
int tracerPid = Integer.parseInt(arrays[1].trim());
|
if (tracerPid != 0) {
|
return true;
|
}
|
}
|
}
|
}
|
|
b.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return false;
|
}
|
|
private static void exitApp(){
|
BeibeiVideoApplication.finishAllActivity();
|
int myPid = android.os.Process.myPid();
|
android.os.Process.killProcess(myPid);
|
System.exit(0);
|
}
|
|
|
static class ToastUtils {
|
private static final String TAG = "ToastUtils";
|
|
public static void show(String msg) {
|
Log.w(TAG, msg);
|
}
|
}
|
|
|
}
|