|
8 | 8 | #include <sys/stat.h> |
9 | 9 | #include <sys/statvfs.h> |
10 | 10 | #include <sys/xattr.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <signal.h> |
| 13 | +#include <time.h> |
11 | 14 | #include <cerrno> |
12 | 15 | #include <cstring> |
| 16 | +#include <vector> |
13 | 17 |
|
14 | 18 | #include "kernel_module_kit_umbrella.h" |
15 | 19 |
|
@@ -62,8 +66,18 @@ static std::string read_quick_actions_json() { |
62 | 66 | return json; |
63 | 67 | } |
64 | 68 |
|
65 | | -static void create_work_dir(const char* module_private_dir) { |
66 | | - fs::path work_dir = fs::path(module_private_dir) / WORK_DIR_NAME; |
| 69 | +static void write_terminal_keep_mode(bool enabled) { |
| 70 | + kernel_module::write_bool_disk_storage("terminal_keep_mode", enabled); |
| 71 | +} |
| 72 | + |
| 73 | +static bool read_terminal_keep_mode() { |
| 74 | + bool enabled = false; |
| 75 | + kernel_module::read_bool_disk_storage("terminal_keep_mode", enabled); |
| 76 | + return enabled; |
| 77 | +} |
| 78 | + |
| 79 | +static void create_work_dir(const char* module_private_dir, const char* work_dir_name) { |
| 80 | + fs::path work_dir = fs::path(module_private_dir) / work_dir_name; |
67 | 81 | std::error_code ec; |
68 | 82 | fs::create_directories(work_dir, ec); |
69 | 83 | ::chmod(work_dir.c_str(), 0777); |
@@ -73,7 +87,7 @@ static void create_work_dir(const char* module_private_dir) { |
73 | 87 |
|
74 | 88 | std::string module_on_install(const char* root_key, const char* module_private_dir) { |
75 | 89 | add_quick_actions({"getenforce", "ls /", "id"}); |
76 | | - create_work_dir(module_private_dir); |
| 90 | + create_work_dir(module_private_dir, WORK_DIR_NAME); |
77 | 91 | return ""; |
78 | 92 | } |
79 | 93 |
|
@@ -91,19 +105,41 @@ static void wait_parent_exit(const std::string& root_key, pid_t ppid) { |
91 | 105 | } |
92 | 106 | } |
93 | 107 |
|
| 108 | + |
94 | 109 | // WebUI HTTP服务器回调函数 |
95 | 110 | class MyWebHttpHandler : public kernel_module::WebUIHttpHandler { |
96 | 111 | public: |
97 | 112 | void onPrepareCreate(const char* root_key, const char* module_private_dir, uint32_t port) override { |
98 | 113 | m_root_key = root_key; |
| 114 | + |
99 | 115 | m_hide_dir = (fs::path(module_private_dir) / WORK_DIR_NAME).string(); |
100 | | - m_su_interactive.start(); |
101 | | - fork_sh_process_daemon(); |
102 | | - m_idle_killer.start(std::chrono::seconds(20), [this] { |
| 116 | + m_keep_terminal = read_terminal_keep_mode(); |
| 117 | + m_su_interactive.start(module_private_dir); |
| 118 | + if (!m_keep_terminal) { |
| 119 | + fork_sh_process_daemon(); |
| 120 | + } |
| 121 | + m_idle_killer.start(std::chrono::seconds(20), [this]() -> bool { |
| 122 | + if (m_keep_terminal) { |
| 123 | + if (!m_su_interactive.isShellForeground()) { |
| 124 | + // 当前 shell 不在前台,说明可能有用户后台程序在跑。 |
| 125 | + // 这次不杀,IdleKiller 继续监控。 |
| 126 | + return false; |
| 127 | + } |
| 128 | + printf("[module_hide_sh_exec] shell foreground, idle kill!\n"); |
| 129 | + } else { |
| 130 | + printf("[module_hide_sh_exec] idle kill!\n"); |
| 131 | + } |
| 132 | + if (m_server) m_server->close(); |
| 133 | + m_su_interactive.stop(true); |
103 | 134 | _exit(0); |
| 135 | + return true; |
104 | 136 | }); |
105 | 137 | } |
106 | 138 |
|
| 139 | + void onServerCreated(CivetServer* server) override { |
| 140 | + m_server = server; |
| 141 | + } |
| 142 | + |
107 | 143 | bool handlePost(CivetServer* server, struct mg_connection* conn, const std::string& path, const std::string& body) override { |
108 | 144 | //printf("[module_hide_sh_exec] POST request\nPath: %s\nBody: %s\n", path.c_str(), body.c_str()); |
109 | 145 | std::string resp; |
@@ -116,6 +152,8 @@ class MyWebHttpHandler : public kernel_module::WebUIHttpHandler { |
116 | 152 | else if(path == "/getHideDir") resp = handle_get_hide_dir(); |
117 | 153 | else if(path == "/checkFileType") resp = handle_check_file_type(body); |
118 | 154 | else if(path == "/checkExecMount") resp = handle_check_exec_mount(body); |
| 155 | + else if(path == "/getTerminalKeepMode") resp = handle_get_terminal_keep_mode(); |
| 156 | + else if(path == "/saveTerminalKeepMode") resp = handle_save_terminal_keep_mode(body); |
119 | 157 | else if(path == "/exitWebui") resp = handle_exit_webui(); |
120 | 158 |
|
121 | 159 | kernel_module::webui::send_text(conn, 200, resp); |
@@ -127,11 +165,13 @@ class MyWebHttpHandler : public kernel_module::WebUIHttpHandler { |
127 | 165 | private: |
128 | 166 | void fork_sh_process_daemon() { |
129 | 167 | pid_t shell_pid = m_su_interactive.get_shell_pid(); |
| 168 | + if (shell_pid <= 0) return; |
| 169 | + |
130 | 170 | pid_t ppid = getpid(); |
131 | 171 | pid_t child = fork(); |
132 | 172 | if(child == 0) { |
133 | 173 | wait_parent_exit(m_root_key, ppid); |
134 | | - printf("[module_hide_sh_exec] kill sh!\n"); |
| 174 | + printf("[module_hide_sh_exec] kill sh: %d\n", shell_pid); |
135 | 175 | kill(shell_pid, SIGKILL); |
136 | 176 | _exit(0); |
137 | 177 | } |
@@ -213,23 +253,36 @@ class MyWebHttpHandler : public kernel_module::WebUIHttpHandler { |
213 | 253 | return is_noexec ? "can not exec" : "can exec"; |
214 | 254 | } |
215 | 255 |
|
| 256 | + std::string handle_get_terminal_keep_mode() { |
| 257 | + return read_terminal_keep_mode() ? "1" : "0"; |
| 258 | + } |
| 259 | + |
| 260 | + std::string handle_save_terminal_keep_mode(const std::string& body) { |
| 261 | + write_terminal_keep_mode(body == "1"); |
| 262 | + return "OK"; |
| 263 | + } |
| 264 | + |
216 | 265 | std::string handle_exit_webui() { |
217 | | - _exit(0); |
218 | | - return "OK"; |
| 266 | + if(m_server) m_server->close(); |
| 267 | + m_su_interactive.stop(true); |
| 268 | + _exit(0); |
| 269 | + return "OK"; |
219 | 270 | } |
220 | 271 | private: |
221 | 272 | std::string m_root_key; |
| 273 | + CivetServer* m_server = nullptr; |
222 | 274 | std::string m_hide_dir; |
| 275 | + bool m_keep_terminal = false; |
223 | 276 | SuInteractive m_su_interactive; |
224 | 277 | IdleKiller m_idle_killer; |
225 | 278 | }; |
226 | 279 |
|
227 | 280 | // SKRoot 模块名片 |
228 | 281 | // 字段说明见 module_descriptor.h |
229 | 282 | SKROOT_MODULE_NAME("隐蔽的系统终端") |
230 | | -SKROOT_MODULE_VERSION("3.0.8") |
| 283 | +SKROOT_MODULE_VERSION("3.1.0") |
231 | 284 | SKROOT_MODULE_DESC("提供独立隐蔽的 sh 执行通道,彻底替代终端类 App,避免终端类 App 带来的特征暴露。") |
232 | | -SKROOT_MODULE_AUTHOR("SKRoot") |
| 285 | +SKROOT_MODULE_AUTHOR("SKRoot、斓梦语") |
233 | 286 | SKROOT_MODULE_ID32("zse9vkTjLjWXbafvx8Mlh1MTf8SMTUEL") |
234 | 287 | SKROOT_MODULE_ON_INSTALL(module_on_install) |
235 | 288 | SKROOT_MODULE_WEB_UI(MyWebHttpHandler) |
|