admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
        }
    }
 
 
}