PluginProbe
WebTotem Security / 2.3.35
WebTotem Security v2.3.35
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.35, at services.php

396 lines 15.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 } 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", sprintf( __( 'The %s service file not found', 'wtotem' ), $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", sprintf( __( 'Unable to uninstall the %s file in the %s', 'wtotem' ), $service,$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 return ['active' => $is_active, 'config_id' => $config_id];
140 }
141
142 function wtsec_generateFile($id, $service)
143 {
144 if ($service === "AM") {
145 $result = WTSEC_LIBRARY_WT::generateAmFile($id);
146 if (!isset($result['data']['auth']['am']['install'])) {
147 $file = ['filename' => null, "body" => null];
148 } else {
149 $result = $result['data']['auth']['am']['install'];
150
151 $url = $result['downloadLink'];
152 $amFilename = $result['amFilename'];
153
154 if($result['wafFilename']){
155 wtsec_app()->set('waf_installed_file', $result['wafFilename']);
156 }
157
158 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
159 if (empty($file)) {
160 $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
161 }
162 }
163
164 $name = $file["filename"];
165 $file = $file["body"];
166 } else {
167 $result = WTSEC_LIBRARY_WT::generateFile($id, $service);
168 $name = $result['data']['auth']['agents']['generate']['agentName'];
169 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
170 if (empty($file)) {
171 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
172 }
173 }
174
175 return ['file' => $file, 'name' => $name];
176 }
177
178 function wtsec_serviceConnect($id, $service, $domain = '')
179 {
180 $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service);
181 $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true;
182 if (!$status) {
183 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
184 }
185 return $status;
186 }
187
188
189 function wtsec_servicePing($service, $domain = '')
190 {
191 if ($domain == NULL) return false;
192 if (stripos($domain, "http") === false) {
193 $domain = "http://" . $domain;
194 }
195 $args = [
196 'timeout' => '10',
197 'sslverify' => false,
198 'redirection' => 3,
199 ];
200 $response = wp_remote_get($domain, $args);
201 $httpcode = wp_remote_retrieve_response_code($response);
202 if ($httpcode >= 200 && $httpcode < 400) {
203 return true;
204 } else {
205 WTSEC_LIBRARY_Session::setNotification("warning", sprintf( __( 'Unable to connect to %s service by the url: %s', 'wtotem' ), $service, $domain ));
206 return false;
207 }
208 }
209
210
211 function wtsec_AVLogsData($logs){
212 foreach ($logs as $key => $log){
213 $statuses = wtsec_getStatuses();
214 $log = $log['node'];
215 $log['originPath'] = $log['filePath'];
216 $log['filePath'] = urldecode($log['filePath']);
217 $log['text'] = (strlen($log['filePath']) > 45) ? substr($log['filePath'],0,45 ).'...' : $log['filePath'];
218 $log['time'] = convertTimeToReadable($log['time']);
219 $log['class_path'] = '';
220 $log['class_info'] = '';
221
222 if ($log['event'] === 'modified') {
223 $log['info'] = __( 'normal file', 'wtotem' );
224 $log['class_path'] = "wtotem_file-table__td_changed";
225 $log['class_info'] = "wtotem_file-table__td_normal";
226 $log['description'] = $statuses['changed'];
227 } elseif ($log['event'] === 'quarantine') {
228 $log['info'] = __( 'in quarantine', 'wtotem' );
229 $log['class_path'] = "wtotem_file-table__td_changed";
230 $log['class_info'] = "wtotem_file-table__td_normal";
231 $log['description'] = $statuses['quarantine'];
232 } elseif ($log['event'] === 'infected') {
233 $log['info'] = __( 'backdoor virus', 'wtotem' );
234 $log['class_path'] = "wtotem_file-table__td_critical";
235 $log['class_info'] = "wtotem_file-table__td_critical";
236 $log['description'] = $statuses['infected'];
237 } elseif ($log['event'] === 'deleted') {
238 $log['info'] = __( 'deleted file', 'wtotem' );;
239 $log['description'] = $statuses['deleted'];
240 }elseif ($log['event'] === 'new'){
241 $log['info'] = __( 'normal file', 'wtotem' );
242 $log['description'] = $statuses['new'];
243 }elseif ($log['event'] === 'scanned'){
244 $log['info'] = __( 'normal file', 'wtotem' );
245 $log['description'] = $statuses['scanned'];
246 }else{
247 $log['info'] = $statuses['error'];
248 $log['description'] = $statuses['error'];
249 }
250
251 $logs[$key]['node'] = $log;
252 }
253 return $logs;
254 }
255
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');
260 }
261
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 = '')
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 {
284 $url = esc_url(admin_url('admin-post.php'));
285 $buttons = [];
286
287 $form = '<form action="' . $url . '" method="post" style="display:inline-block;">
288 <input type="hidden" name="service" value="' . $_service . '">
289 <input type="hidden" name="action" value="ajax_cmd">
290 <input type="hidden" name="uid" value="' . $uid . '">
291 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
292 $result = [];
293 $installedFile = wtsec_checkInstalledFile($_service);
294 $disable_uninstall = false;
295 $first_class = "ww-button--block ";
296
297 if ($service === "AM") {
298 if (!$installedFile['status']) {
299 $cmd = $_service . '_install';
300 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
301 } else {
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(__( 'Uninstall', 'wtotem' ), $first_class . "ww-button--attention"), 'cmd' => $cmd];
307 }
308 }
309 } else {
310 if ($service === "WAF") {
311 $first_class = "";
312 }
313
314 if ($status === "not_installed") {
315 $cmd = $_service . '_install';
316 $buttons[] = ['place' => 'first', 'button' => wtsec_button(__( 'Install', 'wtotem' ), $first_class . "ww-button--success"), 'cmd' => $cmd];
317 }
318 }
319
320
321 foreach ($buttons as $btn) {
322 $newform = $form;
323 $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform);
324 $newform = str_replace("{{{button}}}", $btn['button'], $newform);
325 $result[$btn['place']] = $newform;
326 }
327 return $result;
328 }
329
330 function wtsec_checkInstalledFile($service)
331 {
332 $file = wtsec_getInstalledFile($service);
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];
339 }
340
341 function wtsec_getInstalledFile($service)
342 {
343 return wtsec_app()->get($service . "_installed_file");
344 }
345 function wtsec_getStatuses()
346 {
347 return [
348 'warning' => __( 'Warning', 'wtotem' ),
349 'error' => __( 'Error', 'wtotem' ),
350 'invalid' => __( 'Invalid', 'wtotem' ),
351 'ok' => __( 'Everything is OK', 'wtotem' ),
352 'expired' => __( 'Expired', 'wtotem' ),
353 'expires' => __( 'Expires', 'wtotem' ),
354 'expires_today' => __( 'Expires today', 'wtotem' ),
355 'missing' => __( 'Missing', 'wtotem' ),
356 'success' => __( 'Success', 'wtotem' ),
357 'info' => __( 'Info', 'wtotem' ),
358 'active' => __( 'Active', 'wtotem' ),
359 'inactive' => __( 'Inactive', 'wtotem' ),
360 'pending' => __( 'Pending', 'wtotem' ),
361 'pause' => __( 'Disabled', 'wtotem' ),
362 'available' => __( 'Available', 'wtotem' ),
363 'unavailable' => __( 'Unavailable', 'wtotem' ),
364 //
365 'not_registered' => __( 'Not registered', 'wtotem' ),
366 'unsupported' => __( 'Unsupported', 'wtotem' ),
367 'clean' => __( 'Clean', 'wtotem' ),
368 'clear' => __( 'Clear', 'wtotem' ),
369 'blacklisted' => __( 'Infected', 'wtotem' ),
370 'miner_detected' => __( 'Infected', 'wtotem' ),
371 'deface' => __( 'Deface', 'wtotem' ),
372 'modified' => __( 'Modified', 'wtotem' ),
373 'detected' => __( 'Detected', 'wtotem' ),
374 'open' => __( 'Open', 'wtotem' ),
375 'blocked' => __( 'Blocked', 'wtotem' ),
376 'connected' => __( 'Connected', 'wtotem' ),
377 'attacks_detected' => __( 'Attacks detected', 'wtotem' ),
378 'signature_found' => __( 'Signature found', 'wtotem' ),
379 'file_changes' => __( 'File changes', 'wtotem' ),
380 //
381 'no_cert' => __( 'No cert', 'wtotem' ),
382 'down' => __( 'Down', 'wtotem' ),
383 'up' => __( 'Up', 'wtotem' ),
384 'infected' => __( 'Infected', 'wtotem' ),
385 'not_installed' => __( 'Need to install', 'wtotem' ),
386 'working' => __( 'Working', 'wtotem' ),
387
388 "critical" => __( 'Critical', 'wtotem' ),
389 "deleted" => __( 'Deleted', 'wtotem' ),
390 "changed" => __( 'Changed', 'wtotem' ),
391 "new" => __( 'New', 'wtotem' ),
392 "scanned" => __( 'Scanned', 'wtotem' ),
393 "quarantine" => __( 'In quarantine', 'wtotem' ),
394 ];
395 }
396