-
Notifications
You must be signed in to change notification settings - Fork 53
/
class-mainwp-security.php
709 lines (645 loc) · 24.1 KB
/
class-mainwp-security.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
<?php
/**
* MainWP Security
*
* @package MainWP/Child
*/
namespace MainWP\Child;
/**
* Class MainWP_Security
*
* Detect security issues
*/
class MainWP_Security { //phpcs:ignore -- NOSONAR - multi methods.
/**
* Method get_class_name()
*
* Get class name.
*
* @return string __CLASS__ Class name.
*/
public static function get_class_name() {
return __CLASS__;
}
/**
* Method fix_all()
*
* Fire off functions to fix detected security issues.
*
* @uses MainWP_Security::remove_wp_version() Remove the WordPress version.
* @uses MainWP_Security::remove_rsd() Remove the Really Simple Discovery meta tag.
* @uses MainWP_Security::remove_wlw() Remove the Windows Live Writer meta tag.
* @uses MainWP_Security::remove_php_reporting() Disable the PHP error reporting.
* @uses MainWP_Security::remove_registered_versions() Remove the registered scripts versions.
* @uses MainWP_Security::remove_generator_version() Remove the WordPress Generator meta tag.
* @uses MainWP_Security::remove_readme() Remove the readme.html file.
* @uses MainWP_Security::remove_script_versions() Remove scripts and stylesheets versions.
* @uses MainWP_Security::remove_theme_versions() Remove themes scripts and stylesheets version.
*/
public static function fix_all() {
static::remove_wp_version();
static::remove_rsd();
static::remove_wlw();
static::remove_php_reporting();
static::remove_registered_versions();
static::remove_generator_version();
static::remove_readme();
if ( static::get_security_option( 'db_reporting' ) ) {
static::remove_database_reporting();
}
add_filter( 'style_loader_src', array( static::get_class_name(), 'remove_script_versions' ), PHP_INT_MAX );
add_filter( 'style_loader_src', array( static::get_class_name(), 'remove_theme_versions' ), PHP_INT_MAX );
add_filter( 'script_loader_src', array( static::get_class_name(), 'remove_script_versions' ), PHP_INT_MAX );
add_filter( 'script_loader_src', array( static::get_class_name(), 'remove_theme_versions' ), PHP_INT_MAX );
}
/**
* Private static variable to hold the directory listing information.
*
* @var mixed Default null
*/
private static $listingDirectories = null;
/**
* Method init_listing_directories()
*
* Get directories array to prevent listing.
*/
private static function init_listing_directories() {
if ( null === static::$listingDirectories ) {
$wp_upload_dir = wp_upload_dir();
static::$listingDirectories = array(
WP_CONTENT_DIR,
WP_PLUGIN_DIR,
get_theme_root(),
$wp_upload_dir['basedir'],
);
}
}
/**
* Method prevent_listing()
*
* Prevent directory listing by creating the index.php file.
*
* @used-by \MainWP\Child\MainWP_Security::fix_all() Fire off functions to fix detected security issues.
* @uses \MainWP\Child\MainWP_Helper::get_wp_filesystem()
*/
public static function prevent_listing() {
static::init_listing_directories();
/**
* Global variable containing the instance of the (auto-)configured filesystem object after the filesystem "factory" has been run.
*
* @global object $wp_filesystem Filesystem object.
*/
global $wp_filesystem;
MainWP_Helper::get_wp_filesystem();
foreach ( static::$listingDirectories as $directory ) {
$file = $directory . DIRECTORY_SEPARATOR . 'index.php';
if ( ! $wp_filesystem->exists( $file ) ) {
$content = "<?php \n";
$content .= "header(\$_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden' );\n";
$content .= "die( '403 Forbidden' );\n";
$wp_filesystem->put_contents( $file, $content );
}
}
}
/**
* Method remove_wp_version()
*
* Remove the WordPress version (WordPress Generator).
*
* @param bool $force Force action if true, don't force if false.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_wp_version( $force = false ) {
if ( $force || static::get_security_option( 'wp_version' ) ) {
remove_action( 'wp_head', 'wp_generator' );
remove_filter( 'wp_head', 'wp_generator' );
}
}
/**
* Method remove_rsd()
*
* Remove the Really Simple Discovery meta tag.
*
* @param bool $force Force action if true, don't force if false.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_rsd( $force = false ) {
if ( $force || static::get_security_option( 'rsd' ) ) {
remove_action( 'wp_head', 'rsd_link' );
}
}
/**
* Method remove_wlw()
*
* Remove the Windows Live Writer meta tag.
*
* @param bool $force Force action if true, don't force if false.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_wlw( $force = false ) {
if ( $force || static::get_security_option( 'wlw' ) ) {
remove_action( 'wp_head', 'wlwmanifest_link' );
}
}
/**
* Method remove_php_reporting()
*
* Disable the PHP error reporting.
*
* @param bool $force Force action if true, don't force if false.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_php_reporting( $force = false ) {
if ( $force || static::get_security_option( 'php_reporting' ) ) {
error_reporting( 0 ); //phpcs:ignore -- required to achieve desired results, pull request solutions appreciated.
ini_set( 'display_errors', 'off' ); //phpcs:ignore -- required to achieve desired results, pull request solutions appreciated.
ini_set( 'display_startup_errors', 0 ); //phpcs:ignore -- required to achieve desired results, pull request solutions appreciated.
}
}
/**
* Method remove_database_reporting()
*
* Disable the database error reporting.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_database_reporting() {
/**
* Object, providing access to the WordPress database.
*
* @global $wpdb WordPress Database instance.
*/
global $wpdb;
$wpdb->hide_errors();
$wpdb->suppress_errors();
}
/**
* Method remove_registered_versions()
*
* Remove the scripts and stylesheets registered versions.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_registered_versions() {
if ( static::get_security_option( 'registered_versions' ) ) {
/**
* Global object used to register styles.
*
* @var object $wp_styles Global object used to register styles.
*/
global $wp_styles;
if ( $wp_styles instanceof WP_Styles ) {
foreach ( $wp_styles->registered as $handle => $style ) {
$wp_styles->registered[ $handle ]->ver = null;
}
}
/**
* Global object used to register scripts.
*
* @var object $wp_scripts Global object used to register scripts.
*/
global $wp_scripts;
if ( $wp_scripts instanceof WP_Scripts ) {
foreach ( $wp_scripts->registered as $handle => $script ) {
$wp_scripts->registered[ $handle ]->ver = null;
}
}
}
}
/**
* Method remove_script_versions()
*
* Remove scripts versions.
*
* @param string $src Script or stylesheet location path.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*
* @return string $src Script or stylesheet striped location path.
*/
public static function remove_script_versions( $src ) {
if ( static::get_security_option( 'scripts_version' ) ) {
if ( strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
return $src;
}
/**
* Method remove_generator_version()
*
* Remove the WordPress Generator version.
*
* @param bool $force Force action if true, don't force if false.
*
* @uses MainWP_Security::custom_the_generator() Set custom generator.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*/
public static function remove_generator_version( $force = false ) {
if ( $force || static::get_security_option( 'generator_version' ) ) {
$types = array( 'html', 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export' );
foreach ( $types as $type ) {
add_filter( 'get_the_generator_' . $type, array( static::get_class_name(), 'custom_the_generator' ), 10, 2 );
}
}
}
/**
* Method custom_the_generator()
*
* Set custom generator.
*
* @used-by MainWP_Security::remove_generator_version() Remove the WordPress Generator version.
*
* @return string Return empty string.
*/
public static function custom_the_generator() {
return '';
}
/**
* Method remove_theme_versions()
*
* Remove themes versions.
*
* @param string $src Theme stylesheet location path.
*
* @used-by MainWP_Security::fix_all() Fire off functions to fix detected security issues.
*
* @return string $src Theme stylesheet striped location path.
*/
public static function remove_theme_versions( $src ) {
if ( static::get_security_option( 'styles_version' ) ) {
if ( strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
return $src;
}
/**
* Method remove_readme()
*
* Remove the readme.html file.
*
* @param bool $force Force action if true, don't force if false.
*
* @return bool true Return true to skip the process if child site is on WP Engine host.
*
* @used-by \MainWP\Child\MainWP_Security::fix_all() Fire off functions to fix detected security issues.
* @uses \MainWP\Child\MainWP_Helper::is_wp_engine()
* @uses \MainWP\Child\MainWP_Helper::get_wp_filesystem()
*/
public static function remove_readme( $force = false ) { //phpcs:ignore -- NOSONAR - complex.
// to prevent remove readme.html file on WP Engine hosting.
if ( MainWP_Helper::is_wp_engine() ) {
return true;
}
MainWP_Helper::get_wp_filesystem();
/**
* Global variable containing the instance of the (auto-)configured filesystem object after the filesystem "factory" has been run.
*
* @global object $wp_filesystem Filesystem object.
*/
global $wp_filesystem;
if ( ( $force || static::get_security_option( 'readme' ) ) && $wp_filesystem->connect() ) {
$abs_path = $wp_filesystem->abspath();
if ( $wp_filesystem->exists( $abs_path . 'readme.html' ) && ! wp_delete_file( ABSPATH . 'readme.html' ) ) {
$wp_filesystem->delete( $abs_path . 'readme.html' );
if ( $wp_filesystem->exists( $abs_path . 'readme.html' ) ) {
// prevent repeat delete.
static::update_security_option( 'readme', false );
}
}
}
}
/**
* Method prevent_listing_ok()
*
* Check if the directory listing is prevented.
*
* @return bool true|false If directory listing prevented, return true, if not, return false.
*
* @used-by \MainWP\Child\MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
* @uses \MainWP\Child\MainWP_Helper::get_wp_filesystem()
*/
public static function prevent_listing_ok() {
/**
* Global variable containing the instance of the (auto-)configured filesystem object after the filesystem "factory" has been run.
*
* @global object $wp_filesystem Filesystem object.
*/
global $wp_filesystem;
MainWP_Helper::get_wp_filesystem();
static::init_listing_directories();
foreach ( static::$listingDirectories as $directory ) {
$file = $directory . DIRECTORY_SEPARATOR . 'index.php';
if ( ! $wp_filesystem->exists( $file ) ) {
return false;
}
}
return true;
}
/**
* Method remove_wp_version_ok()
*
* Check if the WordPress version has been removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false If WordPress version removed, return true, if not return false.
*/
public static function remove_wp_version_ok() {
return ! ( has_action( 'wp_head', 'wp_generator' ) || has_filter( 'wp_head', 'wp_generator' ) );
}
/**
* Method remove_rsd_ok()
*
* Check if the Really Simple Discovery meta tag has been removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false If the Really Simple Discovery meta tag has been removed, return true, if not return false.
*/
public static function remove_rsd_ok() {
return ! has_action( 'wp_head', 'rsd_link' );
}
/**
* Method remove_wlw_ok()
*
* Check if the Windows Live Writer meta tag has been removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false If the Windows Live Writer meta tag has been removed, return true, if not return false.
*/
public static function remove_wlw_ok() {
return ! has_action( 'wp_head', 'wlwmanifest_link' );
}
/**
* Method remove_database_reporting_ok()
*
* Check if the database error reporting has been disabled.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false If the database error reporting has been disabled, return true, if not, return false.
*/
public static function remove_database_reporting_ok() {
/**
* Object, providing access to the WordPress database.
*
* @global $wpdb WordPress Database instance.
*/
global $wpdb;
return false === $wpdb->show_errors;
}
/**
* Method remove_php_reporting_ok()
*
* Check if the PHP error reporting has been disabled.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false If the PHP error reporting has been disabled, return true, if not, return false.
*/
public static function remove_php_reporting_ok() {
$is_ok = true;
$display_off = ini_get( 'display_errors' );
if ( ! empty( $display_off ) ) {
$display_off = strtolower( $display_off );
$is_ok = ( $is_ok || 'off' === $display_off );
}
$display_startup_off = ini_get( 'display_startup_errors' );
if ( ! empty( $display_startup_off ) ) {
$display_startup_off = strtolower( $display_startup_off );
$is_ok = ( $is_ok || 'off' === $display_startup_off );
}
return $is_ok;
}
/**
* Method remove_scripts_version_ok()
*
* Check if scripts versions are removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false Return true if versions have been removed, false if not.
*/
public static function remove_scripts_version_ok() {
return static::get_security_option( 'scripts_version' );
}
/**
* Method remove_styles_version_ok()
*
* Check if stylesheets versions are removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false Return true if versions have been removed, false if not.
*/
public static function remove_styles_version_ok() {
return static::get_security_option( 'styles_version' );
}
/**
* Method remove_generator_version_ok()
*
* Check if the WordPress Generator version has been removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false Return true if registered versions have been removed, false if not.
*/
public static function remove_generator_version_ok() {
return static::get_security_option( 'generator_version' );
}
/**
* Method remove_registered_versions_ok()
*
* Check if the scripts and stylesheets registered versions are removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false Return true if registered versions have been removed, false if not.
*/
public static function remove_registered_versions_ok() {
return static::get_security_option( 'registered_versions' );
}
/**
* Method admin_user_ok()
*
* Check if any of administrator accounts has 'admin' as username.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true Return true no 'admin' username detected, fale if not.
*/
public static function admin_user_ok() {
$user = get_user_by( 'login', 'admin' );
if ( ! $user ) {
return true;
}
if ( ! MainWP_Helper::is_admin( $user ) ) {
return true;
}
return false;
}
/**
* Method remove_readme_ok()
*
* Check if the readme.html file has been removed.
*
* @used-by MainWP_Security::get_stats_security() Calculate total number of detected secutiry issues.
*
* @return bool true|false Return true if the readme.html file has been removed, false if not.
*/
public static function remove_readme_ok() {
return ! file_exists( ABSPATH . 'readme.html' );
}
/**
* Method get_stats_security()
*
* Calculate total number of detected secutiry issues.
*
* @uses MainWP_Security::prevent_listing_ok() Check if the directory listing is prevented.
* @uses MainWP_Security::remove_wp_version_ok() Check if the WordPress version has been removed.
* @uses MainWP_Security::remove_rsd_ok() Check if the Really Simple Discovery meta tag has been removed.
* @uses MainWP_Security::remove_wlw_ok() Check if the Windows Live Writer meta tag has been removed.
* @uses MainWP_Security::remove_database_reporting_ok() Check if the database error reporting has been disabled.
* @uses MainWP_Security::remove_php_reporting_ok() Check if the PHP error reporting has been disabled.
* @uses MainWP_Security::remove_scripts_version_ok() Check if scripts versions are removed.
* @uses MainWP_Security::remove_styles_version_ok() Check if stylesheets versions are removed.
* @uses MainWP_Security::remove_generator_version_ok() Check if the WordPress Generator version has been removed.
* @uses MainWP_Security::remove_registered_versions_ok() Check if the scripts and stylesheets registered versions are removed.
* @uses MainWP_Security::admin_user_ok() Check if any of administrator accounts has 'admin' as username.
* @uses MainWP_Security::remove_readme_ok() Check if the readme.html file has been removed.
*
* @return int $total_issues Total number of detected security issues.
*/
public static function get_stats_security() {
$total_issues = 0;
if ( ! static::prevent_listing_ok() ) {
++$total_issues;
}
if ( ! static::remove_wp_version_ok() ) {
++$total_issues;
}
if ( ! static::remove_rsd_ok() ) {
++$total_issues;
}
if ( ! static::remove_wlw_ok() ) {
++$total_issues;
}
if ( ! static::remove_database_reporting_ok() ) {
++$total_issues;
}
if ( ! static::remove_php_reporting_ok() ) {
++$total_issues;
}
if ( ! static::remove_scripts_version_ok() || ! static::remove_styles_version_ok() || ! static::remove_generator_version_ok() ) {
++$total_issues;
}
if ( ! static::remove_registered_versions_ok() ) {
++$total_issues;
}
if ( ! static::admin_user_ok() ) {
++$total_issues;
}
if ( ! static::remove_readme_ok() ) {
++$total_issues;
}
if ( ! static::wpcore_updated_ok() ) {
++$total_issues;
}
if ( ! static::phpversion_ok() ) {
++$total_issues;
}
if ( ! static::sslprotocol_ok() ) {
++$total_issues;
}
if ( ! static::debug_disabled_ok() ) {
++$total_issues;
}
return $total_issues;
}
/**
* Method get_security_option()
*
* Get security check settings.
*
* @param string $option Security check option.
*
* @return bool Security settings.
*
* @uses \MainWP\Child\MainWP_Helper::update_option()
*/
public static function get_security_option( $option ) {
$security = get_option( 'mainwp_security' );
return is_array( $security ) && isset( $security[ $option ] ) && ( true === $security[ $option ] );
}
/**
* Method update_security_option()
*
* Update the security issues feature settings.
*
* @param string $key Security option key.
* @param string $value Security option value.
*/
public static function update_security_option( $key, $value ) {
$security = get_option( 'mainwp_security' );
if ( ! empty( $key ) ) {
$security[ $key ] = $value;
}
MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' );
}
/**
* Method wpcore_updated_ok()
*
* Check WP core updated.
*/
public static function wpcore_updated_ok() {
include_once ABSPATH . '/wp-admin/includes/update.php'; // NOSONAR -- WP compatible.
$ok = true;
$core_updates = get_core_updates();
if ( is_array( $core_updates ) ) {
foreach ( $core_updates as $update ) {
if ( 'upgrade' === $update->response ) {
$ok = false;
}
}
}
return $ok;
}
/**
* Method phpversion_ok()
*
* Check PHP version matches the WP requirement.
*/
public static function phpversion_ok() {
require_once ABSPATH . WPINC . '/version.php'; // NOSONAR - WP compatible.
global $required_php_version;
return version_compare( phpversion(), $required_php_version, '>=' );
}
/**
* Method sslprotocol_ok()
*
* Check SSL protocol is in place.
*/
public static function sslprotocol_ok() {
return is_ssl();
}
/**
* Method debug_disabled_ok()
*
* Check WP Config and check if debugging is disabled.
*/
public static function debug_disabled_ok() {
$ok = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG;
$ok = $ok && ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG );
return $ok;
}
}