|
7 | 7 | #include <thread> |
8 | 8 | #include <sys/capability.h> |
9 | 9 |
|
10 | | -#include "../../../../../testRoot/kernel_root_kit/kernel_root_kit_umbrella.h" |
11 | | - |
| 10 | +#include "../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_umbrella.h" |
| 11 | +#include "../../../../../testRoot/jni/testRoot.h" |
| 12 | +#include "urlEncodeUtils.h" |
12 | 13 | using namespace std; |
13 | 14 |
|
14 | 15 | std::string g_last_su_full_path; |
15 | 16 |
|
16 | | -string getCapabilityInfo() |
17 | | -{ |
18 | | - __uid_t now_uid, now_euid, now_suid; |
19 | | - if (getresuid(&now_uid, &now_euid, &now_suid)) { |
20 | | - return "FAILED getresuid()"; |
21 | | - } |
22 | | - |
23 | | - |
24 | | - __gid_t now_gid, now_egid, now_sgid; |
25 | | - if (getresgid(&now_gid, &now_egid, &now_sgid)) { |
26 | | - return "FAILED getresgid()"; |
27 | | - } |
28 | | - |
29 | | - stringstream sstrCapInfo; |
30 | | - sstrCapInfo<< "Current process information:\n"; |
31 | | - sstrCapInfo<< "uid:"<<now_uid <<"," << std::endl <<"euid:"<< now_euid <<"," << std::endl |
32 | | - <<"suid:"<< now_suid <<"," << std::endl <<"gid:"<< now_gid <<"," << std::endl |
33 | | - <<"egid:"<< now_egid <<"," << std::endl <<"sgid:"<< now_sgid <<"\n"; |
34 | | - |
35 | | - struct __user_cap_header_struct cap_header_data; |
36 | | - cap_user_header_t cap_header = &cap_header_data; |
37 | | - |
38 | | - struct __user_cap_data_struct cap_data_data; |
39 | | - cap_user_data_t cap_data = &cap_data_data; |
40 | | - |
41 | | - cap_header->pid = getpid(); |
42 | | - cap_header->version = _LINUX_CAPABILITY_VERSION_3; //_1、_2、_3 |
43 | | - |
44 | | - if (capget(cap_header, cap_data) < 0) { |
45 | | - return "FAILED capget()"; |
46 | | - // perror("FAILED capget()"); |
47 | | - //exit(1); |
48 | | - } |
49 | | - sstrCapInfo << "cap effective:" << hex <<cap_data->effective << "," << std::endl |
50 | | - <<"cap permitted:"<< hex << cap_data->permitted<< "," << std::endl |
51 | | - <<"cap inheritable:"<< hex <<cap_data->inheritable<< std::endl; |
52 | | - FILE * fp = popen("getenforce", "r"); |
53 | | - if (fp) |
54 | | - { |
55 | | - char cmd[512] = { 0 }; |
56 | | - fread(cmd, 1, sizeof(cmd), fp); |
57 | | - pclose(fp); |
58 | | - |
59 | | - sstrCapInfo<< "read system SELinux status:"<< cmd; |
60 | | - } |
61 | | - |
62 | | - return sstrCapInfo.str(); |
63 | | -} |
64 | | - |
65 | 17 | extern "C" JNIEXPORT jstring JNICALL |
66 | 18 | Java_com_linux_permissionmanager_MainActivity_testRoot( |
67 | 19 | JNIEnv* env, |
@@ -80,11 +32,11 @@ Java_com_linux_permissionmanager_MainActivity_testRoot( |
80 | 32 | result += std::to_string(err); |
81 | 33 | result += "\n\n"; |
82 | 34 | if(err == 0) { |
83 | | - result += getCapabilityInfo(); |
| 35 | + result += get_capability_info(); |
84 | 36 | result += "\n\n"; |
85 | 37 | } |
86 | | - write_errcode_to_father(finfo, err); |
87 | | - write_string_to_father(finfo, result); |
| 38 | + write_errcode_from_child(finfo, err); |
| 39 | + write_string_from_child(finfo, result); |
88 | 40 | _exit(0); |
89 | 41 | return 0; |
90 | 42 | } |
@@ -152,8 +104,7 @@ Java_com_linux_permissionmanager_MainActivity_installSu( |
152 | 104 | JNIEnv* env, |
153 | 105 | jobject /* this */, |
154 | 106 | jstring rootKey, |
155 | | - jstring basePath, |
156 | | - jstring originSuFullPath) { |
| 107 | + jstring basePath) { |
157 | 108 |
|
158 | 109 | const char *str1 = env->GetStringUTFChars(rootKey, 0); |
159 | 110 | string strRootKey= str1; |
@@ -163,14 +114,10 @@ Java_com_linux_permissionmanager_MainActivity_installSu( |
163 | 114 | string strBasePath= str1; |
164 | 115 | env->ReleaseStringUTFChars(basePath, str1); |
165 | 116 |
|
166 | | - str1 = env->GetStringUTFChars(originSuFullPath, 0); |
167 | | - string strOriginSuFullPath= str1; |
168 | | - env->ReleaseStringUTFChars(originSuFullPath, str1); |
169 | | - |
170 | 117 | stringstream sstr; |
171 | 118 | //安装su工具套件 |
172 | 119 | ssize_t err; |
173 | | - std::string su_hide_full_path = kernel_root::safe_install_su(strRootKey.c_str(), strBasePath.c_str(), strOriginSuFullPath.c_str(), err); |
| 120 | + std::string su_hide_full_path = kernel_root::safe_install_su(strRootKey.c_str(), strBasePath.c_str(), err); |
174 | 121 | sstr << "install su err:" << err<<", su_hide_full_path:" << su_hide_full_path << std::endl; |
175 | 122 | g_last_su_full_path = su_hide_full_path; |
176 | 123 | if (err == 0) { |
@@ -234,14 +181,14 @@ Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject( |
234 | 181 | stringstream sstr; |
235 | 182 |
|
236 | 183 | //杀光所有历史进程 |
237 | | - std::vector<pid_t> vOut; |
238 | | - ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), vOut); |
239 | | - sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<<vOut.size() << std::endl; |
| 184 | + std::set<pid_t> out; |
| 185 | + ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), out); |
| 186 | + sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<<out.size() << std::endl; |
240 | 187 | if (err != 0) { |
241 | 188 | return env->NewStringUTF(sstr.str().c_str()); |
242 | 189 | } |
243 | 190 | std::string kill_cmd; |
244 | | - for (pid_t t : vOut) { |
| 191 | + for (pid_t t : out) { |
245 | 192 | err = kernel_root::safe_kill_process(strRootKey.c_str(), t); |
246 | 193 | sstr << "kill_ret err:"<< err << std::endl; |
247 | 194 | if (err != 0) { |
@@ -268,3 +215,130 @@ Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject( |
268 | 215 | sstr << "autoSuEnvInject done."; |
269 | 216 | return env->NewStringUTF(sstr.str().c_str()); |
270 | 217 | } |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +extern "C" JNIEXPORT jstring JNICALL |
| 222 | +Java_com_linux_permissionmanager_MainActivity_getAllCmdlineProcess( |
| 223 | + JNIEnv* env, |
| 224 | + jobject /* this */, |
| 225 | + jstring rootKey) { |
| 226 | + std::stringstream ss; |
| 227 | + |
| 228 | + const char *str1 = env->GetStringUTFChars(rootKey, 0); |
| 229 | + std::string strRootKey = str1; |
| 230 | + env->ReleaseStringUTFChars(rootKey, str1); |
| 231 | + |
| 232 | + std::map<pid_t, std::string> pid_map; |
| 233 | + ssize_t err = kernel_root::safe_get_all_cmdline_process(strRootKey.c_str(), pid_map); |
| 234 | + if(err != 0) { |
| 235 | + ss << "get_all_cmdline_process err:"<< err<< std::endl; |
| 236 | + return env->NewStringUTF(ss.str().c_str()); |
| 237 | + } |
| 238 | + |
| 239 | + ss << "{\"data\":["; |
| 240 | + for (auto iter = pid_map.begin(); iter != pid_map.end(); ) { |
| 241 | + size_t len = iter->second.length(); |
| 242 | + size_t max_encoded_len = 3 * len + 1; |
| 243 | + shared_ptr<char> spData(new (std::nothrow) char[max_encoded_len], std::default_delete<char[]>()); |
| 244 | + memset(spData.get(), 0, max_encoded_len); |
| 245 | + url_encode(const_cast<char*>(iter->second.c_str()), spData.get()); |
| 246 | + ss << "{\"" << iter->first << "\":\"" << spData.get() << "\"}"; |
| 247 | + if (++iter != pid_map.end()) { |
| 248 | + ss << ","; |
| 249 | + } |
| 250 | + } |
| 251 | + ss << "]}"; |
| 252 | + return env->NewStringUTF(ss.str().c_str()); |
| 253 | +} |
| 254 | + |
| 255 | +extern "C" JNIEXPORT jstring JNICALL |
| 256 | +Java_com_linux_permissionmanager_MainActivity_parasitePrecheckApp( |
| 257 | + JNIEnv* env, |
| 258 | + jobject /* this */, |
| 259 | + jstring rootKey, |
| 260 | + jstring targetProcessCmdline) { |
| 261 | + |
| 262 | + const char *str1 = env->GetStringUTFChars(rootKey, 0); |
| 263 | + string strRootKey= str1; |
| 264 | + env->ReleaseStringUTFChars(rootKey, str1); |
| 265 | + |
| 266 | + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); |
| 267 | + string strTargetProcessCmdline = str1; |
| 268 | + env->ReleaseStringUTFChars(targetProcessCmdline, str1); |
| 269 | + |
| 270 | + stringstream sstr; |
| 271 | + std::set<pid_t> test_pid; |
| 272 | + ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), test_pid); |
| 273 | + if (err != 0) { |
| 274 | + sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<< test_pid.size() << std::endl; |
| 275 | + return env->NewStringUTF(sstr.str().c_str()); |
| 276 | + } |
| 277 | + if (test_pid.size() == 0) { |
| 278 | + sstr << "目标进程不存在" << std::endl; |
| 279 | + return env->NewStringUTF(sstr.str().c_str()); |
| 280 | + } |
| 281 | + |
| 282 | + std::set<std::string> so_path_list; |
| 283 | + err = kernel_root::safe_parasite_precheck_app(strRootKey.c_str(), strTargetProcessCmdline.c_str(), so_path_list); |
| 284 | + if (err) { |
| 285 | + sstr << "parasite_precheck_app ret val:" << err << std::endl; |
| 286 | + if(err == -9903) { |
| 287 | + sstr << "此目标APP为32位应用,无法寄生" << err << std::endl; |
| 288 | + } |
| 289 | + return env->NewStringUTF(sstr.str().c_str()); |
| 290 | + } |
| 291 | + |
| 292 | + if (!so_path_list.size()) { |
| 293 | + sstr << "目标APP无法寄生:无法检测到目标APP的JNI环境,您可尝试重启目标APP后再试" << std::endl; |
| 294 | + return env->NewStringUTF(sstr.str().c_str()); |
| 295 | + } |
| 296 | + |
| 297 | + sstr << "{\"soPaths\":["; |
| 298 | + for (auto iter = so_path_list.begin(); iter != so_path_list.end(); ) { |
| 299 | + size_t len = iter->length(); |
| 300 | + size_t max_encoded_len = 3 * len + 1; |
| 301 | + std::shared_ptr<char> spData(new (std::nothrow) char[max_encoded_len], std::default_delete<char[]>()); |
| 302 | + memset(spData.get(), 0, max_encoded_len); |
| 303 | + url_encode(const_cast<char*>(iter->c_str()), spData.get()); |
| 304 | + sstr << "\"" << spData.get() << "\""; |
| 305 | + if (++iter != so_path_list.end()) { |
| 306 | + sstr << ","; |
| 307 | + } |
| 308 | + } |
| 309 | + sstr << "]}"; |
| 310 | + return env->NewStringUTF(sstr.str().c_str()); |
| 311 | + |
| 312 | +} |
| 313 | + |
| 314 | +extern "C" JNIEXPORT jstring JNICALL |
| 315 | +Java_com_linux_permissionmanager_MainActivity_parasiteImplantApp( |
| 316 | + JNIEnv* env, |
| 317 | + jobject /* this */, |
| 318 | + jstring rootKey, |
| 319 | + jstring targetProcessCmdline, |
| 320 | + jstring targetSoFullPath) { |
| 321 | + |
| 322 | + const char *str1 = env->GetStringUTFChars(rootKey, 0); |
| 323 | + string strRootKey= str1; |
| 324 | + env->ReleaseStringUTFChars(rootKey, str1); |
| 325 | + |
| 326 | + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); |
| 327 | + string strTargetProcessCmdline = str1; |
| 328 | + env->ReleaseStringUTFChars(targetProcessCmdline, str1); |
| 329 | + |
| 330 | + str1 = env->GetStringUTFChars(targetSoFullPath, 0); |
| 331 | + string strTargetSoFullPath = str1; |
| 332 | + env->ReleaseStringUTFChars(targetSoFullPath, str1); |
| 333 | + |
| 334 | + stringstream sstr; |
| 335 | + ssize_t err = kernel_root::safe_parasite_implant_app(strRootKey.c_str(), strTargetProcessCmdline.c_str(), strTargetSoFullPath.c_str()); |
| 336 | + if (err != 0) { |
| 337 | + sstr << "parasite_implant_app err:"<< err << std::endl; |
| 338 | + return env->NewStringUTF(sstr.str().c_str()); |
| 339 | + } |
| 340 | + sstr << "parasiteImplantApp done."; |
| 341 | + return env->NewStringUTF(sstr.str().c_str()); |
| 342 | + |
| 343 | +} |
| 344 | + |