| 1 |
<?php |
| 2 |
/** |
| 3 |
* Created by JetBrains PhpStorm. |
| 4 |
* User: nikola milosevic |
| 5 |
* Date: 11/6/13 |
| 6 |
* Time: 3:18 PM |
| 7 |
* To change this template use File | Settings | File Templates. |
| 8 |
*/ |
| 9 |
if(basename($_SERVER['SCRIPT_FILENAME']) == "security.class.php"): |
| 10 |
exit; |
| 11 |
endif; |
| 12 |
class MMB_Security extends MMB_Core |
| 13 |
{ |
| 14 |
function __construct() |
| 15 |
{ |
| 16 |
parent::__construct(); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
function security_check($args) |
| 22 |
{ |
| 23 |
|
| 24 |
if(MMB_Security::prevent_listing_ok()) |
| 25 |
{ |
| 26 |
$output["prevent_listing_ok"] = true; |
| 27 |
} |
| 28 |
else |
| 29 |
{ |
| 30 |
$output["prevent_listing_ok"] = false; |
| 31 |
} |
| 32 |
|
| 33 |
if(MMB_Security::remove_wp_version_ok()){ |
| 34 |
$output["remove_wp_version_ok"] = true; |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
$output["remove_wp_version_ok"] = false; |
| 39 |
} |
| 40 |
|
| 41 |
if(MMB_Security::remove_database_reporting_ok()) |
| 42 |
{ |
| 43 |
$output["remove_database_reporting_ok"] = true; |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
$output["remove_database_reporting_ok"] = false; |
| 48 |
} |
| 49 |
|
| 50 |
if(MMB_Security::remove_php_reporting_ok()) |
| 51 |
{ |
| 52 |
$output["remove_php_reporting_ok"] = true; |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
$output["remove_php_reporting_ok"] = false; |
| 57 |
} |
| 58 |
|
| 59 |
if( MMB_Security::admin_user_ok()) |
| 60 |
{ |
| 61 |
$output["admin_user_ok"] = true; |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
$output["admin_user_ok"] = false; |
| 66 |
} |
| 67 |
|
| 68 |
if( MMB_Security::htaccess_permission_ok()) |
| 69 |
{ |
| 70 |
$output["htaccess_permission_ok"] = true; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
$output["htaccess_permission_ok"] = false; |
| 75 |
} |
| 76 |
if( MMB_Security::remove_scripts_version_ok()&&MMB_Security::remove_styles_version_ok()) |
| 77 |
{ |
| 78 |
$output["remove_scripts_and_styles_version_ok"] = true; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
$output["remove_scripts_and_styles_version_ok"] = false; |
| 83 |
} |
| 84 |
if( MMB_Security::file_permission_ok()) |
| 85 |
{ |
| 86 |
$output["file_permission_ok"] = true; |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
$output["file_permission_ok"] = false; |
| 91 |
} |
| 92 |
return $output; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
function security_fix_dir_listing($args) |
| 97 |
{ |
| 98 |
MMB_Security::prevent_listing(); |
| 99 |
return $this->security_check($args); |
| 100 |
} |
| 101 |
|
| 102 |
function security_fix_permissions($args) |
| 103 |
{ |
| 104 |
MMB_Security::file_permission(); |
| 105 |
return $this->security_check($args); |
| 106 |
} |
| 107 |
|
| 108 |
function security_fix_php_reporting($args) |
| 109 |
{ |
| 110 |
MMB_Security::remove_php_reporting(); |
| 111 |
return $this->security_check($args); |
| 112 |
} |
| 113 |
|
| 114 |
function security_fix_database_reporting($args) |
| 115 |
{ |
| 116 |
MMB_Security::remove_database_reporting(); |
| 117 |
return $this->security_check($args); |
| 118 |
} |
| 119 |
|
| 120 |
function security_fix_wp_version($args) |
| 121 |
{ |
| 122 |
MMB_Security::remove_wp_version(); |
| 123 |
return $this->security_check($args); |
| 124 |
} |
| 125 |
|
| 126 |
function security_fix_admin_username($args) |
| 127 |
{ |
| 128 |
$username = $args[0]; |
| 129 |
MMB_Security::change_admin_username($username); |
| 130 |
$scan_res = $this->security_check($args); |
| 131 |
$scan_res["admin_user_ok"] = true; |
| 132 |
return $scan_res; |
| 133 |
} |
| 134 |
|
| 135 |
function security_fix_scripts_styles($args) |
| 136 |
{ |
| 137 |
|
| 138 |
MMB_Security::remove_styles_version(); |
| 139 |
MMB_Security::remove_scripts_version(); |
| 140 |
return $this->security_check($args); |
| 141 |
} |
| 142 |
|
| 143 |
function security_fix_htaccess_permission($args) |
| 144 |
{ |
| 145 |
MMB_Security::htaccess_permission(); |
| 146 |
return $this->security_check($args); |
| 147 |
} |
| 148 |
|
| 149 |
//Prevent listing wp-content, wp-content/plugins, wp-content/themes, wp-content/uploads |
| 150 |
private static $listingDirectories = null; |
| 151 |
|
| 152 |
private static function init_listingDirectories() |
| 153 |
{ |
| 154 |
if (MMB_Security::$listingDirectories == null) |
| 155 |
{ |
| 156 |
$wp_upload_dir = wp_upload_dir(); |
| 157 |
MMB_Security::$listingDirectories = array(WP_CONTENT_DIR, WP_PLUGIN_DIR, get_theme_root(), $wp_upload_dir['basedir']); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public static function prevent_listing_ok() |
| 162 |
{ |
| 163 |
MMB_Security::init_listingDirectories(); |
| 164 |
foreach (MMB_Security::$listingDirectories as $directory) |
| 165 |
{ |
| 166 |
$file = $directory . DIRECTORY_SEPARATOR . 'index.php'; |
| 167 |
if (!file_exists($file)) |
| 168 |
{ |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
public static function prevent_listing() |
| 176 |
{ |
| 177 |
MMB_Security::init_listingDirectories(); |
| 178 |
foreach (MMB_Security::$listingDirectories as $directory) |
| 179 |
{ |
| 180 |
$file = $directory . DIRECTORY_SEPARATOR . 'index.php'; |
| 181 |
if (!file_exists($file)) |
| 182 |
{ |
| 183 |
chmod($directory,0777); |
| 184 |
$h = fopen($file, 'w'); |
| 185 |
fwrite($h, '<?php die(); ?>'); |
| 186 |
fclose($h); |
| 187 |
chmod($directory,0755); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
//Removed wp-version |
| 193 |
public static function remove_wp_version_ok() |
| 194 |
{ |
| 195 |
return !(has_action('wp_head', 'wp_generator') || has_filter('wp_head', 'wp_generator')); |
| 196 |
} |
| 197 |
|
| 198 |
public static function remove_wp_version() |
| 199 |
{ |
| 200 |
update_option('mwp_remove_wp_version', 'T'); |
| 201 |
if (get_option('mwp_remove_wp_version') == 'T') |
| 202 |
{ |
| 203 |
remove_action('wp_head', 'wp_generator'); |
| 204 |
remove_filter('wp_head', 'wp_generator'); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
//Database error reporting turned on/off |
| 209 |
public static function remove_database_reporting_ok() |
| 210 |
{ |
| 211 |
global $wpdb; |
| 212 |
return ($wpdb->show_errors == false); |
| 213 |
} |
| 214 |
|
| 215 |
public static function remove_database_reporting() |
| 216 |
{ |
| 217 |
global $wpdb; |
| 218 |
|
| 219 |
$wpdb->hide_errors(); |
| 220 |
$wpdb->suppress_errors(); |
| 221 |
} |
| 222 |
|
| 223 |
//PHP error reporting turned on/off |
| 224 |
public static function remove_php_reporting_ok() |
| 225 |
{ |
| 226 |
return !(((ini_get('display_errors') != 0) && (ini_get('display_errors') != 'off')) || ((ini_get('display_startup_errors') != 0) && (ini_get('display_startup_errors') != 'off'))); |
| 227 |
} |
| 228 |
|
| 229 |
public static function remove_php_reporting() |
| 230 |
{ |
| 231 |
update_option('mwp_remove_php_reporting', 'T'); |
| 232 |
if (get_option('mwp_remove_php_reporting') == 'T') |
| 233 |
{ |
| 234 |
@error_reporting(0); |
| 235 |
@ini_set('display_errors', 'off'); |
| 236 |
@ini_set('display_startup_errors', "off"); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
//Admin user name is not admin |
| 241 |
public static function admin_user_ok() |
| 242 |
{ |
| 243 |
$user = get_user_by('login', 'admin'); |
| 244 |
return !($user && ($user->wp_user_level == 10 || (isset($user->user_level) && $user->user_level == 10))); |
| 245 |
} |
| 246 |
|
| 247 |
public static function change_admin_username($new_username) |
| 248 |
{ |
| 249 |
global $wpdb; |
| 250 |
$wpdb->query($wpdb->prepare( "Update {$wpdb->prefix}users SET user_login='%s' where user_login='admin'",$new_username)); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
//Admin user name is not admin |
| 255 |
public static function htaccess_permission_ok() |
| 256 |
{ |
| 257 |
$htaccessPerm = substr(sprintf('%o', fileperms(ABSPATH."/.htaccess")),-4); |
| 258 |
if($htaccessPerm==="0644" || $htaccessPerm==="0444") |
| 259 |
return true; |
| 260 |
else |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
public static function htaccess_permission() |
| 265 |
{ |
| 266 |
$htaccessPerm = fileperms(ABSPATH."/.htaccess"); |
| 267 |
$succ = chmod(ABSPATH."/.htaccess",0644); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
public static function remove_scripts_version_ok() |
| 272 |
{ |
| 273 |
return (get_option('managewp_remove_scripts_version') == 'T'); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
public static function remove_script_versions($src) |
| 278 |
{ |
| 279 |
update_option('managewp_remove_scripts_version', 'T'); |
| 280 |
if (get_option('managewp_remove_scripts_version') == 'T') |
| 281 |
{ |
| 282 |
if (strpos($src, '?ver=')) |
| 283 |
$src = remove_query_arg('ver', $src); |
| 284 |
|
| 285 |
return $src; |
| 286 |
} |
| 287 |
return $src; |
| 288 |
} |
| 289 |
|
| 290 |
public static function remove_theme_versions($src) |
| 291 |
{ |
| 292 |
update_option('managewp_remove_styles_version', 'T'); |
| 293 |
if (get_option('managewp_remove_styles_version') == 'T') |
| 294 |
{ |
| 295 |
if (strpos($src, '?ver=')) |
| 296 |
$src = remove_query_arg('ver', $src); |
| 297 |
|
| 298 |
return $src; |
| 299 |
} |
| 300 |
return $src; |
| 301 |
} |
| 302 |
|
| 303 |
public static function remove_scripts_version() |
| 304 |
{ |
| 305 |
update_option('managewp_remove_scripts_version', 'T'); |
| 306 |
if (get_option('managewp_remove_scripts_version') == 'T') |
| 307 |
{ |
| 308 |
global $wp_scripts; |
| 309 |
if (!is_a($wp_scripts, 'WP_Scripts')) |
| 310 |
return; |
| 311 |
|
| 312 |
foreach ($wp_scripts->registered as $handle => $script) |
| 313 |
$wp_scripts->registered[$handle]->ver = null; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
public static function remove_styles_version_ok() |
| 318 |
{ |
| 319 |
return (get_option('managewp_remove_styles_version') == 'T'); |
| 320 |
} |
| 321 |
|
| 322 |
public static function remove_styles_version() |
| 323 |
{ |
| 324 |
update_option('managewp_remove_styles_version', 'T'); |
| 325 |
if (get_option('managewp_remove_styles_version') == 'T') |
| 326 |
{ |
| 327 |
global $wp_styles; |
| 328 |
if (!is_a($wp_styles, 'WP_Styles')) |
| 329 |
return; |
| 330 |
|
| 331 |
foreach ($wp_styles->registered as $handle => $style) |
| 332 |
$wp_styles->registered[$handle]->ver = null; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
public static function file_permission_ok($dir=ABSPATH) |
| 337 |
{ |
| 338 |
$files = scandir($dir); |
| 339 |
$dir = rtrim($dir,"/"); |
| 340 |
foreach($files as $file) |
| 341 |
{ |
| 342 |
if($file!=="."&&$file!==".."&&$file!=="wp-admin"&&$file!=="wp-includes"&&$file!=="plugins"&&$file!=="themes"&&$file!=="uploads"){ |
| 343 |
if(is_dir($dir."/".$file)) |
| 344 |
{ |
| 345 |
$dirPerm = substr(sprintf('%o', fileperms($dir."/".$file)),-4); |
| 346 |
if($dirPerm!=="0755") |
| 347 |
return false; |
| 348 |
$res = MMB_Security::file_permission_ok($dir."/".$file); |
| 349 |
if($res===false) |
| 350 |
return false; |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
$filePerm = substr(sprintf('%o', fileperms($dir."/".$file)),-4); |
| 355 |
if($filePerm!=="0644") |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
public static function file_permission($dir=ABSPATH) |
| 365 |
{ |
| 366 |
$files = scandir($dir); |
| 367 |
$dir = rtrim($dir,"/"); |
| 368 |
foreach($files as $file) |
| 369 |
{ |
| 370 |
if($file!=="."&&$file!==".."&&$file!=="wp-admin"&&$file!=="wp-includes"&&$file!=="plugins"&&$file!=="themes"&&$file!=="uploads"){ |
| 371 |
if(is_dir($dir."/".$file)) |
| 372 |
{ |
| 373 |
$dirPerm = substr(sprintf('%o', fileperms($dir."/".$file)),-4); |
| 374 |
if($dirPerm!=="0755") |
| 375 |
{ |
| 376 |
chmod($dir."/".$file,0755); |
| 377 |
} |
| 378 |
|
| 379 |
MMB_Security::file_permission($dir."/".$file); |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
$filePerm = substr(sprintf('%o', fileperms($dir."/".$file)),-4); |
| 384 |
if($filePerm!=="0644") |
| 385 |
{ |
| 386 |
chmod($dir."/".$file,0644); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
public function security_fix_all($args) |
| 395 |
{ |
| 396 |
$user_name = $args['new_user_name']; |
| 397 |
|
| 398 |
if($args['fix_listing']) |
| 399 |
{ |
| 400 |
MMB_Security::security_fix_dir_listing($args); |
| 401 |
} |
| 402 |
if($args['fix_wp_version']) |
| 403 |
{ |
| 404 |
MMB_Security::security_fix_wp_version($args); |
| 405 |
} |
| 406 |
if($args['fix_database_reporting']) |
| 407 |
{ |
| 408 |
MMB_Security::security_fix_database_reporting($args); |
| 409 |
} |
| 410 |
if($args['fix_php_reporting']) |
| 411 |
{ |
| 412 |
MMB_Security::security_fix_php_reporting($args); |
| 413 |
} |
| 414 |
if($args['security_fix_scripts_styles']) |
| 415 |
{ |
| 416 |
MMB_Security::security_fix_scripts_styles($args); |
| 417 |
} |
| 418 |
if($args['fix_htaccess_permission']) |
| 419 |
{ |
| 420 |
MMB_Security::security_fix_htaccess_permission($args); |
| 421 |
} |
| 422 |
if($args['security_fix_permissions']) |
| 423 |
{ |
| 424 |
MMB_Security::security_fix_permissions($args); |
| 425 |
} |
| 426 |
$scan_res = $this->security_check($args); |
| 427 |
if($args['fix_admin_username']) |
| 428 |
{ |
| 429 |
$params[] = $user_name; |
| 430 |
MMB_Security::security_fix_admin_username($params); |
| 431 |
$scan_res["admin_user_ok"] = true; |
| 432 |
} |
| 433 |
|
| 434 |
return $scan_res; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
} |
| 440 |
?> |
| 441 |
|