array( 'type' => 'bool', 'label' => __( 'Origin lock: refuse requests that did not come through Cloudflare', 'zonepilot' ), 'default' => true, 'help' => __( 'Pro. A secret header is added by Cloudflare and checked by your server. /.well-known/ stays open for SSL renewals.', 'zonepilot' ), 'pro' => true, ), ); } /** * Whether the Pro lock engine is present and licensed. * * @return bool */ public static function lock_available() { return class_exists( Origin_Lock::class ) && License::is_pro(); } /* ---------------------------------------------------------------- Scan */ /** * Is the value a public IP? * * @param string $ip IP. * @return bool */ private static function public_ip( $ip ) { return (bool) filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); } /** * Whether an IPv4 address is inside a CIDR. * * @param string $ip IP. * @param string $cidr CIDR or single IP. * @return bool */ private static function in_cidr( $ip, $cidr ) { if ( false === strpos( $cidr, '/' ) ) { return $ip === $cidr; } list( $net, $bits ) = explode( '/', $cidr, 2 ); if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) || ! filter_var( $net, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) { return false; } $bits = (int) $bits; $mask = 0 === $bits ? 0 : ( ~0 << ( 32 - $bits ) ) & 0xFFFFFFFF; return ( ip2long( $ip ) & $mask ) === ( ip2long( $net ) & $mask ); } /** * Scan DNS for origin exposure. * * @return array|WP_Error ['origin_ips' => [], 'findings' => [], 'records' => int, 'at' => int] */ public function scan() { $cf = Cloudflare::stored(); $records = $cf->dns_records( Site::zone_id() ); if ( is_wp_error( $records ) ) { return $records; } $hosts = Site::hosts(); $by_name = array(); foreach ( $records as $r ) { $by_name[ strtolower( $r['name'] ) ][] = $r; } // 1. Origin IPs = what the site's own host record points to (+ this server's address). $origin_ips = array(); $findings = array(); foreach ( $hosts as $host ) { $own = isset( $by_name[ $host ] ) ? $by_name[ $host ] : array(); foreach ( $own as $r ) { if ( in_array( $r['type'], array( 'A', 'AAAA' ), true ) ) { $origin_ips[] = $r['content']; } if ( 'CNAME' === $r['type'] && isset( $by_name[ strtolower( $r['content'] ) ] ) ) { foreach ( $by_name[ strtolower( $r['content'] ) ] as $t ) { if ( in_array( $t['type'], array( 'A', 'AAAA' ), true ) ) { $origin_ips[] = $t['content']; } } } if ( in_array( $r['type'], array( 'A', 'AAAA', 'CNAME' ), true ) && empty( $r['proxied'] ) ) { $findings[] = $this->finding( $r, 'critical', __( 'Your website itself is not proxied', 'zonepilot' ), __( 'This record is "DNS only", so every visitor sees the server IP and Cloudflare protects nothing. Turn the proxy on.', 'zonepilot' ), array( 'proxy' ) ); } } } $server_addr = isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) : ''; if ( self::public_ip( $server_addr ) ) { $origin_ips[] = $server_addr; } $extra = Store::get( 'extra_origin_ips', array() ); if ( is_array( $extra ) ) { $origin_ips = array_merge( $origin_ips, $extra ); } $origin_ips = array_values( array_unique( array_filter( $origin_ips, array( __CLASS__, 'public_ip' ) ) ) ); // Names (in the zone) that resolve to the origin. $origin_names = array(); foreach ( $records as $r ) { if ( in_array( $r['type'], array( 'A', 'AAAA' ), true ) && in_array( $r['content'], $origin_ips, true ) ) { $origin_names[ strtolower( $r['name'] ) ] = true; } } foreach ( $records as $r ) { $name = strtolower( $r['name'] ); if ( in_array( $name, $hosts, true ) ) { continue; // Handled above. } switch ( $r['type'] ) { case 'A': case 'AAAA': if ( empty( $r['proxied'] ) && in_array( $r['content'], $origin_ips, true ) ) { $service = (bool) preg_match( '/^(mail|smtp|imap|pop|pop3|ftp|sftp|cpanel|whm|webmail|webdisk|cpcalendars|cpcontacts|autodiscover|autoconfig)\./', $name ); $findings[] = $this->finding( $r, 'high', __( 'DNS-only record points to your server', 'zonepilot' ), $service ? __( 'Anyone can look this name up and get the server IP. It looks like a mail/FTP/panel name: proxying would break non-web services, so delete it if you do not use it, or move that service elsewhere.', 'zonepilot' ) : __( 'Anyone can look this name up and get the server IP. Proxy it through Cloudflare or delete it if unused.', 'zonepilot' ), $service ? array( 'delete', 'proxy' ) : array( 'proxy', 'delete' ) ); } break; case 'CNAME': if ( empty( $r['proxied'] ) && isset( $origin_names[ strtolower( $r['content'] ) ] ) ) { $findings[] = $this->finding( $r, 'medium', __( 'DNS-only alias of a server name', 'zonepilot' ), __( 'This alias leads to a record with your server IP.', 'zonepilot' ), array( 'proxy', 'delete' ) ); } break; case 'MX': $target = strtolower( rtrim( $r['content'], '.' ) ); $hits = isset( $origin_names[ $target ] ); if ( ! $hits && ! isset( $by_name[ $target ] ) ) { $resolved = gethostbyname( $target ); $hits = $resolved !== $target && in_array( $resolved, $origin_ips, true ); } if ( $hits ) { $findings[] = $this->finding( $r, 'high', __( 'Mail (MX) goes to your web server', 'zonepilot' ), __( 'Mail servers must be reachable directly, so this publishes the server IP (Cloudflare even creates a visible _dc-mx record for proxied mail names). Move inbound mail to Cloudflare Email Routing or an external mail provider, then delete this MX.', 'zonepilot' ), array( 'delete' ) ); } break; case 'TXT': $content = trim( $r['content'], '"' ); if ( 0 === stripos( $content, 'v=spf1' ) ) { $finding = $this->spf_finding( $r, $content, $origin_ips, $origin_names, $name ); if ( $finding ) { $findings[] = $finding; } } break; } } $result = array( 'origin_ips' => $origin_ips, 'findings' => $findings, 'records' => count( $records ), 'at' => time(), ); Store::update_module( $this->id(), array( 'scan' => $result ) ); if ( $origin_ips ) { Store::set( 'origin_ip', $origin_ips[0] ); } Store::log( $this->id(), sprintf( /* translators: 1: number of records, 2: number of findings */ __( 'DNS scan: %1$d records checked, %2$d leak(s) found.', 'zonepilot' ), count( $records ), count( $findings ) ), $findings ? 'warn' : 'ok' ); return $result; } /** * SPF finding. * * @param array $r Record. * @param string $content SPF text. * @param array $origin_ips Origin IPs. * @param array $origin_names Names resolving to origin. * @param string $name Record name. * @return array|null */ private function spf_finding( $r, $content, $origin_ips, $origin_names, $name ) { $terms = preg_split( '/\s+/', $content ); $keep = array(); $bad = array(); foreach ( $terms as $term ) { $mech = ltrim( strtolower( $term ), '+~?-' ); $leak = false; if ( 0 === strpos( $mech, 'ip4:' ) || 0 === strpos( $mech, 'ip6:' ) ) { $range = substr( $mech, 4 ); foreach ( $origin_ips as $ip ) { if ( self::in_cidr( $ip, $range ) || $ip === $range ) { $leak = true; } } } elseif ( 'a' === $mech || 'mx' === $mech ) { // "a" = the record's own name; "mx" = its MX hosts. $leak = 'a' === $mech ? isset( $origin_names[ $name ] ) : false; if ( 'mx' === $mech ) { $mx = dns_get_record( $name, DNS_MX ); foreach ( (array) $mx as $m ) { if ( isset( $m['target'] ) && isset( $origin_names[ strtolower( $m['target'] ) ] ) ) { $leak = true; } } } } elseif ( 0 === strpos( $mech, 'a:' ) ) { $leak = isset( $origin_names[ strtolower( substr( $mech, 2 ) ) ] ); } if ( $leak ) { $bad[] = $term; } else { $keep[] = $term; } } if ( ! $bad ) { return null; } $finding = $this->finding( $r, 'high', __( 'SPF record contains your server IP', 'zonepilot' ), /* translators: %s: SPF mechanisms */ sprintf( __( 'These parts reveal the server: %s. If the server no longer sends mail itself (use an email API such as SendGrid, Mailgun or SES instead), remove them.', 'zonepilot' ), implode( ' ', $bad ) ), array( 'spf' ) ); $finding['spf_new'] = implode( ' ', $keep ); return $finding; } /** * Build a finding. * * @param array $r Record. * @param string $severity critical|high|medium. * @param string $title Title. * @param string $detail Detail. * @param array $fixes Fix kinds. * @return array */ private function finding( $r, $severity, $title, $detail, array $fixes ) { return array( 'record' => array( 'id' => $r['id'], 'type' => $r['type'], 'name' => $r['name'], 'content' => $r['content'], 'proxied' => ! empty( $r['proxied'] ), ), 'severity' => $severity, 'title' => $title, 'detail' => $detail, 'fixes' => $fixes, ); } /** * Apply a fix to one DNS record. Deleting requires $confirm === record name. * * @param string $record_id Record id. * @param string $fix proxy|delete|spf. * @param string $confirm Confirmation (record name for delete). * @return array|WP_Error */ public function fix_record( $record_id, $fix, $confirm ) { $scan = Store::module( $this->id() ); $scan = isset( $scan['scan']['findings'] ) ? $scan['scan']['findings'] : array(); $finding = null; foreach ( $scan as $f ) { if ( $f['record']['id'] === $record_id && in_array( $fix, $f['fixes'], true ) ) { $finding = $f; } } if ( ! $finding ) { return new WP_Error( 'fehu_ap_no_finding', __( 'This record is not in the latest scan. Scan again first.', 'zonepilot' ) ); } $record = $finding['record']; if ( 'delete' === $fix && strtolower( trim( $confirm ) ) !== strtolower( $record['name'] ) ) { return new WP_Error( 'fehu_ap_confirm', __( 'Type the record name exactly to confirm the deletion.', 'zonepilot' ) ); } if ( 'delete' !== $fix && 'yes' !== $confirm ) { return new WP_Error( 'fehu_ap_confirm', __( 'Please confirm the change.', 'zonepilot' ) ); } $cf = Cloudflare::stored(); $zone = Site::zone_id(); // Snapshot of the full record for undo. $full = $cf->request( 'GET', '/zones/' . rawurlencode( $zone ) . '/dns_records/' . rawurlencode( $record_id ) ); if ( is_wp_error( $full ) ) { return $full; } switch ( $fix ) { case 'proxy': $res = $cf->dns_patch( $zone, $record_id, array( 'proxied' => true ) ); /* translators: %s: record name */ $msg = sprintf( __( '%s is now proxied through Cloudflare.', 'zonepilot' ), $record['name'] ); break; case 'spf': $res = $cf->dns_patch( $zone, $record_id, array( 'content' => '"' . $finding['spf_new'] . '"' ) ); /* translators: %s: new SPF */ $msg = sprintf( __( 'SPF updated to: %s', 'zonepilot' ), $finding['spf_new'] ); break; case 'delete': $res = $cf->dns_delete( $zone, $record_id ); /* translators: 1: type, 2: name */ $msg = sprintf( __( 'Deleted %1$s record %2$s (a copy is kept for undo).', 'zonepilot' ), $record['type'], $record['name'] ); break; default: return new WP_Error( 'fehu_ap_fix', __( 'Unknown fix.', 'zonepilot' ) ); } if ( is_wp_error( $res ) ) { return $res; } $state = Store::module( $this->id() ); $undo = isset( $state['dns_undo'] ) ? $state['dns_undo'] : array(); $undo[ $record_id . ':' . $fix ] = array( 'fix' => $fix, 'record' => array_intersect_key( $full, array_flip( array( 'id', 'type', 'name', 'content', 'proxied', 'ttl', 'priority', 'comment' ) ) ), 'at' => time(), 'new_id' => '', ); Store::update_module( $this->id(), array( 'dns_undo' => $undo ) ); Store::log( $this->id(), $msg, 'ok' ); $this->scan(); return array( 'messages' => array( $msg ) ); } /** * Undo one DNS fix. * * @param string $key Undo key. * @return array|WP_Error */ public function undo_fix( $key ) { $state = Store::module( $this->id() ); $undo = isset( $state['dns_undo'] ) ? $state['dns_undo'] : array(); if ( ! isset( $undo[ $key ] ) ) { return new WP_Error( 'fehu_ap_undo', __( 'Nothing to undo.', 'zonepilot' ) ); } $item = $undo[ $key ]; $rec = $item['record']; $cf = Cloudflare::stored(); $zone = Site::zone_id(); if ( 'delete' === $item['fix'] ) { $create = array_intersect_key( $rec, array_flip( array( 'type', 'name', 'content', 'proxied', 'ttl', 'priority', 'comment' ) ) ); $res = $cf->dns_create( $zone, $create ); } elseif ( 'proxy' === $item['fix'] ) { $res = $cf->dns_patch( $zone, $rec['id'], array( 'proxied' => ! empty( $rec['proxied'] ) ) ); } else { $res = $cf->dns_patch( $zone, $rec['id'], array( 'content' => $rec['content'] ) ); } if ( is_wp_error( $res ) ) { return $res; } unset( $undo[ $key ] ); Store::update_module( $this->id(), array( 'dns_undo' => $undo ) ); /* translators: %s: record name */ $msg = sprintf( __( 'Restored %s as it was.', 'zonepilot' ), $rec['name'] ); Store::log( $this->id(), $msg, 'ok' ); $this->scan(); return array( 'messages' => array( $msg ) ); } /* ---------------------------------------------------------------- Module API */ /** {@inheritDoc} */ public function preview( array $opts ) { $items = array(); $items[] = array( 'type' => 'info', 'text' => __( 'Scan all DNS records of the zone for anything that reveals the server IP (read-only). Each fix is offered separately and needs your confirmation; nothing is deleted automatically.', 'zonepilot' ), ); if ( ! empty( $opts['lock'] ) ) { if ( ! self::lock_available() ) { $items[] = array( 'type' => 'warn', 'text' => __( 'Origin lock needs ZonePilot Pro.', 'zonepilot' ), ); } else { $items = array_merge( $items, Origin_Lock::preview() ); } } return $items; } /** {@inheritDoc} */ public function apply( array $opts ) { $scan = $this->scan(); if ( is_wp_error( $scan ) ) { return $scan; } $messages = array( sprintf( /* translators: %d: number */ _n( 'DNS scan found %d leak.', 'DNS scan found %d leaks.', count( $scan['findings'] ), 'zonepilot' ), count( $scan['findings'] ) ), ); $this->mark_applied( $opts ); if ( ! empty( $opts['lock'] ) && self::lock_available() ) { $res = Origin_Lock::engage(); if ( is_wp_error( $res ) ) { return new WP_Error( $res->get_error_code(), implode( ' ', $messages ) . ' ' . $res->get_error_message() ); } $messages = array_merge( $messages, $res['messages'] ); } elseif ( empty( $opts['lock'] ) && class_exists( Origin_Lock::class ) ) { $res = Origin_Lock::release( true ); if ( ! is_wp_error( $res ) ) { $messages = array_merge( $messages, $res['messages'] ); } } return array( 'messages' => $messages ); } /** {@inheritDoc} */ public function rollback() { $messages = array(); if ( class_exists( Origin_Lock::class ) ) { $res = Origin_Lock::release( true ); if ( is_wp_error( $res ) ) { return $res; } $messages = $res['messages']; } $state = Store::module( $this->id() ); if ( ! empty( $state['dns_undo'] ) ) { $messages[] = __( 'DNS fixes you made are kept; undo them one by one in the list if needed.', 'zonepilot' ); } $scan = isset( $state['scan'] ) ? $state['scan'] : null; $undo = isset( $state['dns_undo'] ) ? $state['dns_undo'] : array(); $this->mark_off(); Store::update_module( $this->id(), array( 'scan' => $scan, 'dns_undo' => $undo, ) ); return array( 'messages' => $messages ? $messages : array( __( 'Origin protection is off.', 'zonepilot' ) ) ); } /** {@inheritDoc} */ public function time_needed( $operation ) { // Locking waits for the header to arrive through Cloudflare, then tests the lock from outside. if ( in_array( $operation, array( 'apply', 'relock' ), true ) ) { return 150; } return 'verify' === $operation ? 60 : 0; } /** {@inheritDoc} */ public function verify() { $checks = array(); $scan = $this->scan(); if ( is_wp_error( $scan ) ) { $checks[] = self::check( __( 'DNS scan', 'zonepilot' ), null, $scan->get_error_message() ); } else { $checks[] = self::check( __( 'No DNS record reveals the server IP', 'zonepilot' ), ! $scan['findings'], $scan['findings'] ? sprintf( /* translators: %d: number */ _n( '%d leak', '%d leaks', count( $scan['findings'] ), 'zonepilot' ), count( $scan['findings'] ) ) : __( 'Clean', 'zonepilot' ) ); } if ( class_exists( Origin_Lock::class ) ) { $checks = array_merge( $checks, Origin_Lock::verify_checks() ); } return $this->store_checks( $checks ); } /** {@inheritDoc} */ public function action( $action, array $args ) { switch ( $action ) { case 'scan': $res = $this->scan(); return is_wp_error( $res ) ? $res : array( 'messages' => array( __( 'Scan finished.', 'zonepilot' ) ) ); case 'fix': return $this->fix_record( (string) ( $args['record'] ?? '' ), (string) ( $args['fix'] ?? '' ), (string) ( $args['confirm'] ?? '' ) ); case 'undo': return $this->undo_fix( (string) ( $args['key'] ?? '' ) ); case 'unlock': if ( ! class_exists( Origin_Lock::class ) ) { break; } return Origin_Lock::release( false ); case 'relock': if ( ! self::lock_available() ) { break; } // Open first, so a failing re-check can never leave the site locked. Origin_Lock::release( false ); if ( file_exists( Origin_Lock::unlock_file() ) ) { wp_delete_file( Origin_Lock::unlock_file() ); } return Origin_Lock::engage(); } return parent::action( $action, $args ); } /** {@inheritDoc} */ public function card_extra() { $state = Store::module( $this->id() ); return array( 'scan' => isset( $state['scan'] ) ? $state['scan'] : null, 'undo' => isset( $state['dns_undo'] ) ? $state['dns_undo'] : array(), 'lock' => class_exists( Origin_Lock::class ) ? Origin_Lock::state() : array( 'stage' => 'unavailable' ), 'lock_ok' => self::lock_available(), ); } /** {@inheritDoc} */ public function register() { if ( class_exists( Origin_Lock::class ) ) { Origin_Lock::register(); } } }