PluginProbe
WebTotem Security / 2.3.42
WebTotem Security v2.3.42
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.42, at services.php

429 lines 16.2 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 wtsec_deleteAllAgentsFiles();
165 }
166
167 function wtsec_multineedle_stripos($haystack, $needles, $offset = 0) {
168 foreach($needles as $needle) {
169 if(stripos($haystack, $needle, $offset)){
170 return true;
171 }
172 }
173 return false;
174 }
175
176 function wtsec_generateFile($id, $service)
177 {
178 if ($service === "AM") {
179 $result = WTSEC_LIBRARY_WT::generateAmFile($id);
180 if (!isset($result['data']['auth']['am']['install'])) {
181 $file = ['filename' => null, "body" => null];
182 } else {
183 $result = $result['data']['auth']['am']['install'];
184
185 $url = $result['downloadLink'];
186 $amFilename = $result['amFilename'];
187
188 if($result['wafFilename']){
189 wtsec_app()->set('waf_installed_file', $result['wafFilename']);
190 }
191 if($result['avFilename']){
192 wtsec_app()->set('av_installed_file', $result['avFilename']);
193 }
194
195 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
196 if (empty($file)) {
197 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
198 }
199 }
200
201 $name = $file["filename"];
202 $file = $file["body"];
203 } else {
204 $result = WTSEC_LIBRARY_WT::generateFile($id, $service);
205 $name = $result['data']['auth']['agents']['generate']['agentName'];
206 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
207 if (empty($file)) {
208 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
209 }
210 }
211
212 return ['file' => $file, 'name' => $name];
213 }
214
215 function wtsec_serviceConnect($id, $service, $domain = '')
216 {
217 $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service);
218 $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true;
219 if (!$status) {
220 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
221 }
222 return $status;
223 }
224
225 function wtsec_servicePing($service, $domain = '')
226 {
227 if ($domain == NULL) return false;
228 if (stripos($domain, "http") === false) {
229 $domain = "http://" . $domain;
230 }
231 $args = [
232 'timeout' => '10',
233 'sslverify' => false,
234 'redirection' => 3,
235 ];
236 $response = wp_remote_get($domain, $args);
237 $httpcode = wp_remote_retrieve_response_code($response);
238 if ($httpcode >= 200 && $httpcode < 400) {
239 return true;
240 } else {
241 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
242 return false;
243 }
244 }
245
246 function wtsec_AVLogsData($logs){
247 foreach ($logs as $key => $log){
248 $statuses = wtsec_getStatuses();
249 $log = $log['node'];
250 $log['originPath'] = $log['filePath'];
251 $log['filePath'] = urldecode($log['filePath']);
252 $log['text'] = (strlen($log['filePath']) > 45) ? substr($log['filePath'],0,45 ).'...' : $log['filePath'];
253 $log['time'] = convertTimeToReadable($log['time']);
254 $log['class_path'] = '';
255 $log['class_info'] = '';
256
257 if ($log['event'] === 'modified') {
258 $log['info'] = __( 'normal file', 'wtotem' );
259 $log['class_path'] = "wtotem_file-table__td_changed";
260 $log['class_info'] = "wtotem_file-table__td_normal";
261 $log['description'] = $statuses['changed'];
262 } elseif ($log['event'] === 'quarantine') {
263 $log['info'] = __( 'in quarantine', 'wtotem' );
264 $log['class_path'] = "wtotem_file-table__td_changed";
265 $log['class_info'] = "wtotem_file-table__td_normal";
266 $log['description'] = $statuses['quarantine'];
267 } elseif ($log['event'] === 'infected') {
268 $log['info'] = __( 'backdoor virus', 'wtotem' );
269 $log['class_path'] = "wtotem_file-table__td_critical";
270 $log['class_info'] = "wtotem_file-table__td_critical";
271 $log['description'] = $statuses['infected'];
272 } elseif ($log['event'] === 'deleted') {
273 $log['info'] = __( 'deleted file', 'wtotem' );;
274 $log['description'] = $statuses['deleted'];
275 }elseif ($log['event'] === 'new'){
276 $log['info'] = __( 'normal file', 'wtotem' );
277 $log['description'] = $statuses['new'];
278 }elseif ($log['event'] === 'scanned'){
279 $log['info'] = __( 'normal file', 'wtotem' );
280 $log['description'] = $statuses['scanned'];
281 }else{
282 $log['info'] = $statuses['error'];
283 $log['description'] = $statuses['error'];
284 }
285
286 $logs[$key]['node'] = $log;
287 }
288 return $logs;
289 }
290
291 function wtsec_getConfigs($host_id)
292 {
293 $config = WTSEC_LIBRARY_WT::getConfigs($host_id);
294 return wtsec_arrayIndex($config['data']['auth']['viewer']['sites']['one']['configs'], 'service');
295 }
296
297 function wtsec_arrayIndex($array, $key){
298 $newArray = [];
299 foreach ($array as $item){
300 if(array_key_exists($key,$item)){
301 $newArray[$item[$key]] = $item;
302 }
303 }
304 return $newArray;
305 }
306
307 function wtsec_button($label = '', $class = '')
308 {
309 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
310 }
311
312 function wtsec_main_button($label = '', $class = '')
313 {
314 return '<button class="ww-button ' . $class . '">' . $label . '</button>';
315 }
316
317 function wtsec_generateButtons($uid, $_service, $service, $status, $domain = '')
318 {
319 $url = esc_url(admin_url('admin-post.php'));
320 $buttons = [];
321
322 $form = '<form action="' . $url . '" method="post" style="display:inline-block;">
323 <input type="hidden" name="service" value="' . $_service . '">
324 <input type="hidden" name="action" value="ajax_cmd">
325 <input type="hidden" name="uid" value="' . $uid . '">
326 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
327 $result = [];
328 $installedFile = wtsec_checkInstalledFile($_service);
329 $disable_uninstall = false;
330 $first_class = "ww-button--block ";
331
332 if ($service === "AM") {
333 if (!$installedFile['status']) {
334 $cmd = $_service . '_install';
335 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
336 } else {
337 // $url = $domain.'/'.$installedFile['file'];
338 // $connected = wtsec_servicePing($service,$url);
339 if (!$disable_uninstall) {
340 $cmd = $_service . '_uninstall';
341 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Uninstall', 'wtotem' ), $first_class . "ww-button--attention"), 'cmd' => $cmd];
342 }
343 }
344 } else {
345 if ($service === "WAF") {
346 $first_class = "";
347 }
348
349 if ($status === "not_installed") {
350 $cmd = $_service . '_install';
351 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
352 }
353 }
354
355
356 foreach ($buttons as $btn) {
357 $newform = $form;
358 $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform);
359 $newform = str_replace("{{{button}}}", $btn['button'], $newform);
360 $result[$btn['place']] = $newform;
361 }
362 return $result;
363 }
364
365 function wtsec_checkInstalledFile($service)
366 {
367 $file = wtsec_getInstalledFile($service);
368 $root = ABSPATH;
369
370 return ['status' => ((bool)$file) && is_file($root . $file), 'file' => $file];
371 }
372
373 function wtsec_getInstalledFile($service)
374 {
375 return wtsec_app()->get($service . "_installed_file");
376 }
377
378 function wtsec_getStatuses()
379 {
380 return [
381 'warning' => __( 'Warning', 'wtotem' ),
382 'error' => __( 'Error', 'wtotem' ),
383 'invalid' => __( 'Invalid', 'wtotem' ),
384 'ok' => __( 'Everything is OK', 'wtotem' ),
385 'expired' => __( 'Expired', 'wtotem' ),
386 'expires' => __( 'Expires', 'wtotem' ),
387 'expires_today' => __( 'Expires today', 'wtotem' ),
388 'missing' => __( 'Missing', 'wtotem' ),
389 'success' => __( 'Success', 'wtotem' ),
390 'info' => __( 'Info', 'wtotem' ),
391 'active' => __( 'Active', 'wtotem' ),
392 'inactive' => __( 'Inactive', 'wtotem' ),
393 'pending' => __( 'Pending', 'wtotem' ),
394 'pause' => __( 'Disabled', 'wtotem' ),
395 'available' => __( 'Available', 'wtotem' ),
396 'unavailable' => __( 'Unavailable', 'wtotem' ),
397 //
398 'not_registered' => __( 'Not registered', 'wtotem' ),
399 'unsupported' => __( 'Unsupported', 'wtotem' ),
400 'clean' => __( 'Clean', 'wtotem' ),
401 'clear' => __( 'Clear', 'wtotem' ),
402 'blacklisted' => __( 'Infected', 'wtotem' ),
403 'miner_detected' => __( 'Infected', 'wtotem' ),
404 'deface' => __( 'Deface', 'wtotem' ),
405 'modified' => __( 'Modified', 'wtotem' ),
406 'detected' => __( 'Detected', 'wtotem' ),
407 'open' => __( 'Open', 'wtotem' ),
408 'blocked' => __( 'Blocked', 'wtotem' ),
409 'connected' => __( 'Connected', 'wtotem' ),
410 'attacks_detected' => __( 'Attacks detected', 'wtotem' ),
411 'signature_found' => __( 'Signature found', 'wtotem' ),
412 'file_changes' => __( 'File changes', 'wtotem' ),
413 //
414 'no_cert' => __( 'No cert', 'wtotem' ),
415 'down' => __( 'Down', 'wtotem' ),
416 'up' => __( 'Up', 'wtotem' ),
417 'infected' => __( 'Infected', 'wtotem' ),
418 'not_installed' => __( 'Need to install', 'wtotem' ),
419 'working' => __( 'Working', 'wtotem' ),
420
421 "critical" => __( 'Critical', 'wtotem' ),
422 "deleted" => __( 'Deleted', 'wtotem' ),
423 "changed" => __( 'Changed', 'wtotem' ),
424 "new" => __( 'New', 'wtotem' ),
425 "scanned" => __( 'Scanned', 'wtotem' ),
426 "quarantine" => __( 'In quarantine', 'wtotem' ),
427 ];
428 }
429