PluginProbe
WebTotem Security / 2.3.39
WebTotem Security v2.3.39
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.39, at services.php

438 lines 16.5 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", __( 'Failed to download agent file.', 'wtotem' ));
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", sprintf( __( 'Unable to install the %s file in the %s', 'wtotem' ), $service, $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", sprintf( __( 'Unable to install the %s file in the %s', 'wtotem' ), $service, $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
56 } catch (Exception $e) {
57 WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
58 return false;
59 }
60 break;
61 case $_service . "_start":
62 WTSEC_LIBRARY_WT::changeStatus($status['config_id'], $uid);
63 wtsec_app()->set($_service . '_status', "start");
64 break;
65 case $_service . "_stop":
66 // WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
67 wtsec_app()->set($_service . '_status', "stop");
68 break;
69 case $_service . "_connect":
70 $file = wtsec_getInstalledFile($_service);
71 if ($file) {
72 //wtsec_serviceConnect($uid, $service, $domain . '/' . $file);
73 } else {
74 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'The %s service file not found', 'wtotem' ), $service ));
75 }
76 break;
77 case $_service . "_uninstall":
78 try {
79 $file = wtsec_getInstalledFile($_service);
80 if ($service === "WAF") {
81 $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
82 $mainDir = $plugin_path;
83 } else {
84 $mainDir = $wp_filesystem->abspath();
85 }
86 $target_dir = $wp_filesystem->find_folder($mainDir);
87 $target_file = trailingslashit($mainDir) . $file;
88 if (!$wp_filesystem->delete($target_file)) {
89 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to uninstall the %s file in the %s', 'wtotem' ), $service,$target_dir ));
90 }
91 wtsec_app()->set($_service . '_status', "uninstalled");
92 WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file');
93 } catch (Exception $e) {
94 WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
95 return false;
96 }
97 break;
98 }
99 return true;
100 }
101
102 // Add check file if plugin is activated
103 function wtsec_addCheckFile($name){
104 global $wp_filesystem;
105
106 if (empty($wp_filesystem)) {
107 require_once(ABSPATH . '/wp-admin/includes/file.php');
108 WP_Filesystem();
109 }
110
111 //retry checking
112 if (empty($wp_filesystem)) {
113 WTSEC_LIBRARY_Session::setNotification("error", "WP FileSystem path error");
114 return false;
115 }
116
117 $target_dir = $wp_filesystem->find_folder(WTSEC_PLUGIN_PATH);
118 $target_file = trailingslashit($target_dir) . 'generate.php';
119
120 if ($wp_filesystem->put_contents($target_file, '<?php exit(); ?>'.$name, FS_CHMOD_FILE)) {
121 return true;
122 }
123 return false;
124 }
125
126 function wtsec_checkStatus($id, $service)
127 {
128 $service = ucfirst(strtolower($service));
129 $is_active = null;
130 $config_id = null;
131 $result = WTSEC_LIBRARY_WT::checkStatus($id, $service);
132 $data = $result['data']['auth']['viewer']['sites']['one']['configs'];
133 foreach ($data as &$cfg) {
134 if (!empty($cfg)) {
135 $is_active = $cfg['isActive'];
136 $config_id = $cfg['id'];
137 }
138 }
139
140 return ['active' => $is_active, 'config_id' => $config_id];
141 }
142
143 function wtsec_deleteAllAgentsFiles(){
144 global $wp_filesystem;
145
146 if (empty($wp_filesystem)) {
147 require_once(ABSPATH . '/wp-admin/includes/file.php');
148 WP_Filesystem();
149 }
150 $target_dir = $wp_filesystem->find_folder($wp_filesystem->abspath());
151 $list = $wp_filesystem->dirlist($target_dir);
152
153 foreach ($list as $item){
154 $target_item = trailingslashit($target_dir) . $item['name'];
155 $recursive = ($item['type'] == 'd') ? true : false;
156 if( wtsec_multineedle_stripos( $item['name'], ['.am.php','.waf.php','.av.php','wtotem_'] ) ){
157 $wp_filesystem->delete($target_item, $recursive, $item['type']);
158 }
159 }
160 return true;
161 }
162
163 function wtsec_DeactivatedPlugin(){
164 if(WTSEC_LIBRARY_App::_get('api_key')){
165 $host = WTSEC_LIBRARY_WT::getOwnSite();
166 if(!empty($host)){
167 if(WTSEC_LIBRARY_WT::deleteAm($host['id'])){
168 wtsec_deleteAllAgentsFiles();
169 }
170 }
171 } else{
172 wtsec_deleteAllAgentsFiles();
173 }
174 }
175
176 function wtsec_multineedle_stripos($haystack, $needles, $offset = 0) {
177 foreach($needles as $needle) {
178 if(stripos($haystack, $needle, $offset)){
179 return true;
180 }
181 }
182 return false;
183 }
184
185 function wtsec_generateFile($id, $service)
186 {
187 if ($service === "AM") {
188 $result = WTSEC_LIBRARY_WT::generateAmFile($id);
189 if (!isset($result['data']['auth']['am']['install'])) {
190 $file = ['filename' => null, "body" => null];
191 } else {
192 $result = $result['data']['auth']['am']['install'];
193
194 $url = $result['downloadLink'];
195 $amFilename = $result['amFilename'];
196
197 if($result['wafFilename']){
198 wtsec_app()->set('waf_installed_file', $result['wafFilename']);
199 }
200 if($result['avFilename']){
201 wtsec_app()->set('av_installed_file', $result['avFilename']);
202 }
203
204 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
205 if (empty($file)) {
206 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
207 }
208 }
209
210 $name = $file["filename"];
211 $file = $file["body"];
212 } else {
213 $result = WTSEC_LIBRARY_WT::generateFile($id, $service);
214 $name = $result['data']['auth']['agents']['generate']['agentName'];
215 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
216 if (empty($file)) {
217 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
218 }
219 }
220
221 return ['file' => $file, 'name' => $name];
222 }
223
224 function wtsec_serviceConnect($id, $service, $domain = '')
225 {
226 $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service);
227 $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true;
228 if (!$status) {
229 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
230 }
231 return $status;
232 }
233
234 function wtsec_servicePing($service, $domain = '')
235 {
236 if ($domain == NULL) return false;
237 if (stripos($domain, "http") === false) {
238 $domain = "http://" . $domain;
239 }
240 $args = [
241 'timeout' => '10',
242 'sslverify' => false,
243 'redirection' => 3,
244 ];
245 $response = wp_remote_get($domain, $args);
246 $httpcode = wp_remote_retrieve_response_code($response);
247 if ($httpcode >= 200 && $httpcode < 400) {
248 return true;
249 } else {
250 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
251 return false;
252 }
253 }
254
255 function wtsec_AVLogsData($logs){
256 foreach ($logs as $key => $log){
257 $statuses = wtsec_getStatuses();
258 $log = $log['node'];
259 $log['originPath'] = $log['filePath'];
260 $log['filePath'] = urldecode($log['filePath']);
261 $log['text'] = (strlen($log['filePath']) > 45) ? substr($log['filePath'],0,45 ).'...' : $log['filePath'];
262 $log['time'] = convertTimeToReadable($log['time']);
263 $log['class_path'] = '';
264 $log['class_info'] = '';
265
266 if ($log['event'] === 'modified') {
267 $log['info'] = __( 'normal file', 'wtotem' );
268 $log['class_path'] = "wtotem_file-table__td_changed";
269 $log['class_info'] = "wtotem_file-table__td_normal";
270 $log['description'] = $statuses['changed'];
271 } elseif ($log['event'] === 'quarantine') {
272 $log['info'] = __( 'in quarantine', 'wtotem' );
273 $log['class_path'] = "wtotem_file-table__td_changed";
274 $log['class_info'] = "wtotem_file-table__td_normal";
275 $log['description'] = $statuses['quarantine'];
276 } elseif ($log['event'] === 'infected') {
277 $log['info'] = __( 'backdoor virus', 'wtotem' );
278 $log['class_path'] = "wtotem_file-table__td_critical";
279 $log['class_info'] = "wtotem_file-table__td_critical";
280 $log['description'] = $statuses['infected'];
281 } elseif ($log['event'] === 'deleted') {
282 $log['info'] = __( 'deleted file', 'wtotem' );;
283 $log['description'] = $statuses['deleted'];
284 }elseif ($log['event'] === 'new'){
285 $log['info'] = __( 'normal file', 'wtotem' );
286 $log['description'] = $statuses['new'];
287 }elseif ($log['event'] === 'scanned'){
288 $log['info'] = __( 'normal file', 'wtotem' );
289 $log['description'] = $statuses['scanned'];
290 }else{
291 $log['info'] = $statuses['error'];
292 $log['description'] = $statuses['error'];
293 }
294
295 $logs[$key]['node'] = $log;
296 }
297 return $logs;
298 }
299
300 function wtsec_getConfigs($host_id)
301 {
302 $config = WTSEC_LIBRARY_WT::getConfigs($host_id);
303 return wtsec_arrayIndex($config['data']['auth']['viewer']['sites']['one']['configs'], 'service');
304 }
305
306 function wtsec_arrayIndex($array, $key){
307 $newArray = [];
308 foreach ($array as $item){
309 if(array_key_exists($key,$item)){
310 $newArray[$item[$key]] = $item;
311 }
312 }
313 return $newArray;
314 }
315
316 function wtsec_button($label = '', $class = '')
317 {
318 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
319 }
320
321 function wtsec_main_button($label = '', $class = '')
322 {
323 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
324 }
325
326 function wtsec_generateButtons($uid, $_service, $service, $status, $domain = '')
327 {
328 $url = esc_url(admin_url('admin-post.php'));
329 $buttons = [];
330
331 $form = '<form action="' . $url . '" method="post" style="display:inline-block;">
332 <input type="hidden" name="service" value="' . $_service . '">
333 <input type="hidden" name="action" value="ajax_cmd">
334 <input type="hidden" name="uid" value="' . $uid . '">
335 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
336 $result = [];
337 $installedFile = wtsec_checkInstalledFile($_service);
338 $disable_uninstall = false;
339 $first_class = "ww-button--block ";
340
341 if ($service === "AM") {
342 if (!$installedFile['status']) {
343 $cmd = $_service . '_install';
344 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
345 } else {
346 // $url = $domain.'/'.$installedFile['file'];
347 // $connected = wtsec_servicePing($service,$url);
348 if (!$disable_uninstall) {
349 $cmd = $_service . '_uninstall';
350 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Uninstall', 'wtotem' ), $first_class . "ww-button--attention"), 'cmd' => $cmd];
351 }
352 }
353 } else {
354 if ($service === "WAF") {
355 $first_class = "";
356 }
357
358 if ($status === "not_installed") {
359 $cmd = $_service . '_install';
360 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
361 }
362 }
363
364
365 foreach ($buttons as $btn) {
366 $newform = $form;
367 $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform);
368 $newform = str_replace("{{{button}}}", $btn['button'], $newform);
369 $result[$btn['place']] = $newform;
370 }
371 return $result;
372 }
373
374 function wtsec_checkInstalledFile($service)
375 {
376 $file = wtsec_getInstalledFile($service);
377 $root = ABSPATH;
378
379 return ['status' => ((bool)$file) && is_file($root . $file), 'file' => $file];
380 }
381
382 function wtsec_getInstalledFile($service)
383 {
384 return wtsec_app()->get($service . "_installed_file");
385 }
386
387 function wtsec_getStatuses()
388 {
389 return [
390 'warning' => __( 'Warning', 'wtotem' ),
391 'error' => __( 'Error', 'wtotem' ),
392 'invalid' => __( 'Invalid', 'wtotem' ),
393 'ok' => __( 'Everything is OK', 'wtotem' ),
394 'expired' => __( 'Expired', 'wtotem' ),
395 'expires' => __( 'Expires', 'wtotem' ),
396 'expires_today' => __( 'Expires today', 'wtotem' ),
397 'missing' => __( 'Missing', 'wtotem' ),
398 'success' => __( 'Success', 'wtotem' ),
399 'info' => __( 'Info', 'wtotem' ),
400 'active' => __( 'Active', 'wtotem' ),
401 'inactive' => __( 'Inactive', 'wtotem' ),
402 'pending' => __( 'Pending', 'wtotem' ),
403 'pause' => __( 'Disabled', 'wtotem' ),
404 'available' => __( 'Available', 'wtotem' ),
405 'unavailable' => __( 'Unavailable', 'wtotem' ),
406 //
407 'not_registered' => __( 'Not registered', 'wtotem' ),
408 'unsupported' => __( 'Unsupported', 'wtotem' ),
409 'clean' => __( 'Clean', 'wtotem' ),
410 'clear' => __( 'Clear', 'wtotem' ),
411 'blacklisted' => __( 'Infected', 'wtotem' ),
412 'miner_detected' => __( 'Infected', 'wtotem' ),
413 'deface' => __( 'Deface', 'wtotem' ),
414 'modified' => __( 'Modified', 'wtotem' ),
415 'detected' => __( 'Detected', 'wtotem' ),
416 'open' => __( 'Open', 'wtotem' ),
417 'blocked' => __( 'Blocked', 'wtotem' ),
418 'connected' => __( 'Connected', 'wtotem' ),
419 'attacks_detected' => __( 'Attacks detected', 'wtotem' ),
420 'signature_found' => __( 'Signature found', 'wtotem' ),
421 'file_changes' => __( 'File changes', 'wtotem' ),
422 //
423 'no_cert' => __( 'No cert', 'wtotem' ),
424 'down' => __( 'Down', 'wtotem' ),
425 'up' => __( 'Up', 'wtotem' ),
426 'infected' => __( 'Infected', 'wtotem' ),
427 'not_installed' => __( 'Need to install', 'wtotem' ),
428 'working' => __( 'Working', 'wtotem' ),
429
430 "critical" => __( 'Critical', 'wtotem' ),
431 "deleted" => __( 'Deleted', 'wtotem' ),
432 "changed" => __( 'Changed', 'wtotem' ),
433 "new" => __( 'New', 'wtotem' ),
434 "scanned" => __( 'Scanned', 'wtotem' ),
435 "quarantine" => __( 'In quarantine', 'wtotem' ),
436 ];
437 }
438