array( 'type' => 'bool', 'label' => __( 'Block config/backup file probes, PHP in uploads and scanner bots', 'zonepilot' ), 'default' => true, ), 'block_xmlrpc' => array( 'type' => 'bool', 'label' => __( 'Block XML-RPC', 'zonepilot' ), 'default' => ! $jetpack, 'help' => $jetpack ? __( 'Jetpack is active and needs XML-RPC, so this is off by default.', 'zonepilot' ) : __( 'Only the mobile app and Jetpack still use it.', 'zonepilot' ), ), 'block_user_enum' => array( 'type' => 'bool', 'label' => __( 'Hide user names (?author= and the public users API)', 'zonepilot' ), 'default' => true, ), 'rate_limit' => array( 'type' => 'bool', 'label' => __( 'Rate-limit login attempts', 'zonepilot' ), 'default' => true, 'help' => __( 'More than 5 login POSTs from one IP within 10 seconds are blocked for a while.', 'zonepilot' ), ), 'challenge_login' => array( 'type' => 'bool', 'label' => __( 'Cloudflare challenge before the login page', 'zonepilot' ), 'default' => false, 'help' => __( 'Extra wall for sites under attack. Turnstile already protects the form itself.', 'zonepilot' ), ), 'headers' => array( 'type' => 'bool', 'label' => __( 'Security headers', 'zonepilot' ), 'default' => true, ), 'hsts' => array( 'type' => 'bool', 'label' => __( 'HSTS (force HTTPS in browsers for 6 months)', 'zonepilot' ), 'default' => 0 === strpos( home_url(), 'https://' ), ), ); } /** * WAF block expression. * * @param array $opts Options. * @return string Empty when nothing to block. */ private function block_expression( array $opts ) { $parts = array(); if ( ! empty( $opts['block_xmlrpc'] ) ) { $parts[] = 'http.request.uri.path eq ' . Site::q( Site::url_path( site_url( 'xmlrpc.php' ) ) ); } if ( ! empty( $opts['block_probes'] ) ) { foreach ( array( '/.env', '/.git/', '/.svn/', '/.htaccess', '/.htpasswd', '/.DS_Store', 'wp-config.php', '/debug.log', '/error_log', '/phpinfo.php', '/composer.json', '/composer.lock', '/package.json', '/.vscode/', '/.idea/' ) as $needle ) { $parts[] = 'http.request.uri.path contains ' . Site::q( $needle ); } foreach ( array( '.sql', '.sql.gz', '.bak', '.old', '.swp', '.tar.gz', '.zip.bak', '.log' ) as $ext ) { $parts[] = 'ends_with(http.request.uri.path, ' . Site::q( $ext ) . ')'; } $parts[] = '(starts_with(http.request.uri.path, ' . Site::q( self::uploads_path() ) . ') and ends_with(lower(http.request.uri.path), ".php"))'; $parts[] = 'http.request.uri.path eq ' . Site::q( Site::url_path( site_url( 'readme.html' ) ) ); $parts[] = 'http.request.uri.path eq ' . Site::q( Site::url_path( site_url( 'license.txt' ) ) ); $parts[] = 'http.request.uri.path eq ' . Site::q( Site::url_path( admin_url( 'install.php' ) ) ); $parts[] = 'http.request.uri.path eq ' . Site::q( Site::url_path( admin_url( 'setup-config.php' ) ) ); $bad_agents = array( 'sqlmap', 'nikto', 'wpscan', 'masscan', 'zgrab', 'acunetix', 'netsparker', 'nuclei', 'dirbuster', 'gobuster', 'fimap', 'havij', 'jorgee', 'nmap scripting engine' ); foreach ( $bad_agents as $agent ) { $parts[] = 'lower(http.user_agent) contains ' . Site::q( $agent ); } $parts[] = 'http.user_agent eq ""'; } if ( ! empty( $opts['block_user_enum'] ) ) { $parts[] = '(http.request.uri.path eq ' . Site::q( Site::url_path( home_url( '/' ), true ) ) . ' and http.request.uri.query contains "author=")'; $users_path = self::users_api_path(); if ( '' !== $users_path ) { $parts[] = '(starts_with(http.request.uri.path, ' . Site::q( $users_path ) . ') and not http.cookie contains "wordpress_logged_in_")'; } // Works with every permalink setting (plain permalinks only have this form). $parts[] = '(http.request.uri.query contains "rest_route=/wp/v2/users" and not http.cookie contains "wordpress_logged_in_")'; } if ( ! $parts ) { return ''; } return Site::host_expr() . ' and (' . implode( ' or ', $parts ) . ')'; } /** * URL path of the users endpoint of the REST API, as this site serves it (custom REST prefixes * included). Empty with plain permalinks, where the endpoint is only reachable via ?rest_route=. * * @return string */ private static function users_api_path() { $url = rest_url( 'wp/v2/users' ); if ( false !== strpos( (string) wp_parse_url( $url, PHP_URL_QUERY ), 'rest_route=' ) ) { return ''; } return Site::url_path( $url ); } /** * URL path of the uploads folder, with a trailing slash. * * @return string */ private static function uploads_path() { $uploads = wp_upload_dir( null, false ); $base = empty( $uploads['baseurl'] ) ? content_url( 'uploads' ) : $uploads['baseurl']; return Site::url_path( $base, true ); } /** * Login challenge expression. * * @return string */ private function challenge_expression() { return Site::host_expr() . ' and http.request.uri.path eq ' . Site::q( Site::url_path( wp_login_url() ) ) . ' and not http.cookie contains "wordpress_logged_in_" and not cf.client.bot'; } /** * Rate limit rule. * * @return array */ private function rate_limit_rule() { return array( 'description' => Site::describe( 'login rate limit' ), 'expression' => Site::host_expr() . ' and http.request.uri.path eq ' . Site::q( Site::url_path( wp_login_url() ) ) . ' and http.request.method eq "POST"', 'action' => 'block', 'ratelimit' => array( 'characteristics' => array( 'ip.src', 'cf.colo.id' ), 'period' => 10, 'requests_per_period' => 5, 'mitigation_timeout' => 10, ), ); } /** * Response headers. * * @param array $opts Options. * @return array name => value */ public function header_values( array $opts ) { $headers = array( 'X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'SAMEORIGIN', 'Referrer-Policy' => 'strict-origin-when-cross-origin', 'Permissions-Policy' => 'camera=(), microphone=(), geolocation=(), interest-cohort=()', 'X-Permitted-Cross-Domain-Policies' => 'none', ); if ( ! empty( $opts['hsts'] ) ) { $headers['Strict-Transport-Security'] = 'max-age=15552000'; } return $headers; } /** {@inheritDoc} */ public function preview( array $opts ) { $items = array(); $expr = $this->block_expression( $opts ); if ( '' !== $expr ) { $what = array(); if ( ! empty( $opts['block_xmlrpc'] ) ) { $what[] = 'xmlrpc.php'; } if ( ! empty( $opts['block_probes'] ) ) { $what[] = __( '.env/.git/wp-config/backup/log files, PHP in uploads, readme.html, scanner bots, empty user agents', 'zonepilot' ); } if ( ! empty( $opts['block_user_enum'] ) ) { $what[] = __( 'user enumeration', 'zonepilot' ); } $items[] = array( 'type' => 'create', /* translators: %s: list */ 'text' => sprintf( __( 'WAF custom rule "block attack probes" (Block): %s.', 'zonepilot' ), implode( '; ', $what ) ), ); } if ( ! empty( $opts['challenge_login'] ) ) { $items[] = array( 'type' => 'create', 'text' => __( 'WAF custom rule "login challenge" (Managed Challenge) for wp-login.php when not logged in.', 'zonepilot' ), ); } if ( ! empty( $opts['rate_limit'] ) ) { $items[] = array( 'type' => 'create', 'text' => __( 'Rate limiting rule: 5 login POSTs per 10 s per IP, then block.', 'zonepilot' ), ); if ( Site::is_free_plan() ) { $items[] = array( 'type' => 'warn', 'text' => __( 'The Free plan allows one rate limiting rule per zone. If the zone already has one, this part is skipped.', 'zonepilot' ), ); } } if ( ! empty( $opts['headers'] ) ) { $list = array(); foreach ( $this->header_values( $opts ) as $name => $value ) { $list[] = $name . ': ' . $value; } $items[] = array( 'type' => 'create', /* translators: %s: header list */ 'text' => sprintf( __( 'Response header rule: %s', 'zonepilot' ), implode( '; ', $list ) ), ); } $items[] = array( 'type' => 'info', /* translators: %s: host names */ 'text' => sprintf( __( 'Every rule only matches %s. Undo removes all of them.', 'zonepilot' ), implode( ', ', Site::hosts() ) ), ); return $items; } /** {@inheritDoc} */ public function apply( array $opts ) { $rules = new Rules(); $messages = array(); $warnings = array(); $expr = $this->block_expression( $opts ); if ( '' !== $expr ) { $res = $rules->ensure( self::PHASE_WAF, 'waf_block', array( 'description' => Site::describe( 'block attack probes' ), 'expression' => $expr, 'action' => 'block', ) ); if ( is_wp_error( $res ) ) { return $res; } $messages[] = __( 'WAF rule saved.', 'zonepilot' ); } else { $rules->remove( self::PHASE_WAF, 'waf_block' ); } if ( ! empty( $opts['challenge_login'] ) ) { $res = $rules->ensure( self::PHASE_WAF, 'waf_login_challenge', array( 'description' => Site::describe( 'login challenge' ), 'expression' => $this->challenge_expression(), 'action' => 'managed_challenge', ) ); if ( is_wp_error( $res ) ) { $warnings[] = sprintf( /* translators: %s: error */ __( 'Login challenge not saved: %s', 'zonepilot' ), $res->get_error_message() ); } } else { $rules->remove( self::PHASE_WAF, 'waf_login_challenge' ); } $rate_limit_ok = false; if ( ! empty( $opts['rate_limit'] ) ) { $res = $rules->ensure( self::PHASE_RL, 'login_rate_limit', $this->rate_limit_rule() ); if ( is_wp_error( $res ) ) { $warnings[] = sprintf( /* translators: %s: error */ __( 'Login rate limit not saved (plan limit?): %s', 'zonepilot' ), $res->get_error_message() ); } else { $rate_limit_ok = true; $messages[] = __( 'Login rate limit saved.', 'zonepilot' ); } } else { $rules->remove( self::PHASE_RL, 'login_rate_limit' ); } if ( ! empty( $opts['headers'] ) ) { $headers = array(); foreach ( $this->header_values( $opts ) as $name => $value ) { $headers[ $name ] = array( 'operation' => 'set', 'value' => $value, ); } $res = $rules->ensure( self::PHASE_HEADERS, 'security_headers', array( 'description' => Site::describe( 'security headers' ), 'expression' => Site::host_expr(), 'action' => 'rewrite', 'action_parameters' => array( 'headers' => $headers ), ) ); if ( is_wp_error( $res ) ) { $warnings[] = sprintf( /* translators: %s: error */ __( 'Security headers not saved: %s', 'zonepilot' ), $res->get_error_message() ); } else { $messages[] = __( 'Security headers saved.', 'zonepilot' ); } } else { $rules->remove( self::PHASE_HEADERS, 'security_headers' ); } $this->mark_applied( $opts, array( 'rate_limit_ok' => $rate_limit_ok, 'warnings' => $warnings, ) ); foreach ( $warnings as $warning ) { Store::log( $this->id(), $warning, 'warn' ); } Store::log( $this->id(), implode( ' ', $messages ), 'ok' ); return array( 'messages' => array_merge( $messages, $warnings ) ); } /** {@inheritDoc} */ public function rollback() { $rules = new Rules(); $errors = array(); foreach ( array( array( self::PHASE_WAF, 'waf_block' ), array( self::PHASE_WAF, 'waf_login_challenge' ), array( self::PHASE_RL, 'login_rate_limit' ), array( self::PHASE_HEADERS, 'security_headers' ), ) as $pair ) { $res = $rules->remove( $pair[0], $pair[1] ); if ( is_wp_error( $res ) ) { $errors[] = $res->get_error_message(); } } if ( $errors ) { return new WP_Error( 'fehu_ap_rollback', implode( '; ', $errors ) ); } $this->mark_off(); Store::log( $this->id(), __( 'Security rules removed.', 'zonepilot' ), 'ok' ); return array( 'messages' => array( __( 'Security rules removed.', 'zonepilot' ) ) ); } /** {@inheritDoc} */ public function time_needed( $operation ) { // apply and verify retry the live checks for up to about a minute while the rules spread. return in_array( $operation, array( 'apply', 'verify' ), true ) ? 150 : 0; } /** {@inheritDoc} */ public function verify() { $opts = $this->options(); $checks = array(); $base = home_url( '/' ); $tries = array(); if ( ! empty( $opts['block_xmlrpc'] ) ) { $tries[ __( 'xmlrpc.php is blocked', 'zonepilot' ) ] = array( site_url( 'xmlrpc.php' ), array() ); } if ( ! empty( $opts['block_probes'] ) ) { $tries[ __( '.env probe is blocked', 'zonepilot' ) ] = array( home_url( '/.env' ), array() ); $tries[ __( 'Scanner user agent is blocked', 'zonepilot' ) ] = array( $base, array( 'user-agent' => 'sqlmap/1.7' ) ); } if ( ! empty( $opts['block_user_enum'] ) ) { $tries[ __( 'Public users API is blocked', 'zonepilot' ) ] = array( rest_url( 'wp/v2/users' ), array() ); } foreach ( $tries as $label => $try ) { $status = 0; $edge = false; for ( $i = 0; $i < 3; $i++ ) { $res = Http::get( $try[0], $try[1] ); $status = $res['status']; // A Cloudflare WAF block page, not a 403 from the server itself. $edge = 403 === $status && ( false !== stripos( $res['body'], 'you have been blocked' ) || false !== stripos( $res['body'], 'cf-error-details' ) || false !== stripos( $res['body'], 'Attention Required! | Cloudflare' ) ); if ( $edge ) { break; } sleep( 2 ); } $detail = $edge ? __( 'Blocked by Cloudflare (HTTP 403)', 'zonepilot' ) : ( 403 === $status ? __( 'HTTP 403 from your server, not from Cloudflare', 'zonepilot' ) : 'HTTP ' . $status ); $checks[] = self::check( $label, $edge ? true : ( 403 === $status ? null : false ), $detail ); } $home = Http::get( $base ); $checks[] = self::check( __( 'Home page still works', 'zonepilot' ), $home['status'] >= 200 && $home['status'] < 400, 'HTTP ' . $home['status'] ); if ( ! empty( $opts['headers'] ) ) { // A new response header rule needs a few seconds to reach every Cloudflare location: retry for up to ~30 s. for ( $i = 0; $i < 7; $i++ ) { $missing = array(); foreach ( array_keys( $this->header_values( $opts ) ) as $name ) { if ( ! isset( $home['headers'][ strtolower( $name ) ] ) ) { $missing[] = $name; } } if ( ! $missing || $i >= 6 ) { break; } sleep( 5 ); $home = Http::get( add_query_arg( 'fehu_ap_check', (string) wp_rand(), $base ) ); } $checks[] = self::check( __( 'Security headers are sent', 'zonepilot' ), ! $missing, $missing ? sprintf( /* translators: %s: header names */ __( 'Missing: %s', 'zonepilot' ), implode( ', ', $missing ) ) : __( 'All present', 'zonepilot' ) ); } if ( ! empty( $opts['rate_limit'] ) ) { $rl = ( new Rules() )->get( self::PHASE_RL, 'login_rate_limit' ); $checks[] = self::check( __( 'Login rate limit active', 'zonepilot' ), is_array( $rl ) && ! empty( $rl['enabled'] ), is_wp_error( $rl ) ? $rl->get_error_message() : ( $rl ? __( 'Active', 'zonepilot' ) : __( 'Not saved (plan limit)', 'zonepilot' ) ) ); } return $this->store_checks( $checks ); } }