| @@ -20,9 +20,9 @@ | ||
| 20 | 20 | public function __construct( $core ) { |
| 21 | 21 | parent::__construct( $core ); |
| 22 | 22 | |
| 23 | 23 | // In case the software has never been synchronized, force it. |
| 24 | - if ( ! get_option( 'patchstack_software_data_hash', false ) && ! get_option( 'patchstack_software_upload_attempted', false ) ) { | |
| 24 | + if ( ! get_option( 'patchstack_software_data_hash', false ) ) { | |
| 25 | 25 | $this->upload_software(); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // Register the actions. |
| @@ -28,9 +28,8 @@ | ||
| 28 | 28 | // Register the actions. |
| 29 | 29 | add_action( 'patchstack_send_software_data', [ $this, 'upload_software' ] ); |
| 30 | 30 | add_action( 'patchstack_send_hacker_logs', [ $this, 'upload_firewall_logs' ] ); |
| 31 | 31 | add_action( 'patchstack_send_event_logs', [ $this, 'upload_activity_logs' ] ); |
| 32 | - add_action( 'patchstack_import_ap_logs', [ $this, 'import_ap_logs' ] ); | |
| 33 | 32 | |
| 34 | 33 | // In case a plugin or upgrade has been performed, re-synchronize with the app. |
| 35 | 34 | add_action( 'activated_plugin', [ $this, 'upload_software' ] ); |
| 36 | 35 | add_action( 'deactivated_plugin', [ $this, 'upload_software' ] ); |
| @@ -48,17 +47,12 @@ | ||
| 48 | 47 | public function upload_software() { |
| 49 | 48 | // Get the software data and hash. |
| 50 | 49 | $data = $this->get_software_data(); |
| 51 | 50 | $hash = sha1( json_encode( $data ) ); |
| 52 | - | |
| 53 | - // Do not sync for no reason. | |
| 54 | - if ( ! defined( 'DOING_CRON' ) && ! isset( $_POST['patchstack_secret'] ) && get_option( 'patchstack_software_data_hash', false ) === $hash && ! is_admin() && ! defined( 'WP_CLI' ) ) { | |
| 51 | + if ( ! defined( 'DOING_CRON' ) && ! isset( $_POST['webarx_secret'] ) && get_option( 'patchstack_software_data_hash', false ) === $hash && ! is_admin() ) { | |
| 55 | 52 | return; |
| 56 | 53 | } |
| 57 | 54 | |
| 58 | - // Make sure to not keep calling this function. | |
| 59 | - update_option( 'patchstack_software_upload_attempted', 1 ); | |
| 60 | - | |
| 61 | 55 | // Synchronize the software list with the API. |
| 62 | 56 | $results = $this->plugin->api->upload_software( [ 'software' => json_encode( $data ) ] ); |
| 63 | 57 | if ( isset( $results['success'] ) ) { |
| 64 | 58 | update_option( 'patchstack_software_data_hash', $hash ); |
| @@ -74,9 +68,9 @@ | ||
| 74 | 68 | // If we have vulnerable plugins, determine if we had them before and if not, pull latest firewall rules. |
| 75 | 69 | if ( isset( $results['vulnerable'] ) && count( $results['vulnerable'] ) > 0 ) { |
| 76 | 70 | $prev = get_site_option( 'patchstack_latest_vulnerable', [] ); |
| 77 | 71 | foreach ( $results['vulnerable'] as $vuln ) { |
| 78 | - if ( is_array( $prev ) && ! in_array ( $vuln, $prev ) ) { | |
| 72 | + if ( ! in_array ( $vuln, $prev ) ) { | |
| 79 | 73 | do_action( 'patchstack_post_dynamic_firewall_rules' ); |
| 80 | 74 | break; |
| 81 | 75 | } |
| 82 | 76 | } |
| @@ -85,14 +79,8 @@ | ||
| 85 | 79 | } else { |
| 86 | 80 | update_site_option( 'patchstack_latest_vulnerable', [] ); |
| 87 | 81 | } |
| 88 | 82 | |
| 89 | - // If we received the number of vulnerable count. | |
| 90 | - if ( isset ( $results['vulnerability_count'], $results['vulnerability_fix_count'] ) ) { | |
| 91 | - update_option( 'patchstack_vulns_present', $results['vulnerability_count'] ); | |
| 92 | - update_option( 'patchstack_fixes_present', $results['vulnerability_fix_count'] ); | |
| 93 | - } | |
| 94 | - | |
| 95 | 83 | return $results; |
| 96 | 84 | } |
| 97 | 85 | |
| 98 | 86 | return; |
| @@ -104,90 +92,55 @@ | ||
| 104 | 92 | * @return void |
| 105 | 93 | */ |
| 106 | 94 | public function upload_firewall_logs() { |
| 107 | 95 | global $wpdb; |
| 96 | + $lastid = get_option( 'patchstack_firewall_log_lastid', 0 ); | |
| 97 | + $items = $wpdb->get_results( $wpdb->prepare( 'SELECT ip, log_date, request_uri, user_agent, fid, method, post_data FROM ' . $wpdb->prefix . 'patchstack_firewall_log WHERE id > %d ORDER BY id', $lastid ) ); | |
| 108 | 98 | |
| 109 | - // Do not execute upload action on free sites. | |
| 110 | - if ( ! $this->license_is_active() || $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { | |
| 99 | + // No need to synchronize if there are no new logs present. | |
| 100 | + if ( $wpdb->num_rows == 0 ) { | |
| 111 | 101 | return; |
| 112 | 102 | } |
| 113 | 103 | |
| 114 | - // Do not process if we are already processing a previous batch. | |
| 115 | - if ( get_option( 'patchstack_firewall_log_processing', false ) ) { | |
| 116 | - return; | |
| 117 | - } | |
| 104 | + // Construct the array to be uploaded to our API. | |
| 105 | + $logs = []; | |
| 106 | + foreach ( $items as $item ) { | |
| 118 | 107 | |
| 119 | - update_option( 'patchstack_firewall_log_processing', 1 ); | |
| 120 | - | |
| 121 | - // Attempt to fetch data, if any. | |
| 122 | - $lastId = get_option( 'patchstack_firewall_log_lastid', 0 ); | |
| 123 | - $successId = $lastId; | |
| 124 | - | |
| 125 | - // Do a maximum of 100 log entries per cronjob. | |
| 126 | - for ($i = 0; $i <= 1; $i++) { | |
| 127 | - // Pull the data from the database, in batches of 100. | |
| 128 | - $items = $wpdb->get_results( | |
| 129 | - $wpdb->prepare( | |
| 130 | - 'SELECT id, ip, log_date, request_uri, user_agent, fid, method, post_data FROM ' . $wpdb->prefix . 'patchstack_firewall_log WHERE id > %d ORDER BY id LIMIT 0,100', | |
| 131 | - $lastId | |
| 132 | - ) | |
| 133 | - ); | |
| 134 | - | |
| 135 | - // No need to continue if we have no data. | |
| 136 | - if ( $wpdb->num_rows == 0 ) { | |
| 137 | - update_option( 'patchstack_firewall_log_lastid', 0 ); | |
| 138 | - break; | |
| 108 | + // Entries that we don't want to store on the API side. | |
| 109 | + if ( stripos( $item->request_uri, 'wp-comments-post' ) !== false ) { | |
| 110 | + continue; | |
| 139 | 111 | } |
| 140 | - | |
| 141 | - // Construct the array to be uploaded to our API. | |
| 142 | - $logs = []; | |
| 143 | - foreach ( $items as $item ) { | |
| 144 | 112 | |
| 145 | - // Always advance the cursor so filtered rows don't stall the batch. | |
| 146 | - $lastId = $item->id; | |
| 113 | + // Push to entries to be uploaded. | |
| 114 | + $logs[] = [ | |
| 115 | + 'ip' => $item->ip, | |
| 116 | + 'fid' => $item->fid, | |
| 117 | + 'request_uri' => $item->request_uri, | |
| 118 | + 'user_agent' => $item->user_agent, | |
| 119 | + 'method' => $item->method, | |
| 120 | + 'log_date' => $item->log_date, | |
| 121 | + 'post_data' => $item->post_data, | |
| 122 | + ]; | |
| 123 | + } | |
| 147 | 124 | |
| 148 | - // Entries that we don't want to store on the API side. | |
| 149 | - if ( stripos( $item->request_uri, 'wp-comments-post' ) !== false ) { | |
| 150 | - continue; | |
| 151 | - } | |
| 152 | - | |
| 153 | - // Push to entries to be uploaded. | |
| 154 | - $logs[] = [ | |
| 155 | - 'ip' => $item->ip, | |
| 156 | - 'fid' => $item->fid, | |
| 157 | - 'request_uri' => $item->request_uri, | |
| 158 | - 'user_agent' => $item->user_agent, | |
| 159 | - 'method' => $item->method, | |
| 160 | - 'log_date' => $item->log_date, | |
| 161 | - 'post_data' => $item->post_data, | |
| 162 | - ]; | |
| 163 | - } | |
| 164 | - | |
| 165 | - // JSON encode the logs and upload. | |
| 166 | - $logs = json_encode( $logs ); | |
| 167 | - $results = $this->plugin->api->upload_firewall_logs( | |
| 168 | - [ | |
| 169 | - 'logs' => $logs, | |
| 170 | - 'type' => 'firewall', | |
| 171 | - ] | |
| 172 | - ); | |
| 173 | - | |
| 174 | - // A failed upload returns a status code or null instead of an array; | |
| 175 | - // bail without advancing so the logs are not deleted before they reach the API. | |
| 176 | - if ( ! is_array( $results ) || isset( $results['errors'] ) ) { | |
| 177 | - update_option( 'patchstack_firewall_log_lastid', $successId ); | |
| 178 | - break; | |
| 179 | - } | |
| 180 | - | |
| 181 | - $successId = $lastId; | |
| 182 | - update_option( 'patchstack_firewall_log_lastid', $successId ); | |
| 125 | + // JSON encode the logs and upload. | |
| 126 | + $logs = json_encode( $logs ); | |
| 127 | + $results = $this->plugin->api->upload_firewall_logs( | |
| 128 | + [ | |
| 129 | + 'logs' => $logs, | |
| 130 | + 'type' => 'firewall', | |
| 131 | + ] | |
| 132 | + ); | |
| 133 | + if ( isset( $results['errors'] ) ) { | |
| 134 | + return; | |
| 183 | 135 | } |
| 184 | 136 | |
| 185 | - // Delete the logs. | |
| 186 | - $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_firewall_log WHERE id <= ' . (int) $successId ); | |
| 137 | + // Get the most recent id of the logs. | |
| 138 | + $lastid = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'patchstack_firewall_log ORDER BY id DESC LIMIT 0, 1' ); | |
| 139 | + update_option( 'patchstack_firewall_log_lastid', $lastid ); | |
| 187 | 140 | |
| 188 | - // No longer processing. | |
| 189 | - update_option( 'patchstack_firewall_log_processing', 0 ); | |
| 141 | + // Delete logs that are older than 2 weeks. | |
| 142 | + $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_firewall_log WHERE log_date < DATE_SUB(NOW(), INTERVAL 14 DAY)' ); | |
| 190 | 143 | } |
| 191 | 144 | |
| 192 | 145 | /** |
| 193 | 146 | * Synchronize the activity logs with our API. |
| @@ -196,20 +149,8 @@ | ||
| 196 | 149 | */ |
| 197 | 150 | public function upload_activity_logs() { |
| 198 | 151 | global $wpdb; |
| 199 | 152 | |
| 200 | - // Do not execute upload action on free sites. | |
| 201 | - if ( ! $this->license_is_active() || $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { | |
| 202 | - return; | |
| 203 | - } | |
| 204 | - | |
| 205 | - // Do not process if we are already processing a previous batch. | |
| 206 | - if ( get_option( 'patchstack_eventlog_processing', false ) ) { | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - update_option( 'patchstack_eventlog_processing', 1 ); | |
| 211 | - | |
| 212 | 153 | // Determine if we should upload failed logins to the app. |
| 213 | 154 | $where = " AND action != 'failed login' "; |
| 214 | 155 | if ( $this->get_option( 'patchstack_activity_log_failed_logins_db', 0 ) == 1 ) { |
| 215 | 156 | $where = ' '; |
| @@ -214,52 +155,28 @@ | ||
| 214 | 155 | if ( $this->get_option( 'patchstack_activity_log_failed_logins_db', 0 ) == 1 ) { |
| 215 | 156 | $where = ' '; |
| 216 | 157 | } |
| 217 | 158 | |
| 218 | - // Attempt to fetch data, if any. | |
| 219 | - $lastId = get_option( 'patchstack_eventlog_lastid', 0 ); | |
| 220 | - $successId = $lastId; | |
| 159 | + // Do we have data to upload? | |
| 160 | + $lastid = get_option( 'patchstack_eventlog_lastid', 0 ); | |
| 161 | + $items = $wpdb->get_results( $wpdb->prepare( 'SELECT author, ip, object, object_id, object_name, action, date FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE id > %d' . $where . 'ORDER BY id', [ $lastid ] ) ); | |
| 162 | + if ( $wpdb->num_rows == 0 ) { | |
| 163 | + return; | |
| 164 | + } | |
| 221 | 165 | |
| 222 | - // Do a maximum of several hundred log entries per cronjob. | |
| 223 | - for ($i = 0; $i <= 1; $i++) { | |
| 224 | - // Pull the data from the database, in batches of 100. | |
| 225 | - $items = $wpdb->get_results( | |
| 226 | - $wpdb->prepare( | |
| 227 | - 'SELECT id, author, ip, object, object_id, object_name, action, date FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE id > %d' . $where . 'ORDER BY id LIMIT 0,100', | |
| 228 | - $lastId | |
| 229 | - ), | |
| 230 | - ARRAY_A | |
| 231 | - ); | |
| 232 | - | |
| 233 | - // No need to continue if we have no data. | |
| 234 | - if ( $wpdb->num_rows == 0 ) { | |
| 235 | - update_option( 'patchstack_eventlog_lastid', 0 ); | |
| 236 | - break; | |
| 237 | - } | |
| 238 | - | |
| 239 | - // Get the last ID in the result set. | |
| 240 | - $lastId = $items[count($items) - 1]['id']; | |
| 241 | - | |
| 242 | - // Send to the API. | |
| 243 | - $logs = json_encode( $items ); | |
| 244 | - $results = $this->plugin->api->upload_activity_logs( [ 'logs' => $logs ] ); | |
| 245 | - | |
| 246 | - // A failed upload returns a status code or null instead of an array; | |
| 247 | - // bail without advancing so the logs are not deleted before they reach the API. | |
| 248 | - if ( ! is_array( $results ) || isset( $results['errors'] ) ) { | |
| 249 | - update_option( 'patchstack_eventlog_lastid', $successId ); | |
| 250 | - break; | |
| 251 | - } | |
| 252 | - | |
| 253 | - $successId = $lastId; | |
| 254 | - update_option( 'patchstack_eventlog_lastid', $successId ); | |
| 166 | + // Send to the API. | |
| 167 | + $logs = json_encode( $items ); | |
| 168 | + $results = $this->plugin->api->upload_activity_logs( [ 'logs' => $logs ] ); | |
| 169 | + if ( isset( $results['errors'] ) ) { | |
| 170 | + return; | |
| 255 | 171 | } |
| 256 | 172 | |
| 257 | - // Delete the logs. | |
| 258 | - $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE id <= ' . (int) $successId ); | |
| 173 | + // Get the most recent id of the logs. | |
| 174 | + $lastid = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'patchstack_event_log ORDER BY id DESC LIMIT 0, 1' ); | |
| 175 | + update_option( 'patchstack_eventlog_lastid', $lastid ); | |
| 259 | 176 | |
| 260 | - // No longer processing. | |
| 261 | - update_option( 'patchstack_eventlog_processing', 0 ); | |
| 177 | + // Delete logs that are older than 2 weeks. | |
| 178 | + $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE date < DATE_SUB(NOW(), INTERVAL 14 DAY)' ); | |
| 262 | 179 | } |
| 263 | 180 | |
| 264 | 181 | /** |
| 265 | 182 | * Obtain information about the software that the user has installed. |
| @@ -275,9 +192,9 @@ | ||
| 275 | 192 | require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 276 | 193 | } |
| 277 | 194 | |
| 278 | 195 | // Refetch updates data if we are performing a plugin listener related action. |
| 279 | - if ( isset( $_POST['patchstack_secret'] ) ) { | |
| 196 | + if ( isset( $_POST['webarx_secret'] ) ) { | |
| 280 | 197 | @require_once ABSPATH . 'wp-includes/update.php'; |
| 281 | 198 | @wp_update_themes(); |
| 282 | 199 | @wp_update_plugins(); |
| 283 | 200 | } |
| @@ -370,98 +287,7 @@ | ||
| 370 | 287 | 'sw_new_ver' => '' |
| 371 | 288 | ]; |
| 372 | 289 | } |
| 373 | 290 | |
| 374 | - $software_list = apply_filters( 'patchstack_get_software_data', $software_list ); | |
| 375 | 291 | return $software_list; |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | - * Import the logs generated by the auto prepend firewall rules. | |
| 380 | - * | |
| 381 | - * @return void | |
| 382 | - */ | |
| 383 | - public function import_ap_logs() | |
| 384 | - { | |
| 385 | - if ( ! get_option( 'patchstack_firewall_ap_enabled', false ) ) { | |
| 386 | - return; | |
| 387 | - } | |
| 388 | - | |
| 389 | - // Do not process if we are already processing a previous batch. | |
| 390 | - if ( get_option( 'patchstack_firewall_log_ap_processing', false ) ) { | |
| 391 | - return; | |
| 392 | - } | |
| 393 | - | |
| 394 | - // Attempt to load config file. | |
| 395 | - $logs = __DIR__ . '/../../../pslogs/logs.php'; | |
| 396 | - if ( ! file_exists( $logs ) ) { | |
| 397 | - return; | |
| 398 | - } | |
| 399 | - | |
| 400 | - // Load the extension. | |
| 401 | - if ( ! file_exists( __DIR__ . '/../lib/patchstack/vendor/autoload.php' ) ) { | |
| 402 | - return; | |
| 403 | - } | |
| 404 | - | |
| 405 | - // Set the processing lock only once we know there is work to do, otherwise an | |
| 406 | - // early return above would leave the lock stuck and block all future imports. | |
| 407 | - update_option( 'patchstack_firewall_log_ap_processing', 1 ); | |
| 408 | - | |
| 409 | - global $wpdb; | |
| 410 | - | |
| 411 | - require_once __DIR__ . '/../lib/patchstack/vendor/autoload.php'; | |
| 412 | - $extension = new Patchstack\Extensions\WordPress\Extension( [], $this ); | |
| 413 | - | |
| 414 | - // Read the logs file. | |
| 415 | - $file = new SplFileObject( $logs ); | |
| 416 | - | |
| 417 | - // Iterate through each line. | |
| 418 | - while ( ! $file->eof() ) { | |
| 419 | - $line = $file->fgets(); | |
| 420 | - | |
| 421 | - // Skip first line. | |
| 422 | - if ( trim($line) == '' || strpos( $line, '<?php' ) !== false ) { | |
| 423 | - continue; | |
| 424 | - } | |
| 425 | - | |
| 426 | - // Decode the line to import. | |
| 427 | - $data = json_decode( base64_decode( $line ), true ); | |
| 428 | - if ( ! $data || ! is_array( $data ) ) { | |
| 429 | - continue; | |
| 430 | - } | |
| 431 | - | |
| 432 | - // Skip malformed entries that are missing the fields we rely on below. | |
| 433 | - if ( ! isset( $data['site_id'], $data['ip'], $data['request_uri'], $data['user_agent'], $data['method'], $data['fid'], $data['post_data'] ) ) { | |
| 434 | - continue; | |
| 435 | - } | |
| 436 | - | |
| 437 | - // Insert into the logs. | |
| 438 | - $wpdb->insert( | |
| 439 | - $wpdb->get_blog_prefix( $data['site_id'] ) . 'patchstack_firewall_log', | |
| 440 | - [ | |
| 441 | - 'ip' => $data['ip'], | |
| 442 | - 'request_uri' => $data['request_uri'], | |
| 443 | - 'user_agent' => $data['user_agent'], | |
| 444 | - 'method' => $data['method'], | |
| 445 | - 'fid' => $data['fid'], | |
| 446 | - 'flag' => '', | |
| 447 | - 'post_data' => $data['post_data'], | |
| 448 | - 'block_type' => 'BLOCK' | |
| 449 | - ] | |
| 450 | - ); | |
| 451 | - | |
| 452 | - // Update counters. | |
| 453 | - $hits = (int) $this->get_blog_option( $data['site_id'], 'patchstack_hits_all_time', 0 ); | |
| 454 | - $this->update_blog_option( $data['site_id'], 'patchstack_hits_all_time', $hits + 1 ); | |
| 455 | - | |
| 456 | - $counters = $this->get_blog_option( $data['site_id'], 'patchstack_hits_last_30', [] ); | |
| 457 | - $counters = $extension->merge_counters( [ date('Y-m-d') => 1 ], $counters ); | |
| 458 | - $this->update_blog_option( $data['site_id'], 'patchstack_hits_last_30', $counters ); | |
| 459 | - } | |
| 460 | - | |
| 461 | - $file = null; | |
| 462 | - file_put_contents( $logs, '<?php exit; ?>' . PHP_EOL ); | |
| 463 | - | |
| 464 | - // Update processing state. | |
| 465 | - update_option( 'patchstack_firewall_log_ap_processing', 0 ); | |
| 466 | 292 | } |
| 467 | 293 | } |