Skip to content

Commit

Permalink
feat: 文件结构调整
Browse files Browse the repository at this point in the history
  • Loading branch information
unkn0wName authored and unkn0wName committed Aug 17, 2023
1 parent 646699d commit e6be9cc
Show file tree
Hide file tree
Showing 29 changed files with 580 additions and 868 deletions.
6 changes: 3 additions & 3 deletions PermissionManager/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ add_library( # Sets the name of the library.
SHARED

# Provides a relative path to your source file(s).
../../../../../testRoot/process64_inject.cpp
../../../../../testRoot/ptrace_arm64_utils.cpp
../../../../../testRoot/su_install_helper.cpp
../../../../../testRoot/kernel_root_kit/kernel_root_kit_process64_inject.cpp
../../../../../testRoot/kernel_root_kit/kernel_root_kit_ptrace_arm64_utils.cpp
../../../../../testRoot/kernel_root_kit/kernel_root_kit_su_install_helper.cpp
../../../../../testRoot/base64.cpp
root.cpp)

Expand Down
22 changes: 9 additions & 13 deletions PermissionManager/app/src/main/cpp/root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
#include <thread>
#include <sys/capability.h>

#include "../../../../../testRoot/testRoot.h"
#include "../../../../../testRoot/kernel_root_helper.h"
#include "../../../../../testRoot/process64_inject.h"
#include "../../../../../testRoot/init64_process_helper.h"
#include "../../../../../testRoot/su_install_helper.h"
#include "../../../../../testRoot/kernel_root_kit/kernel_root_kit_umbrella.h"

using namespace std;

Expand Down Expand Up @@ -76,7 +72,7 @@ Java_com_linux_permissionmanager_MainActivity_testRoot(
env->ReleaseStringUTFChars(rootKey, str1);

std::string result;
fork_pipe_info finfo;
kernel_root::fork_pipe_info finfo;
ssize_t err = 0;
if(fork_pipe_child_process(finfo)) {
err = kernel_root::get_root(strRootKey.c_str());
Expand Down Expand Up @@ -144,7 +140,7 @@ Java_com_linux_permissionmanager_MainActivity_runInit64ProcessCmd(


ssize_t err;
string result = safe_run_init64_cmd_wrapper(strRootKey.c_str(), strCmd.c_str(), err);
string result = kernel_root::safe_run_init64_cmd_wrapper(strRootKey.c_str(), strCmd.c_str(), err);

stringstream sstr;
sstr << "runInit64Cmd err:" << err << ", result:" << result;
Expand Down Expand Up @@ -174,7 +170,7 @@ Java_com_linux_permissionmanager_MainActivity_installSu(
stringstream sstr;
//安装su工具套件
ssize_t err;
std::string su_hide_full_path = safe_install_su(strRootKey.c_str(), strBasePath.c_str(), strOriginSuFullPath.c_str(), err);
std::string su_hide_full_path = kernel_root::safe_install_su(strRootKey.c_str(), strBasePath.c_str(), strOriginSuFullPath.c_str(), err);
sstr << "install su err:" << err<<", su_hide_full_path:" << su_hide_full_path << std::endl;
g_last_su_full_path = su_hide_full_path;
if (err == 0) {
Expand Down Expand Up @@ -207,7 +203,7 @@ Java_com_linux_permissionmanager_MainActivity_uninstallSu(

stringstream sstr;

ssize_t err = safe_uninstall_su(strRootKey.c_str(), strBasePath.c_str());
ssize_t err = kernel_root::safe_uninstall_su(strRootKey.c_str(), strBasePath.c_str());
sstr << "uninstallSu err:" << err << std::endl;
if (err != 0) {
return env->NewStringUTF(sstr.str().c_str());
Expand Down Expand Up @@ -239,21 +235,21 @@ Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject(

//杀光所有历史进程
std::vector<pid_t> vOut;
ssize_t err = safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), vOut);
ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), vOut);
sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<<vOut.size() << std::endl;
if (err != 0) {
return env->NewStringUTF(sstr.str().c_str());
}
std::string kill_cmd;
for (pid_t t : vOut) {
err = safe_kill_process(strRootKey.c_str(), t);
err = kernel_root::safe_kill_process(strRootKey.c_str(), t);
sstr << "kill_ret err:"<< err << std::endl;
if (err != 0) {
return env->NewStringUTF(sstr.str().c_str());
}
}
pid_t pid;
err = safe_wait_and_find_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), 60*1000, pid);
err = kernel_root::safe_wait_and_find_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), 60*1000, pid);

