*/ private $two_factor = array(); /** * Logger ID. * * @return string */ public function id() { return 'user'; } /** * Label. * * @return string */ public function label() { return __( 'Users and logins', 'admintrace-activity-logger' ); } /** * Description. * * @return string */ public function description() { return __( 'Logins, logouts, ended sessions, failed logins, second-factor checks, profile changes, roles, and application passwords.', 'admintrace-activity-logger' ); } /** * Register hooks. */ public function register() { $this->listen( 'wp_login', 'on_login', 2 ); $this->listen( 'wp_logout', 'on_logout', 1 ); $this->listen( 'wp_login_failed', 'on_failed', 2 ); $this->listen( 'user_register', 'on_register', 1 ); $this->listen( 'profile_update', 'on_profile', 2 ); $this->listen( 'delete_user', 'on_delete', 2 ); $this->listen( 'set_user_role', 'on_set_role', 3 ); $this->listen( 'add_user_role', 'on_add_role', 2 ); $this->listen( 'remove_user_role', 'on_remove_role', 2 ); $this->listen( 'retrieve_password', 'on_reset_request', 1 ); $this->listen( 'after_password_reset', 'on_password_reset', 1 ); $this->listen( 'wp_create_application_password', 'on_app_password_created', 2 ); $this->listen( 'wp_delete_application_password', 'on_app_password_deleted', 2 ); $this->listen( 'application_password_failed_authentication', 'on_app_password_failed', 1 ); $this->listen( 'application_password_did_authenticate', 'on_app_password_login', 2 ); $this->listen( 'two_factor_user_authenticated', 'on_two_factor', 2 ); $this->listen( 'two_factor_login_nonce_failed', 'on_two_factor_nonce', 2 ); $this->listen( 'wp_ajax_destroy_sessions', 'on_destroy_sessions', 1, 0 ); } /** * Someone ended login sessions from a user profile. * * Runs before WordPress destroys the tokens. A failed nonce is ignored. */ public function on_destroy_sessions() { if ( empty( $_POST['user_id'] ) || empty( $_POST['nonce'] ) ) { return; } $user_id = absint( wp_unslash( $_POST['user_id'] ) ); $nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ) ); if ( ! $user_id || ! wp_verify_nonce( $nonce, 'update-user_' . $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) { return; } $user = get_userdata( $user_id ); if ( ! $user instanceof \WP_User ) { return; } $self = get_current_user_id() === (int) $user->ID; $this->log( array( 'summary' => $self ? __( 'Ended the other login sessions for this account.', 'admintrace-activity-logger' ) : sprintf( /* translators: %s: username. */ __( 'Ended every login session for "%s".', 'admintrace-activity-logger' ), Text::plain( $user->user_login, 60 ) ), 'category' => 'security', 'action' => 'user.sessions_ended', 'severity' => 'warning', 'object_type' => 'user', 'object_id' => (int) $user->ID, 'object_title' => $user->user_login, 'context' => array( 'everywhere' => ! $self ), ) ); } /** * Successful login. * * @param string $user_login Username. * @param mixed $user User. Core passes a WP_User; other plugins fire wp_login too. */ public function on_login( $user_login, $user ) { $user_id = ( $user instanceof \WP_User ) ? (int) $user->ID : 0; $context = array(); $login = Text::plain( $user_login, 60 ); $summary = sprintf( /* translators: %s: username. */ __( 'Logged in as "%s".', 'admintrace-activity-logger' ), $login ); if ( $user_id && ! empty( $this->two_factor[ $user_id ] ) ) { $method = $this->two_factor[ $user_id ]; $context['two_factor'] = is_string( $method ) ? $method : 'yes'; if ( is_string( $method ) && '' !== $method ) { $summary = sprintf( /* translators: 1: username, 2: second-factor method. */ __( 'Logged in as "%1$s" after the "%2$s" second factor.', 'admintrace-activity-logger' ), $login, $method ); } else { $summary = sprintf( /* translators: %s: username. */ __( 'Logged in as "%s" after a second factor.', 'admintrace-activity-logger' ), $login ); } } $this->log( array( 'summary' => $summary, 'category' => 'security', 'action' => 'user.login', 'severity' => 'notice', 'user_id' => $user_id, 'user_login' => $user_login, 'object_type' => 'user', 'object_id' => $user_id, 'context' => $context, ) ); } /** * Logout. * * @param int $user_id User ID. */ public function on_logout( $user_id ) { $this->log( array( 'summary' => __( 'Logged out.', 'admintrace-activity-logger' ), 'category' => 'user', 'action' => 'user.logout', 'severity' => 'info', 'user_id' => absint( $user_id ), 'object_type' => 'user', 'object_id' => absint( $user_id ), ) ); } /** * Failed password login. * * @param string $username Attempted username. * @param \WP_Error $error Authentication error. */ public function on_failed( $username, $error ) { if ( empty( Settings::all()['log_failed_logins'] ) ) { return; } $username = Text::plain( $username, 60 ); $code = 'unknown'; if ( $error instanceof \WP_Error ) { $codes = $error->get_error_codes(); if ( in_array( 'incorrect_password', $codes, true ) ) { $code = 'bad_password'; } elseif ( in_array( 'invalid_username', $codes, true ) || in_array( 'invalid_email', $codes, true ) ) { $code = 'unknown_user'; } } if ( 'bad_password' === $code ) { $summary = sprintf( /* translators: %s: username. */ __( 'Failed login for "%s" because the password was incorrect.', 'admintrace-activity-logger' ), $username ); } elseif ( 'unknown_user' === $code ) { $summary = sprintf( /* translators: %s: username. */ __( 'Failed login for an unknown username "%s".', 'admintrace-activity-logger' ), $username ); } else { $summary = __( 'Failed login attempt.', 'admintrace-activity-logger' ); } $hit = $this->count_failure(); $skip = $this->throttled( $username ); if ( ! $skip ) { $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? Text::plain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 180 ) : ''; $this->log( array( 'summary' => $summary, 'category' => 'security', 'action' => 'user.login_failed', 'severity' => 'warning', 'user_id' => 0, 'user_login' => $username, 'initiator' => 'anonymous', 'context' => array( 'reason' => $code, 'user_agent' => $agent, ), ) ); } if ( $hit > 0 ) { $this->log( array( 'summary' => sprintf( /* translators: %d: number of failed logins. */ __( 'Failed login threshold reached: %d attempts in 15 minutes.', 'admintrace-activity-logger' ), $hit ), 'category' => 'security', 'action' => 'security.login_threshold', 'severity' => 'critical', 'user_id' => 0, 'initiator' => 'system', ) ); } } /** * New account. * * @param int $user_id User ID. */ public function on_register( $user_id ) { $user = get_userdata( $user_id ); $role = ( $user && ! empty( $user->roles ) ) ? implode( ', ', $user->roles ) : ''; $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Created the account "%s".', 'admintrace-activity-logger' ), $user ? $user->user_login : (string) $user_id ), 'category' => 'user', 'action' => 'user.created', 'severity' => ( false !== strpos( $role, 'administrator' ) ) ? 'warning' : 'notice', 'object_type' => 'user', 'object_id' => absint( $user_id ), 'context' => array( 'role' => $role ), ) ); } /** * Profile field changes. Passwords are recorded only as "updated". * * @param int $user_id User ID. * @param \WP_User $old Previous user. */ public function on_profile( $user_id, $old ) { $new = get_userdata( $user_id ); if ( ! $new instanceof \WP_User || ! $old instanceof \WP_User ) { return; } $fields = array( 'user_email' => __( 'Email', 'admintrace-activity-logger' ), 'display_name' => __( 'Display name', 'admintrace-activity-logger' ), 'user_url' => __( 'Website', 'admintrace-activity-logger' ), 'user_nicename' => __( 'Nicename', 'admintrace-activity-logger' ), ); $changes = array(); foreach ( $fields as $key => $label ) { $from = (string) $old->$key; $to = (string) $new->$key; if ( $from !== $to ) { $changes[] = array( 'field' => $label, 'from' => Text::plain( $from, 180 ), 'to' => Text::plain( $to, 180 ), ); } } if ( (string) $old->user_pass !== (string) $new->user_pass ) { $changes[] = array( 'field' => __( 'Password', 'admintrace-activity-logger' ), 'from' => '••••', 'to' => __( 'Updated', 'admintrace-activity-logger' ), ); } if ( ! $changes ) { return; } $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Updated the profile for "%s".', 'admintrace-activity-logger' ), $new->user_login ), 'category' => 'user', 'action' => 'user.updated', 'severity' => 'notice', 'object_type' => 'user', 'object_id' => (int) $user_id, 'context' => array( 'changes' => $changes ), ) ); } /** * Account deletion. * * @param int $user_id User ID. * @param int|null $reassign Reassign target. */ public function on_delete( $user_id, $reassign ) { $user = get_userdata( $user_id ); $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Deleted the account "%s".', 'admintrace-activity-logger' ), $user ? $user->user_login : (string) $user_id ), 'category' => 'user', 'action' => 'user.deleted', 'severity' => 'warning', 'object_type' => 'user', 'object_id' => absint( $user_id ), 'object_title' => $user ? $user->user_login : '', 'context' => array( 'reassign' => absint( $reassign ), ), ) ); } /** * Primary role change. * * @param int $user_id User ID. * @param string $role New role. * @param string[] $old_roles Previous roles. */ public function on_set_role( $user_id, $role, $old_roles ) { $this->log_role_change( $user_id, array( (string) $role ), (array) $old_roles ); } /** * Added role. * * @param int $user_id User ID. * @param string $role Role. */ public function on_add_role( $user_id, $role ) { $this->log_role_change( $user_id, array( (string) $role ), array() ); } /** * Removed role. * * @param int $user_id User ID. * @param string $role Role. */ public function on_remove_role( $user_id, $role ) { $this->log_role_change( $user_id, array(), array( (string) $role ) ); } /** * Password reset requested. The reset key is never stored. * * @param string $user_login Username. */ public function on_reset_request( $user_login ) { $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Password reset requested for "%s".', 'admintrace-activity-logger' ), Text::plain( $user_login, 60 ) ), 'category' => 'security', 'action' => 'user.password_reset_requested', 'severity' => 'notice', 'user_login' => $user_login, ) ); } /** * Password reset completed. The new password is not accepted by this callback. * * @param \WP_User $user User. */ public function on_password_reset( $user ) { if ( ! $user instanceof \WP_User ) { return; } $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Password reset completed for "%s".', 'admintrace-activity-logger' ), $user->user_login ), 'category' => 'security', 'action' => 'user.password_reset', 'severity' => 'warning', 'user_id' => (int) $user->ID, 'object_type' => 'user', 'object_id' => (int) $user->ID, ) ); } /** * Application password created. The password itself is not received. * * @param int $user_id User ID. * @param array $item Password metadata. */ public function on_app_password_created( $user_id, $item ) { $name = is_array( $item ) && isset( $item['name'] ) ? $item['name'] : ''; $this->log( array( 'summary' => sprintf( /* translators: %s: application password name. */ __( 'Created an application password named "%s".', 'admintrace-activity-logger' ), Text::plain( $name, 80 ) ), 'category' => 'security', 'action' => 'user.app_password_created', 'severity' => 'notice', 'object_type' => 'user', 'object_id' => absint( $user_id ), ) ); } /** * Application password deleted. * * @param int $user_id User ID. * @param array $item Password metadata. */ public function on_app_password_deleted( $user_id, $item ) { $name = is_array( $item ) && isset( $item['name'] ) ? $item['name'] : ''; $this->log( array( 'summary' => sprintf( /* translators: %s: application password name. */ __( 'Deleted the application password "%s".', 'admintrace-activity-logger' ), Text::plain( $name, 80 ) ), 'category' => 'security', 'action' => 'user.app_password_deleted', 'severity' => 'warning', 'object_type' => 'user', 'object_id' => absint( $user_id ), ) ); } /** * Failed application-password authentication. * * @param \WP_Error $error Error. */ public function on_app_password_failed( $error ) { unset( $error ); if ( $this->throttled( 'application-password' ) ) { return; } $this->log( array( 'summary' => __( 'An application password failed to authenticate.', 'admintrace-activity-logger' ), 'category' => 'security', 'action' => 'user.login_failed', 'severity' => 'warning', 'user_id' => 0, 'initiator' => 'anonymous', ) ); } /** * Successful application-password authentication. * * @param \WP_User $user User. * @param array $item Password metadata. */ public function on_app_password_login( $user, $item ) { if ( ! $user instanceof \WP_User ) { return; } $name = is_array( $item ) && isset( $item['name'] ) ? $item['name'] : ''; $this->log( array( 'summary' => sprintf( /* translators: 1: username, 2: application password name. */ __( '"%1$s" authenticated with the application password "%2$s".', 'admintrace-activity-logger' ), $user->user_login, Text::plain( $name, 80 ) ), 'category' => 'security', 'action' => 'user.login', 'severity' => 'notice', 'user_id' => (int) $user->ID, 'object_type' => 'user', 'object_id' => (int) $user->ID, ) ); } /** * Remember that a second factor was accepted. * * The one-time code is never stored. * * @param \WP_User $user User. * @param mixed $provider Provider instance, when the Two Factor plugin supplies one. */ public function on_two_factor( $user, $provider = null ) { if ( $user instanceof \WP_User ) { $this->two_factor[ (int) $user->ID ] = $this->provider_label( $provider ); } } /** * A Two Factor login check was rejected. * * Only expired and mismatched checks are stored. Requests with no pending * login are ignored so a guessed user ID cannot fill the journal. * * @param int $user_id User ID. * @param string $reason Rejection reason. */ public function on_two_factor_nonce( $user_id, $reason ) { if ( empty( Settings::all()['log_failed_logins'] ) ) { return; } $reason = sanitize_key( (string) $reason ); if ( ! in_array( $reason, array( 'expired', 'mismatch' ), true ) ) { return; } $user = get_userdata( absint( $user_id ) ); $login = $user ? $user->user_login : (string) absint( $user_id ); $summary = ( 'expired' === $reason ) ? sprintf( /* translators: %s: username. */ __( 'Second-factor login for "%s" was rejected because the check had expired.', 'admintrace-activity-logger' ), Text::plain( $login, 60 ) ) : sprintf( /* translators: %s: username. */ __( 'Second-factor login for "%s" was rejected because the check did not match.', 'admintrace-activity-logger' ), Text::plain( $login, 60 ) ); $this->log( array( 'summary' => $summary, 'category' => 'security', 'action' => 'user.login_failed', 'severity' => 'warning', 'user_id' => 0, 'user_login' => $login, 'initiator' => 'anonymous', 'context' => array( 'reason' => 'two_factor_' . $reason ), ) ); } /** * Public name of a second-factor provider. * * @param mixed $provider Provider object or name. * @return string|bool Method label, or true when the method name is unknown. */ private function provider_label( $provider ) { $label = ''; if ( is_object( $provider ) && method_exists( $provider, 'get_label' ) ) { $label = (string) $provider->get_label(); } elseif ( is_object( $provider ) && method_exists( $provider, 'get_key' ) ) { $label = (string) $provider->get_key(); } elseif ( is_string( $provider ) ) { $label = $provider; } $label = Text::plain( wp_strip_all_tags( $label ), 40 ); return '' !== $label ? $label : true; } /** * Record a role change. * * @param int $user_id User ID. * @param string[] $new_value New roles. * @param string[] $old Old roles. */ private function log_role_change( $user_id, array $new_value, array $old ) { $user = get_userdata( $user_id ); $login = $user ? $user->user_login : (string) $user_id; $serious = in_array( 'administrator', $new_value, true ) || in_array( 'administrator', $old, true ); $this->log( array( 'summary' => sprintf( /* translators: %s: username. */ __( 'Changed roles for "%s".', 'admintrace-activity-logger' ), $login ), 'category' => 'user', 'action' => 'user.role_changed', 'severity' => $serious ? 'warning' : 'notice', 'object_type' => 'user', 'object_id' => absint( $user_id ), 'context' => array( 'changes' => array( array( 'field' => __( 'Roles', 'admintrace-activity-logger' ), 'from' => implode( ', ', array_map( 'sanitize_key', $old ) ), 'to' => implode( ', ', array_map( 'sanitize_key', $new_value ) ), ), ), ), ) ); } /** * Count failures in a 15-minute window. * * @return int Threshold count when it has just been reached, otherwise 0. */ private function count_failure() { $state = get_option( 'srah_fail_window', array() ); if ( ! is_array( $state ) || empty( $state['start'] ) || (int) $state['start'] < time() - ( 15 * MINUTE_IN_SECONDS ) ) { $state = array( 'start' => time(), 'count' => 0, 'alerted' => 0, ); } $state['count'] = (int) $state['count'] + 1; $hit = 0; $threshold = (int) Settings::all()['alert_threshold']; if ( $state['count'] >= $threshold && empty( $state['alerted'] ) ) { $state['alerted'] = 1; $hit = (int) $state['count']; } update_option( 'srah_fail_window', $state, false ); return $hit; } /** * Group repeated failures so a flood cannot fill the journal. * * @param string $bucket Username or other bucket. * @return bool True when this attempt should not create its own row. */ private function throttled( $bucket ) { $store = 'srah_fail_logins'; $map = get_transient( $store ); if ( ! is_array( $map ) ) { $map = array(); } $id = md5( strtolower( (string) $bucket ) ); if ( ! isset( $map[ $id ] ) && count( $map ) >= 32 ) { $id = '_other'; } $count = isset( $map[ $id ] ) ? (int) $map[ $id ] : 0; $map[ $id ] = $count + 1; set_transient( $store, $map, 10 * MINUTE_IN_SECONDS ); if ( $count < 8 ) { return false; } if ( 0 === ( $count % 25 ) ) { $this->log( array( 'summary' => sprintf( /* translators: 1: attempt count, 2: username. */ __( 'Grouped %1$d additional failed logins for "%2$s".', 'admintrace-activity-logger' ), $count, Text::plain( $bucket, 60 ) ), 'category' => 'security', 'action' => 'user.login_failed_grouped', 'severity' => 'warning', 'user_id' => 0, 'initiator' => 'anonymous', ) ); } return true; } }