settings = $settings; $this->ip = $ip; } /** * Registers hooks. * * @return void */ public function boot() { // After core has checked the password (priority 20) so we can override any result. add_filter( 'authenticate', array( $this, 'authenticate' ), 99, 1 ); add_action( 'wp_login_failed', array( $this, 'failed' ), 10, 2 ); add_action( 'wp_login', array( $this, 'succeeded' ), 10, 1 ); add_filter( 'login_errors', array( $this, 'login_errors' ) ); add_filter( 'shake_error_codes', array( $this, 'shake_codes' ) ); } // State. /** * Lockout for an IP, if any. * * @param string $ip IP. * @return array{until:int,long:bool}|null */ public function lockout( $ip ) { if ( '' === $ip ) { return null; } $lock = get_transient( 'cclg_l_' . Ip::key( $ip ) ); if ( ! is_array( $lock ) || empty( $lock['until'] ) || (int) $lock['until'] <= time() ) { return null; } return array( 'until' => (int) $lock['until'], 'long' => ! empty( $lock['long'] ), ); } /** * Failed attempts counted for an IP in the current window. * * @param string $ip IP. * @return int */ public function failures( $ip ) { return '' === $ip ? 0 : (int) get_transient( 'cclg_f_' . Ip::key( $ip ) ); } /** * Whether the IP may skip all limits (allow list, or a recent email unlock). * * @param string $ip IP. * @return bool */ public function is_trusted( $ip ) { if ( '' === $ip ) { return false; } return Ip::in_list( $ip, (string) $this->settings->get( 'allowlist' ) ) || (bool) get_transient( 'cclg_b_' . Ip::key( $ip ) ); } /** * Whether the IP is on the block list. * * @param string $ip IP. * @return bool */ public function is_blocked( $ip ) { return '' !== $ip && ! Ip::in_list( $ip, (string) $this->settings->get( 'allowlist' ) ) && Ip::in_list( $ip, (string) $this->settings->get( 'denylist' ) ); } /** * Removes the lockout and the failure count for an IP. * * @param string $ip IP. * @param string $note Why (for the log). * @return void */ public function unlock( $ip, $note = '' ) { $key = Ip::key( $ip ); delete_transient( 'cclg_l_' . $key ); delete_transient( 'cclg_f_' . $key ); delete_transient( 'cclg_n_' . $key ); Log::release( $ip ); Log::add( 'unlock', $ip, '', null, $note ); } /** * Lets an IP past the limits for an hour (used after an email unlock). * * @param string $ip IP. * @return void */ public function trust_for_an_hour( $ip ) { set_transient( 'cclg_b_' . Ip::key( $ip ), 1, HOUR_IN_SECONDS ); } // Hooks. /** * Refuses the login while the IP is locked out or blocked. * * @param WP_User|WP_Error|null $user Result so far. * @return WP_User|WP_Error|null */ public function authenticate( $user ) { $ip = $this->ip->client_ip(); if ( $this->is_trusted( $ip ) ) { return $user; } if ( $this->is_blocked( $ip ) ) { $this->refused = true; Log::add( 'blocked', $ip, $this->posted_username() ); return new WP_Error( 'cclg_blocked', __( 'Error: Logins from your network are not allowed on this site.', 'codingcush-login-guard' ) ); } $lock = $this->lockout( $ip ); if ( $lock ) { $this->refused = true; return new WP_Error( 'cclg_locked', $this->locked_message( $lock['until'] ) ); } return $user; } /** * Counts a failed login and locks the IP out when the limit is reached. * * @param string $username Username or email that was tried. * @param WP_Error|null $error Why the login failed. * @return void */ public function failed( $username, $error = null ) { $ip = $this->ip->client_ip(); if ( $this->refused || '' === $ip || $this->is_trusted( $ip ) ) { return; } // A missed Turnstile check is logged by the Turnstile module, not counted as a password guess. if ( $error instanceof WP_Error && 'cclg_turnstile' === $error->get_error_code() ) { return; } $key = Ip::key( $ip ); $window = (int) $this->settings->get( 'window_hours' ) * HOUR_IN_SECONDS; $retries = (int) $this->settings->get( 'retries' ); $count = $this->failures( $ip ) + 1; Log::add( 'failed', $ip, (string) $username ); if ( $count < $retries ) { set_transient( 'cclg_f_' . $key, $count, $window ); return; } // Limit reached: lock out. Count lockouts in the last 24 hours to escalate. delete_transient( 'cclg_f_' . $key ); $lockouts = (int) get_transient( 'cclg_n_' . $key ) + 1; set_transient( 'cclg_n_' . $key, $lockouts, DAY_IN_SECONDS ); $long = $lockouts >= (int) $this->settings->get( 'lockouts_to_long' ); $duration = $long ? (int) $this->settings->get( 'long_hours' ) * HOUR_IN_SECONDS : (int) $this->settings->get( 'lockout_minutes' ) * MINUTE_IN_SECONDS; $until = time() + $duration; set_transient( 'cclg_l_' . $key, array( 'until' => $until, 'long' => $long, ), $duration ); if ( $long ) { delete_transient( 'cclg_n_' . $key ); } Log::add( 'lockout', $ip, (string) $username, $until, $long ? __( 'Long lockout', 'codingcush-login-guard' ) : '' ); $this->notify( $ip, (string) $username, $until, $long ); // The attempt that triggered the lockout already shows the lockout message. $this->refused = true; } /** * A successful login clears the failure count for that IP. * * @param string $user_login Username. * @return void */ public function succeeded( $user_login ) { unset( $user_login ); $ip = $this->ip->client_ip(); if ( '' !== $ip ) { delete_transient( 'cclg_f_' . Ip::key( $ip ) ); } } /** * Adds "N attempts left" and the lockout message on wp-login.php. * * @param string $errors Error HTML. * @return string */ public function login_errors( $errors ) { $ip = $this->ip->client_ip(); if ( '' === $ip || $this->is_trusted( $ip ) ) { return $errors; } $lock = $this->lockout( $ip ); if ( $lock ) { // The lockout message replaces the generic "incorrect password" text. return $this->locked_message( $lock['until'] ) . apply_filters( 'cclg_locked_message_after', '', $ip ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Prefixed with cclg_. } if ( ! $this->settings->get( 'show_remaining' ) ) { return $errors; } $used = $this->failures( $ip ); if ( $used < 1 ) { return $errors; } $left = max( 0, (int) $this->settings->get( 'retries' ) - $used ); return $errors . '
' . esc_html( sprintf( /* translators: %d: number of login attempts left. */ _n( '%d attempt remaining.', '%d attempts remaining.', $left, 'codingcush-login-guard' ), $left ) ); } /** * Shakes the login box for our errors too. * * @param string[] $codes Codes. * @return string[] */ public function shake_codes( $codes ) { $codes[] = 'cclg_locked'; $codes[] = 'cclg_blocked'; return $codes; } // Helpers. /** * Lockout message with the time left. * * @param int $until Unix time the lockout ends. * @return string */ public function locked_message( $until ) { $minutes = max( 1, (int) ceil( ( $until - time() ) / MINUTE_IN_SECONDS ) ); if ( $minutes >= 120 ) { $hours = (int) ceil( $minutes / 60 ); /* translators: %d: hours. */ $when = sprintf( _n( '%d hour', '%d hours', $hours, 'codingcush-login-guard' ), $hours ); } else { /* translators: %d: minutes. */ $when = sprintf( _n( '%d minute', '%d minutes', $minutes, 'codingcush-login-guard' ), $minutes ); } return '' . esc_html__( 'Too many failed login attempts.', 'codingcush-login-guard' ) . ' ' . esc_html( sprintf( /* translators: %s: time left, e.g. "20 minutes". */ __( 'Please try again in %s.', 'codingcush-login-guard' ), $when ) ); } /** * The username submitted with the current login request, if any. * * @return string */ private function posted_username() { // phpcs:disable WordPress.Security.NonceVerification.Missing -- Read-only, for the log; the login form has no nonce. if ( isset( $_POST['log'] ) ) { return sanitize_user( wp_unslash( $_POST['log'] ) ); } if ( isset( $_POST['username'] ) ) { return sanitize_user( wp_unslash( $_POST['username'] ) ); } // phpcs:enable return ''; } /** * Emails the site owner about a lockout, if enabled. * * @param string $ip IP. * @param string $username Username tried. * @param int $until Lockout end. * @param bool $long Long lockout. * @return void */ private function notify( $ip, $username, $until, $long ) { $mode = (string) $this->settings->get( 'notify' ); if ( 'off' === $mode || ( 'long' === $mode && ! $long ) ) { return; } $to = (string) $this->settings->get( 'notify_email' ); if ( ! is_email( $to ) ) { $to = (string) get_option( 'admin_email' ); } $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); /* translators: %s: site name. */ $subject = sprintf( __( '[%s] An IP address was locked out', 'codingcush-login-guard' ), $site ); $lines = array( /* translators: %s: site name. */ sprintf( __( 'CodingCush Login Guard locked out an IP address on %s after too many failed logins.', 'codingcush-login-guard' ), $site ), '', /* translators: %s: IP address. */ sprintf( __( 'IP address: %s', 'codingcush-login-guard' ), $ip ), /* translators: %s: username. */ sprintf( __( 'Last username tried: %s', 'codingcush-login-guard' ), '' !== $username ? $username : '-' ), /* translators: %s: date and time. */ sprintf( __( 'Locked until: %s', 'codingcush-login-guard' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $until ) ), $long ? __( 'This was a long lockout because the address was locked out several times.', 'codingcush-login-guard' ) : '', '', /* translators: %s: URL of the plugin's settings screen. */ sprintf( __( 'Review lockouts: %s', 'codingcush-login-guard' ), admin_url( 'options-general.php?page=codingcush-login-guard' ) ), ); wp_mail( $to, $subject, implode( "\n", $lines ) ); } }