-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-vulnerability-scanner.php
169 lines (131 loc) · 5.27 KB
/
wp-vulnerability-scanner.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
<?php
/*
Plugin Name: WP Vulnerability Scanner
Description: Checks if any of your plugins has vulnerabilities
Plugin URI: https://angrycreative.se
Author: angrycreative, moelleer, pekz0r
Author URI: https://angrycreative.se
Version: 1.0
License: GPL2
Text Domain: wp-vulnerability-scanner
*/
define( 'NAGIOS_STATUS_OK', 'OK' );
define( 'NAGIOS_STATUS_WARNING', 'Warning' );
define( 'NAGIOS_STATUS_CRITICAL', 'Critical' );
define( 'NAGIOS_STATUS_UNKNOWN', 'Unknown' );
class WP_Vulnerability_Scanner {
public $verbose = false;
public function __construct() {
add_action( 'wpvs_perform_scan', array( $this, 'wpvs_perform_scan' ) );
if( !wp_next_scheduled( 'wpvs_perform_scan' ) ) {
wp_schedule_event( time(), 'daily', 'wpvs_perform_scan' );
}
}
function wpvs_perform_scan( $output = 'syslog', $verbose = false ) {
$this->verbose = $verbose;
$base_url = 'https://wpvulndb.com/api/v2/plugins/';
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
$unsafe_plugins = array();
foreach( $all_plugins as $path => $plugin ) {
$plugin_version = $plugin['Version'];
$plugin_slug = untrailingslashit( plugin_dir_path( $path ) );
$result = (array) json_decode( wp_remote_retrieve_body( wp_remote_get( $base_url . $plugin_slug ) ) );
$this->print_output( 'Scanning ' . $plugin['Name'] . ' (v' . $plugin_version . ')' );
if( !empty( $result[$plugin_slug]->vulnerabilities ) ) {
$vulnerabilities = $result[$plugin_slug]->vulnerabilities;
$unsafe = false;
foreach( $vulnerabilities as $vul ) {
$fixed_in = $vul->fixed_in;
if( empty( $fixed_in ) || version_compare( $plugin_version, $fixed_in ) === -1 ) {
$unsafe = true;
break;
}
}
if( $unsafe ) {
$unsafe_plugins[] = $plugin_slug;
$this->print_output( 'You have an unsafe version of ' . $plugin_slug . '.' , $output, NAGIOS_STATUS_CRITICAL );
$this->print_output( $result[$plugin_slug]->vulnerabilities, $output );
} else {
$this->print_output( 'Exploit exists in ' . $plugin_slug . ', but you have a safe version.', $output, NAGIOS_STATUS_WARNING );
}
} else {
$this->print_output( $plugin_slug . ' is OK!', $output, NAGIOS_STATUS_OK );
}
}
return $unsafe_plugins;
}
public function print_output( $msg = '', $output = 'pretty', $status = '' ) {
if( !$this->verbose ) {
return;
}
if( $output === 'nagios' ) {
echo $status . ': ' . $msg . PHP_EOL;
} else if( $output === 'pretty' ) {
if( is_array( $msg ) ) {
print_r( $msg );
} else {
echo $msg . PHP_EOL;
}
}
}
}
if( class_exists( 'WP_CLI' ) ) {
class WP_Vulnerability_Scanner_CLI extends WP_CLI_Command {
public $verbose = false;
/**
* Prints a greeting.
*
* ## OPTIONS
*
*
* [--output=<output>]
* : Whether or not to greet the person with success or error.
* ---
* default: pretty
* options:
* - pretty
* - syslog
* - nagios
* ---
*
* [--verbose]
* : Show verbose output
* ---
*
* ## EXAMPLES
*
* wp wpsv scan
*
* @when after_wp_load
*/
function scan( $args, $assoc_args ) {
$output = $assoc_args['output'];
if( isset( $assoc_args['verbose'] ) ) {
$this->verbose = true;
}
$scanner = new WP_Vulnerability_Scanner();
$unsafe_plugins = $scanner->wpvs_perform_scan( $output, $this->verbose );
if( !empty( $unsafe_plugins ) ) {
$unsafe_plugins_str = implode(', ', $unsafe_plugins );
$unsafe_plugins_num = count( $unsafe_plugins );
$error_str = '[WP_Vulnerability_Scanner] [' . get_site_url() . ']: Found ' . sprintf( _n( '%s plugin', '%s plugins', $unsafe_plugins_num, 'wp-vulnerability-scanner' ), number_format_i18n( $unsafe_plugins_num ) ) . ' (' . $unsafe_plugins_str . ') with vulnerabilities...';
if( $output === 'syslog' ) {
syslog( LOG_ERR, $error_str );
} else {
WP_CLI::error( $error_str );
}
} else {
WP_CLI::success( '[WP_Vulnerability_Scanner] [' . get_site_url() . ']: Congratulations, you are safe!' );
}
}
}
WP_CLI::add_command( 'wpvs', 'WP_Vulnerability_Scanner_CLI' );
}
if( !class_exists( 'WP_CLI' ) ) {
$wpvs = new WP_Vulnerability_Scanner();
}