PluginProbe
WebTotem Security / 2.3.2
WebTotem Security v2.3.2
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / services.php

services.php in WebTotem Security 2.3.2, at services.php

343 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die("Protected By WT!");
2
3 function wtsec_cmd($cmd, $service, $_service, $uid, $status, $domain = '')
4 {
5 global $wp_filesystem;
6
7 //bug fix with wp file system, which return null
8 if (empty($wp_filesystem)) {
9 require_once(ABSPATH . '/wp-admin/includes/file.php');
10 WP_Filesystem();
11 }
12
13 //retry checking
14 if (empty($wp_filesystem)) {
15 WTSEC_LIBRARY_Session::setNotification("error", "WP FileSystem path error");
16 return false;
17 }
18
19 switch ($cmd) {
20 case $_service . "_install":
21 try {
22 $file = wtsec_generateFile($uid, $service);
23 if (is_null($file['name'])) {
24 WTSEC_LIBRARY_Session::setNotification("error", wtsec_locale("failed_to_download_agent"));
25 return false;
26 }
27 if ($service === "WAF") {
28 $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
29 $path = $plugin_path;
30 $target_dir = $wp_filesystem->find_folder($path);
31 if (!$wp_filesystem->is_dir($path)) {
32 $wp_filesystem->mkdir($target_dir);
33 }
34 $target_file = trailingslashit($target_dir) . $file['name'];
35 if (!$wp_filesystem->put_contents($target_file, $file['file'], FS_CHMOD_FILE)) {
36 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service, 'directory' => $target_dir]));
37 } else {
38 wtsec_app()->set($_service . '_installed_file', $file['name']);
39 }
40 } else {
41 $target_dir = $wp_filesystem->find_folder($wp_filesystem->abspath());
42 $target_file = trailingslashit($target_dir) . $file['name'];
43 if (!$wp_filesystem->put_contents($target_file, $file['file'])) {
44 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service, 'directory' => $target_dir]));
45 } else {
46 wtsec_app()->set($_service . '_installed_file', $file['name']);
47 if ($service === "AM") {
48 wtsec_addCheckFile($file['name']);
49 }
50 }
51 }
52 if ($service !== "AM") {
53 //wtsec_serviceConnect($uid, $service, $domain . '/' . $file['name']);
54 }
55 } catch (Exception $e) {
56 WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
57 return false;
58 }
59 break;
60 case $_service . "_start":
61 WTSEC_LIBRARY_WT::changeStatus($status['config_id'], $uid);
62 wtsec_app()->set($_service . '_status', "start");
63 break;
64 case $_service . "_stop":
65 // WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
66 wtsec_app()->set($_service . '_status', "stop");
67 break;
68 case $_service . "_connect":
69 $file = wtsec_getInstalledFile($_service);
70 if ($file) {
71 //wtsec_serviceConnect($uid, $service, $domain . '/' . $file);
72 } else {
73 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service]));
74 }
75 break;
76 case $_service . "_uninstall":
77 try {
78 $file = wtsec_getInstalledFile($_service);
79 if ($service === "WAF") {
80 $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
81 $mainDir = $plugin_path;
82 } else {
83 $mainDir = $wp_filesystem->abspath();
84 }
85 $target_dir = $wp_filesystem->find_folder($mainDir);
86 $target_file = trailingslashit($mainDir) . $file;
87 if (!$wp_filesystem->delete($target_file)) {
88 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service, 'directory' => $target_dir]));
89 }
90 wtsec_app()->set($_service . '_status', "uninstalled");
91 WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file');
92 } catch (Exception $e) {
93 WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
94 return false;
95 }
96 break;
97 }
98 return true;
99 }
100
101 // Add check file if plugin is activated
102 function wtsec_addCheckFile($name){
103 global $wp_filesystem;
104
105 if (empty($wp_filesystem)) {
106 require_once(ABSPATH . '/wp-admin/includes/file.php');
107 WP_Filesystem();
108 }
109
110 //retry checking
111 if (empty($wp_filesystem)) {
112 WTSEC_LIBRARY_Session::setNotification("error", "WP FileSystem path error");
113 return false;
114 }
115
116 $target_dir = $wp_filesystem->find_folder(WTSEC_PLUGIN_PATH);
117 $target_file = trailingslashit($target_dir) . 'generate.php';
118
119 if ($wp_filesystem->put_contents($target_file, '<?php exit(); ?>'.$name, FS_CHMOD_FILE)) {
120 return true;
121 }
122 return false;
123 }
124
125 function wtsec_checkStatus($id, $service)
126 {
127 $service = ucfirst(strtolower($service));
128 $is_active = null;
129 $config_id = null;
130 $result = WTSEC_LIBRARY_WT::checkStatus($id, $service);
131 $data = $result['data']['auth']['viewer']['sites']['one']['configs'];
132 foreach ($data as &$cfg) {
133 if (!empty($cfg)) {
134 $is_active = $cfg['isActive'];
135 $config_id = $cfg['id'];
136 }
137 }
138
139 if ($is_active === false) {
140 wtsec_app()->set($service . '_connected_' . $id, true);
141 }
142 return ['active' => $is_active, 'config_id' => $config_id];
143 }
144
145 function wtsec_generateFile($id, $service)
146 {
147 if ($service === "AM") {
148 $result = WTSEC_LIBRARY_WT::generateAmFile($id);
149 if (!isset($result['data']['auth']['am']['install'])) {
150 $file = ['filename' => null, "body" => null];
151 } else {
152 $result = $result['data']['auth']['am']['install'];
153
154 $url = $result['downloadLink'];
155 $amFilename = $result['amFilename'];
156 $wafFilename = $result['wafFilename'];
157
158 if($wafFilename){
159 wtsec_app()->set('waf_installed_file', $wafFilename);
160 }
161
162 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
163 if (empty($file)) {
164 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
165 }
166 }
167
168 $name = $file["filename"];
169 $file = $file["body"];
170 } else {
171 $result = WTSEC_LIBRARY_WT::generateFile($id, $service);
172 $name = $result['data']['auth']['agents']['generate']['agentName'];
173 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
174 if (empty($file)) {
175 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
176 }
177 }
178
179 return ['file' => $file, 'name' => $name];
180 }
181
182 function wtsec_serviceConnect($id, $service, $domain = '')
183 {
184 $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service);
185 $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true;
186 if (!$status) {
187 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain]));
188 }
189 return $status;
190 }
191
192
193 function wtsec_servicePing($service, $domain = '')
194 {
195 if ($domain == NULL) return false;
196 if (stripos($domain, "http") === false) {
197 $domain = "http://" . $domain;
198 }
199 $args = [
200 'timeout' => '10',
201 'sslverify' => false,
202 'redirection' => 3,
203 ];
204 $response = wp_remote_get($domain, $args);
205 $httpcode = wp_remote_retrieve_response_code($response);
206 if ($httpcode >= 200 && $httpcode < 400) {
207 return true;
208 } else {
209 WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain]));
210 return false;
211 }
212 }
213
214
215 function wtsec_logsAddData($logs){
216 foreach ($logs as $key => $log){
217 $log = $log['node'];
218 $log['text'] = (strlen($log['filePath']) > 55) ? substr($log['filePath'],0,55).'...' : $log['filePath'];
219 $log['time'] = convertToCurrentTime($log['time']);
220
221 if ($log['event'] === 'modified') {
222 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
223 $log['class_path'] = "wtotem_file-table__td_changed";
224 $log['class_info'] = "wtotem_file-table__td_normal";
225 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.changed");
226 } elseif ($log['event'] === 'quarantine') {
227 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.backdoor_virus");
228 $log['class_path'] = "wtotem_file-table__td_changed";
229 $log['class_info'] = "wtotem_file-table__td_normal";
230 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.quarantine");
231 } elseif ($log['event'] === 'infected') {
232 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.backdoor_virus");
233 $log['class_path'] = "wtotem_file-table__td_critical";
234 $log['class_info'] = "wtotem_file-table__td_critical";
235 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.infected");
236 } elseif ($log['event'] === 'deleted') {
237 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.deleted");
238 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.deleted");
239 }elseif ($log['event'] === 'new'){
240 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
241 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.new");
242 }elseif ($log['event'] === 'scanned'){
243 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
244 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.scanned");
245 }else{
246 $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.error");
247 $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.error");
248 }
249
250 $logs[$key]['node'] = $log;
251 }
252 return $logs;
253 }
254
255 function wtsec_getConfigs($host_id)
256 {
257 $config = WTSEC_LIBRARY_WT::getConfigs($host_id);
258 return wtsec_arrayIndex($config['data']['auth']['viewer']['sites']['one']['configs'], 'service');
259 }
260
261 function wtsec_arrayIndex($array, $key){
262 $newArray = [];
263 foreach ($array as $item){
264 if(array_key_exists($key,$item)){
265 $newArray[$item[$key]] = $item;
266 }
267 }
268 return $newArray;
269 }
270
271 function wtsec_button($label = '', $class = '')
272 {
273 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
274 }
275
276 function wtsec_main_button($label = '', $class = '')
277 {
278 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
279 }
280
281 function wtsec_generateButtons($uid, $_service, $service, $status, $domain = '')
282 {
283 $url = esc_url(admin_url('admin-post.php'));
284 $buttons = [];
285
286 $form = '<form action="' . $url . '" method="post" style="display:inline-block;">
287 <input type="hidden" name="service" value="' . $_service . '">
288 <input type="hidden" name="action" value="ajax_cmd">
289 <input type="hidden" name="uid" value="' . $uid . '">
290 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
291 $result = [];
292 $installedFile = wtsec_checkInstalledFile($_service);
293 $disable_uninstall = false;
294 $first_class = "ww-button--block ";
295
296 if ($service === "AM") {
297 if (!$installedFile['status']) {
298 $cmd = $_service . '_install';
299 $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd];
300 } else {
301 // $url = $domain.'/'.$installedFile['file'];
302 // $connected = wtsec_servicePing($service,$url);
303 if (!$disable_uninstall) {
304 $cmd = $_service . '_uninstall';
305 $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'), $first_class . "ww-button--attention"), 'cmd' => $cmd];
306 }
307 }
308 } else {
309 if ($service === "WAF") {
310 $first_class = "";
311 }
312
313 if ($status === "not_installed") {
314 $cmd = $_service . '_install';
315 $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd];
316 }
317 }
318
319
320 foreach ($buttons as $btn) {
321 $newform = $form;
322 $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform);
323 $newform = str_replace("{{{button}}}", $btn['button'], $newform);
324 $result[$btn['place']] = $newform;
325 }
326 return $result;
327 }
328
329 function wtsec_checkInstalledFile($service)
330 {
331 $file = wtsec_getInstalledFile($service);
332 if ($service == "waf") {
333 $root = WTSEC_INSTALLATION_DIR;
334 } else {
335 $root = ABSPATH;
336 }
337 return ['status' => ((bool)$file) && is_file($root . $file), 'file' => $file];
338 }
339
340 function wtsec_getInstalledFile($service)
341 {
342 return wtsec_app()->get($service . "_installed_file");
343 }