| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rate Limiter Class. |
| 4 |
* |
| 5 |
* @package Welcart |
| 6 |
*/ |
| 7 |
class RateLimiter { |
| 8 |
|
| 9 |
public $folder_log_path; |
| 10 |
public $login_failed_log_path; |
| 11 |
public $ip_blocked_path; |
| 12 |
public $monitoring; /* minutes */ |
| 13 |
public $num_of_errors; |
| 14 |
public $rejection_time; /* minutes */ |
| 15 |
public $status; |
| 16 |
public $excluded_ip; |
| 17 |
|
| 18 |
/** |
| 19 |
* Construct. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
|
| 23 |
$this->folder_log_path = USCES_WP_CONTENT_DIR . '/uploads/usces_logs/'; |
| 24 |
$this->login_failed_log_path = $this->folder_log_path . 'member_login_failed.log'; |
| 25 |
$this->ip_blocked_path = $this->folder_log_path . 'ip_addresses_blocked.log'; |
| 26 |
|
| 27 |
// get brute force config. |
| 28 |
$options = get_option( 'usces_ex', array() ); |
| 29 |
$this->monitoring = ( ! isset( $options['system']['brute_force']['monitoring_span'] ) ) ? 5 : (int) $options['system']['brute_force']['monitoring_span']; |
| 30 |
$this->num_of_errors = ( ! isset( $options['system']['brute_force']['num_of_errors'] ) ) ? 3 : (int) $options['system']['brute_force']['num_of_errors']; |
| 31 |
$this->rejection_time = ( ! isset( $options['system']['brute_force']['rejection_time'] ) ) ? 10 : (int) $options['system']['brute_force']['rejection_time']; |
| 32 |
$this->status = ( ! isset( $options['system']['brute_force']['status'] ) ) ? 0 : (int) $options['system']['brute_force']['status']; |
| 33 |
$this->excluded_ip = ( ! isset( $options['system']['brute_force']['excluded_ip'] ) ) ? array() : $options['system']['brute_force']['excluded_ip']; |
| 34 |
|
| 35 |
if ( $this->status ) { |
| 36 |
$this->initLogsFolder(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Check block IP |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function checkBlockIP() { |
| 46 |
if ( $this->status ) { |
| 47 |
try { |
| 48 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 49 |
if ( in_array( $ip, $this->excluded_ip, true ) ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$data = $this->getLoginFailedDataByIP( $ip ); |
| 54 |
$ip_blocked = $this->getIpAddressesBlocked(); |
| 55 |
|
| 56 |
if ( isset( $ip_blocked[ $ip ] ) && ( strtotime( "-{$this->rejection_time} minutes" ) < $ip_blocked[ $ip ] ) ) { |
| 57 |
$this->saveLoginFailed(); |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
if ( count( $data ) ) { |
| 62 |
$count = 0; |
| 63 |
foreach ( $data as $key => $value ) { |
| 64 |
if ( strtotime( "-{$this->monitoring} minutes" ) < $key ) { |
| 65 |
$count += $value; |
| 66 |
} |
| 67 |
} |
| 68 |
if ( $count >= $this->num_of_errors ) { |
| 69 |
$ip_blocked[ $ip ] = strtotime( 'now' ); |
| 70 |
file_put_contents( $this->ip_blocked_path, json_encode( $ip_blocked ) ); |
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
} catch ( Throwable $exception ) { |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Save login failed |
| 83 |
*/ |
| 84 |
public function saveLoginFailed() { |
| 85 |
global $wp_query; |
| 86 |
|
| 87 |
try { |
| 88 |
if ( $this->status ) { |
| 89 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 90 |
if ( in_array( $ip, $this->excluded_ip, true ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$content = $this->getLoginFailedData(); |
| 95 |
$row = $this->getLoginFailedDataByIP( $ip ); |
| 96 |
|
| 97 |
$row[ strtotime( 'now' ) ] = ( isset( $row[ strtotime( 'now' ) ] ) ) ? ( $row[ strtotime( 'now' ) ] + 1 ) : 1; |
| 98 |
|
| 99 |
if ( count( $row ) > $this->num_of_errors ) { |
| 100 |
unset( $row[ array_key_first( $row ) ] ); |
| 101 |
} |
| 102 |
$content[ $ip ] = $row; |
| 103 |
file_put_contents( $this->login_failed_log_path, json_encode( $content ) ); |
| 104 |
|
| 105 |
$number_of_login_fail = 0; |
| 106 |
foreach ( $content[ $ip ] as $key => $value ) { |
| 107 |
if ( strtotime( "-{$this->monitoring} minutes" ) < $key ) { |
| 108 |
$number_of_login_fail++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if ( $number_of_login_fail >= $this->num_of_errors ) { |
| 113 |
$ip_blocked = $this->getIpAddressesBlocked(); |
| 114 |
$ip_blocked[ $ip ] = strtotime( 'now' ); |
| 115 |
file_put_contents( $this->ip_blocked_path, json_encode( $ip_blocked ) ); |
| 116 |
|
| 117 |
$wp_query->set_403(); |
| 118 |
status_header( 403 ); |
| 119 |
exit(); |
| 120 |
} |
| 121 |
} |
| 122 |
} catch ( Throwable $exception ) { |
| 123 |
|
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Clear login failed |
| 129 |
*/ |
| 130 |
public function clear_login_failed() { |
| 131 |
try { |
| 132 |
if ( $this->status ) { |
| 133 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 134 |
$content = $this->getLoginFailedData(); |
| 135 |
unset( $content[ $ip ] ); |
| 136 |
file_put_contents( $this->login_failed_log_path, json_encode( $content ) ); |
| 137 |
} |
| 138 |
} catch ( Throwable $exception ) { |
| 139 |
|
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get login failed data |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public function getLoginFailedData() { |
| 149 |
if ( usces_is_reserved_file( $this->login_failed_log_path, 1 ) ) { |
| 150 |
$res = json_decode( file_get_contents( $this->login_failed_log_path ), true ); |
| 151 |
} else { |
| 152 |
$res = array(); |
| 153 |
} |
| 154 |
return $res; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get login failed data by IP |
| 159 |
* |
| 160 |
* @param string $ip IP Address. |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function getLoginFailedDataByIP( $ip ) { |
| 164 |
$content = $this->getLoginFailedData(); |
| 165 |
return ( isset( $content[ $ip ] ) ) ? $content[ $ip ] : array(); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get IP Addresses blocked |
| 170 |
* |
| 171 |
* @return array |
| 172 |
*/ |
| 173 |
public function getIpAddressesBlocked() { |
| 174 |
if ( usces_is_reserved_file( $this->ip_blocked_path, 1 ) ) { |
| 175 |
$res = json_decode( file_get_contents( $this->ip_blocked_path ), true ); |
| 176 |
} else { |
| 177 |
$res = array(); |
| 178 |
} |
| 179 |
return $res; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Init logs folder |
| 184 |
*/ |
| 185 |
public function initLogsFolder() { |
| 186 |
if ( ! is_dir( $this->folder_log_path ) ) { |
| 187 |
mkdir( $this->folder_log_path, 0775 ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|