|
4 | 4 | #include <string.h> |
5 | 5 | #include <unistd.h> |
6 | 6 | #include <sstream> |
| 7 | +#include <thread> |
7 | 8 | #include <sys/capability.h> |
8 | 9 |
|
9 | | -#include "../../../../../testRoot/super_root.h" |
10 | | -#include "../../../../../testRoot/adb_inject.h" |
| 10 | +#include "../../../../../testRoot/testRoot.h" |
| 11 | +#include "../../../../../testRoot/kernel_root_helper.h" |
| 12 | +#include "../../../../../testRoot/process64_inject.h" |
| 13 | +#include "../../../../../testRoot/adb64_helper.h" |
| 14 | +#include "../../../../../testRoot/su_install_helper.h" |
11 | 15 |
|
12 | 16 | using namespace std; |
13 | 17 | string getCapabilityInfo() |
14 | 18 | { |
| 19 | + __uid_t now_uid, now_euid, now_suid; |
| 20 | + if (getresuid(&now_uid, &now_euid, &now_suid)) { |
| 21 | + return "FAILED getresuid()"; |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + __gid_t now_gid, now_egid, now_sgid; |
| 26 | + if (getresgid(&now_gid, &now_egid, &now_sgid)) { |
| 27 | + return "FAILED getresgid()"; |
| 28 | + } |
| 29 | + |
| 30 | + stringstream sstrCapInfo; |
| 31 | + sstrCapInfo<< "now_uid="<<now_uid <<", now_euid="<< now_euid <<", now_suid="<< now_suid <<", now_gid="<< now_gid <<", now_egid="<< now_egid << ", now_sgid="<< now_sgid <<"\n"; |
| 32 | + |
15 | 33 | struct __user_cap_header_struct cap_header_data; |
16 | 34 | cap_user_header_t cap_header = &cap_header_data; |
17 | 35 |
|
@@ -26,9 +44,7 @@ string getCapabilityInfo() |
26 | 44 | // perror("FAILED capget()"); |
27 | 45 | //exit(1); |
28 | 46 | } |
29 | | - stringstream sstrCapInfo; |
30 | 47 | sstrCapInfo << "Cap data effective:"<<(uint64_t *) cap_data->effective<<", permitted:"<<(uint64_t *) cap_data->permitted<<", inheritable:"<<(uint64_t *) cap_data->inheritable<<"\n"; |
31 | | - sstrCapInfo << "now getuid()="<< getuid() <<",geteuid()="<< geteuid() <<",getgid()="<< getgid() <<",getegid()="<< getegid() << "\n"; |
32 | 48 |
|
33 | 49 | FILE * fp = popen("getenforce", "r"); |
34 | 50 | if (fp) |
@@ -47,6 +63,7 @@ extern "C" JNIEXPORT jstring JNICALL |
47 | 63 | Java_com_linux_permissionmanager_MainActivity_getCapabilityInfo( |
48 | 64 | JNIEnv* env, |
49 | 65 | jobject /* this */) { |
| 66 | + |
50 | 67 | return env->NewStringUTF(getCapabilityInfo().c_str()); |
51 | 68 | } |
52 | 69 |
|
@@ -74,7 +91,7 @@ Java_com_linux_permissionmanager_MainActivity_enableSElinux( |
74 | 91 | } |
75 | 92 |
|
76 | 93 | extern "C" JNIEXPORT jstring JNICALL |
77 | | -Java_com_linux_permissionmanager_MainActivity_runRootCmd( |
| 94 | +Java_com_linux_permissionmanager_MainActivity_runNormalCmd( |
78 | 95 | JNIEnv* env, |
79 | 96 | jobject /* this */, |
80 | 97 | jlong rootKey, |
@@ -85,28 +102,158 @@ Java_com_linux_permissionmanager_MainActivity_runRootCmd( |
85 | 102 | env->ReleaseStringUTFChars(cmd, str1); |
86 | 103 |
|
87 | 104 | char szResult[0x1000] = {0}; |
88 | | - ssize_t ret = safe_run_root_cmd(rootKey, strCmd.c_str(), szResult, sizeof(szResult)); |
| 105 | + ssize_t ret = safe_run_normal_cmd(rootKey, strCmd.c_str(), szResult, sizeof(szResult)); |
89 | 106 | stringstream sstr; |
90 | 107 | sstr << "runRootCmd ret val:" << ret << ", result:" << szResult; |
91 | 108 | return env->NewStringUTF(sstr.str().c_str()); |
92 | 109 | } |
93 | 110 |
|
94 | 111 | extern "C" JNIEXPORT jstring JNICALL |
95 | | -Java_com_linux_permissionmanager_MainActivity_runAdbShell( |
| 112 | +Java_com_linux_permissionmanager_MainActivity_runRootCmd( |
96 | 113 | JNIEnv* env, |
97 | 114 | jobject /* this */, |
98 | 115 | jlong rootKey, |
99 | | - jstring shell, |
100 | | - jboolean keepAdbRoot) { |
| 116 | + jstring cmd) { |
101 | 117 |
|
102 | | - const char *str1 = env->GetStringUTFChars(shell, 0); |
103 | | - string strShell= str1; |
104 | | - env->ReleaseStringUTFChars(shell, str1); |
| 118 | + const char *str1 = env->GetStringUTFChars(cmd, 0); |
| 119 | + string strCmd= str1; |
| 120 | + env->ReleaseStringUTFChars(cmd, str1); |
105 | 121 |
|
| 122 | + stringstream sstr; |
106 | 123 | char szResult[0x1000] = {0}; |
107 | | - ssize_t inject = safe_inject_adb_process_run_shell_wrapper(rootKey, strShell.c_str(), keepAdbRoot, szResult, sizeof(szResult)); |
108 | | - //ssize_t inject = safe_inject_adb_process_run_shell_wrapper(rootKey, strShell.c_str(), NULL, 0); |
| 124 | + ssize_t inject = safe_inject_adbd64_run_cmd_wrapper(rootKey, strCmd.c_str(), szResult, sizeof(szResult)); |
| 125 | + //ssize_t inject = safe_inject_adbd64_run_cmd_wrapper(rootKey, strCmd.c_str(),NULL, 0); |
| 126 | + sstr << "runRootCmd ret val:" << inject << ", result:" << szResult; |
| 127 | + return env->NewStringUTF(sstr.str().c_str()); |
| 128 | +} |
| 129 | +extern "C" JNIEXPORT jstring JNICALL |
| 130 | +Java_com_linux_permissionmanager_MainActivity_adbRoot( |
| 131 | + JNIEnv* env, |
| 132 | + jobject /* this */, |
| 133 | + jlong rootKey) { |
| 134 | + |
| 135 | + stringstream sstr; |
| 136 | + char szResult[0x1000] = {0}; |
| 137 | + ssize_t inject = safe_inject_adbd64_run_cmd_wrapper(rootKey, "id", NULL, 0, false ,true ,false, false); |
| 138 | + sstr << "adbRoot ret val:" << inject << ", result:" << szResult; |
| 139 | + return env->NewStringUTF(sstr.str().c_str()); |
| 140 | +} |
| 141 | +extern "C" JNIEXPORT jstring JNICALL |
| 142 | +Java_com_linux_permissionmanager_MainActivity_installSuTools( |
| 143 | + JNIEnv* env, |
| 144 | + jobject /* this */, |
| 145 | + jlong rootKey, |
| 146 | + jstring basePath, |
| 147 | + jstring suToolsFilePath) { |
| 148 | + |
| 149 | + const char *str1 = env->GetStringUTFChars(basePath, 0); |
| 150 | + string strBasePath= str1; |
| 151 | + env->ReleaseStringUTFChars(basePath, str1); |
| 152 | + |
| 153 | + str1 = env->GetStringUTFChars(suToolsFilePath, 0); |
| 154 | + string strSuFilePath= str1; |
| 155 | + env->ReleaseStringUTFChars(suToolsFilePath, str1); |
| 156 | + |
| 157 | + stringstream sstr; |
| 158 | + |
| 159 | + //安装su工具套件 |
| 160 | + std::string su_hidden_path; |
| 161 | + int install_su_tools_ret = safe_install_su_tools(rootKey, strBasePath.c_str(), su_hidden_path); |
| 162 | + sstr << "install_su_tools ret val:" << install_su_tools_ret<<", su_hidden_path:" << su_hidden_path << std::endl; |
| 163 | + |
| 164 | + if (install_su_tools_ret == -504) { |
| 165 | + //需要补一下su文件的释放 |
| 166 | + std::string tmpCmd = "cp "+strSuFilePath + " " + su_hidden_path+"/su"; |
| 167 | + int cp_ret = safe_run_normal_cmd(rootKey, tmpCmd.c_str(), NULL, 0); |
| 168 | + if(cp_ret != 0) { |
| 169 | + sstr << "safe_run_normal_cmd cp_ret val:" <<cp_ret; |
| 170 | + return env->NewStringUTF(sstr.str().c_str()); |
| 171 | + } |
| 172 | + install_su_tools_ret = safe_install_su_tools(rootKey, strBasePath.c_str(), su_hidden_path); |
| 173 | + sstr << "install_su_tools ret val:" << install_su_tools_ret<<", su_hidden_path:" << su_hidden_path << std::endl; |
| 174 | + } |
| 175 | + if (install_su_tools_ret != 0) { |
| 176 | + return env->NewStringUTF(sstr.str().c_str()); |
| 177 | + } |
| 178 | + sstr << "installSuTools done."; |
| 179 | + return env->NewStringUTF(sstr.str().c_str()); |
| 180 | +} |
| 181 | +extern "C" JNIEXPORT jstring JNICALL |
| 182 | +Java_com_linux_permissionmanager_MainActivity_uninstallSuTools( |
| 183 | + JNIEnv* env, |
| 184 | + jobject /* this */, |
| 185 | + jlong rootKey, |
| 186 | + jstring basePath) { |
| 187 | + |
| 188 | + const char *str1 = env->GetStringUTFChars(basePath, 0); |
| 189 | + string strBasePath= str1; |
| 190 | + env->ReleaseStringUTFChars(basePath, str1); |
| 191 | + |
109 | 192 | stringstream sstr; |
110 | | - sstr << "runAdbShell ret val:" << inject << ", result:" << szResult; |
| 193 | + |
| 194 | + int uninstall_su_tools_ret = safe_uninstall_su_tools(rootKey, strBasePath.c_str()); |
| 195 | + sstr << "uninstallSuTools ret val:" << uninstall_su_tools_ret << std::endl; |
| 196 | + |
| 197 | + if (uninstall_su_tools_ret != 0) { |
| 198 | + return env->NewStringUTF(sstr.str().c_str()); |
| 199 | + } |
| 200 | + sstr << "uninstallSuTools done."; |
| 201 | + return env->NewStringUTF(sstr.str().c_str()); |
| 202 | +} |
| 203 | +extern "C" JNIEXPORT jstring JNICALL |
| 204 | +Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject( |
| 205 | + JNIEnv* env, |
| 206 | + jobject /* this */, |
| 207 | + jlong rootKey, |
| 208 | + jstring targetProcessCmdline, |
| 209 | + jstring basePath) { |
| 210 | + |
| 211 | + const char *str1 = env->GetStringUTFChars(targetProcessCmdline, 0); |
| 212 | + string strTargetProcessCmdline= str1; |
| 213 | + env->ReleaseStringUTFChars(targetProcessCmdline, str1); |
| 214 | + |
| 215 | + str1 = env->GetStringUTFChars(basePath, 0); |
| 216 | + string strBasePath= str1; |
| 217 | + env->ReleaseStringUTFChars(basePath, str1); |
| 218 | + |
| 219 | + stringstream sstr; |
| 220 | + |
| 221 | + //杀光所有历史进程 |
| 222 | + std::vector<pid_t> vOut; |
| 223 | + int find_all_cmdline_process_ret = safe_find_all_cmdline_process(rootKey, strTargetProcessCmdline.c_str(), vOut); |
| 224 | + sstr << "find_all_cmdline_process ret val:"<< find_all_cmdline_process_ret<<", cnt:"<<vOut.size() << std::endl; |
| 225 | + if (find_all_cmdline_process_ret != 0) { |
| 226 | + return env->NewStringUTF(sstr.str().c_str()); |
| 227 | + } |
| 228 | + std::string kill_cmd; |
| 229 | + for (pid_t t : vOut) { |
| 230 | + kill_cmd += "kill -9 "; |
| 231 | + kill_cmd += std::to_string(t); |
| 232 | + kill_cmd += ";"; |
| 233 | + } |
| 234 | + int kill_ret = run_normal_cmd(rootKey, kill_cmd.c_str()); |
| 235 | + sstr << "kill_ret ret val:"<< kill_ret << std::endl; |
| 236 | + if (kill_ret != 0) { |
| 237 | + return env->NewStringUTF(sstr.str().c_str()); |
| 238 | + } |
| 239 | + |
| 240 | + //注入su环境变量到指定进程 |
| 241 | + std::string su_hidden_path; |
| 242 | + int install_su_tools_ret = safe_install_su_tools(rootKey, strBasePath.c_str(), su_hidden_path); |
| 243 | + sstr << "install_su_tools ret val:" << install_su_tools_ret<<", su_hidden_path:" << su_hidden_path << std::endl; |
| 244 | + if (install_su_tools_ret != 0) { |
| 245 | + return env->NewStringUTF(sstr.str().c_str()); |
| 246 | + } |
| 247 | + |
| 248 | + int pid = safe_wait_and_find_cmdline_process(rootKey, strTargetProcessCmdline.c_str()); |
| 249 | + |
| 250 | + sstr << "autoSuEnvInject("<< pid<<", " << su_hidden_path<<")" << std::endl; |
| 251 | + ssize_t ret = safe_inject_process_env64_PATH_wrapper(rootKey, pid, su_hidden_path.c_str()); |
| 252 | + sstr << "autoSuEnvInject ret val:" << ret << std::endl; |
| 253 | + |
| 254 | + if (ret != 0) { |
| 255 | + return env->NewStringUTF(sstr.str().c_str()); |
| 256 | + } |
| 257 | + sstr << "autoSuEnvInject done."; |
111 | 258 | return env->NewStringUTF(sstr.str().c_str()); |
112 | 259 | } |