From 94acd9479e8d86d24e82b0ff036bfec5f7968e0f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 22 Nov 2018 10:05:28 -0500 Subject: [PATCH] Add source code --- .gitignore | 8 + app/.gitignore | 1 + app/CMakeLists.txt | 44 +++++ app/build.gradle | 44 +++++ app/proguard-rules.pro | 21 +++ app/src/main/AndroidManifest.xml | 23 +++ app/src/main/cpp/native-lib.cpp | 80 ++++++++ .../com/topjohnwu/procgate/MainActivity.java | 88 +++++++++ .../drawable-v24/ic_launcher_foreground.xml | 34 ++++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++++ app/src/main/res/layout/main_layout.xml | 37 ++++ .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 8 + build.gradle | 27 +++ gradle.properties | 15 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 5 + gradlew | 172 ++++++++++++++++++ gradlew.bat | 84 +++++++++ settings.gradle | 1 + 27 files changed, 876 insertions(+) create mode 100644 .gitignore create mode 100644 app/.gitignore create mode 100644 app/CMakeLists.txt create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/cpp/native-lib.cpp create mode 100644 app/src/main/java/com/topjohnwu/procgate/MainActivity.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/main_layout.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09b993d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.iml +.gradle +/local.properties +/.idea +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt new file mode 100644 index 0000000..f8e6e8b --- /dev/null +++ b/app/CMakeLists.txt @@ -0,0 +1,44 @@ +# For more information about using CMake with Android Studio, read the +# documentation: https://d.android.com/studio/projects/add-native-code.html + +# Sets the minimum version of CMake required to build the native library. + +cmake_minimum_required(VERSION 3.4.1) + +# Creates and names a library, sets it as either STATIC +# or SHARED, and provides the relative paths to its source code. +# You can define multiple libraries, and CMake builds them for you. +# Gradle automatically packages shared libraries with your APK. + +add_library( # Sets the name of the library. + native-lib + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + src/main/cpp/native-lib.cpp ) + +# Searches for a specified prebuilt library and stores the path as a +# variable. Because CMake includes system libraries in the search path by +# default, you only need to specify the name of the public NDK library +# you want to add. CMake verifies that the library exists before +# completing its build. + +find_library( # Sets the name of the path variable. + log-lib + + # Specifies the name of the NDK library that + # you want CMake to locate. + log ) + +# Specifies libraries CMake should link to your target library. You +# can link multiple libraries, such as libraries you define in this +# build script, prebuilt third-party libraries, or system libraries. + +target_link_libraries( # Specifies the target library. + native-lib + + # Links the target library to the log library + # included in the NDK. + ${log-lib} ) \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f6d5c94 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,44 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "com.topjohnwu.procgate" + minSdkVersion 24 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + externalNativeBuild { + cmake { + cppFlags "-std=c++11" + arguments "-DANDROID_STL=none" + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + } + buildTypes { + release { + minifyEnabled true + shrinkResources true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + externalNativeBuild { + cmake { + path "CMakeLists.txt" + } + } +} + +repositories { + maven { url 'https://jitpack.io' } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.annotation:annotation:1.0.0' + implementation 'com.github.topjohnwu:libsu:2.0.3' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..bd631c5 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/cpp/native-lib.cpp b/app/src/main/cpp/native-lib.cpp new file mode 100644 index 0000000..9f05f05 --- /dev/null +++ b/app/src/main/cpp/native-lib.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "POC", __VA_ARGS__) + +static JNIEnv *gEnv; +static jobject gThis; +static jmethodID jAddText; +static bool haveLeak; + +static void addText(const char *fmt, ...) { + char buf[1024]; + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + jstring jstr = gEnv->NewStringUTF(buf); + gEnv->CallVoidMethod(gThis, jAddText, jstr); + gEnv->DeleteLocalRef(jstr); +} + +static bool isDigit(const char *s) { + for (const char *c = s; *c; ++c) { + if (*c < '0' || *c > '9') + return false; + } + return true; +} + +static void tryOpen(const char *pid) { + char buf[128]; + FILE *f; + struct stat st; + sprintf(buf, "/proc/%s", pid); + if (stat(buf, &st)) + return; + /* Do not print process with same UID */ + if (st.st_uid == getuid()) + return; + sprintf(buf, "/proc/%s/cmdline", pid); + if ((f = fopen(buf, "r"))) { + haveLeak = true; + if (fgets(buf, sizeof(buf), f) == 0) + buf[0] = '\0'; + addText("Leak PID=[%s] UID=[%d] cmdline=[%s]\n", pid, st.st_uid, buf); + fclose(f); + } +} + +extern "C" +JNIEXPORT void JNICALL +Java_com_topjohnwu_procgate_MainActivity_inspectProcFS(JNIEnv *env, jobject _this) { + gEnv = env; + gThis = _this; + jclass clazz = env->GetObjectClass(_this); + jAddText = env->GetMethodID(clazz, "addText", "(Ljava/lang/String;)V"); + + pid_t pid = getpid(); + DIR *procfs = opendir("/proc"); + struct dirent *dir; + haveLeak = false; + while ((dir = readdir(procfs))) { + if (isDigit(dir->d_name) && atoi(dir->d_name) != pid) + tryOpen(dir->d_name); + } + if (!haveLeak) + addText("No leaks detected!\n"); + closedir(procfs); +} diff --git a/app/src/main/java/com/topjohnwu/procgate/MainActivity.java b/app/src/main/java/com/topjohnwu/procgate/MainActivity.java new file mode 100644 index 0000000..a60260c --- /dev/null +++ b/app/src/main/java/com/topjohnwu/procgate/MainActivity.java @@ -0,0 +1,88 @@ +package com.topjohnwu.procgate; + +import android.app.Activity; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.io.SuFile; +import com.topjohnwu.superuser.io.SuFileOutputStream; + +import java.io.IOException; + +import androidx.annotation.Keep; + +public class MainActivity extends Activity { + + // Used to load the 'native-lib' library on application startup. + static { + System.loadLibrary("native-lib"); + } + + private TextView text; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main_layout); + text = findViewById(R.id.text); + text.setMovementMethod(new ScrollingMovementMethod()); + text.setHorizontallyScrolling(true); + } + + @Keep + private void addText(String s) { + text.append(s); + } + + /** + * A native method that is implemented by the 'native-lib' native library, + * which is packaged with this application. + */ + public native void inspectProcFS(); + + public void onClick(View view) { + text.setText(""); + inspectProcFS(); + } + + public void remount(View view) { + if (!Shell.rootAccess()) { + Toast.makeText(this, "No root access detected", Toast.LENGTH_SHORT).show(); + } else { + if (Shell.su("mount -o remount,hidepid=2,gid=3009 /proc").exec().isSuccess()) + Toast.makeText(this, "Remount success", Toast.LENGTH_SHORT).show(); + else + Toast.makeText(this, "Remount failed", Toast.LENGTH_SHORT).show(); + } + } + + public void inject(View view) { + if (!Shell.rootAccess()) + Toast.makeText(this, "No root access detected", Toast.LENGTH_SHORT).show(); + else { + SuFile dir = new SuFile("/sbin/.core/img/.core/post-fs-data.d"); + if (!dir.exists()) + dir = new SuFile("/su/su.d"); + if (!dir.exists()) + Toast.makeText(this, "Cannot find location to place boot scripts", + Toast.LENGTH_SHORT).show(); + else { + SuFile script = new SuFile(dir.getPath(), "procfix.sh"); + try (SuFileOutputStream out = new SuFileOutputStream(script)) { + out.write("#!/system/bin/sh\n".getBytes()); + out.write("mount -o remount,hidepid=2,gid=3009 /proc\n".getBytes()); + } catch (IOException e) { + Toast.makeText(this, "Script addition failed", Toast.LENGTH_SHORT).show(); + e.printStackTrace(); + return; + } + Toast.makeText(this, "Script added", Toast.LENGTH_SHORT).show(); + script.setExecutable(true, false); + } + } + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/main_layout.xml b/app/src/main/res/layout/main_layout.xml new file mode 100644 index 0000000..9bd496c --- /dev/null +++ b/app/src/main/res/layout/main_layout.xml @@ -0,0 +1,37 @@ + + + +