├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── android │ │ │ │ └── antidebug │ │ │ │ ├── IAntiDebugCallback.java │ │ │ │ ├── MainActivity.java │ │ │ │ └── AntiDebug.java │ │ ├── cpp │ │ │ ├── Log.h │ │ │ ├── native-lib.cpp │ │ │ ├── AntiDebug.h │ │ │ └── AntiDebug.cpp │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── android │ │ │ └── antidebug │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── android │ │ └── antidebug │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro ├── build.gradle └── CMakeLists.txt ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AntiDebug 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/name-cpu/AntiDebug/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/java/com/android/antidebug/IAntiDebugCallback.java: -------------------------------------------------------------------------------- 1 | package com.android.antidebug; 2 | 3 | public interface IAntiDebugCallback { 4 | void beInjectedDebug(); 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .idea 12 | .externalNativeBuild 13 | /app/build -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/android/antidebug/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.android.antidebug; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/cpp/Log.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LOG_TAG "AntiDebug" 5 | #define LOG_PRINT_D(fmt,args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) 6 | #define LOG_PRINT_I(fmt,args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args) 7 | #define LOG_PRINT_W(fmt,args...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, fmt, ##args) 8 | #define LOG_PRINT_E(fmt,args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args) 9 | #define LOG_PRINT_F(fmt,args...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, fmt, ##args) -------------------------------------------------------------------------------- /app/src/main/cpp/native-lib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "AntiDebug.h" 4 | 5 | jobject g_callbackRef; 6 | jmethodID g_MethodCallback; 7 | 8 | extern "C" JNIEXPORT void JNICALL 9 | Java_com_android_antidebug_AntiDebug_setAntiDebugCallback( 10 | JNIEnv* env, 11 | jclass type, jobject jCallback) { 12 | jclass jclazz = env->GetObjectClass(jCallback); 13 | g_callbackRef = env->NewGlobalRef(jCallback); 14 | g_MethodCallback = env->GetMethodID(jclazz, "beInjectedDebug", "()V"); 15 | } 16 | 17 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){ 18 | AntiDebug::antiDebug(vm); 19 | return JNI_VERSION_1_4; //这里很重要,必须返回版本,否则加载会失败。 20 | } 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/cpp/AntiDebug.h: -------------------------------------------------------------------------------- 1 | #ifndef _ANTI_DEBUG_H 2 | #define _ANTI_DEBUG_H 3 | #include 4 | 5 | #define MACRO_HIDE_SYMBOL __attribute__ ((visibility ("hidden"))) 6 | 7 | class AntiDebug{ 8 | public: 9 | static void antiDebug(JavaVM* jvm); 10 | static bool isDebugMode(); 11 | private: 12 | AntiDebug(); 13 | void antiDebugInner(); 14 | static void* antiDebugCallback(void *arg); 15 | char* getPackageName(JNIEnv* env); 16 | void getGlobalRef(); 17 | bool readStatus(); 18 | bool isBeDebug(); 19 | bool IsHookByXPosed(); 20 | bool analyzeStackTrace(); 21 | private: 22 | jclass mDebugGlobalRef; 23 | jclass mXPosedGlobalRef; 24 | jclass mExceptionGlobalRef; 25 | jclass mStackElementRef; 26 | static int mAppFlags; 27 | static AntiDebug* s_instance; 28 | }; 29 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AntiDebug 2 | android apk反调试工具,反hook、反xposed、反virtual xposed、反substrate 3 | 4 | 该项目主要实现android app反第三方调试功能,主要功能用c++实现,因为考虑到用java实现会被xposed等功能拦截,导致反调试功能失效。 5 | 该项目使用 __attribute__ ((visibility ("hidden"))) 隐藏了关键的函数符号,避免被静态分析。 6 | 7 | # 主要的思路和步骤如下: 8 | # native层主要工作: 9 | 1.检测进程status文件TracerPid字段状态,如果该状态不为0,表示native层代码被调试 10 | 11 | 2.检测进程的maps文件,app进程中加载的所有模块信息地址均会写入该文件中。我们分析是否有com.saurik.substrate/io.va.exposed/de.robv.android.xposed等一系列敏感模块信息来判断是否被反调试 12 | 13 | 3.检测进程的调试器状态,首先检测app是否处于Debug模式,然后检测java层调试器是否打开。如果处于Release模式并且调试器打开,说明被反调试 14 | 15 | # java层主要工作: 16 | 在关键业务的地方调用AntiDeubg.isInject()方法,判断进程的堆栈是否包含两次com.android.internal.os.ZygoteInit信息。正常情况只会调用一次,如果app被xposed注入,被调用两次。 17 | 18 | 19 | # 使用方法: 20 | 在Activity中调用此方法注册监听即可,AntiDebug.setAntiDebugCallback(this); 21 | Native层会实时监测进程状态,如果监测到被反调试会回调到beInjectedDebug方法中,详情见Demo。 22 | 23 | 如有问题欢迎提Issues。 24 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/android/antidebug/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.android.antidebug; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.android.antidebug", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/antidebug/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.android.antidebug; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.widget.TextView; 7 | import android.widget.Toast; 8 | 9 | public class MainActivity extends AppCompatActivity implements IAntiDebugCallback{ 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | AntiDebug.setAntiDebugCallback(this); 16 | } 17 | 18 | 19 | @Override 20 | public void beInjectedDebug() { 21 | runOnUiThread(new Runnable() { 22 | @Override 23 | public void run() { 24 | Toast.makeText(MainActivity.this, "app正在被调试或被注入", Toast.LENGTH_SHORT).show(); 25 | } 26 | }); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/antidebug/AntiDebug.java: -------------------------------------------------------------------------------- 1 | package com.android.antidebug; 2 | 3 | public class AntiDebug { 4 | private static String KEY_SUBSTRATE = "com.saurik.substrate.MS$2"; 5 | 6 | static { 7 | System.loadLibrary("AntiDebug"); 8 | } 9 | 10 | /* 设置native检查回调接口 */ 11 | public static native void setAntiDebugCallback(IAntiDebugCallback callback); 12 | 13 | /* 通过堆栈判断apk是否被注入,被注入时ZygoteInit对象会被执行两次 */ 14 | public static boolean isInject() { 15 | try { 16 | throw new Exception(""); 17 | } catch (Exception e) { 18 | int i = 0; 19 | for (StackTraceElement stackTraceElement : e.getStackTrace()) { 20 | if (stackTraceElement.getClassName().equals("com.android.internal.os.ZygoteInit")) { 21 | i++; 22 | if (i == 2) { 23 | return true; 24 | } 25 | } 26 | if (stackTraceElement.getClassName().equals(KEY_SUBSTRATE)) { 27 | return true; 28 | } 29 | } 30 | return false; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.android.antidebug" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | externalNativeBuild { 13 | cmake { 14 | cppFlags "" 15 | } 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | externalNativeBuild { 25 | cmake { 26 | path "CMakeLists.txt" 27 | } 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | implementation 'com.android.support:appcompat-v7:28.0.0' 34 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 35 | testImplementation 'junit:junit:4.12' 36 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 37 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 38 | } 39 | -------------------------------------------------------------------------------- /app/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # For more information about using CMake with Android Studio, read the 2 | # documentation: https://d.android.com/studio/projects/add-native-code.html 3 | 4 | # Sets the minimum version of CMake required to build the native library. 5 | 6 | cmake_minimum_required(VERSION 3.4.1) 7 | 8 | # Creates and names a library, sets it as either STATIC 9 | # or SHARED, and provides the relative paths to its source code. 10 | # You can define multiple libraries, and CMake builds them for you. 11 | # Gradle automatically packages shared libraries with your APK. 12 | 13 | add_library( # Sets the name of the library. 14 | AntiDebug 15 | 16 | # Sets the library as a shared library. 17 | SHARED 18 | 19 | # Provides a relative path to your source file(s). 20 | src/main/cpp/native-lib.cpp 21 | src/main/cpp/AntiDebug.cpp) 22 | 23 | # Searches for a specified prebuilt library and stores the path as a 24 | # variable. Because CMake includes system libraries in the search path by 25 | # default, you only need to specify the name of the public NDK library 26 | # you want to add. CMake verifies that the library exists before 27 | # completing its build. 28 | 29 | find_library( # Sets the name of the path variable. 30 | AntiDebug 31 | 32 | # Specifies the name of the NDK library that 33 | # you want CMake to locate. 34 | log ) 35 | 36 | # Specifies libraries CMake should link to your target library. You 37 | # can link multiple libraries, such as libraries you define in this 38 | # build script, prebuilt third-party libraries, or system libraries. 39 | 40 | target_link_libraries( # Specifies the target library. 41 | AntiDebug 42 | android 43 | # Links the target library to the log library 44 | # included in the NDK. 45 | log ) -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/cpp/AntiDebug.cpp: -------------------------------------------------------------------------------- 1 | #include "AntiDebug.h" 2 | #include "Log.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | using namespace std; 15 | 16 | extern jobject g_callbackRef; 17 | extern jmethodID g_MethodCallback; 18 | 19 | MACRO_HIDE_SYMBOL JavaVM* g_jvm = NULL; 20 | MACRO_HIDE_SYMBOL bool g_bAttached = false; 21 | MACRO_HIDE_SYMBOL jobject g_context = 0; 22 | 23 | MACRO_HIDE_SYMBOL AntiDebug* AntiDebug::s_instance = NULL; 24 | MACRO_HIDE_SYMBOL int AntiDebug::mAppFlags = 0; 25 | 26 | MACRO_HIDE_SYMBOL JNIEnv *GetEnv() 27 | { 28 | if(g_jvm == NULL) 29 | return NULL; 30 | 31 | int status; 32 | JNIEnv *env = NULL; 33 | status = g_jvm->GetEnv((void **)&env, JNI_VERSION_1_4); 34 | if(status < 0) 35 | { 36 | status = g_jvm->AttachCurrentThread(&env, NULL); 37 | if(status < 0) 38 | { 39 | return NULL; 40 | } 41 | } 42 | 43 | g_bAttached = true; 44 | return env; 45 | } 46 | 47 | MACRO_HIDE_SYMBOL void DetachCurrent() 48 | { 49 | if(g_bAttached && g_jvm != NULL) 50 | { 51 | g_jvm->DetachCurrentThread(); 52 | } 53 | } 54 | 55 | MACRO_HIDE_SYMBOL jobject getGlobalAppContext(JNIEnv *env) 56 | { 57 | if(env == NULL) 58 | return NULL; 59 | 60 | if(g_context != NULL) 61 | return g_context; 62 | 63 | //获取Activity Thread的实例对象 64 | jclass activityThread = env->FindClass("android/app/ActivityThread"); 65 | jmethodID currentActivityThread = env->GetStaticMethodID(activityThread, "currentActivityThread", "()Landroid/app/ActivityThread;"); 66 | if(currentActivityThread == NULL) 67 | return NULL; 68 | 69 | jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread); 70 | if(at == NULL) 71 | return NULL; 72 | 73 | //获取Application,也就是全局的Context 74 | jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;"); 75 | if(getApplication == NULL) 76 | return NULL; 77 | 78 | g_context = env->CallObjectMethod(at, getApplication); 79 | return g_context; 80 | } 81 | 82 | MACRO_HIDE_SYMBOL void string_replace( std::string &strBig, const std::string &strsrc, const std::string &strdst) 83 | { 84 | string::size_type pos = 0; 85 | string::size_type srclen = strsrc.size(); 86 | string::size_type dstlen = strdst.size(); 87 | 88 | while( (pos=strBig.find(strsrc, pos)) != string::npos ) 89 | { 90 | strBig.replace( pos, srclen, strdst ); 91 | pos += dstlen; 92 | } 93 | } 94 | 95 | MACRO_HIDE_SYMBOL AntiDebug::AntiDebug(){ 96 | mDebugGlobalRef = 0; 97 | mXPosedGlobalRef = 0; 98 | mExceptionGlobalRef = 0; 99 | mStackElementRef = 0; 100 | } 101 | 102 | //检测进程状态 103 | MACRO_HIDE_SYMBOL bool AntiDebug::readStatus(){ 104 | const int bufSize = 1024; 105 | char fileName[bufSize]; 106 | char contentLine[bufSize]; 107 | int ppid = 0; 108 | int pid = getpid(); 109 | sprintf(fileName, "/proc/%d/status", pid); 110 | FILE* fd = fopen(fileName, "r"); 111 | if (fd != NULL) 112 | { 113 | while (fgets(contentLine, bufSize, fd)) 114 | { 115 | if (strncmp(contentLine, "PPid", 4) == 0) 116 | { 117 | ppid = atoi(&contentLine[5]); 118 | } 119 | 120 | if (strncmp(contentLine, "TracerPid", 9) == 0) 121 | { 122 | int statue = atoi(&contentLine[10]); 123 | if (statue != 0 && ppid != statue) 124 | { 125 | LOG_PRINT_E("app be debug by ida or lldb."); 126 | fclose(fd); 127 | return true; 128 | } 129 | break; 130 | } 131 | } 132 | fclose(fd); 133 | } 134 | else 135 | { 136 | LOG_PRINT_E("status file open %s fail...", fileName); 137 | } 138 | 139 | return false; 140 | } 141 | 142 | //检测是否被xposed注入 143 | MACRO_HIDE_SYMBOL bool AntiDebug::IsHookByXPosed(){ 144 | char buf[1024] = {0}; 145 | FILE *fp; 146 | int pid = getpid(); 147 | sprintf(buf,"/proc/%d/maps",pid); 148 | fp = fopen(buf, "r"); 149 | if(fp == NULL){ 150 | LOG_PRINT_E("Error open maps file in progress %d",pid); 151 | return false; 152 | } 153 | 154 | if(mXPosedGlobalRef != 0){ 155 | LOG_PRINT_E("app be injected by xposed or substrate."); 156 | return true; 157 | } 158 | 159 | while (fgets(buf,sizeof(buf),fp)){ 160 | if(strstr(buf, "com.saurik.substrate") || strstr(buf, "io.va.exposed") || strstr(buf, "de.robv.android.xposed")){ 161 | LOG_PRINT_E("app be injected by xposed or substrate."); 162 | fclose(fp); 163 | return true; 164 | } 165 | } 166 | fclose(fp); 167 | 168 | return false; 169 | } 170 | 171 | //分析java层堆栈,获取不到堆栈信息 172 | MACRO_HIDE_SYMBOL bool AntiDebug::analyzeStackTrace(){ 173 | JNIEnv* env = GetEnv(); 174 | if(env == NULL || mExceptionGlobalRef == 0 || mStackElementRef == 0) 175 | return false; 176 | 177 | jmethodID throwable_init = env->GetMethodID(mExceptionGlobalRef, "", "(Ljava/lang/String;)V"); 178 | jobject throwable_obj = env->NewObject(mExceptionGlobalRef, throwable_init, env->NewStringUTF("test")); 179 | 180 | jmethodID throwable_getStackTrace = env->GetMethodID(mExceptionGlobalRef, "getStackTrace", "()[Ljava/lang/StackTraceElement;"); 181 | jobjectArray jStackElements = (jobjectArray)env->CallObjectMethod(throwable_obj, throwable_getStackTrace); 182 | 183 | jmethodID jMthGetClassName = env->GetMethodID(mStackElementRef, "getClassName", "()Ljava/lang/String;"); 184 | int len = env->GetArrayLength(jStackElements); 185 | LOG_PRINT_E("jStackElements = %p, jMthGetClassName = %p, len = %d", jStackElements, jMthGetClassName, len); 186 | 187 | for(int i = 0; i < len; i++){ 188 | jobject jStackElement = env->GetObjectArrayElement(jStackElements, i); 189 | jstring jClassName = (jstring)env->CallObjectMethod(jStackElement, jMthGetClassName); 190 | const char* szClassName = env->GetStringUTFChars(jClassName, 0); 191 | LOG_PRINT_I("szClassName = %s", szClassName); 192 | } 193 | 194 | return true; 195 | } 196 | 197 | //检测调试器状态 198 | MACRO_HIDE_SYMBOL bool AntiDebug::isBeDebug(){ 199 | if(g_context == NULL || mDebugGlobalRef == 0) 200 | return false; 201 | 202 | JNIEnv* env = GetEnv(); 203 | if(env == NULL) 204 | return false; 205 | 206 | jclass jDebugClazz = env->FindClass("android/os/Debug"); 207 | bool jDebug = ((mAppFlags & 2) != 0); 208 | jmethodID mthIsDebuggerConn = env->GetStaticMethodID(jDebugClazz, "isDebuggerConnected", "()Z"); 209 | jboolean jIsDebuggerConnected = env->CallStaticBooleanMethod(jDebugClazz, mthIsDebuggerConn); 210 | 211 | //DetachCurrent(); 212 | if(!jDebug && jIsDebuggerConnected){ 213 | LOG_PRINT_E("app be debug in release mode jDebug = %d,jIsDebuggerConnected = %d", jDebug, jIsDebuggerConnected); 214 | return true; 215 | } 216 | 217 | return false; 218 | } 219 | 220 | //检测是否在虚拟机内运行 221 | MACRO_HIDE_SYMBOL bool IsRunInVirtual(){ 222 | return true; 223 | } 224 | 225 | //反调试检测 226 | MACRO_HIDE_SYMBOL void* AntiDebug::antiDebugCallback(void *arg) 227 | { 228 | if(arg == NULL) 229 | return NULL; 230 | 231 | AntiDebug* pAntiDebug = (AntiDebug*)arg; 232 | 233 | while (true) 234 | { 235 | try 236 | { 237 | bool bRet1 = pAntiDebug->readStatus(); 238 | bool bRet2 = pAntiDebug->IsHookByXPosed(); 239 | bool bRet3 = pAntiDebug->isBeDebug(); 240 | if(bRet1 || bRet2 || bRet3){ 241 | if(g_callbackRef != 0 && g_MethodCallback != 0){ 242 | JNIEnv* env = GetEnv(); 243 | if(env != NULL){ 244 | env->CallVoidMethod(g_callbackRef, g_MethodCallback); 245 | } 246 | } 247 | } 248 | } catch (...) 249 | { 250 | 251 | } 252 | 253 | sleep(1); 254 | } 255 | } 256 | 257 | MACRO_HIDE_SYMBOL void AntiDebug::getGlobalRef() 258 | { 259 | int status; 260 | JNIEnv *env = NULL; 261 | status = g_jvm->GetEnv((void **)&env, JNI_VERSION_1_4); 262 | if(status >= 0){ 263 | getGlobalAppContext(env); 264 | } 265 | 266 | char* szPackageName = getPackageName(env); 267 | if(env == NULL || szPackageName == NULL) 268 | return ; 269 | 270 | string strPackageName = szPackageName; 271 | string_replace(strPackageName, ".", "/"); 272 | 273 | try{ 274 | char szClazzName[256] = {0}; 275 | jclass jApplication = env->GetObjectClass(g_context); 276 | jmethodID jMthApplicationInfo = env->GetMethodID(jApplication, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;"); 277 | if(jMthApplicationInfo != 0){ 278 | jobject jAppinfo = env->CallObjectMethod(g_context, jMthApplicationInfo); 279 | jclass jClazAppInfo = env->GetObjectClass(jAppinfo); 280 | jfieldID jfieldFlags = env->GetFieldID(jClazAppInfo, "flags", "I"); 281 | mAppFlags = env->GetIntField(jAppinfo, jfieldFlags); 282 | env->DeleteLocalRef(jClazAppInfo); 283 | } 284 | env->DeleteLocalRef(jApplication); 285 | 286 | memset(szClazzName, 0, 256); 287 | sprintf(szClazzName, "android/os/Debug"); 288 | jclass jDebugClazz = env->FindClass(szClazzName); 289 | if(jDebugClazz != 0){ 290 | mDebugGlobalRef = (jclass)env->NewGlobalRef(jDebugClazz); 291 | } 292 | 293 | 294 | memset(szClazzName, 0, 256); 295 | sprintf(szClazzName, "de/robv/android/xposed/XposedBridge"); 296 | jclass jXPosedClazz = env->FindClass(szClazzName); 297 | if(env->ExceptionCheck()){ 298 | env->ExceptionClear(); 299 | } 300 | if(jXPosedClazz != 0) 301 | { 302 | mXPosedGlobalRef = (jclass)env->NewGlobalRef(jXPosedClazz); 303 | } 304 | } 305 | catch(...) 306 | { 307 | 308 | } 309 | } 310 | 311 | MACRO_HIDE_SYMBOL bool AntiDebug::isDebugMode() 312 | { 313 | return (mAppFlags & 2) != 0; 314 | } 315 | 316 | MACRO_HIDE_SYMBOL char* AntiDebug::getPackageName(JNIEnv* env) 317 | { 318 | if(env == NULL || g_context == NULL) 319 | return NULL; 320 | 321 | jclass context_class = env->GetObjectClass(g_context); 322 | 323 | //反射获取PackageManager 324 | jmethodID methodId = env->GetMethodID(context_class, "getPackageManager", "()Landroid/content/pm/PackageManager;"); 325 | jobject package_manager = env->CallObjectMethod(g_context, methodId); 326 | if (package_manager == NULL) { 327 | LOG_PRINT_E("checkPackageName package_manager is NULL"); 328 | return NULL; 329 | } 330 | 331 | //反射获取包名 332 | methodId = env->GetMethodID(context_class, "getPackageName", "()Ljava/lang/String;"); 333 | jstring package_name = (jstring)env->CallObjectMethod(g_context, methodId); 334 | if (package_name == NULL) { 335 | LOG_PRINT_E("checkPackageName package_name is NULL"); 336 | return NULL; 337 | } 338 | env->DeleteLocalRef(context_class); 339 | 340 | char* szPackageName = (char*)env->GetStringUTFChars(package_name, 0); 341 | return szPackageName; 342 | } 343 | 344 | MACRO_HIDE_SYMBOL void AntiDebug::antiDebugInner() 345 | { 346 | getGlobalRef(); 347 | ptrace(PTRACE_TRACEME, 0, 0, 0); 348 | pthread_t ptid; 349 | pthread_create(&ptid, NULL, AntiDebug::antiDebugCallback, this); 350 | } 351 | 352 | MACRO_HIDE_SYMBOL void AntiDebug::antiDebug(JavaVM* jvm) 353 | { 354 | g_jvm = jvm; 355 | if(s_instance == NULL){ 356 | s_instance = new AntiDebug(); 357 | s_instance->antiDebugInner(); 358 | } 359 | } 360 | 361 | --------------------------------------------------------------------------------