| 1 |
<?php |
| 2 |
/** |
| 3 |
* Software functions |
| 4 |
* |
| 5 |
* @package WPVulnerability |
| 6 |
* |
| 7 |
* @version 3.5.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); |
| 11 |
|
| 12 |
/** |
| 13 |
* Retrieves the specified software version. |
| 14 |
* |
| 15 |
* This function returns the version of PHP, Apache, Nginx, MySQL, or MariaDB |
| 16 |
* after performing necessary validations. It ensures that the returned value |
| 17 |
* is clean and sanitized. |
| 18 |
* |
| 19 |
* @since 3.5.0 |
| 20 |
* @since 4.3.0 Updated to handle hybrid detection format from ImageMagick, Redis, Memcached, and SQLite. |
| 21 |
* |
| 22 |
* @param string $software The name of the software ('php', 'apache', 'nginx', 'mysql', 'mariadb'). |
| 23 |
* @return string|null The sanitized version of the software, or null if not found. |
| 24 |
*/ |
| 25 |
function wpvulnerability_get_software_version( $software ) { |
| 26 |
switch ( $software ) { |
| 27 |
case 'php': |
| 28 |
$php_version = wpvulnerability_detect_php(); |
| 29 |
if ( null !== $php_version && '' !== $php_version ) { |
| 30 |
return wp_kses( (string) $php_version, 'strip' ); |
| 31 |
} |
| 32 |
|
| 33 |
break; |
| 34 |
|
| 35 |
case 'apache': |
| 36 |
case 'nginx': |
| 37 |
$webserver = wpvulnerability_detect_webserver(); |
| 38 |
if ( isset( $webserver['id'] ) && $webserver['id'] === $software && ! empty( $webserver['version'] ) ) { |
| 39 |
return wp_kses( (string) $webserver['version'], 'strip' ); |
| 40 |
} |
| 41 |
break; |
| 42 |
|
| 43 |
case 'mysql': |
| 44 |
case 'mariadb': |
| 45 |
$sqlserver = wpvulnerability_detect_sqlserver(); |
| 46 |
if ( isset( $sqlserver['id'] ) && $sqlserver['id'] === $software && ! empty( $sqlserver['version'] ) ) { |
| 47 |
return wp_kses( (string) $sqlserver['version'], 'strip' ); |
| 48 |
} |
| 49 |
break; |
| 50 |
|
| 51 |
case 'imagemagick': |
| 52 |
$detection = wpvulnerability_detect_imagemagick(); |
| 53 |
if ( is_array( $detection ) && isset( $detection['version'] ) && null !== $detection['version'] && 'unknown' !== $detection['version'] ) { |
| 54 |
return wp_kses( (string) $detection['version'], 'strip' ); |
| 55 |
} |
| 56 |
|
| 57 |
break; |
| 58 |
|
| 59 |
case 'curl': |
| 60 |
$curl_version = wpvulnerability_detect_curl(); |
| 61 |
if ( null !== $curl_version && '' !== $curl_version ) { |
| 62 |
return wp_kses( (string) $curl_version, 'strip' ); |
| 63 |
} |
| 64 |
|
| 65 |
break; |
| 66 |
|
| 67 |
case 'memcached': |
| 68 |
$detection = wpvulnerability_detect_memcached(); |
| 69 |
if ( is_array( $detection ) && isset( $detection['version'] ) && null !== $detection['version'] && 'unknown' !== $detection['version'] ) { |
| 70 |
return wp_kses( (string) $detection['version'], 'strip' ); |
| 71 |
} |
| 72 |
|
| 73 |
break; |
| 74 |
|
| 75 |
case 'redis': |
| 76 |
$detection = wpvulnerability_detect_redis(); |
| 77 |
if ( is_array( $detection ) && isset( $detection['version'] ) && null !== $detection['version'] && 'unknown' !== $detection['version'] ) { |
| 78 |
return wp_kses( (string) $detection['version'], 'strip' ); |
| 79 |
} |
| 80 |
|
| 81 |
break; |
| 82 |
|
| 83 |
case 'sqlite': |
| 84 |
$detection = wpvulnerability_detect_sqlite(); |
| 85 |
if ( is_array( $detection ) && isset( $detection['version'] ) && null !== $detection['version'] && 'unknown' !== $detection['version'] ) { |
| 86 |
return wp_kses( (string) $detection['version'], 'strip' ); |
| 87 |
} |
| 88 |
|
| 89 |
break; |
| 90 |
|
| 91 |
default: |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Retrieves vulnerabilities for a given software version and updates its data. |
| 100 |
* |
| 101 |
* This function detects the installed software version, checks for vulnerabilities using an external API, |
| 102 |
* and updates the data array with the vulnerabilities found. |
| 103 |
* |
| 104 |
* @since 3.5.0 |
| 105 |
* |
| 106 |
* @param string $software The software name (e.g., 'php', 'apache', 'nginx', 'mysql', 'mariadb'). |
| 107 |
* |
| 108 |
* @return array The updated data array containing vulnerability information. |
| 109 |
*/ |
| 110 |
function wpvulnerability_get_fresh_vulnerabilities( $software ) { |
| 111 |
|
| 112 |
$version = null; |
| 113 |
$data = array( |
| 114 |
'vulnerabilities' => null, |
| 115 |
'vulnerable' => 0, |
| 116 |
); |
| 117 |
|
| 118 |
switch ( $software ) { |
| 119 |
case 'php': |
| 120 |
case 'apache': |
| 121 |
case 'nginx': |
| 122 |
case 'mysql': |
| 123 |
case 'mariadb': |
| 124 |
case 'imagemagick': |
| 125 |
case 'curl': |
| 126 |
case 'memcached': |
| 127 |
case 'redis': |
| 128 |
case 'sqlite': |
| 129 |
$version = wpvulnerability_get_software_version( $software ); |
| 130 |
break; |
| 131 |
|
| 132 |
default: |
| 133 |
return $data; |
| 134 |
} |
| 135 |
|
| 136 |
if ( $version ) { |
| 137 |
switch ( $software ) { |
| 138 |
case 'php': |
| 139 |
case 'apache': |
| 140 |
case 'nginx': |
| 141 |
case 'mysql': |
| 142 |
case 'mariadb': |
| 143 |
case 'imagemagick': |
| 144 |
case 'curl': |
| 145 |
case 'memcached': |
| 146 |
case 'redis': |
| 147 |
case 'sqlite': |
| 148 |
$api_response = wpvulnerability_get_vulnerabilities( $software, $version, 0 ); |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! empty( $api_response ) ) { |
| 153 |
$data['vulnerabilities'] = $api_response; |
| 154 |
$data['vulnerable'] = 1; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $data; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Get Installed Software |
| 164 |
* |
| 165 |
* Retrieves the list of installed software versions, checks for vulnerabilities, |
| 166 |
* caches the data, and sends an email notification if vulnerabilities are detected. |
| 167 |
* |
| 168 |
* @since 3.5.0 |
| 169 |
* |
| 170 |
* @param string $software The software name (e.g., 'php', 'apache'). |
| 171 |
* |
| 172 |
* @return string JSON-encoded array of software data with vulnerabilities and vulnerable status. |
| 173 |
*/ |
| 174 |
function wpvulnerability_get_installed( $software ) { |
| 175 |
|
| 176 |
$wpvulnerability_software_vulnerable = 0; |
| 177 |
|
| 178 |
// Retrieve fresh vulnerabilities for the installed software version. |
| 179 |
$data = wpvulnerability_get_fresh_vulnerabilities( $software ); |
| 180 |
|
| 181 |
// Check if the software version is vulnerable and count the vulnerabilities. |
| 182 |
if ( isset( $data['vulnerable'] ) && (int) $data['vulnerable'] ) { |
| 183 |
$wpvulnerability_software_vulnerable = count( $data['vulnerabilities'] ); |
| 184 |
} |
| 185 |
|
| 186 |
// Cache the vulnerability data and the timestamp for cache expiration. |
| 187 |
if ( is_multisite() ) { |
| 188 |
update_site_option( 'wpvulnerability-' . $software, wp_json_encode( $data ) ); |
| 189 |
update_site_option( 'wpvulnerability-' . $software . '-vulnerable', wp_json_encode( number_format( $wpvulnerability_software_vulnerable, 0, '.', '' ) ) ); |
| 190 |
update_site_option( 'wpvulnerability-' . $software . '-cache', wp_json_encode( number_format( time() + ( 3600 * wpvulnerability_cache_hours() ), 0, '.', '' ) ) ); |
| 191 |
} else { |
| 192 |
update_option( 'wpvulnerability-' . $software, wp_json_encode( $data ) ); |
| 193 |
update_option( 'wpvulnerability-' . $software . '-vulnerable', wp_json_encode( number_format( $wpvulnerability_software_vulnerable, 0, '.', '' ) ) ); |
| 194 |
update_option( 'wpvulnerability-' . $software . '-cache', wp_json_encode( number_format( time() + ( 3600 * wpvulnerability_cache_hours() ), 0, '.', '' ) ) ); |
| 195 |
} |
| 196 |
|
| 197 |
// Return the JSON-encoded array of software data. |
| 198 |
return wp_json_encode( $data ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get cached software vulnerabilities without triggering remote calls. |
| 203 |
* |
| 204 |
* @since 3.5.0 |
| 205 |
* |
| 206 |
* @param string $software The software name (e.g., 'php', 'apache'). |
| 207 |
* |
| 208 |
* @return array|null Array of software data with vulnerabilities, or null if software is invalid. |
| 209 |
*/ |
| 210 |
function wpvulnerability_software_get_vulnerabilities( $software ) { |
| 211 |
|
| 212 |
$valid_software = array( 'php', 'apache', 'mariadb', 'mysql', 'nginx', 'imagemagick', 'curl', 'memcached', 'redis', 'sqlite' ); |
| 213 |
|
| 214 |
// Use strict comparison for in_array. |
| 215 |
if ( in_array( $software, $valid_software, true ) ) { |
| 216 |
if ( is_multisite() ) { |
| 217 |
$data = json_decode( get_site_option( 'wpvulnerability-' . $software ), true ); |
| 218 |
} else { |
| 219 |
$data = json_decode( get_option( 'wpvulnerability-' . $software ), true ); |
| 220 |
} |
| 221 |
|
| 222 |
return is_array( $data ) ? $data : array(); |
| 223 |
} else { |
| 224 |
return null; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Update the software cache and remove any old cache data. |
| 230 |
* |
| 231 |
* @since 3.0.0 |
| 232 |
* |
| 233 |
* @param string $software The software name (e.g., 'php', 'apache'). |
| 234 |
* |
| 235 |
* @return void |
| 236 |
*/ |
| 237 |
function wpvulnerability_get_vulnerabilities_clean( $software ) { |
| 238 |
|
| 239 |
// Update the installed software cache. |
| 240 |
wpvulnerability_get_installed( $software ); |
| 241 |
} |
| 242 |
|