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