-
Notifications
You must be signed in to change notification settings - Fork 44
/
system-status.php
559 lines (465 loc) · 17.1 KB
/
system-status.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
<?php
# No need for the template engine
if ( ! defined( 'WP_USE_THEMES' ) ) {
define( 'WP_USE_THEMES', false ); // phpcs:ignore
}
//we set wp admin to true, so the backend features get loaded.
if ( ! defined( 'RSSSL_DOING_SYSTEM_STATUS' ) ) {
define( 'RSSSL_DOING_SYSTEM_STATUS', true ); // phpcs:ignore
}
#find the base path
if ( ! defined( 'BASE_PATH' ) ) {
define( 'BASE_PATH', rsssl_find_wordpress_base_path() . '/' );
}
# Load WordPress Core
if ( ! file_exists( BASE_PATH . 'wp-load.php' ) ) {
die( 'WordPress not installed here' );
}
require_once BASE_PATH . 'wp-load.php';
require_once ABSPATH . 'wp-includes/class-phpass.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
//by deleting these we make sure these functions run again
delete_transient( 'rsssl_testpage' );
function rsssl_get_system_status() {
$output = '';
global $wp_version;
$output .= "General\n";
$output .= 'Domain: ' . site_url() . "\n";
$output .= 'Plugin version: ' . rsssl_version . "\n";
$output .= 'WordPress version: ' . $wp_version . "\n";
if ( RSSSL()->certificate->is_valid() ) {
$output .= "SSL certificate is valid\n";
} else {
if ( ! function_exists( 'stream_context_get_params' ) ) {
$output .= "stream_context_get_params not available\n";
} elseif ( RSSSL()->certificate->detection_failed() ) {
$output .= "Not able to detect certificate\n";
} else {
$output .= "Invalid SSL certificate\n";
}
}
$output .= ( rsssl_get_option( 'ssl_enabled' ) ) ? "SSL is enabled\n\n"
: "SSL is not yet enabled\n\n";
$output .= "Options\n";
if ( rsssl_get_option( 'mixed_content_fixer' ) ) {
$output .= "* Mixed content fixer\n";
}
$output .= '* WordPress redirect' . rsssl_get_option( 'redirect' ) . "\n";
if ( rsssl_get_option( 'switch_mixed_content_fixer_hook' ) ) {
$output .= "* Use alternative method to fix mixed content\n";
}
if ( rsssl_get_option( 'dismiss_all_notices' ) ) {
$output .= "* Dismiss all Really Simple Security notices\n";
}
$output .= "\n";
$output .= "Server information:\n";
$output .= 'Server: ' . RSSSL()->server->get_server() . "\n";
$output .= 'SSL Type: ' . RSSSL()->admin->ssl_type . "\n";
if ( function_exists( 'phpversion' ) ) {
$output .= 'PHP Version: ' . phpversion() . "\n\n";
}
if ( is_multisite() ) {
$output .= "MULTISITE\n";
}
if ( rsssl_is_networkwide_active() ) {
$output .= "Really Simple Security network wide activated\n";
} elseif ( is_multisite() ) {
$output .= "Really Simple Security per site activated\n";
}
$output .= 'SSL Configuration:' . "\n";
$domain = RSSSL()->certificate->get_domain();
$certinfo = RSSSL()->certificate->get_certinfo( $domain );
if ( ! $certinfo ) {
$output .= 'SSL certificate not valid' . "\n";
}
$domain_valid = RSSSL()->certificate->is_domain_valid( $certinfo, $domain );
if ( ! $domain_valid ) {
$output .= "Domain on certificate does not match website's domain" . "\n";
}
$date_valid = RSSSL()->certificate->is_date_valid( $certinfo );
if ( ! $date_valid ) {
$output .= 'Date on certificate expired or not valid' . "\n";
}
$filecontents = get_transient( 'rsssl_testpage' );
if ( strpos( $filecontents, '#SSL TEST PAGE#' ) !== false ) {
$output .= 'SSL test page loaded successfully' . "\n";
} else {
$output .= 'Could not open testpage' . "\n";
}
if ( RSSSL()->admin->wpconfig_siteurl_not_fixed ) {
$output .= 'siteurl or home url defines found in wp-config.php' . "\n";
}
if ( RSSSL()->admin->wpconfig_siteurl_not_fixed ) {
$output .= 'not able to fix wpconfig siteurl/homeurl.' . "\n";
}
if ( ! is_writable( rsssl_find_wp_config_path() ) ) {
$output .= 'wp-config.php not writable<br>';
}
$output .= 'Detected SSL setup: ' . RSSSL()->admin->ssl_type . "\n";
if ( file_exists( RSSSL()->admin->htaccess_file() ) ) {
$output .= 'htaccess file exists.' . "\n";
if ( ! is_writable( RSSSL()->admin->htaccess_file() ) ) {
$output .= 'htaccess file not writable.' . "\n";
}
} else {
$output .= 'no htaccess file available.' . "\n";
}
if ( get_transient( 'rsssl_htaccess_test_success' ) === 'success' ) {
$output .= 'htaccess redirect tested successfully.' . "\n";
} elseif ( get_transient( 'rsssl_htaccess_test_success' ) === 'error' ) {
$output .= 'htaccess redirect test failed.' . "\n";
} elseif ( get_transient( 'rsssl_htaccess_test_success' ) === 'no-response' ) {
$output .= 'htaccess redirect test failed: no response from server.' . "\n";
}
$mixed_content_fixer_detected = get_transient( 'rsssl_mixed_content_fixer_detected' );
if ( 'no-response' === $mixed_content_fixer_detected ) {
$output .= 'Could not connect to webpage to detect mixed content fixer' . "\n";
}
if ( 'not-found' === $mixed_content_fixer_detected ) {
$output .= 'Mixed content marker not found in websource' . "\n";
}
if ( 'error' === $mixed_content_fixer_detected ) {
$output .= 'Mixed content marker not found: unknown error' . "\n";
}
if ( 'curl-error' === $mixed_content_fixer_detected ) {
//Site has has a cURL error
$output .= 'Mixed content fixer could not be detected: cURL error' . "\n";
}
if ( 'found' === $mixed_content_fixer_detected ) {
$output .= 'Mixed content fixer successfully detected' . "\n";
}
if ( ! rsssl_get_option( 'mixed_content_fixer' ) ) {
$output .= 'Mixed content fixer not enabled' . "\n";
}
if ( ! RSSSL()->admin->htaccess_contains_redirect_rules() ) {
$output .= '.htaccess does not contain default Really Simple Security redirect.' . "\n";
}
$output .= "\nConstants\n";
if ( defined( 'RSSSL_FORCE_ACTIVATE' ) ) {
$output .= "RSSSL_FORCE_ACTIVATE defined\n";
}
if ( defined( 'RSSSL_NO_FLUSH' ) ) {
$output .= "RSSSL_NO_FLUSH defined\n";
}
if ( defined( 'RSSSL_DISMISS_ACTIVATE_SSL_NOTICE' ) ) {
$output .= "RSSSL_DISMISS_ACTIVATE_SSL_NOTICE defined\n";
}
if ( defined( 'RSSSL_SAFE_MODE' ) ) {
$output .= "RSSSL_SAFE_MODE defined\n";
}
if ( defined( 'RSSSL_SERVER_OVERRIDE' ) ) {
$output .= "RSSSL_SERVER_OVERRIDE defined\n";
}
if ( defined( 'rsssl_no_rest_api_redirect' ) ) {
$output .= "rsssl_no_rest_api_redirect defined\n";
}
if ( defined( 'rsssl_no_wp_redirect' ) ) {
$output .= "rsssl_no_wp_redirect defined\n";
}
if ( defined( 'RSSSL_CONTENT_FIXER_ON_INIT' ) ) {
$output .= "RSSSL_CONTENT_FIXER_ON_INIT defined\n";
}
if ( defined( 'FORCE_SSL_ADMIN' ) ) {
$output .= "FORCE_SSL_ADMIN defined\n";
}
if ( defined( 'RSSSL_CSP_MAX_REQUESTS' ) ) {
$output .= "RSSSL_CSP_MAX_REQUESTS defined\n";
}
if ( defined( 'RSSSL_DISABLE_CHANGE_LOGIN_URL' ) ) {
$output .= "RSSSL_DISABLE_CHANGE_LOGIN_URL defined\n";
}
if ( defined( 'RSSSL_LEARNING_MODE' ) ) {
$output .= "RSSSL_LEARNING_MODE defined\n";
}
if ( defined( 'RSSSL_DEACTIVATING_FREE' ) ) {
$output .= "RSSSL_DEACTIVATING_FREE defined\n";
}
if ( defined( 'RSSSL_UPGRADING_TO_PRO' ) ) {
$output .= "RSSSL_UPGRADING_TO_PRO defined\n";
}
if ( ! defined( 'RSSSL_FORCE_ACTIVATE' )
&& ! defined( 'RSSSL_NO_FLUSH' )
&& ! defined( 'RSSSL_DISMISS_ACTIVATE_SSL_NOTICE' )
&& ! defined( 'RSSSL_SAFE_MODE' )
&& ! defined( 'RSSSL_SERVER_OVERRIDE' )
&& ! defined( 'rsssl_no_rest_api_redirect' )
&& ! defined( 'rsssl_no_wp_redirect' )
&& ! defined( 'RSSSL_CONTENT_FIXER_ON_INIT' )
&& ! defined( 'FORCE_SSL_ADMIN' )
&& ! defined( 'RSSSL_CSP_MAX_REQUESTS' )
&& ! defined( 'RSSSL_DISABLE_CHANGE_LOGIN_URL' )
&& ! defined( 'RSSSL_LEARNING_MODE' )
&& ! defined( 'RSSSL_DEACTIVATING_FREE' )
&& ! defined( 'RSSSL_UPGRADING_TO_PRO' )
) {
$output .= "No constants defined\n";
}
$output .= "\n";
$output .= "rsssl_options:\n";
if ( is_multisite() && rsssl_is_networkwide_active() ) {
$stored_options = get_site_option( 'rsssl_options', [] );
} else {
$stored_options = get_option( 'rsssl_options', [] );
}
unset($stored_options['permissions_policy']);
unset($stored_options['upgrade_insecure_requests']);
unset($stored_options['x_xss_protection']);
unset($stored_options['x_content_type_options']);
unset($stored_options['x_frame_options']);
unset($stored_options['referrer_policy']);
unset($stored_options['content_security_policy']);
unset($stored_options['xmlrpc_status_lm_enabled_once']);
unset($stored_options['csp_status_lm_enabled_once']);
unset($stored_options['csp_frame_ancestors_urls']);
unset($stored_options['file_change_exclusions']);
unset($stored_options['license']);
unset($stored_options['cross_origin_opener_policy']);
unset($stored_options['cross_origin_resource_policy']);
unset($stored_options['cross_origin_embedder_policy']);
$output .= print_r( $stored_options, true ) . "\n\n";
$output .= "Installed plugins:\n";
$output .= rsssl_system_status_get_installed_plugins() . "\n\n";
if ( rsssl_get_option( 'enable_firewall' ) == 1 ) {
$output .= "Blocked regions firewall: \n";
$output .= rsssl_system_status_get_blocked_countries_firewall() . "\n\n";
$output .= "Whitelist firewall: \n";
$output .= rsssl_system_status_get_whitelist() . "\n\n";
$output .= "Blocked IPs firewall: \n";
$output .= rsssl_system_status_get_blocked_ips() . "\n\n";
}
if ( rsssl_get_option("enable_limited_login_attempts") == 1 ) {
$output .= "Blocked regions LLA: \n";
$output .= rsssl_system_status_get_blocked_countries_lla() . "\n\n";
$output .= "Blocked users LLA: \n";
$output .= rsssl_system_status_get_blocked_users_lla() . "\n\n";
$output .= "Blocked ips LLA: \n";
$output .= rsssl_system_status_get_blocked_ips_lla() . "\n\n";
}
if ( rsssl_get_option( 'login_protection_enabled' ) == 1 ) {
$output .= "Users with 2FA enabled: \n";
$output .= rsssl_system_status_get_2fa_users() . "\n\n";
}
return $output;
}
if ( rsssl_user_can_manage() && isset( $_GET['download'] ) ) {
$rsssl_content = rsssl_get_system_status();
$rsssl_fsize = function_exists( 'mb_strlen' ) ? mb_strlen( $rsssl_content, '8bit' ) : strlen( $rsssl_content );
$rsssl_file_name = 'really-simple-ssl-system-status.txt';
//direct download
header( 'Content-type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . $rsssl_file_name . '"' );
//open in browser
//header("Content-Disposition: inline; filename=\"".$file_name."\"");
header( "Content-length: $rsssl_fsize" );
header( 'Cache-Control: private', false ); // required for certain browsers
header( 'Pragma: public' ); // required
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Transfer-Encoding: binary' );
echo $rsssl_content;
}
function rsssl_find_wordpress_base_path() {
$path = __DIR__;
// Check for Bitnami WordPress installation
if ( isset( $_SERVER['DOCUMENT_ROOT'] ) && $_SERVER['DOCUMENT_ROOT'] === '/opt/bitnami/wordpress' ) {
return '/opt/bitnami/wordpress';
}
do {
if ( file_exists( $path . '/wp-config.php' ) ) {
//check if the wp-load.php file exists here. If not, we assume it's in a subdir.
if ( file_exists( $path . '/wp-load.php' ) ) {
return $path;
} else {
//wp not in this directory. Look in each folder to see if it's there.
if ( file_exists( $path ) && $handle = opendir( $path ) ) { //phpcs:ignore
while ( false !== ( $file = readdir( $handle ) ) ) {//phpcs:ignore
if ( '.' !== $file && '..' !== $file ) {
$file = $path . '/' . $file;
if ( is_dir( $file ) && file_exists( $file . '/wp-load.php' ) ) {
$path = $file;
break;
}
}
}
closedir( $handle );
}
}
return $path;
}
} while ( $path = realpath( "$path/.." ) ); //phpcs:ignore
return false;
}
function rsssl_system_status_get_installed_plugins() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
// Load the plugin admin functions
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Get the list of all installed plugins
$all_plugins = get_plugins();
$plugin_list = array();
// Loop through plugins to format the list with name and version
foreach ( $all_plugins as $plugin_path => $plugin_data ) {
$plugin_list[] = $plugin_data['Name'] . ' (' . $plugin_data['Version'] . ')';
}
// Return the list as a comma-separated string
return implode( ', ', $plugin_list );
}
function rsssl_system_status_get_blocked_countries_firewall() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
$table_name = $wpdb->base_prefix . 'rsssl_geo_block';
$query_string = $wpdb->prepare(
"SELECT iso2_code FROM {$table_name} WHERE data_type = %s AND ip_address is NULL",
'country'
);
// phpcs:ignore
$result = $wpdb->get_results( $query_string );
$column_results = array_column( $result, 'iso2_code' );
return implode( ',', $column_results );
}
function rsssl_system_status_get_whitelist() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
$table_name = $wpdb->base_prefix . 'rsssl_geo_block';
$query_string = $wpdb->prepare(
"SELECT ip_address FROM {$table_name} WHERE data_type = %s",
'trusted'
);
// phpcs:ignore
$result = $wpdb->get_results( $query_string );
$column_results = array_column( $result, 'ip_address' );
return implode( ',', $column_results );
}
function rsssl_system_status_get_blocked_countries_lla() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
$table_name = $wpdb->base_prefix . 'rsssl_login_attempts';
// Query to get all the blocked countries from the login attempts table where attempt_type is 'country'
$query_string = $wpdb->prepare(
"SELECT attempt_value FROM {$table_name} WHERE attempt_type = %s AND status = %s",
'country', 'blocked'
);
// phpcs:ignore
$result = $wpdb->get_results( $query_string );
$column_results = array_column( $result, 'attempt_value' );
if ( empty( $column_results ) ) {
return "No blocked countries found.";
}
return implode( ',', $column_results );
}
function rsssl_system_status_get_blocked_ips() {
if ( ! current_user_can( 'manage_security' ) ) {
return '';
}
$output = '';
global $wpdb;
$sql = $wpdb->prepare(
"SELECT ip_address FROM {$wpdb->base_prefix}rsssl_geo_block WHERE blocked = %d AND data_type = %s",
1,
'404'
);
$results = $wpdb->get_results( $sql, ARRAY_A );
if ( empty( $results ) ) {
return "No blocked IPs found.";
}
foreach ( $results as $row ) {
$output .= $row['ip_address'] . "\n";
}
return $output;
}
function rsssl_system_status_get_blocked_users_lla() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
$table_name = $wpdb->base_prefix . 'rsssl_login_attempts';
// Query to get all blocked users from login attempts where attempt_type is 'username'
$query_string = $wpdb->prepare(
"SELECT attempt_value FROM {$table_name} WHERE attempt_type = %s AND status = %s",
'username', 'blocked'
);
// phpcs:ignore
$result = $wpdb->get_results( $query_string );
$column_results = array_column( $result, 'attempt_value' );
if ( empty( $column_results ) ) {
return "No blocked users found.";
}
return implode( ',', $column_results );
}
function rsssl_system_status_get_blocked_ips_lla() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
$table_name = $wpdb->base_prefix . 'rsssl_login_attempts';
// Query to get blocked single IPs (without CIDR notation) from login attempts
$query_string = $wpdb->prepare(
"SELECT attempt_value FROM {$table_name} WHERE attempt_type = %s AND status = %s AND attempt_value NOT LIKE %s",
'source_ip', 'blocked', '%/%'
);
// phpcs:ignore
$result = $wpdb->get_results( $query_string );
$column_results = array_column( $result, 'attempt_value' );
if ( empty( $column_results ) ) {
return "No blocked users found.";
}
return implode( ',', $column_results );
}
function rsssl_system_status_get_2fa_users() {
if ( ! current_user_can( 'manage_security' ) ) {
return;
}
global $wpdb;
// Query to get all users with TOTP or email 2FA status
$query = "
SELECT user_id, meta_key, meta_value
FROM {$wpdb->usermeta}
WHERE meta_key IN ('rsssl_two_fa_status_totp', 'rsssl_two_fa_status_email')
";
// Execute the query
$results = $wpdb->get_results( $query );
// If no results, return a message
if ( empty( $results ) ) {
return 'No users found with 2FA settings.';
}
// Array to store users and their 2FA methods
$users_2fa = array();
// Organize the 2FA status by user
foreach ( $results as $row ) {
if ( ! isset( $users_2fa[ $row->user_id ] ) ) {
$users_2fa[ $row->user_id ] = array(
'rsssl_two_fa_status_totp' => '',
'rsssl_two_fa_status_email' => '',
);
}
// Update TOTP or email 2FA status
$users_2fa[ $row->user_id ][ $row->meta_key ] = $row->meta_value;
}
// Prepare output for users with active 2FA methods
$output = array();
foreach ( $users_2fa as $user_id => $user_data ) {
$user_info = get_userdata( $user_id );
// Determine the active 2FA method
if ( $user_data['rsssl_two_fa_status_totp'] === 'active' ) {
$output[] = $user_info->user_login . ' - TOTP';
} elseif ( $user_data['rsssl_two_fa_status_email'] === 'active' ) {
$output[] = $user_info->user_login . ' - Email';
}
}
// If no users are found with 2FA, add a note
if ( empty( $output ) ) {
return 'No users found with 2FA enabled.';
}
// Return a newline-separated list of users and their 2FA statuses
return implode( "\n", $output );
}