Skip to content

Commit 924030d

Browse files
authored
Merge branch 'master' into issue-3
2 parents 459c509 + 201d1de commit 924030d

24 files changed

+469
-338
lines changed

.github/workflows/tests.yml

-26
This file was deleted.

README.md

+109-56
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,189 @@
11
# wp-cli/secure-command
2+
Official website: [Hackthewp.com](https://hackthewp.com/)
3+
24
Manages common security aspects of WordPress. Supports nginx and Apache.
35

46
## Basic Usage
57
This package implements the following commands:
68

7-
**wp secure block_access_to_htaccess**
9+
### Deploy All Security rules
810

9-
Blocks access to `.htaccess` and `nginx.conf` files.
11+
Deploys all above-mentioned rules at once.
1012

11-
```
12-
wp secure block_access_to_htaccess [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
13+
```bash
14+
wp secure all
1315
```
1416

15-
**wp secure block_access_to_sensitive_directories**
17+
### Remove All Security Rules
1618

17-
Blocks direct access to sensitive directories - `.git`, `svn`, `cache` and `vendors`
19+
Removes all security rules.
1820

21+
```bash
22+
wp secure flush
1923
```
20-
wp secure block_access_to_sensitive_directories [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
24+
25+
### Block the access to sensitive files and directories
26+
```bash
27+
wp secure block-access <what-to-block>
2128
```
2229

23-
**wp secure block_access_to_sensitive_files**
30+
By default, this command blocks the direct access to sensitive files and directories:
31+
`readme.txt`, `readme.html`, `xmlrpc.php`, `wp-config.php`, `wp-admin/install.php`, `wp-admin/upgrade.php`, `.git`, `svn`, `cache` and `vendors`
2432

25-
Blocks direct access to sensitive files - `readme.txt`, `readme.html`, `wp-config.php`, `wp-admin/install.php` and `wp-admin/upgrade.php`
33+
Possible options are:
34+
- sensitive-files
35+
- sensitive-directories
36+
- xmlrpc
37+
- htaccess
38+
- custom
39+
- all (does all the above)
2640

27-
```
28-
wp secure block_access_to_sensitive_files [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
41+
Examples:
42+
43+
```bash
44+
wp secure block-access sensitive-files
45+
wp secure block-access sensitive-directories
46+
wp secure block-access xmlrpc
47+
wp secure block-access htaccess
48+
wp secure block-access all
2949
```
3050

31-
**wp secure block_access_to_xmlrpc**
51+
However, you can also block custom files and/or folders of your choice. To do that you should use `custom` argument
52+
and pass one of two additional options `--files` and/or `--directories`.
3253

33-
Blocks direct access XML-RPC
54+
If you want to block custom files, make sure that you pass only file names, not a full file paths.
3455

35-
```
36-
wp secure block_access_to_xmlrpc [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
37-
```
56+
Examples:
3857

39-
### wp secure block_author_scanning
58+
````bash
59+
wp secure block-access custom --files=dump.sql,phpinfo.php,adminer.php
60+
wp secure block-access custom --directories=wp-content/mu-plugins
61+
````
4062

41-
Blocks author scanning. Author scanning is a common technique of brute force attacks on WordPress. It is used to crack passwords for the known usernames and to gather additional information about the WordPress itself.
63+
### Block Author Scanning
4264

43-
```
44-
wp secure block_author_scanning [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
65+
```bash
66+
wp secure block-author-scanning
4567
```
4668

47-
### wp secure block_php_execution_in_plugins
69+
Blocks author scanning. Author scanning is a common technique of brute force attacks on WordPress. It is used to crack passwords for the known usernames and to gather additional information about the WordPress itself.
4870

49-
Blocks direct access and execution of PHP files in `wp-content/plugins` directory.
71+
Examples:
5072

73+
```bash
74+
wp secure block-author-scanning
5175
```
52-
wp secure block_php_execution_in_plugins [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
76+
77+
### Block Direct Access and Execution in certain directories
78+
79+
```bash
80+
wp secure block-php-execution <where>
5381
```
5482

55-
### wp secure block_php_execution_in_uploads
83+
Blocks direct access and execution of PHP files in `wp-content/plugins`, `wp-content/uploads`, `wp-content/themes` and `wp-includes` directories.
5684

57-
Blocks direct access and execution of PHP files in `wp-content/uploads` directory.
85+
You need to specify where you want to prevent direct access to PHP files. Possible options are:
86+
- all
87+
- plugins
88+
- uploads
89+
- themes
90+
- wp-includes
5891

92+
Examples:
93+
94+
```bash
95+
wp secure block-php-execution all
96+
wp secure block-php-execution plugins
97+
wp secure block-php-execution uploads
98+
wp secure block-php-execution themes
99+
wp secure block-php-execution wp-includes
59100
```
60-
wp secure block_php_execution_in_uploads [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
101+
102+
### Disable Directory Browsing
103+
```bash
104+
wp secure disable-directory-browsing
61105
```
62106

63-
### wp secure block_php_execution_in_themes
107+
Disables directory browsing.
108+
109+
By default, when your web server does not find an index file (i.e. a file like index.php or index.html), it
110+
automatically displays an index page showing the contents of the directory.
111+
This could make your site vulnerable to hack attacks by revealing important information needed to exploit a vulnerability in a WordPress plugin, theme, or your server in general.
64112

65-
Blocks direct access and execution of PHP files in `wp-content/themes` directory.
113+
Examples:
66114

67-
```
68-
wp secure block_php_execution_in_themes [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
115+
```bash
116+
wp secure disable-directory-browsing
69117
```
70118

71-
### wp secure block_php_execution_in_wp_includes
72-
Blocks direct access and execution of PHP files in include directories - `wp-admin/includes`, `wp-includes/*.php`, `wp-includes/js/tinymce/langs/*.php`, `wp-includes/theme-compat`
119+
### Disable WordPress File Editor
73120

121+
Disables the WordPress file editor. It could be used to edit arbitrary files using the web interface.
122+
This makes it easier for attackers to change files on the server using a web browser.
123+
124+
```bash
125+
wp secure disable-file-editor
74126
```
75-
wp secure block_php_execution_in_wp_includes [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
127+
128+
### Fix file and directory permissions
129+
130+
```bash
131+
wp secure fix-permissions
76132
```
77133

78-
### wp secure disable_directory_browsing
134+
Use this command to verify that the permissions of all files and directories are set according the WordPress recommendations.
135+
This command will set **0666** to all files and **0755** to all folders inside WordPress installation.
79136

80-
Disables directory browsing.
137+
**IMPORTANT: Don't use this command if you don't know what you are doing here!**
81138

82-
By default when your web server does not find an index file (i.e. a file like index.php or index.html), it
83-
automatically displays an index page showing the contents of the directory.
84-
This could make your site vulnerable to hack attacks by revealing important information needed to exploit a vulnerability in a WordPress plugin, theme, or your server in general.
139+
### Check the integrity of WordPress files
85140

86-
```
87-
wp secure disable_directory_browsing [--remove] [--file-path=/alternative/path] [--output] [--server=apache|nginx]
88-
```
141+
Downloads MD5 checksums for the current version from WordPress.org, and compares those checksums against the currently
142+
installed files.
89143

90-
### wp secure flush
144+
It also returns a list of files that shouldn't be part of default WordPress installation, which can be very useful when you are
145+
looking for a possible injected files.
91146

92-
Removes all security rules.
147+
Examples:
93148

94-
```
95-
wp secure flush
149+
```bash
150+
wp secure integrity-scan
96151
```
97152

98153
## Global options
99154

100155
### Remove single security rule
101156
Using `--remove` with any rule command, you can remove it from configuration.
102157

103-
```
104-
wp secure block_php_execution_in_wp_includes --remove
158+
```bash
159+
wp secure block-access xmlrpc --remove
105160
```
106161

107162
### Get the output instead of writing in configuration files
108163
Using `--output` option with any rule command, you can see actual rule code which you can inspect or manually copy to any file of your choice.
109164

110-
```
111-
wp secure block_php_execution_in_wp_includes --output
112-
wp secure block_php_execution_in_wp_includes --output --server=nginx
165+
```bash
166+
wp secure block-access htaccess --output
167+
wp secure block-access htaccess --output --server=nginx
113168
```
114169

115170
### Specify server type
116171
By default, all rules are generated for Apache or LiteSpeed web servers that utilize `.htaccess` file. However, you can use `--server` to specify nginx if you want.
117172

118-
```
119-
wp secure block_php_execution_in_wp_includes --server=nginx
120-
wp secure block_php_execution_in_wp_includes --server=--file-path=/home/user/mysite.com/nginx.conf
173+
```bash
174+
wp secure block-access htaccess --server=nginx
121175
```
122176

123177
### Specify custom file path
124178
By default, all commands assume that rules should be written in the root of WordPress installation in `.htaccess` and `nginx.conf`, depending on which server you choose.
125179
However, you can specify a custom file path that is going to be used for storing security rules.
126180

127181
```
128-
wp secure block_php_execution_in_plugins --file-path=/home/user/mysite.com/.htaccess
182+
wp secure block-access htaccess --file-path=/home/user/mysite.com/.htaccess
129183
```
130184

131185
## Important Note for nginx users
132-
nginx rules are stored in the `nginx.conf` file. However, for rules to actually work, you need to manually include this file in your vhost configuration and then restart nginx server:
186+
The nginx rules are stored in the `nginx.conf` file. However, for rules to actually work, you need to manually include this file in your vhost configuration and then restart nginx server:
133187
```
134188
systemctl restart nginx
135189
```
@@ -160,4 +214,3 @@ These paths and URLs are going to be used during tests, so make sure that they a
160214
We appreciate you taking the initiative to contribute to this project.
161215

162216
Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.
163-

src/FileManager.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function setFileContent() : array {
8282
}
8383

8484
if(!$this->isReadable()) {
85-
throw new (FileIsNotReadable::class);
85+
throw new FileIsNotReadable();
8686
}
8787

8888
return $this->read();
@@ -156,7 +156,7 @@ private function fileExist() : bool {
156156
*
157157
* @return array|string
158158
*/
159-
private static function removeZeroSpace($content): array|string
159+
private static function removeZeroSpace($content)
160160
{
161161
if(is_array($content)) {
162162
return array_map([static::class, 'removeZeroSpace'], $content);
@@ -203,9 +203,9 @@ public function extractRuleBlock(string $marker) : array {
203203
*
204204
* @param string $needle
205205
*
206-
* @return int|bool
206+
* @return bool|int|string
207207
*/
208-
private function findInFile(string $needle) : int|bool {
208+
private function findInFile(string $needle) {
209209
return array_search($needle, $this->file);
210210
}
211211

@@ -214,7 +214,7 @@ private function findInFile(string $needle) : int|bool {
214214
*
215215
* @return array|bool
216216
*/
217-
public function extractSecureBlock(): bool|array {
217+
public function extractSecureBlock() {
218218
$start = $this->findInFile(self::MARKER_GLOBAL_START . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
219219
$end = $this->findInFile(self::MARKER_GLOBAL_END . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
220220

@@ -330,7 +330,7 @@ private function backup() : bool
330330
public function add(array $content, string $marker = ''): bool {
331331
//If the rule block already exist, there is no reason to add it again
332332
if($this->hasRuleBlock($marker)) {
333-
throw new(RuleAlreadyExist::class);
333+
throw new RuleAlreadyExist();
334334
}
335335

336336
//Check if file exist?
@@ -339,7 +339,7 @@ public function add(array $content, string $marker = ''): bool {
339339
}
340340

341341
if(!$this->isWritable()) {
342-
throw new(FileIsNotWritable::class);
342+
throw new FileIsNotWritable();
343343
}
344344

345345
//Wrap the rule block with markers
@@ -450,4 +450,4 @@ private function flattenArray(array $array, int $depth = 1) : array {
450450

451451
return $result;
452452
}
453-
}
453+
}

0 commit comments

Comments
 (0)