-
Notifications
You must be signed in to change notification settings - Fork 21
/
login-security-solution.php
3043 lines (2723 loc) · 88.7 KB
/
login-security-solution.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Login Security Solution
*
* Description: Requires very strong passwords, repels brute force login attacks, prevents login information disclosures, expires idle sessions, notifies admins of attacks and breaches, permits administrators to disable logins for maintenance or emergency reasons and reset all passwords.
*
* Plugin URI: https://wordpress.org/plugins/login-security-solution/
* Version: 0.56.0
* (Remember to change the VERSION constant, below, as well!)
* Author: Daniel Convissor
* Author URI: http://www.analysisandsolutions.com/
* License: GPLv2
* Network: true
* Text Domain: login-security-solution
* Domain Path: /languages
* @package login-security-solution
*/
/**
* The instantiated version of this plugin's class
*/
$GLOBALS['login_security_solution'] = new login_security_solution;
/**
* The Login Security Solution plugin enhances WordPress' security
*
* @package login-security-solution
* @link https://wordpress.org/plugins/login-security-solution/
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @author Daniel Convissor <[email protected]>
* @copyright The Analysis and Solutions Company, 2012-2015
*/
class login_security_solution {
/**
* This plugin's identifier
*/
const ID = 'login-security-solution';
/**
* This plugin's name
*/
const NAME = 'Login Security Solution';
/**
* This plugin's version
*/
const VERSION = '0.56.0';
/**
* This plugin's table name prefix
* @var string
*/
protected $prefix = 'login_security_solution_';
const E_ASCII = 'pw-ascii';
const E_CASE = 'pw-case';
const E_COMMON = 'pw-common';
const E_DICT = 'pw-dict';
const E_EMPTY = 'pw-empty';
const E_NUMBER = 'pw-number';
const E_PUNCT = 'pw-punct';
const E_REUSED = 'pw-reused';
const E_SEQ_CHAR = 'pw-seqchar';
const E_SEQ_KEY = 'pw-seqkey';
const E_SHORT = 'pw-short';
const E_SITE = 'pw-site';
const E_STRING = 'pw-string';
const E_USER = 'pw-user';
const LOGIN_FORCE_PW_CHANGE = 2;
const LOGIN_NOTIFY = 4;
const LOGIN_VERIFIED_IP_SAFE = 8;
const LOGIN_UNKNOWN_IP = 16;
const LOGIN_CLEAN = 32;
const LOGIN_VERIFIED_IP_NEW = 64;
const LOGIN_VERIFIED_IP_OLD = 128;
/**
* Is the dict command available?
* @var bool true/false if known, null if unknown
*/
protected $available_dict;
/**
* Is PHP's mbstring extension enabled?
* @var bool true/false if known, null if unknown
*/
protected $available_mbstring;
/**
* Location of our dictionary files
*
* Public for use by utilities.
*
* @var string
*/
public $dir_dictionaries;
/**
* Location of our sequence files
* @var string
*/
protected $dir_sequences;
/**
* Is the current request coming from the XML-RPC interface?
* @var bool
*/
protected $is_xmlrpc = false;
/**
* Our URI query string key for passing messages to the login form
* @var string
*/
protected $key_login_msg;
/**
* Has the internationalization text domain been loaded?
* @var bool
*/
protected $loaded_textdomain = false;
/**
* This plugin's options
*
* Options from the database are merged on top of the default options.
*
* @see login_security_solution::set_options() to obtain the saved
* settings
* @var array
*/
protected $options = array();
/**
* This plugin's default options
* @var array
*/
protected $options_default = array(
'admin_email' => '',
'block_author_query' => 1,
'deactivate_deletes_data' => 0,
'disable_logins' => 0,
'idle_timeout' => 15,
'login_fail_minutes' => 120,
'login_fail_tier_2' => 5,
'login_fail_tier_3' => 10,
'login_fail_tier_dos' => 500,
'login_fail_notify' => 50,
'login_fail_notify_multiple' => 0,
'login_fail_breach_notify' => 6,
'login_fail_breach_pw_force_change' => 6,
'login_fail_delete_interval' => 0,
'login_fail_delete_days' => 120,
'pw_change_days' => 0,
'pw_change_grace_period_minutes' => 15,
'pw_complexity_exemption_length' => 20,
'pw_dict_file' => '/usr/share/dictd/gcide.index',
'pw_length' => 10,
'pw_reuse_count' => 0,
);
/**
* Our option name for storing the plugin's settings
* @var string
*/
protected $option_name;
/**
* Should the wp_login_failed action be skipped?
* @var bool
*/
protected $skip_wp_login_failed = false;
/**
* How many seconds were slept
* @var int
*/
protected $sleep;
/**
* Name, with $table_prefix, of the table tracking login failures
* @var string
*/
protected $table_fail;
/**
* Our usermeta key for tracking when passwords were changed
* @var string
*/
protected $umk_changed;
/**
* Our usermeta key for tracking when a password grace period started
* @var string
*/
protected $umk_grace_period;
/**
* Our usermeta key for tracking old passwords
* @var string
*/
protected $umk_hashes;
/**
* Our usermeta key for tracking when the user last hit the site
* @var string
*/
protected $umk_last_active;
/**
* Our usermeta key for tracking if a user's password needs to be changed
* @var string
*/
protected $umk_pw_force_change;
/**
* Our usermeta key for tracking this user's verified IP addresses
* @var string
*/
protected $umk_verified_ips;
/**
* The user's password from the authenticate filter
* @var string
*/
protected $user_pass;
/**
* Is this an XML-RPC request?
* @var bool
*/
protected $xmlrpc_enabled = false;
/**
* Declares the WordPress action and filter callbacks
*
* @return void
* @uses login_security_solution::initialize() to set the object's
* properties
*/
public function __construct() {
$this->initialize();
add_action('auth_cookie_bad_username', array(&$this, 'auth_cookie_bad'));
add_action('auth_cookie_bad_hash', array(&$this, 'auth_cookie_bad'));
add_action('auth_cookie_valid', array(&$this, 'check'), 1, 2);
add_action('password_reset', array(&$this, 'password_reset'), 10, 2);
add_action('user_profile_update_errors',
array(&$this, 'user_profile_update_errors'), 999, 3);
if (version_compare($GLOBALS['wp_version'], '4.1', '<')) {
add_action('login_form_resetpass', array(&$this, 'pw_policy_establish'));
add_action('validate_password_reset', array(&$this, 'pw_policy_establish'));
add_action('personal_options', array(&$admin, 'pw_policy_establish'));
add_action('user_new_form_tag', array(&$admin, 'pw_policy_establish'));
} else {
add_filter('password_hint', array(&$this, 'password_hint'));
}
add_filter('xmlrpc_enabled', array(&$this, 'xmlrpc_enabled'));
add_filter('authenticate', array(&$this, 'authenticate'), 999, 3);
add_action('wp_login_failed', array(&$this, 'wp_login_failed'));
add_action('wp_login', array(&$this, 'wp_login'), 1, 2);
add_filter('login_errors', array(&$this, 'login_errors'));
add_filter('login_message', array(&$this, 'login_message'));
if ($this->options['block_author_query']) {
add_filter('wp_redirect', array(&$this, 'author_query_redirect'));
}
if ($this->options['disable_logins']) {
add_filter('comments_open', array(&$this, 'comments_open'));
}
if ($this->options['idle_timeout']) {
add_action('wp_logout', array(&$this, 'delete_last_active'));
add_action('auth_cookie_expired', array(&$this, 'auth_cookie_expired'));
}
if (is_admin()) {
$this->load_plugin_textdomain();
require_once dirname(__FILE__) . '/admin.php';
$admin = new login_security_solution_admin;
if (is_multisite()) {
$admin_menu = 'network_admin_menu';
$admin_notices = 'network_admin_notices';
$plugin_action_links = 'network_admin_plugin_action_links_login-security-solution/login-security-solution.php';
} else {
$admin_menu = 'admin_menu';
$admin_notices = 'admin_notices';
$plugin_action_links = 'plugin_action_links_login-security-solution/login-security-solution.php';
}
add_action($admin_menu, array(&$admin, 'admin_menu'));
add_action('admin_init', array(&$admin, 'admin_init'));
add_filter($plugin_action_links, array(&$admin, 'plugin_action_links'));
if ($this->options['disable_logins']) {
add_action('admin_notices', array(&$admin, 'admin_notices_disable_logins'));
}
register_activation_hook(__FILE__, array(&$admin, 'activate'));
if ($this->options['deactivate_deletes_data']) {
register_deactivation_hook(__FILE__, array(&$admin, 'deactivate'));
}
// NON-STANDARD: This is for the password change page.
add_action($admin_menu, array(&$admin, 'admin_menu_pw_force_change'));
add_action('admin_head', array(&$admin, 'admin_menu_pw_force_change_hide'));
if (!$admin->was_pw_force_change_done()) {
add_action($admin_notices, array(&$admin, 'admin_notices_pw_force_change'));
}
add_action('admin_init', array(&$admin, 'admin_init_pw_force_change'));
}
}
/**
* Sets the object's properties and options
*
* This is separated out from the constructor to avoid undesirable
* recursion. The constructor sometimes instantiates the admin class,
* which is a child of this class. So this method permits both the
* parent and child classes access to the settings and properties.
*
* @return void
*
* @uses login_security_solution::set_options() to replace the default
* options with those stored in the database
*/
protected function initialize() {
global $wpdb;
$prefix = $wpdb->get_blog_prefix(0);
$prefix = str_replace('`', '``', $prefix);
$this->table_fail = $prefix . $this->prefix . 'fail';
$this->key_login_msg = self::ID . '-login-msg-id';
$this->option_name = self::ID . '-options';
$this->umk_changed = self::ID . '-pw-changed-time';
$this->umk_pw_force_change = self::ID . '-pw-force-change';
$this->umk_grace_period = self::ID . '-pw-grace-period-start-time';
$this->umk_hashes = self::ID . '-pw-hashes';
$this->umk_last_active = self::ID . '-last-active';
$this->umk_verified_ips = self::ID . '-verified-ips';
$this->dir_dictionaries = dirname(__FILE__) . '/pw_dictionaries/';
$this->dir_sequences = dirname(__FILE__) . '/pw_sequences/';
$this->set_options();
if ($this->options['login_fail_tier_2'] < 2) {
$this->options['login_fail_tier_2'] = 2;
}
if ($this->options['pw_change_days']
&& !$this->options['pw_reuse_count'])
{
$this->options['pw_reuse_count'] = 5;
}
if ($this->options['pw_change_grace_period_minutes'] < 5) {
$this->options['pw_change_grace_period_minutes'] = 5;
}
if ($this->options['pw_complexity_exemption_length'] < 20) {
$this->options['pw_complexity_exemption_length'] = 20;
}
if ($this->options['pw_length'] < 10) {
$this->options['pw_length'] = 10;
}
}
/*
* ===== ACTION & FILTER CALLBACK METHODS =====
*/
/**
* Sends failed auth cookie data to our login failure process
*
* NOTE: This method is automatically called by WordPress when a user's
* cookie has an invalid user name or password hash.
*
* @param array $cookie_elements the auth cookie data
* @return mixed return values provided for unit testing
*
* @uses login_security_solution::process_login_fail() to log the failure
* and slow down the response as necessary
*/
public function auth_cookie_bad($cookie_elements) {
if ($this->sleep) {
###$this->log(__FUNCTION__, "already called ($this->sleep)");
return -3;
}
// Remove cookies to prevent further mayhem.
wp_clear_auth_cookie();
if (empty($cookie_elements['username'])) {
###$this->log(__FUNCTION__, "empty username");
return -1;
} else {
$username = $cookie_elements['username'];
}
if (empty($cookie_elements['hmac'])) {
###$this->log(__FUNCTION__, "empty hmac");
return -2;
} else {
$hmac = $cookie_elements['hmac'];
}
###$this->log(__FUNCTION__, "$username, $hmac");
return $this->process_login_fail($username, $hmac);
}
/**
* Removes the current user's last active time metadata
*
* NOTE: This method is automatically called by WordPress when a user's
* cookie has expired.
*
* @param array $cookie_elements the auth cookie data
* @return mixed return values provided for unit testing
*/
public function auth_cookie_expired($cookie_elements) {
if (empty($cookie_elements['username'])) {
return;
}
$user = get_user_by('login', $cookie_elements['username']);
if (! $user instanceof WP_User) {
return -1;
}
###$this->log(__FUNCTION__, $user->user_login);
return delete_user_meta($user->ID, $this->umk_last_active);
}
/**
* Stores the user's password for later use and handles XML-RPC checks
*
* NOTE: This method is automatically called by WordPress from the
* wp_authenticate() function, which is used during web and XML-RPC logins.
*
* @param mixed $user watever the prior filter gave us
* @param string $user_name the user name from the current login form
* @param string $user_pass the unhashed new password
* @return mixed whatever the prior filter gave us
*
* @uses login_security_solution::$user_pass to hold the password
*/
public function authenticate($user, $user_name, $user_pass = null) {
if (!$user_name) {
###$this->log(__FUNCTION__, "empty user_name");
return $user;
}
if ($user_pass === null) {
###$this->log(__FUNCTION__, "empty user_pass");
die(self::NAME . ": password not passed to authenticate filter");
}
$this->user_pass = $user_pass;
if (!$this->is_xmlrpc) {
###$this->log(__FUNCTION__, "$user_name web");
return $user;
}
###$this->log(__FUNCTION__, "$user_name xmlrpc");
if ($user instanceof WP_Error) {
###$this->log(__FUNCTION__, "$user_name already wp_error");
return $user;
}
$this->process_login_success($user);
if ($this->check(null, $user) !== true) {
###$this->log(__FUNCTION__, "$user_name check failed");
// Login is legit, but pw needs resetting. Don't insert fail.
$this->skip_wp_login_failed = true;
return null;
}
###$this->log(__FUNCTION__, "$user_name good");
return $user;
}
/**
* Blocks attempts to discover user names via the "?author=<id>" URI
*
* WP redirects the author query string to a page showing the author's
* name. This allows attackers to probe sites for the names of actual
* users, which can then be used for brute force attacks.
*
* Tried "author_link" filter, but is called multiple times and does
* not work.
*
* @param string $location the requested URI to go to
* @return string the URI to go to
*
* @uses get_site_url() to know where the home page is
*/
public function author_query_redirect($location) {
###$this->log(__FUNCTION__, $location);
if (strpos($location, 'index.php/author/') !== false) {
return get_site_url();
}
return $location;
}
/**
* Redirects the current user to the login screen if their password
* is expired, needs to be reset, or their session was idle too long
*
* NOTE: This method is automatically called by WordPress after
* successful validation of authentication cookies.
*
* @param array $cookie_elements values from the user's cookies (ignored)
* @param WP_User $user the current user
* @return mixed return values provided for unit testing
*
* @uses login_security_solution::is_idle() to know if it has been too
* long since the user's last action
* @uses login_security_solution::is_pw_expired() to know if it has been
* too long since the password was last changed
* @uses login_security_solution::get_pw_force_change() to know if the
* user has to change their password for other reasons
* @uses login_security_solution::$options for the disable_logins setting
* @uses login_security_solution::redirect_to_login() to send the user to
* the login form and tell them what the problem is
*/
public function check($cookie_elements, $user) {
global $current_user;
// The auth_cookie_valid action may be executed multiple times.
// Bail if the current_user has not been determined yet.
if (!($current_user instanceof WP_User) || empty($user->ID)) {
###$this->log(__FUNCTION__, "empty user");
return false;
}
/*
* NOTE: redirect_to_login() calls exit(), except when unit testing.
*/
if (!$this->is_xmlrpc) {
if ($this->is_idle($user->ID)) {
###$this->log(__FUNCTION__, "idle");
$this->redirect_to_login('idle', true);
return -5;
}
}
if ($this->is_pw_expired($user->ID)) {
$grace = $this->check_pw_grace_period($user->ID);
if ($grace === true) {
###$this->log(__FUNCTION__, "first since password expired");
if (!$this->is_xmlrpc) {
$this->redirect_to_login('pw_grace', true);
}
return -1;
} elseif ($grace === false) {
###$this->log(__FUNCTION__, "grace period expired");
if (!$this->is_xmlrpc) {
$this->redirect_to_login('pw_expired', false, 'retrievepassword');
}
return -2;
}
// Grace period is in effect, let them slide for now.
}
if ($this->get_pw_force_change($user->ID)) {
###$this->log(__FUNCTION__, "password force change");
if (!$this->is_xmlrpc) {
$this->redirect_to_login('pw_force', false, 'retrievepassword');
}
return -3;
}
if ($this->options['disable_logins']
&& !current_user_can('administrator'))
{
###$this->log(__FUNCTION__, "disable logins");
if (!$this->is_xmlrpc) {
$this->redirect_to_login();
}
return -4;
}
###$this->log(__FUNCTION__, "good");
return true;
}
/**
* Tells WordPress to disallow commenting on posts
*
* NOTE: This method is automatically called by WordPress when checking
* to see if comments are allowed on a post AND our "disable_logins"
* option is enabled
*
* @return bool always returns false
*/
public function comments_open() {
return false;
}
/**
* Removes the current user's last active time metadata
*
* NOTE: This method is automatically called by WordPress when users
* log in or out.
*
* @return mixed return values provided for unit testing
*/
public function delete_last_active() {
global $user_ID, $user_name;
if (empty($user_ID)) {
if (empty($user_name)) {
###$this->log(__FUNCTION__, "empty user_ID, user_name");
return;
}
$user = get_user_by('login', $user_name);
if (! $user instanceof WP_User) {
###$this->log(__FUNCTION__, "unknown user_name");
return -1;
}
$user_ID = $user->ID;
}
return delete_user_meta($user_ID, $this->umk_last_active);
}
/**
* Alters the failure messages from logins and password resets that
* contain information disclosures
*
* The following measures are necessary, at least in WordPress 3.3:
* + Changes invalid user name message from log in process.
* + Changes invalid password message from log in process.
* + Unsets the user name when the password is wrong.
* + Changes invalid user name message from lost password process.
*
* These cloaking measures complicate cracking attempts by keeping
* attackers from knowing that half of the puzzle has been solved.
*
* NOTE: This method is automatically called by WordPress when attempted
* logins via web forms are unssucessful.
*
* @param string $out the output from earlier login_errors filters
* @return string
*/
public function login_errors($out = '') {
global $errors, $wp_error;
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'register') {
// Do not alter "invalid_username" or "invalid_email" messages
// from registration process. (WP 3.3 reuses error codes.)
###$this->log(__FUNCTION__, "register");
return $out;
}
if (is_wp_error($errors)) {
$error_codes = $errors->get_error_codes();
} elseif (is_wp_error($wp_error)) {
$error_codes = $wp_error->get_error_codes();
} else {
###$this->log(__FUNCTION__, "not wp_error");
return $out;
}
$codes_to_cloak = array('incorrect_password', 'invalid_username');
if (array_intersect($error_codes, $codes_to_cloak)) {
###$this->log(__FUNCTION__, "invalid username or password");
// Unset user name to avoid information disclosure.
unset($_POST['log']);
$this->load_plugin_textdomain();
return $this->hsc_utf8(__('Invalid username or password.', 'login-security-solution'));
}
$codes_to_cloak = array('invalid_email', 'invalidcombo');
if (array_intersect($error_codes, $codes_to_cloak)) {
###$this->log(__FUNCTION__, "password reset invalid user");
// Translation already in WP.
return $this->hsc_utf8(__('Password reset is not allowed for this user'));
}
###$this->log(__FUNCTION__, "flow through");
return $out;
}
/**
* Adds our message to the other messages that appear above the login form
*
* NOTE: This method is automatically called by WordPress for displaying
* text above the login form.
*
* @param string $out the output from earlier login_message filters
* @return string
*
* @uses login_security_solution::$key_login_msg to know which $_GET
* parameter to watch for our message ID's
*/
public function login_message($out = '') {
$this->load_plugin_textdomain();
$ours = '';
if (!empty($_GET[$this->key_login_msg])) {
switch ($_GET[$this->key_login_msg]) {
case 'idle':
$ours = sprintf(__('It has been over %d minutes since your last action.', 'login-security-solution'), $this->options['idle_timeout']);
$ours .= ' ' . __('Please log back in.', 'login-security-solution');
break;
case 'pw_expired':
$ours = __('The grace period for changing your password has expired.', 'login-security-solution');
$ours .= ' ' . __('Please submit this form to reset your password.', 'login-security-solution');
break;
case 'pw_force':
$ours = __('Your password must be reset.', 'login-security-solution');
$ours .= ' ' . __('Please submit this form to reset it.', 'login-security-solution');
break;
case 'pw_grace':
$ours = __('Your password has expired. Please log in and change it.', 'login-security-solution');
$ours .= ' ' . sprintf(__('We provide a %d minute grace period to do so.', 'login-security-solution'), $this->options['pw_change_grace_period_minutes']);
break;
default:
$ours .= $this->msg($_GET[$this->key_login_msg]);
}
}
if ($this->options['disable_logins']) {
$msg = __('The site is undergoing maintenance.', 'login-security-solution');
$msg .= ' ' . __('Please try again later.', 'login-security-solution');
$out .= '<p class="login message">'
. $this->hsc_utf8($msg) . '</p>';
}
if ($ours) {
$out .= '<p class="login message">'
. $this->hsc_utf8($ours) . '</p>';
}
return $out;
}
/**
* For WP >= 4.1, replaces WP's password policy text with ours
*
* NOTE: This method is automatically called by WordPress'
* password_hint filter in wp_get_password_hint().
*
* @param string $hint the output from earlier password_hint filters
* @return string
*
* @uses login_security_solution::$options for the pw_length and
* pw_complexity_exemption_length values
*/
public function password_hint($hint = '') {
$this->load_plugin_textdomain();
return $this->hsc_utf8(sprintf(__("The password should either be: A) at least %d characters long and contain upper and lower case letters (except languages that only have one case) plus numbers and punctuation, or B) at least %d characters long. The password can not contain words related to you or this website.", 'login-security-solution'), $this->options['pw_length'], $this->options['pw_complexity_exemption_length']));
}
/**
* Conveys the password change information to the user's metadata
*
* NOTE: This method is automatically called by WordPress when users
* provide their new password via the password reset functionality.
*
* @param WP_User $user the user object being edited
* @param string $user_pass the unhashed new password
* @return mixed return values provided for unit testing
*
* @uses login_security_solution::process_pw_metadata() to update user
* metadata
*/
public function password_reset($user, $user_pass) {
if (empty($user->ID)) {
###$this->log(__FUNCTION__, "user->ID not set");
return false;
}
$user->user_pass = $user_pass;
$errors = new WP_Error;
if (!$this->validate_pw($user, $errors)) {
###$this->log(__FUNCTION__, "invalid password chosen");
$this->set_pw_force_change($user->ID);
$code = $errors->get_error_code();
$code = str_replace(self::ID . '_', '', $code);
$this->redirect_to_login($code, false, 'rp');
return -1;
}
if ($this->is_pw_reused($user_pass, $user->ID)) {
###$this->log(__FUNCTION__, "password reused");
$this->redirect_to_login(self::E_REUSED, false, 'rp');
return -2;
}
$this->save_verified_ip($user->ID, $this->get_ip());
$this->process_pw_metadata($user->ID, $user_pass);
}
/**
* Declares our password policy gettext filter and deactivates the
* password strength indicator script
*
* NOTE: This method is automatically called by WordPress
* on the wp-login.php, user-new.php, and user-edit.php pages.
*
* @return void
*/
public function pw_policy_establish() {
add_filter('gettext', array(&$this, 'pw_policy_rewrite'), 11, 2);
wp_deregister_script('password-strength-meter');
}
/**
* For WP < 4.1, replaces WP's password policy text with ours
*
* NOTE: This method is automatically called by WordPress during gettext
* calls on the wp-login.php, user-new.php, and user-edit.php pages.
*
* @param string $translated the translated output from earlier filters
* @param string $original the un-translated text
* @return string our translated password policy
*
* @uses login_security_solution::$options for the pw_length and
* pw_complexity_exemption_length values
*/
public function pw_policy_rewrite($translated, $original) {
$policy = array(
'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ & ).',
'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).',
);
if (in_array($original,$policy)) {
$translated = $this->password_hint();
}
return $translated;
}
/**
* Ensures passwords meet policy requirements
*
* NOTE: This method is automatically called by WordPress when users save
* their profile information or when admins add a user. The callback
* is activated in the edit_user() function in wp-admin/includes/user.php.
*
* @param WP_Error $errors the means to provide specific error messages
* @param bool $update is this an existing user?
* @param WP_User $user the user object being edited
* @return bool|null return values provided for unit testing
*
* @uses login_security_solution::is_pw_reused() to know if it's an old
* pw
* @uses login_security_solution::validate_pw() to know if the pw is
* kosher
* @uses login_security_solution::process_pw_metadata() to update user
* metadata
* @uses login_security_solution::save_verified_ip() to store good IPs
*/
public function user_profile_update_errors(&$errors, $update, $user) {
if (!empty($user->ID) && $user->ID == get_current_user_id()) {
$this->save_verified_ip($user->ID, $this->get_ip());
}
if ($update) {
if (empty($user->user_pass) || empty($user->ID)) {
// Password is not being changed.
return null;
}
if ($this->is_pw_reused($user->user_pass, $user->ID)) {
$errors->add(self::ID,
$this->err($this->msg(self::E_REUSED)),
array('form-field' => 'pass1')
);
return false;
}
}
$answer = $this->validate_pw($user, $errors);
// Empty ID means an admin is adding a new user.
if (!empty($user->ID) && !$errors->get_error_codes()) {
$this->process_pw_metadata($user->ID, $user->user_pass);
}
return $answer;
}
/**
* Passes good web form logins to process_login_success()
*
* NOTE: This method is automatically called by WordPress upon successful
* logins via wp_signon().
*
* @param string $user_name the user name from the current login form
* @param WP_User $user the current user
* @return mixed return values provided for unit testing
*
* @uses login_security_solution::process_login_success() to, uh, process
*/
public function wp_login($user_name, $user) {
###$this->log(__FUNCTION__, is_object($user) ? $user->user_name : 'ERROR: non-object');
return $this->process_login_success($user);
}
/**
* Catches failed login attempts and passes them to our failure processor
*
* NOTE: This method is automatically called by WordPress when web or
* XML-RPC login attempts fail.
*
* @param string $user_name the user name from the current login form
* @return mixed return values provided for unit testing
*
* @uses login_security_solution::process_login_fail() to log the failure
* and slow down the response as necessary
*/
public function wp_login_failed($user_name) {
if ($this->skip_wp_login_failed) {
###$this->log(__FUNCTION__, "$user_name skip login failed");
$this->skip_wp_login_failed = false;
return -1;
}
if (!$user_name) {
###$this->log(__FUNCTION__, "empty user_name");
return -2;
}
if ($this->user_pass === null) {
###$this->log(__FUNCTION__, "authenticate filter not called");
###global $wp_filter;
###$this->log(__FUNCTION__, 'authenticate filters', $wp_filter['authenticate']);
###$this->log(__FUNCTION__, 'backtrace', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
die(self::NAME . ": authenticate filter not called");
}
###$this->log(__FUNCTION__, $user_name);
return $this->process_login_fail($user_name, $this->user_pass);
}
/**
* Makes a note that the current request is via XML-RPC
*
* NOTE: This method is automatically called by WordPress on XML-RPC
* requests.
*
* @param mixed $out watever the prior filter gave us
* @return mixed whatever the prior filter gave us
*
* @uses login_security_solution::$is_xmlrpc to say it's an RPC request
*/
public function xmlrpc_enabled($out) {
if ($this->options['disable_logins']
&& !current_user_can('administrator'))
{
###$this->log(__FUNCTION__, "disable logins");
return false;
}
###$this->log(__FUNCTION__, "");
$this->is_xmlrpc = true;
return $out;
}
/*
* ===== INTERNAL METHODS ====
*/
/**
* Increasingly slows down attackers to the point they'll give up
*
* Disconnects the database, sleeps, then reconnects the database.
*
* @param int $fails_total how many falures have taken place
* @param array $args the data to pass to the
* login_security_solution_fail_tier_dos action
* @return int the number of seconds sleept
*/
protected function call_sleep($fails_total, $args = array()) {
global $wpdb;
if ($this->sleep) {
###$this->log(__FUNCTION__, "already called");
return 0;
}
if ($fails_total < $this->options['login_fail_tier_2']) {
// Use random, overlapping sleep times to complicate profiling.
$this->sleep = rand(1, 7);
} elseif ($fails_total < $this->options['login_fail_tier_3']) {
$this->sleep = rand(4, 30);
} elseif (!$this->options['login_fail_tier_dos']
|| $fails_total < $this->options['login_fail_tier_dos'])
{
$this->sleep = rand(25, 60);
} else {
// Oh, boy. Really avoid creating a Denial of Service attack.