PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.2.2
SiteGuard WP Plugin v1.2.2
1.8.7 1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.4.3 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.11 1.7.12 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.0-beta1 1.8.0-beta2 1.8.0-beta3 1.8.0-beta4
siteguard / classes / siteguard-login-lock.php
siteguard / classes Last commit date
siteguard-admin-filter.php 11 years ago siteguard-base.php 11 years ago siteguard-captcha.php 11 years ago siteguard-config.php 11 years ago siteguard-disable-pingback.php 11 years ago siteguard-htaccess.php 11 years ago siteguard-login-alert.php 11 years ago siteguard-login-history.php 11 years ago siteguard-login-lock.php 11 years ago siteguard-rename-login.php 11 years ago siteguard-updates-notify.php 11 years ago siteguard-waf-exclude-rule.php 11 years ago
siteguard-login-lock.php
137 lines
1 <?php
2
3 class SiteGuard_LoginLock extends SiteGuard_Base {
4 var $status = SITEGUARD_LOGIN_FAILED;
5 function __construct( ) {
6 global $config;
7 if ( '1' == $config->get( 'loginlock_enable' ) ) {
8 add_action( 'wp_login_failed', array( $this, 'handler_wp_login_failed' ) );
9 add_filter( 'authenticate', array( $this, 'handler_authenticate' ), 20, 3 );
10 }
11 if ( '1' == $config->get( 'loginlock_fail_once' ) ) {
12 add_filter( 'wp_authenticate_user', array( $this, 'handler_wp_authenticate_user' ), 99, 2 );
13 }
14 }
15 function init( ) {
16 global $config;
17 if ( true === check_multisite( ) ) {
18 $config->set( 'loginlock_enable', '1' );
19 } else {
20 $config->set( 'loginlock_enable', '0' );
21 }
22 $config->set( 'loginlock_interval', '5' );
23 $config->set( 'loginlock_threshold', '3' );
24 $config->set( 'loginlock_locksec', '60' );
25 $config->set( 'loginlock_fail_once', '0' );
26 $config->set( 'fail_once_admin_only', '1' );
27 $config->update( );
28 }
29 function get_status( ) {
30 return $this->status;
31 }
32 function handler_wp_login_failed( $username ) {
33 global $wpdb, $config, $login_history;
34 $table_name = $wpdb->prefix . SITEGUARD_TABLE_LOGIN;
35
36 $ip_address = $_SERVER['REMOTE_ADDR'];
37
38 $wpdb->query( 'START TRANSACTION' );
39 $wpdb->query( "DELETE FROM $table_name WHERE status <> 1 AND last_login_time < SYSDATE() - INTERVAL 1 HOUR;" );
40 $result = $wpdb->get_row( $wpdb->prepare( "SELECT status, count, last_login_time from $table_name WHERE ip_address = %s", $ip_address ) );
41 $data = array(
42 'ip_address' => $ip_address,
43 'status' => SITEGUARD_LOGIN_FAILED,
44 'count' => 1,
45 'last_login_time' => 0,
46 );
47 $now_str = current_time( 'mysql' );
48 $now_bin = strtotime( $now_str );
49 if ( null == $result ) {
50 $data['last_login_time'] = $now_str;
51 $wpdb->insert( $table_name, $data );
52 } else {
53 $data['last_login_time'] = $result->last_login_time;
54 $interval = intval( $config->get( 'loginlock_interval' ) );
55 $limit = strtotime( $result->last_login_time ) + $interval;
56 if ( SITEGUARD_LOGIN_FAILED == $result->status ) {
57 if ( $now_bin <= $limit ) {
58 $data['count'] = $result->count + 1;
59 } else {
60 $data['count'] = 1;
61 $data['last_login_time'] = $now_str;
62 }
63 if ( $data['count'] >= intval( $config->get( 'loginlock_threshold' ) ) ) {
64 $data['status'] = SITEGUARD_LOGIN_LOCKED;
65 $data['last_login_time'] = $now_str;
66 $this->status = SITEGUARD_LOGIN_LOCKED;
67 }
68 $wpdb->update( $table_name, $data, array( 'ip_address' => $ip_address ) );
69 } else if ( SITEGUARD_LOGIN_FAIL_ONCE == $result->status || ( SITEGUARD_LOGIN_LOCKED == $result->status && $now_bin > strtotime( $result->last_login_time ) + intval( $config->get( 'loginlock_locksec' ) ) ) ) {
70 $data['status'] = SITEGUARD_LOGIN_FAILED;
71 $data['count'] = 1;
72 $data['last_login_time'] = $now_str;
73 $wpdb->update( $table_name, $data, array( 'ip_address' => $ip_address ) );
74 }
75 }
76
77 $wpdb->query( 'COMMIT' );
78
79 return;
80 }
81 function is_locked( $ip_address ) {
82 global $wpdb, $config;
83
84 $now_bin = strtotime( current_time( 'mysql' ) );
85 $table_name = $wpdb->prefix . SITEGUARD_TABLE_LOGIN;
86 $result = $wpdb->get_row( $wpdb->prepare( "SELECT status, last_login_time from $table_name WHERE ip_address = %s", $ip_address ) );
87 if ( null != $result ) {
88 if ( SITEGUARD_LOGIN_LOCKED == $result->status && $now_bin <= strtotime( $result->last_login_time ) + intval( $config->get( 'loginlock_locksec' ) ) ) {
89 return true;
90 }
91 }
92 return false;
93 }
94 function handler_authenticate( $user, $username, $password ) {
95 if ( $this->is_locked( $_SERVER['REMOTE_ADDR'] ) ) {
96 $new_errors = new WP_Error( );
97 $new_errors->add( 'siteguard-error', esc_html__( 'ERROR: LOGIN LOCKED', 'siteguard' ) );
98 $this->status = SITEGUARD_LOGIN_LOCKED;
99 return $new_errors;
100 }
101 return $user;
102 }
103 function handler_wp_authenticate_user( $user, $password ) {
104 global $login_history, $config;
105
106 if ( basename( $_SERVER['SCRIPT_NAME'] ) == 'xmlrpc.php' ) {
107 return $user;
108 }
109
110 // Login failed
111 if ( is_wp_error( $user ) ) {
112 return $user;
113 }
114 if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
115 return $user;
116 }
117 if ( '1' == $config->get( 'fail_once_admin_only' ) ) {
118 if ( ! $user->has_cap( 'administrator' ) ) {
119 return $user;
120 }
121 }
122
123 $user_login = $user->user_login;
124
125 if ( ! $login_history->is_exist( $user_login, SITEGUARD_LOGIN_FAIL_ONCE, 5/* secs after */, 60/* secs less */ ) ) {
126 $this->status = SITEGUARD_LOGIN_FAIL_ONCE;
127
128 $new_error = new WP_Error( );
129 $new_error->add( 'siteguard-error', esc_html__( 'ERROR: Please login entry again', 'siteguard' ) );
130 return $new_error;
131 }
132 return $user;
133 }
134 }
135
136 ?>
137