| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component\Security_Tweaks\Servers; |
| 4 |
|
| 5 |
class Nginx { |
| 6 |
/** |
| 7 |
* Service type. |
| 8 |
* |
| 9 |
* @var string |
| 10 |
*/ |
| 11 |
private $type = null; |
| 12 |
|
| 13 |
public function __construct( $type ) { |
| 14 |
$this->type = $type; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Check whether the issue has been resolved or not. |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function check() { |
| 23 |
$url = ''; |
| 24 |
|
| 25 |
if ( 'prevent-php-executed' === $this->type ) { |
| 26 |
$dir = wp_upload_dir(); |
| 27 |
$url = $dir['baseurl'] . '/wp-defender/index.php'; |
| 28 |
} |
| 29 |
|
| 30 |
if ( 'protect-information' === $this->type ) { |
| 31 |
$url = defender_asset_url( '/languages/wpdef-default.pot' ); |
| 32 |
} |
| 33 |
|
| 34 |
return Server::ping_test_failed( $url ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Process the rule. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function process() { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Revert the rule. |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
public function revert() { |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get rules. |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get_rules() { |
| 61 |
$rules = ''; |
| 62 |
if ( 'prevent-php-executed' === $this->type ) { |
| 63 |
if ( defender_is_windows() ) { |
| 64 |
$wp_includes = str_replace( ABSPATH, '', WPINC ); |
| 65 |
$wp_content = str_replace( ABSPATH, '', WP_CONTENT_DIR ); |
| 66 |
} else { |
| 67 |
$wp_includes = str_replace( $_SERVER['DOCUMENT_ROOT'], '', ABSPATH . WPINC ); |
| 68 |
$wp_content = str_replace( $_SERVER['DOCUMENT_ROOT'], '', WP_CONTENT_DIR ); |
| 69 |
} |
| 70 |
|
| 71 |
$rules .= "## WP Defender - Prevent PHP Execution ##" . PHP_EOL; |
| 72 |
$rules .= PHP_EOL; |
| 73 |
$rules .= "# Stop php access except to needed files in wp-includes" . PHP_EOL; |
| 74 |
$rules .= "location ~* ^$wp_includes/.*(?<!(js/tinymce/wp-tinymce))\.php$ {" . PHP_EOL; |
| 75 |
$rules .= " internal; #internal allows ms-files.php rewrite in multisite to work" . PHP_EOL; |
| 76 |
$rules .= "}" . PHP_EOL; |
| 77 |
$rules .= PHP_EOL; |
| 78 |
$rules .= "# Specifically locks down upload directories in case full wp-content rule below is skipped" . PHP_EOL; |
| 79 |
$rules .= "location ~* /(?:uploads|files)/.*\.php$ {" . PHP_EOL; |
| 80 |
$rules .= " deny all;" . PHP_EOL; |
| 81 |
$rules .= "}" . PHP_EOL; |
| 82 |
$rules .= PHP_EOL; |
| 83 |
$rules .= "# Deny direct access to .php files in the /wp-content/ directory (including sub-folders)." . PHP_EOL; |
| 84 |
$rules .= "# Note this can break some poorly coded plugins/themes, replace the plugin or remove this block if it causes trouble" . PHP_EOL; |
| 85 |
$rules .= "location ~* ^$wp_content/.*\.php$ {" . PHP_EOL; |
| 86 |
$rules .= " deny all;" . PHP_EOL; |
| 87 |
$rules .= "}" . PHP_EOL; |
| 88 |
$rules .= PHP_EOL; |
| 89 |
$rules .= "## WP Defender - End ##"; |
| 90 |
} |
| 91 |
|
| 92 |
if ( 'protect-information' === $this->type ) { |
| 93 |
if ( defender_is_windows() ) { |
| 94 |
$wp_content = str_replace( ABSPATH, '', WP_CONTENT_DIR ); |
| 95 |
} else { |
| 96 |
$wp_content = str_replace( $_SERVER['DOCUMENT_ROOT'], '', WP_CONTENT_DIR ); |
| 97 |
} |
| 98 |
$rules .= '## WP Defender - Prevent information disclosure ##'; |
| 99 |
$rules .= PHP_EOL; |
| 100 |
$rules .= '# Turn off directory indexing autoindex off;' . PHP_EOL; |
| 101 |
$rules .= PHP_EOL; |
| 102 |
$rules .= '# Deny access to htaccess and other hidden files' . PHP_EOL; |
| 103 |
$rules .= 'location ~ /\. {' . PHP_EOL; |
| 104 |
$rules .= ' deny all;' . PHP_EOL; |
| 105 |
$rules .= '}' . PHP_EOL; |
| 106 |
$rules .= PHP_EOL; |
| 107 |
$rules .= '# Deny access to wp-config.php file' . PHP_EOL; |
| 108 |
$rules .= 'location = /wp-config.php {' . PHP_EOL; |
| 109 |
$rules .= ' deny all;' . PHP_EOL; |
| 110 |
$rules .= '}' . PHP_EOL; |
| 111 |
$rules .= PHP_EOL; |
| 112 |
$rules .= '# Deny access to revealing or potentially dangerous files in the /wp-content/ directory (including sub-folders)' . PHP_EOL; |
| 113 |
$rules .= "location ~* ^$wp_content/.*\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$ {" . PHP_EOL; |
| 114 |
$rules .= ' deny all;' . PHP_EOL; |
| 115 |
$rules .= '}' . PHP_EOL; |
| 116 |
$rules .= '## WP Defender - End ##'; |
| 117 |
$rules .= PHP_EOL; |
| 118 |
} |
| 119 |
|
| 120 |
return $rules; |
| 121 |
} |
| 122 |
} |
| 123 |
|