C:\xampp\htdocs\landing\wp-content\plugins\better-wp-security\core\modules\ipcheck\utilities.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php

final class ITSEC_Network_Brute_Force_Utilities {
    private static 
$network_endpoint 'https://ipcheck-api.ithemes.com/';

    
/**
     * Check visitor IP to see if it is banned by IPCheck.
     *
     * @param string $ip
     *
     * @return bool true if banned, false otherwise.
     */
    
public static function is_ip_banned$ip '' ) {
        if ( ! 
$ip ) {
            
$ip ITSEC_Lib::get_ip();
        }

        require_once( 
ITSEC_Core::get_core_dir() . '/lib/class-itsec-lib-ip-tools.php' );

        if ( ! 
ITSEC_Lib_IP_Tools::validate$ip ) || ITSEC_Lib::is_ip_whitelisted$ip ) ) {
            return 
false;
        }

        return 
self::get_server_response'check-ip'$ip );
    }

    
/**
     * Report visitor IP for blacklistable-offense to IPCheck.
     *
     * @param string $ip
     *
     * @return bool true if banned, false otherwise.
     */
    
public static function report_ip$ip '' ) {
        if ( ! 
$ip ) {
            
$ip ITSEC_Lib::get_ip();
        }

        require_once( 
ITSEC_Core::get_core_dir() . '/lib/class-itsec-lib-ip-tools.php' );

        if ( ! 
ITSEC_Lib_IP_Tools::validate$ip ) || ITSEC_Lib::is_ip_whitelisted$ip ) ) {
            return 
false;
        }

        return 
self::get_server_response'report-ip'$ip );
    }

    private static function 
get_server_response$action$ip ) {
        
$api_key    ITSEC_Modules::get_setting'network-brute-force''api_key' );
        
$api_secret ITSEC_Modules::get_setting'network-brute-force''api_secret' );

        if ( ! 
$api_key || ! $api_secret ) {
            return 
false;
        }

        
$cache self::get_cache$ip );

        if ( 
'check-ip' === $action ) {
            if ( 
$cache['cache_ttl'] >= ITSEC_Core::get_current_time_gmt() ) {
                return 
$cache['block'];
            }
        } elseif ( 
'report-ip' === $action ) {
            if ( 
$cache['report_ttl'] >= ITSEC_Core::get_current_time_gmt() ) {
                return 
$cache['block'];
            }
        }

        
