forked from abcz316/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsu_install_helper.cpp
More file actions
276 lines (232 loc) · 8.12 KB
/
Copy pathsu_install_helper.cpp
File metadata and controls
276 lines (232 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "su_install_helper.h"
#include "kernel_root_helper.h"
#include "init64_helper.h"
#include "testRoot.h"
#include "../su/su_hide_path_utils.h"
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <memory>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
/*
* xattr name for SELinux attributes.
* This may have been exported via Kernel uapi header.
*/
#ifndef XATTR_NAME_SELINUX
#define XATTR_NAME_SELINUX "security.selinux"
#endif
const char* selinux_file_flag = "u:object_r:system_file:s0";
const char* check_file_list[] = {
"su",
};
bool check_su_file_exist(const char* path) {
std::string str_path = path;
for (size_t i = 0; i < sizeof(check_file_list) / sizeof(check_file_list[0]); i++) {
if (access(std::string(str_path + "/" + check_file_list[i]).c_str(), F_OK)) {
TRACE("check_su_file_exist could not found %s.\n", check_file_list[i]);
return false;
}
}
return true;
}
bool set_su_file_access_mode(const char* path) {
std::string str_path = path;
if (setxattr(str_path.c_str(), XATTR_NAME_SELINUX, selinux_file_flag, strlen(selinux_file_flag) + 1, 0)) {
TRACE("setxattr error %s.\n", str_target_file_path.c_str());
return false;
}
for (size_t i = 0; i < sizeof(check_file_list) / sizeof(check_file_list[0]); i++) {
std::string str_target_file_path = std::string(str_path + "/" + check_file_list[i]);
if (chmod(str_target_file_path.c_str(), 0777)) {
TRACE("set_su_file_access_mode could not found %s.\n", check_file_list[i]);
return false;
}
if (setxattr(str_target_file_path.c_str(), XATTR_NAME_SELINUX, selinux_file_flag, strlen(selinux_file_flag) + 1, 0)) {
TRACE("setxattr error %s.\n", str_target_file_path.c_str());
return false;
}
}
return true;
}
bool move_su_file_to_su_hide_path(const char* source_path, const char* target_path) {
std::string str_source_path = source_path;
std::string str_target_path = target_path;
for (size_t i = 0; i < sizeof(check_file_list) / sizeof(check_file_list[0]); i++) {
std::string old_file_path = std::string(str_source_path + "/" + check_file_list[i]);
std::string new_file_path = std::string(str_target_path + "/" + check_file_list[i]);
if (access(old_file_path.c_str(), F_OK)) {
TRACE("move_su_file_to_su_hide_path could not found %s.\n", old_file_path.c_str());
return false;
}
std::fstream file1;
file1.open(old_file_path.c_str(), std::ios::binary | std::ios::in | std::ios::ate); //打开时指针在文件尾
if (!file1.is_open()) {
TRACE("Could not open file %s.\n", old_file_path.c_str());
return false;
}
size_t length = file1.tellg();
std::unique_ptr<char[]> up_new_file_data = std::make_unique<char[]>(length);
file1.seekg(0);
file1.read(up_new_file_data.get(), length); //二进制只能用这个读
file1.close();
std::fstream file2;
file2.open(new_file_path.c_str(), std::ios::binary | std::ios::out);
if (!file2.is_open()) {
TRACE("Could not open file %s.\n", new_file_path.c_str());
return false;
}
file2.write(up_new_file_data.get(), length); //二进制只能用这个写
file2.close();
if (chmod(new_file_path.c_str(), 0777)) {
TRACE("Could not chmod file %s.\n", new_file_path.c_str());
return false;
}
}
return true;
}
bool del_su_file(const char* path) {
std::string str_path = path;
for (size_t i = 0; i < sizeof(check_file_list) / sizeof(check_file_list[0]); i++) {
std::string file_path = std::string(str_path + "/" + check_file_list[i]);
TRACE("del_su_file:%s\n", file_path.c_str());
remove(file_path.c_str());
}
return true;
}
int install_su(const char* str_root_key, const char* base_path, std::string & su_hide_folder_path, const char* su_hide_folder_head_flag/* = "su"*/) {
if (kernel_root::get_root(str_root_key) != 0) {
return -501;
}
std::string _su_hide_folder_head_flag = su_hide_folder_head_flag;
_su_hide_folder_head_flag += "_";
//1.获取su_xxx隐藏目录
std::string _su_hide_folder_path = find_su_hide_folder_path(base_path, _su_hide_folder_head_flag.c_str()); //没有再看看子目录
if (_su_hide_folder_path.empty()) {
//2.取不到,那就创建一个
_su_hide_folder_path = create_su_hide_folder(str_root_key, base_path, _su_hide_folder_head_flag.c_str());
}
if (_su_hide_folder_path.empty()) {
TRACE("su hide folder path empty error\n");
return -503;
}
su_hide_folder_path = _su_hide_folder_path + "/";
//3.检查su_xxx目录下的文件是否齐全
if (!check_su_file_exist(_su_hide_folder_path.c_str())) {
//4.不齐全则开始补齐
if (!check_su_file_exist(base_path)) {
//自身目录都没有,怎么补过去
TRACE("base_path su file not exist:%s\n", base_path);
return -504;
}
//5.开始移动文件补齐到su_xxx目录
if (!move_su_file_to_su_hide_path(base_path, _su_hide_folder_path.c_str())) {
TRACE("move_su_file_to_su_hide_folder_path error:%s -> %s\n", base_path, _su_hide_path.c_str());
return -505;
}
}
//6.赋值文件运行权限
if(!set_su_file_access_mode(_su_hide_folder_path.c_str())) {
TRACE("set_su_file_access_mode error:%s\n", _su_hide_folder_path.c_str());
return -506;
}
//7.从自身路径中删除文件,移除痕迹,防止被检测
del_su_file(base_path);
return 0;
}
int safe_install_su(const char* str_root_key, const char* base_path, std::string & su_hide_folder_path, const char* su_hide_folder_head_flag) {
int fd[2];
if (pipe(fd)) {
return -432;
}
pid_t pid;
if ((pid = fork()) < 0) {
//fork error
return -433;
}
else if (pid == 0) { // child process
close(fd[0]); //close read pipe
pid_t ret = install_su(str_root_key, base_path, su_hide_folder_path, su_hide_folder_head_flag);
write(fd[1], &ret, sizeof(ret));
char buf[4096] = { 0 };
strcpy(buf, su_hide_folder_path.c_str());
write(fd[1], &buf, sizeof(buf));
close(fd[1]); //close write pipe
_exit(0);
}
else { // father process
close(fd[1]); //close write pipe
int status;
if (waitpid(pid, &status, WUNTRACED) < 0 && errno != EACCES) {
return -434;
}
pid_t ret = -435;
read(fd[0], (void*)&ret, sizeof(ret));
char buf[4096] = { 0 };
read(fd[0], (void*)&buf, sizeof(buf));
su_hide_folder_path = buf;
close(fd[0]); //close read pipe
return ret;
}
return -436;
}
int uninstall_su(const char* str_root_key, const char* base_path, const char* su_hide_folder_head_flag) {
if (kernel_root::get_root(str_root_key) != 0) {
return -511;
}
std::string _su_hide_folder_head_flag = su_hide_folder_head_flag;
_su_hide_folder_head_flag += "_";
//从自身路径中删除文件,移除痕迹,防止被检测
del_su_file(base_path);
do {
//获取su_xxx隐藏目录
std::string _su_hide_path = find_su_hide_folder_path(base_path, _su_hide_folder_head_flag.c_str()); //没有再看看子目录
if (_su_hide_path.empty()) {
break;
}
//取到了,再删
del_su_file(_su_hide_path.c_str());
//文件夹也删掉
std::string del_dir_cmd = "rm -rf ";
del_dir_cmd += _su_hide_path;
int err = kernel_root::run_root_cmd(str_root_key, del_dir_cmd.c_str(), NULL, 0);
if (err) {
return err;
}
return access(_su_hide_path.c_str(), F_OK) == -1 ? 0 : -512;
} while (1);
return 0;
}
int safe_uninstall_su(const char* str_root_key, const char* base_path, const char* su_hide_folder_head_flag) {
int fd[2];
if (pipe(fd)) {
return -521;
}
pid_t pid;
if ((pid = fork()) < 0) {
//fork error
return -522;
}
else if (pid == 0) { // child process
close(fd[0]); //close read pipe
int ret = uninstall_su(str_root_key, base_path, su_hide_folder_head_flag);
write(fd[1], &ret, sizeof(ret));
close(fd[1]); //close write pipe
_exit(0);
}
else { // father process
close(fd[1]); //close write pipe
int status;
if (waitpid(pid, &status, WUNTRACED) < 0 && errno != EACCES) {
return -523;
}
int ret = -524;
read(fd[0], (void*)&ret, sizeof(ret));
close(fd[0]); //close read pipe
return ret;
}
return -525;
}