get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", self::OPTION ) ); $stored = is_string( $raw ) ? maybe_unserialize( $raw ) : array(); } else { $stored = get_option( self::OPTION, array() ); } $stored = is_array( $stored ) ? $stored : array(); // Lockdown is either automatic (the store's own numbers) or switched on by an administrator here. $reason = null; if ( isset( $stored['reason'] ) && is_string( $stored['reason'] ) ) { $reason = 'auto' === $stored['reason'] ? 'auto' : 'manual'; } return array( 'on' => ! empty( $stored['on'] ), 'reason' => $reason, 'since' => isset( $stored['since'] ) ? (int) $stored['since'] : 0, 'until' => isset( $stored['until'] ) ? (int) $stored['until'] : 0, 'attack_id' => isset( $stored['attack_id'] ) ? (int) $stored['attack_id'] : 0, 'quiet_since' => isset( $stored['quiet_since'] ) ? (int) $stored['quiet_since'] : 0, 'snooze_until' => isset( $stored['snooze_until'] ) ? (int) $stored['snooze_until'] : 0, ); } /** * Saves the state. * * @param array $state State. */ private static function save( array $state ) { update_option( self::OPTION, $state, true ); } /** * Whether lockdown is in force right now. * * @return bool */ public static function is_on() { $state = self::state(); return $state['on'] && ( 0 === $state['until'] || $state['until'] > time() ); } /** * Id of the attack in progress, 0 when there is none. * * @return int */ public static function attack_id() { $state = self::state(); return $state['attack_id']; } /** * The state as the admin screens read it. * * @return array */ public static function to_rest() { $state = self::state(); $on = self::is_on(); return array( 'on' => $on, 'reason' => $on ? $state['reason'] : null, 'since' => $on ? Util::iso( $state['since'] ) : null, 'until' => $on && $state['until'] > 0 ? Util::iso( $state['until'] ) : null, 'attackId' => $state['attack_id'] > 0 ? $state['attack_id'] : null, ); } /** * The numbers the triggers look at. * * @return array{declines15:int,attempts15:int,stopped5:int,baseline15:float} */ public static function stats() { $sums = Counters::sums( array( 'attempts15' => array( 'g:a', 900 ), 'declines15' => array( 'g:d', 900 ), 'stopped5' => array( 'g:s', 300 ), ) ); $sums['baseline15'] = (float) get_option( self::AVERAGE_OPTION, 0 ); return $sums; } /** * Checks the triggers: starts an attack, keeps it going, or ends it. Safe to call often. */ public static function evaluate() { if ( ! Util::lock( 'attack' ) ) { return; } try { $state = self::state( true ); $now = time(); // A lockdown an administrator switched on ends by itself after 24 hours. if ( $state['on'] && $state['until'] > 0 && $state['until'] <= $now ) { self::finish( $state, false ); return; } $trigger = Attack_Rules::trigger( self::stats() ); if ( 0 === $state['attack_id'] ) { if ( '' !== $trigger && $now >= $state['snooze_until'] ) { self::begin( $state, Settings::value( 'autoLockdown' ) ? 'auto' : null, $trigger ); } return; } if ( $state['until'] > 0 ) { return; // Timed lockdowns do not end on quiet. } if ( '' !== $trigger ) { if ( 0 !== $state['quiet_since'] ) { $state['quiet_since'] = 0; self::save( $state ); } return; } if ( 0 === $state['quiet_since'] ) { $state['quiet_since'] = $now; self::save( $state ); return; } if ( Attack_Rules::should_end( $state['quiet_since'], $now ) ) { self::finish( $state, false ); } } catch ( \Throwable $error ) { Util::log_error( 'Checking for an attack failed', $error ); } finally { Util::unlock( 'attack' ); } } /** * Switches lockdown on because an administrator of this site asked: the "Start lockdown" button * on the Brickmoat screen, or the WP-CLI command. Nothing outside this site can call it. * * @return bool Whether lockdown is on afterwards. */ public static function start() { $locked = Util::lock( 'attack' ); try { $state = self::state( true ); if ( $state['on'] && ( 0 === $state['until'] || $state['until'] > time() ) ) { // "Start lockdown" pressed during an automatic lockdown: hold it for 24 hours. $state['reason'] = 'manual'; $state['until'] = time() + Attack_Rules::MANUAL_SECONDS; self::save( $state ); return true; } self::begin( $state, 'manual', 'manual' ); return true; } catch ( \Throwable $error ) { Util::log_error( 'Starting lockdown failed', $error ); return false; } finally { if ( $locked ) { Util::unlock( 'attack' ); } } } /** * Switches lockdown off because someone asked. * * @return bool Whether anything was ended. */ public static function stop() { $locked = Util::lock( 'attack' ); try { $state = self::state( true ); if ( ! $state['on'] && 0 === $state['attack_id'] ) { return false; } self::finish( $state, true ); return true; } catch ( \Throwable $error ) { Util::log_error( 'Ending lockdown failed', $error ); return false; } finally { if ( $locked ) { Util::unlock( 'attack' ); } } } /** * Opens an attack and, unless $reason is null, switches lockdown on. * * @param array $state Current state. * @param string|null $reason auto or manual; null records the attack without lockdown. * @param string $trigger declines, decline_rate, blocked_burst or manual. */ private static function begin( array $state, $reason, $trigger ) { $now = time(); $attack_id = $state['attack_id']; if ( 0 === $attack_id ) { $attack_id = Attacks::open( $trigger, $now, (float) get_option( self::AVERAGE_OPTION, 0 ) ); } $timed = 'manual' === $reason; $state = array_merge( $state, array( 'on' => null !== $reason, 'reason' => $reason, 'since' => $now, 'until' => $timed ? $now + Attack_Rules::MANUAL_SECONDS : 0, 'attack_id' => $attack_id, 'quiet_since' => 0, ) ); self::save( $state ); if ( null !== $reason ) { Attacks::update( $attack_id, array( 'lockdown_at' => $now ) ); } Scheduler::start_minute(); delete_transient( 'brickmoat_cleanup_pending' ); $attack = Attacks::get( $attack_id ); if ( $attack ) { // The merchant does not need an email about a button they pressed themselves. if ( 'manual' !== $trigger ) { Alerts::attack_started( $attack ); } Reporter::attack( $attack ); } /** * Fires when Brickmoat records the start of an attack. * * @param int $attack_id Attack id. * @param string|null $reason Why lockdown is on, or null when it was not switched on. */ do_action( 'brickmoat_attack_started', $attack_id, $reason ); } /** * Closes the attack and switches lockdown off. * * @param array $state Current state. * @param bool $manual Whether a person ended it. */ private static function finish( array $state, $manual ) { $attack_id = $state['attack_id']; self::save( array( 'on' => false, 'reason' => null, 'since' => 0, 'until' => 0, 'attack_id' => 0, 'quiet_since' => 0, 'snooze_until' => $manual ? time() + self::SNOOZE_SECONDS : 0, ) ); Scheduler::stop_minute(); delete_transient( 'brickmoat_cleanup_pending' ); if ( $attack_id > 0 ) { Attacks::close( $attack_id ); $attack = Attacks::get( $attack_id ); if ( $attack ) { Alerts::attack_ended( $attack ); Reporter::attack( $attack ); } } /** * Fires when Brickmoat closes an attack and returns to normal mode. * * @param int $attack_id Attack id. */ do_action( 'brickmoat_attack_ended', $attack_id ); } /** * Every 15 minutes: saves the store's attempts and declines, and refreshes the 14-day average. */ public static function record_baseline() { $now = time(); $state = self::state(); $samples = get_option( self::BASELINE_OPTION, array() ); $samples = is_array( $samples ) ? $samples : array(); // An attack is not "normal": leave it out so it cannot raise the bar for the next one. if ( 0 === $state['attack_id'] ) { $sums = Counters::sums( array( 'attempts' => array( 'g:a', 900 ), 'declines' => array( 'g:d', 900 ), ), $now ); $samples[] = array( $now, (int) $sums['attempts'], (int) $sums['declines'] ); } $from = $now - Attack_Rules::BASELINE_DAYS * DAY_IN_SECONDS; $samples = array_values( array_filter( $samples, static function ( $sample ) use ( $from ) { return is_array( $sample ) && isset( $sample[0] ) && (int) $sample[0] >= $from; } ) ); update_option( self::BASELINE_OPTION, $samples, false ); update_option( self::AVERAGE_OPTION, round( Attack_Rules::baseline( $samples, $now ), 4 ), true ); } /** * Forgets everything (reset command and uninstall). */ public static function reset() { delete_option( self::OPTION ); delete_option( self::BASELINE_OPTION ); delete_option( self::AVERAGE_OPTION ); Scheduler::stop_minute(); } }