$args json_encode( array(
            
'apikey'    => $api_key,
            
'behavior'  => 'brute-force-login',
            
'ip'        => $ip,
            
'site'      => home_url'''http' ),
            
'timestamp' => ITSEC_Core::get_current_time_gmt(),
            
'login'     => array(
                
'details' => ITSEC_Lib::get_login_details(),
                
'agent'   => isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '',
            ),
        ) );

        
$response self::call_api$action, array(), array(
            
'method' => 'POST',
            
'body' => array(
                
'request'   => $args,
                
'signature' => self::hmac_sha1$api_secret$action $args ),
            ),
        ) );

        if ( 
is_wp_error$response ) || ! isset( $response['body'] ) ) {
            return 
false;
        }

        
$response json_decode$response['body'], true );

        if ( ! 
is_array$response ) || empty( $response['success'] ) ) {
            return 
false;
        }

        
self::set_cache$ip$response );

        
$cache_seconds = isset( $response['cache_ttl'] ) ? absint$response['cache_ttl'] ) : 3600;

        if ( ! empty( 
$response['block'] ) ) {
            
$data = array(
                
'expires'     => date'Y-m-d H:i:s'ITSEC_Core::get_current_time() + $cache_seconds ),
                
'expires_gmt' => date'Y-m-d H:i:s'ITSEC_Core::get_current_time_gmt() + $cache_seconds ),
                
'type'        => 'host',
            );

            
ITSEC_Log::add_action'ipcheck''ip-blocked'$data );

            return 
true;
        }

        return 
false;
    }

    private static function 
set_cache$ip$response ) {
        
$cache self::get_cache$ip );
        
$time  ITSEC_Core::get_current_time_gmt();

        if ( isset( 
$response['block'] ) ) {
            
$cache['block'] = (boolean) $response['block'];
        }

        if ( isset( 
$response['cache_ttl'] ) ) {
            
$cache['cache_ttl'] = intval$response['cache_ttl'] ) + $time;
        } elseif ( 
=== $cache['cache_ttl'] ) {
            
$cache['cache_ttl'] = $time HOUR_IN_SECONDS;
        }

        if ( isset( 
$response['report_ttl'] ) ) {
            
$cache['report_ttl'] = intval$response['report_ttl'] ) + $time;
        }

        
$transient_time max$cache['cache_ttl'], $cache['report_ttl'] ) - $time;


        
set_site_transient"itsec_ipcheck_$ip"$cache$transient_time );
    }

    private static function 
get_cache$ip ) {
        
$cache get_site_transient"itsec_ipcheck_$ip);

        
$defaults = array(
            
'block'      => false,
            
'cache_ttl'  => 0,
            
'report_ttl' => 0,
        );

        if ( ! 
is_array$cache ) ) {
            return 
$defaults;
        }

        return 
array_merge$defaults$cache );
    }

    
/**
     * Calculates the HMAC of a string using SHA1.
     *
     * there is a native PHP hmac function, but we use this one for
     * the widest compatibility with older PHP versions
     *
     * @param string $key  the shared secret key used to generate the mac
     * @param string $data data to be signed
     *
     *
     * @return  string    base64 encoded hmac
     */
    
private static function hmac_sha1$key$data ) {
        if ( 
strlen$key ) > 64 ) {
            
$key pack'H*'sha1$key ) );
        }

        
$key  str_pad$key64chr0x00 ) );
        
$ipad str_repeatchr0x36 ), 64 );
        
$opad str_repeatchr0x5c ), 64 );
        
$hmac pack'H*'sha1( ( $key $opad ) . pack'H*'sha1( ( $key $ipad ) . $data ) ) ) );

        return 
base64_encode$hmac );
    }

    
/**
     * Retrieve an API key from the IPCheck server
     *
     * @since 4.5
     *
     * @param string $email the email address to associate with the key
     * @param bool   $optin true to optin to mailing list else false
     *
     * @return string|WP_Error The API key or a WP_Error object.
     */
    
public static function get_api_key$email$optin ) {
        
$email sanitize_text_fieldtrim$email ) );

        if ( ! 
is_email$email ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-get-api-key-bad-email'sprintf__'The supplied email address (%s) is invalid. A valid email address is required in order to sign up for the Network Bruteforce Protection by iThemes.''better-wp-security' ), $email ) );
        }

        
$response self::call_api'request-key', array(
            
'email' => $email,
            
'optin' => $optin,
        ) );

        if ( 
is_wp_error$response ) ) {
            return 
$response;
        }

        if ( ! isset( 
$response['body'] ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-get-api-key-failed-get-request'__'An unknown error prevented the API key request from succeeding. This problem could be due to a server configuration or plugin compatibility issue. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        
$body json_decode$response['body'], true );

        if ( ! 
is_array$body ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-get-api-key-bad-response'__'An unknown error prevented the API key request from succeeding. The request for an API key returned an unrecognized response. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        if ( isset( 
$body['error']['message'] ) ) {
            return new 
WP_Error(
                
'itsec-network-brute-force-utilities-get-api-key-' . ( isset( $body['error']['type'] ) ? $body['error']['type'] : 'unknown' ),
                
sprintf__'There was an error returned from the Network Brute Force Protection API: %1$s''better-wp-security' ), $body['error']['message'] )
            );
        }

        
$key trimsanitize_text_field$body['apikey'] ) );

        if ( empty( 
$key ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-get-api-key-bad-response'__'An unknown error prevented the API key request from succeeding. The request for an API key returned an empty key. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        return 
$key;
    }

    
/**
     * Activate an IPCheck API Key
     *
     * @since 4.5
     *
     * @param string $api_key the API key to activate
     *
     * @return string|WP_Error IPCheck activation secret or a WP_Error object.
     */
    
public static function activate_api_key$api_key ) {
        
$api_key sanitize_text_fieldtrim$api_key ) );

        if ( empty( 
$api_key ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-activate-api-key-empty-key'__'An unknown error prevented the API key secret request from succeeding. The request for an API key submitted an empty key. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        
$response self::call_api'activate-key', array(
            
'apikey' => $api_key,
            
'site'   => home_url'''http' ),
        ) );

        if ( 
is_wp_error$response ) ) {
            return 
$response;
        }

        if ( ! isset( 
$response['body'] ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-activate-api-key-failed-get-request'__'An unknown error prevented the API key secret request from succeeding. This problem could be due to a server configuration or plugin compatibility issue. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        
$body json_decode$response['body'], true );

        if ( ! 
is_array$body ) || ! isset( $body['secret'] ) ) {
            
// If this is an error with a message, show that to the user
            
if ( ! empty( $body['error'] ) && ! empty( $body['error']['message'] ) ) {
                return new 
WP_Error'itsec-network-brute-force-utilities-activate-api-key-error-response'sprintf__'There was an error returned from the Network Brute Force Protection API: %1$s''better-wp-security' ), $body['error']['message'] ) );
            }

            return new 
WP_Error'itsec-network-brute-force-utilities-activate-api-key-bad-response'__'An unknown error prevented the API key secret request from succeeding. The request for an API key secret returned an unrecognized response. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        
$secret trimsanitize_text_field$body['secret'] ) );

        if ( empty( 
$secret ) ) {
            return new 
WP_Error'itsec-network-brute-force-utilities-activate-api-key-bad-response'__'An unknown error prevented the API key secrete request from succeeding. The request for an API key secret returned an empty key secret. Please wait a few minutes and try again.''better-wp-security' ) );
        }

        return 
$secret;
    }

    private static function 
call_api$action$query = array(), $args = array() ) {

        
$url self::$network_endpoint;
        
$url add_query_arg'action'$action$url );

        if ( 
$query ) {
            
$url add_query_arg$query$url );
        }

        if ( ! isset( 
$args['user-agent'] ) ) {
            if ( 
ITSEC_Core::is_pro() ) {
                
$args['user-agent'] = 'iThemes Security Pro/';
            } else {
                
$args['user-agent'] = 'iThemes Security/';
            }

            
$args['user-agent'] .= ITSEC_Core::get_plugin_version() . '-' ITSEC_Core::get_plugin_build() . '; WordPress/' get_bloginfo'version' );
        }

        
$url  apply_filters'itsec_ipcheck_api_request_url'$url$action$query$args );
        
$args apply_filters'itsec_ipcheck_api_request_args'$args$url$action$query );

        return 
wp_remote_request$url$args );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586