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