std::string folder_path = g_last_su_full_path;
int n = folder_path.find_last_of("/");
Expand All @@ -264,7 +260,7 @@ Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject(
if (err != 0) {
return env->NewStringUTF(sstr.str().c_str());
}
err = safe_inject_process_env64_PATH_wrapper(strRootKey.c_str(), pid, folder_path.c_str());
err = kernel_root::safe_inject_process_env64_PATH_wrapper(strRootKey.c_str(), pid, folder_path.c_str());
sstr << "autoSuEnvInject ret val:" << err << std::endl;
if (err != 0) {
return env->NewStringUTF(sstr.str().c_str());
Expand Down
4 changes: 1 addition & 3 deletions su/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_CPPFLAGS += -std=c++17
LOCAL_CFLAGS += -fPIE
LOCAL_CFLAGS += -fvisibility=hidden
LOCAL_CPPFLAGS += -std=c++17 -fPIE -fvisibility=hidden -frtti -fexceptions
LOCAL_LDFLAGS += -fPIE -pie
LOCAL_DISABLE_FATAL_LINKER_WARNINGS := true
LOCAL_MODULE := su
Expand Down
25 changes: 17 additions & 8 deletions su/su.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include "su.h"
#include "log.h"
#include "su_hide_path_utils.h"
#include "../testRoot/kernel_root_helper.h"
#include "../testRoot/myself_path_utils.h"
#include "../testRoot/kernel_root_kit/kernel_root_kit_command.h"

namespace {
std::string get_executable_directory() {
char processdir[4096] = { 0 }; // Consider using PATH_MAX from limits.h
ssize_t path_len = readlink("/proc/self/exe", processdir, sizeof(processdir));
if(path_len > 0) {
char* path_end = strrchr(processdir, '/');
if(path_end) {
*path_end = '\0';
return std::string(processdir);
}
}
return {};
}

/*
* Bionic's atoi runs through strtol().
* Use our own implementation for faster conversion.
Expand Down Expand Up @@ -55,11 +67,8 @@ void usage(int status) {
}

static inline std::string get_root_key() {
char myself_path[1024] = { 0 };
char processname[1024];
get_executable_path(myself_path, processname, sizeof(myself_path));
TRACE("su start: my directory:%s, processname:%s\n", myself_path, processname);
std::string str_root_key = parse_root_key_by_myself_path(myself_path);
std::string myself_path = get_executable_directory();
std::string str_root_key = kernel_root::parse_root_key_by_myself_path(myself_path.c_str());
return str_root_key;
}

Expand Down Expand Up @@ -167,7 +176,7 @@ int su_client_main(int argc, char* argv[]) {
new_argv[1] = "-c";
new_argv[2] = su_req.command.data();
}

// If you need it, you can unblock this line of code yourself
//set_identity(su_req.uid);

Expand Down
98 changes: 0 additions & 98 deletions su/su.vcxproj

This file was deleted.

27 changes: 0 additions & 27 deletions su/su.vcxproj.filters

This file was deleted.

4 changes: 0 additions & 4 deletions su/su.vcxproj.user

This file was deleted.

5 changes: 3 additions & 2 deletions su/su_hide_path_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
#include <time.h>
#include "base64.h"
#include "log.h"
#include "../testRoot/random_utils.h"
#include "../testRoot/kernel_root_kit/kernel_root_kit_random.h"

#define RANDOM_GUID_LEN 10
#define ROOT_KEY_LEN 48

namespace kernel_root {
namespace {
namespace __private {

Expand Down Expand Up @@ -196,5 +197,5 @@ static inline std::string parse_root_key_by_myself_path(const char* myself_path)
}
return decodeRootKey.substr(decodeRootKey.length() - ROOT_KEY_LEN);
}

}
#endif /* _SU_HIDDEN_FOLDER_PATH_UTILS_H_ */
10 changes: 5 additions & 5 deletions testRoot/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_CPPFLAGS += -std=c++17 -fPIE -frtti -fvisibility=hidden
LOCAL_CPPFLAGS += -std=c++17 -fPIE -fvisibility=hidden -frtti -fexceptions
LOCAL_LDFLAGS += -fPIE -pie
LOCAL_DISABLE_FATAL_LINKER_WARNINGS := true
LOCAL_MODULE := testRoot.out
LOCAL_MODULE := testRoot
LOCAL_SRC_FILES := \
../testRoot.cpp \
../base64.cpp \
../process64_inject.cpp \
../ptrace_arm64_utils.cpp \
../su_install_helper.cpp
../kernel_root_kit/kernel_root_kit_process64_inject.cpp \
../kernel_root_kit/kernel_root_kit_ptrace_arm64_utils.cpp \
../kernel_root_kit/kernel_root_kit_su_install_helper.cpp

include $(BUILD_EXECUTABLE)
4 changes: 0 additions & 4 deletions testRoot/kernel_root_key.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef KERNEL_ROOT_HELPER_H_
#define KERNEL_ROOT_HELPER_H_
#ifndef _KERNEL_ROOT_KIT_COMMAND_H_
#define _KERNEL_ROOT_KIT_COMMAND_H_

#ifdef __linux__
#include <iostream>
#include <stdio.h>
#include <stdint.h>
Expand All @@ -16,18 +15,19 @@
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "safe_fork_helper.h"
#include "kernel_root_kit_fork_helper.h"

namespace kernel_root {

//获取ROOT权限,返回值为0则代表成功
static inline ssize_t get_root(const char* str_root_key) {
if (str_root_key == NULL) { return -100; }
syscall(__NR_execve, str_root_key, NULL, NULL);
if(getuid() != 0) { return -101; }
return 0;
}

//是否启用SELinux
//检查系统SELinux的是否为禁用状态
static bool is_enable_selinux() {
int cnt = 0;
DIR* dir = opendir("/");
Expand Down Expand Up @@ -101,8 +101,4 @@ namespace kernel_root {
return result;
}
}

#endif /*__linux__*/


#endif /* KERNEL_ROOT_HELPER_H_ */
#endif /* _KERNEL_ROOT_KIT_COMMAND_H_ */
Loading

0 comments on commit e6be9cc

Please sign in to comment.