getTimestamp(), $digest, 'aowpcs_cron_digest'); } } public static function recipients() { $list = AOWPCS_Settings::get('notify_recipients'); if ($list === '') { $list = is_multisite() ? get_site_option('admin_email') : get_option('admin_email'); } return array_values(array_filter(array_map('trim', explode(',', $list)), 'is_email')); } public static function wants($event) { return AOWPCS_Settings::get('notify_email') && in_array($event, (array) AOWPCS_Settings::get('notify_events'), true); } /** * Send an e-mail unless the hourly limit is reached. Returns true when sent. */ public static function send($subject, $lines, $type) { $recipients = self::recipients(); if (empty($recipients)) { return false; } $hour = gmdate('YmdH'); $sent = AOWPCS_Settings::state('mail_counter'); if (!is_array($sent) || !isset($sent['hour']) || $sent['hour'] !== $hour) { $sent = array('hour' => $hour, 'count' => 0); } if ($sent['count'] >= (int) AOWPCS_Settings::get('notify_max_per_hour')) { return false; } $site = wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES); $footer = array( '', /* translators: %s: url */ sprintf(__('Security Overview: %s', 'all-in-one-wp-content-security'), AOWPCS_Plugin::admin_url('aowpcs')), '', /* translators: 1: value, 2: url */ sprintf(__('You receive this e-mail because notifications are enabled in %1$s → Settings → Notifications on %2$s.', 'all-in-one-wp-content-security'), AOWPCS_PLUGIN_FULLNAME, home_url('/')), ); // Plain text only: titles and paths can contain text chosen by an attacker (file names, user // names), which must not become markup if another plugin switches e-mails to HTML $lines = array_map(function ($line) { return wp_strip_all_tags((string) $line); }, array_merge($lines, $footer)); $mail_subject = trim(preg_replace('/[\r\n\t]+/', ' ', wp_strip_all_tags(sprintf('[%s] %s', $site, $subject)))); $ok = wp_mail($recipients, $mail_subject, implode("\n", $lines), array('Content-Type: text/plain; charset=UTF-8')); if ($ok) { $sent['count']++; AOWPCS_Settings::set_state('mail_counter', $sent); /* translators: 1: subject, 2: recipients */ AOWPCS_Event_Log::log('notification_sent', array('category' => 'system', 'severity' => 'low', 'action' => 'notified', 'message' => sprintf(__('E-mail "%1$s" sent to %2$d recipient(s)', 'all-in-one-wp-content-security'), $subject, count($recipients)), 'data' => array('type' => $type))); } return $ok; } private static function finding_line($finding) { $severities = AOWPCS_Plugin::severities(); return array( sprintf('[%s] %s', strtoupper($severities[$finding->severity]), $finding->title), ' ' . AOWPCS_Plugin::admin_url('aowpcs-issues', array('finding' => (int) $finding->id)), ); } /** * After a scan: new or changed issues at or above the configured severity, failures, summary */ public static function scan_finished($scan_id, $status) { global $wpdb; $scan = AOWPCS_Scan_Store::get_scan($scan_id); if (!$scan) { return; } if ($status === 'partial' && self::wants('scan_failed') && !$scan->cancel_requested) { /* translators: 1: scan profile, 2: scan id, 3: scan status reason */ self::send(__('Security scan did not complete', 'all-in-one-wp-content-security'), array(sprintf(__('The %1$s scan #%2$d ended early: %3$s', 'all-in-one-wp-content-security'), $scan->profile, $scan->id, $scan->status_reason)), 'scan_failed'); } $min = AOWPCS_Plugin::severity_rank(AOWPCS_Settings::get('notify_min_severity')); $rows = $wpdb->get_results("SELECT id, check_id, severity, title, kind, evidence_hash, notified_hash FROM " . AOWPCS_Plugin::table('findings') . " WHERE state = 'open' ORDER BY FIELD(severity,'critical','high','medium','low') LIMIT 500"); $new = array(); foreach ($rows as $row) { $hash = sha1($row->evidence_hash . '|' . $row->severity); if ($row->notified_hash === $hash || AOWPCS_Plugin::severity_rank($row->severity) < $min || $row->kind === 'informational') { continue; } $new[] = $row; } if ($new && self::wants('findings')) { /* translators: Placeholder is a number or value shown in the message */ $lines = array(sprintf(_n('%d new security issue needs attention:', '%d new security issues need attention:', count($new), 'all-in-one-wp-content-security'), count($new)), ''); foreach (array_slice($new, 0, 25) as $finding) { $lines = array_merge($lines, self::finding_line($finding)); } if (count($new) > 25) { /* translators: %d: count */ $lines[] = sprintf(__('… and %d more.', 'all-in-one-wp-content-security'), count($new) - 25); } $critical = count(array_filter($new, function ($f) { return $f->severity === 'critical'; })); /* translators: Placeholder is a number or value shown in the message */ $subject = $critical ? sprintf(_n('%d critical security issue', '%d critical security issues', $critical, 'all-in-one-wp-content-security'), $critical) : sprintf(_n('%d new security issue', '%d new security issues', count($new), 'all-in-one-wp-content-security'), count($new)); if (self::send($subject, $lines, 'findings')) { foreach ($new as $finding) { $wpdb->update(AOWPCS_Plugin::table('findings'), array('notified_hash' => sha1($finding->evidence_hash . '|' . $finding->severity)), array('id' => (int) $finding->id)); } } } if (self::wants('scan_complete') && in_array($scan->profile, array('full', 'quick'), true)) { $counts = AOWPCS_Scan_Store::severity_counts('open'); /* translators: %d: scan score */ self::send(sprintf(__('Security scan completed — score %d', 'all-in-one-wp-content-security'), (int) $scan->score), array( /* translators: 1: scan profile, 2: scan id, 3: status */ sprintf(__('%1$s scan #%2$d finished (%3$s).', 'all-in-one-wp-content-security'), ucfirst($scan->profile), $scan->id, $status), /* translators: %d: scan score */ sprintf(__('Score: %d / 100', 'all-in-one-wp-content-security'), (int) $scan->score), /* translators: 1: counts critical, 2: counts high, 3: counts medium, 4: counts low */ sprintf(__('Open issues: %1$d critical, %2$d high, %3$d medium, %4$d low', 'all-in-one-wp-content-security'), $counts['critical'], $counts['high'], $counts['medium'], $counts['low']), /* translators: %d: scan rules passed */ sprintf(__('Checks passed: %d', 'all-in-one-wp-content-security'), (int) $scan->rules_passed), ), 'scan_complete'); } } /** * Immediate alerts for new administrators; login attacks are summarised hourly */ public static function event_logged($type, $row) { if (in_array($type, array('admin_created', 'admin_granted'), true) && self::wants('admin_created')) { self::send(__('A new administrator account was added', 'all-in-one-wp-content-security'), array( $row['message'], /* translators: %s: date and time */ sprintf(__('Time: %s', 'all-in-one-wp-content-security'), AOWPCS_Plugin::format_time($row['created_at'])), '', __('If you did not expect this, review Users → All Users immediately and change your passwords.', 'all-in-one-wp-content-security'), admin_url('users.php?role=administrator'), ), 'admin_created'); } if ($type === 'login_lockout' && self::wants('login_attacks') && !wp_next_scheduled('aowpcs_cron_login_alert')) { wp_schedule_single_event(time() + 15 * MINUTE_IN_SECONDS, 'aowpcs_cron_login_alert'); } } public static function login_alert() { $since = AOWPCS_Plugin::gmt_ago(HOUR_IN_SECONDS); $lockouts = AOWPCS_Event_Log::count(array('event_type' => 'login_lockout', 'since' => $since)); $failed = AOWPCS_Event_Log::counter_total('login_failed', gmdate('Y-m-d H:00:00', time() - HOUR_IN_SECONDS)); if ($lockouts < 1) { return; } self::send(__('Repeated login attacks were blocked', 'all-in-one-wp-content-security'), array( /* translators: 1: lockouts, 2: failed */ sprintf(__('In the last hour %1$d addresses were blocked temporarily after repeated failed logins (%2$d failed logins in total).', 'all-in-one-wp-content-security'), $lockouts, $failed), __('No action is needed unless one of your own users was locked out (you can unblock addresses in Firewall & Protection).', 'all-in-one-wp-content-security'), AOWPCS_Plugin::admin_url('aowpcs-events', array('event_type' => 'login_lockout')), ), 'login_attacks'); } /** * Daily or weekly summary */ public static function digest() { $period = AOWPCS_Settings::get('digest') === 'weekly' ? WEEK_IN_SECONDS : DAY_IN_SECONDS; $since = AOWPCS_Plugin::gmt_ago($period); $counts = AOWPCS_Scan_Store::severity_counts('open'); $score = AOWPCS_Score::current(); $band = AOWPCS_Score::band($score); $latest = AOWPCS_Scan_Store::latest_scan(); $lines = array( /* translators: 1: score, 2: band label */ sprintf(__('Security score: %1$d / 100 (%2$s)', 'all-in-one-wp-content-security'), $score, $band['label']), /* translators: 1: counts critical, 2: counts high, 3: counts medium, 4: counts low */ sprintf(__('Open issues: %1$d critical, %2$d high, %3$d medium, %4$d low', 'all-in-one-wp-content-security'), $counts['critical'], $counts['high'], $counts['medium'], $counts['low']), /* translators: %s: date and time */ sprintf(__('Last scan: %s', 'all-in-one-wp-content-security'), $latest ? AOWPCS_Plugin::format_time($latest->finished_at) : __('never', 'all-in-one-wp-content-security')), '', /* translators: %d: value */ sprintf(__('Failed logins: %d', 'all-in-one-wp-content-security'), AOWPCS_Event_Log::counter_total('login_failed', $since)), /* translators: %d: count */ sprintf(__('Temporary blocks: %d', 'all-in-one-wp-content-security'), AOWPCS_Event_Log::count(array('event_type' => array('ip_blocked'), 'since' => $since))), /* translators: %d: value */ sprintf(__('Suspicious requests: %d', 'all-in-one-wp-content-security'), AOWPCS_Event_Log::counter_total('suspicious', $since)), /* translators: Placeholder is a number or value shown in the message */ sprintf(__('Issues resolved: %d', 'all-in-one-wp-content-security'), count(array_filter((array) AOWPCS_Scan_Store::recently_resolved(100), function ($f) use ($since) { return $f->resolved_at >= $since; }))), ); $top = AOWPCS_Scan_Store::findings(array('state' => 'open', 'limit' => 5)); if ($top) { $lines[] = ''; $lines[] = __('Most important open issues:', 'all-in-one-wp-content-security'); foreach ($top as $finding) { $lines = array_merge($lines, self::finding_line($finding)); } } self::send(AOWPCS_Settings::get('digest') === 'weekly' ? __('Weekly security summary', 'all-in-one-wp-content-security') : __('Daily security summary', 'all-in-one-wp-content-security'), $lines, 'digest'); } } /** * Daily housekeeping: retention, expired blocks, published address lists */ class AOWPCS_Maintenance { public static function init() { add_action('aowpcs_cron_maintenance', array(__CLASS__, 'run')); } public static function run() { AOWPCS_Event_Log::prune((int) AOWPCS_Settings::get('log_retention_days')); AOWPCS_Rate_Limiter::prune(); AOWPCS_Block_List::clear_expired(); AOWPCS_Scan_Store::prune(180); AOWPCS_Scan_Runner::recover_stale(); if (AOWPCS_Settings::get('verify_crawlers')) { $ranges = get_site_option(AOWPCS_Crawler_Verifier::RANGES_OPTION, array()); if (empty($ranges['fetched']) || $ranges['fetched'] < time() - WEEK_IN_SECONDS) { AOWPCS_Crawler_Verifier::refresh_ranges(); } } if (AOWPCS_Settings::get('proxy_cloudflare')) { $stored = get_site_option('aowpcs_cloudflare_ranges'); if (!is_array($stored) || empty($stored['fetched']) || $stored['fetched'] < time() - WEEK_IN_SECONDS) { self::refresh_cloudflare(); } } } public static function refresh_cloudflare() { $ranges = array(); foreach (array('https://www.cloudflare.com/ips-v4', 'https://www.cloudflare.com/ips-v6') as $url) { $response = wp_remote_get($url, array('timeout' => 15)); if (is_wp_error($response) || (int) wp_remote_retrieve_response_code($response) !== 200) { return; } foreach (preg_split('/\s+/', trim(wp_remote_retrieve_body($response))) as $range) { if (AOWPCS_IP::is_valid_range($range)) { $ranges[] = $range; } } } if (count($ranges) > 5) { update_site_option('aowpcs_cloudflare_ranges', array('ranges' => $ranges, 'fetched' => time())); } } }