2022-03-15 19:25:08 +01:00
# wp-cli/secure-command
2022-03-21 11:10:51 +01:00
Official website: [Hackthewp.com ](https://hackthewp.com/ )
2022-03-21 11:11:00 +01:00
2022-03-15 19:25:08 +01:00
Manages common security aspects of WordPress. Supports nginx and Apache.
## Basic Usage
This package implements the following commands:
2022-03-20 18:43:03 +01:00
### Deploy All Security rules
Deploys all above-mentioned rules at once.
```bash
wp secure all
```
### Remove All Security Rules
Removes all security rules.
```bash
wp secure flush
```
2022-03-21 11:40:40 +01:00
### Add Security Headers
2022-04-05 13:23:36 +02:00
Adds the HSTS, Referrer-Policy, X-Content-Type-Options and X-Frame-Options
2022-03-21 11:40:40 +01:00
You can choose to add all above or only one or more by using `--headers` argument.
Example:
```bash
wp secure add-security-headers
wp secure add-security-headers --headers=Strict-Transport-Security
wp secure add-security-headers --headers=Strict-Transport-Security,X-Frame-Options
```
2022-03-20 22:59:20 +01:00
### Block the access to sensitive files and directories
2022-03-20 18:37:32 +01:00
```bash
wp secure block-access < what-to-block >
```
2022-03-15 19:32:00 +01:00
2022-03-20 22:59:20 +01:00
By default, this command blocks the direct access to sensitive files and directories:
2022-03-20 18:31:25 +01:00
`readme.txt` , `readme.html` , `xmlrpc.php` , `wp-config.php` , `wp-admin/install.php` , `wp-admin/upgrade.php` , `.git` , `svn` , `cache` and `vendors`
2022-03-15 19:25:08 +01:00
2022-03-20 18:31:25 +01:00
Possible options are:
- sensitive-files
- sensitive-directories
- xmlrpc
- htaccess
2022-03-20 22:59:20 +01:00
- custom
2022-03-20 18:31:25 +01:00
- all (does all the above)
2022-03-15 19:25:08 +01:00
2022-03-20 18:37:32 +01:00
Examples:
```bash
2022-03-20 18:31:25 +01:00
wp secure block-access sensitive-files
wp secure block-access sensitive-directories
wp secure block-access xmlrpc
wp secure block-access htaccess
wp secure block-access all
2022-03-15 19:25:08 +01:00
```
2022-03-20 22:59:20 +01:00
However, you can also block custom files and/or folders of your choice. To do that you should use `custom` argument
and pass one of two additional options `--files` and/or `--directories` .
If you want to block custom files, make sure that you pass only file names, not a full file paths.
Examples:
````bash
wp secure block-access custom --files=dump.sql,phpinfo.php,adminer.php
wp secure block-access custom --directories=wp-content/mu-plugins
````
2022-03-20 18:37:32 +01:00
### Block Author Scanning
```bash
wp secure block-author-scanning
```
2022-03-15 19:32:00 +01:00
2022-03-15 19:25:08 +01:00
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.
2022-03-20 18:37:32 +01:00
Examples:
```bash
2022-03-20 18:31:25 +01:00
wp secure block-author-scanning
2022-03-15 19:25:08 +01:00
```
2022-03-20 18:37:32 +01:00
### Block Direct Access and Execution in certain directories
```bash
wp secure block-php-execution < where >
```
2022-03-15 19:32:00 +01:00
2022-03-20 18:31:25 +01:00
Blocks direct access and execution of PHP files in `wp-content/plugins` , `wp-content/uploads` , `wp-content/themes` and `wp-includes` directories.
2022-03-15 19:25:08 +01:00
2022-03-20 18:31:25 +01:00
You need to specify where you want to prevent direct access to PHP files. Possible options are:
- all
- plugins
- uploads
- themes
- wp-includes
2022-03-15 19:25:08 +01:00
2022-03-20 18:37:32 +01:00
Examples:
```bash
2022-03-20 18:31:25 +01:00
wp secure block-php-execution all
wp secure block-php-execution plugins
wp secure block-php-execution uploads
wp secure block-php-execution themes
wp secure block-php-execution wp-includes
2022-03-15 19:25:08 +01:00
```
2022-03-20 18:37:32 +01:00
### Disable Directory Browsing
```bash
wp secure disable-directory-browsing
```
2022-03-15 19:32:00 +01:00
2022-03-15 19:25:08 +01:00
Disables directory browsing.
2022-03-20 22:59:20 +01:00
By default, when your web server does not find an index file (i.e. a file like index.php or index.html), it
2022-03-15 19:25:08 +01:00
automatically displays an index page showing the contents of the directory.
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.
2022-03-20 18:37:32 +01:00
Examples:
```bash
2022-03-20 18:31:25 +01:00
wp secure disable-directory-browsing
2022-03-15 19:25:08 +01:00
```
2022-03-20 18:37:32 +01:00
### Disable WordPress File Editor
2022-03-20 15:49:32 +01:00
2022-03-20 18:37:32 +01:00
Disables the WordPress file editor. It could be used to edit arbitrary files using the web interface.
2022-03-20 15:49:32 +01:00
This makes it easier for attackers to change files on the server using a web browser.
2022-03-20 18:37:32 +01:00
```bash
2022-03-20 15:49:32 +01:00
wp secure disable-file-editor
```
2022-03-20 22:59:20 +01:00
### Fix file and directory permissions
```bash
wp secure fix-permissions
```
Use this command to verify that the permissions of all files and directories are set according the WordPress recommendations.
2022-04-05 13:29:58 +02:00
This command will set **0644** to all files and **0755** to all folders inside WordPress installation.
2022-03-20 22:59:20 +01:00
**IMPORTANT: Don't use this command if you don't know what you are doing here!**
### Check the integrity of WordPress files
Downloads MD5 checksums for the current version from WordPress.org, and compares those checksums against the currently
installed files.
It also returns a list of files that shouldn't be part of default WordPress installation, which can be very useful when you are
looking for a possible injected files.
Examples:
```bash
wp secure integrity-scan
```
2022-03-15 19:25:08 +01:00
## Global options
2022-03-15 19:32:00 +01:00
### Remove single security rule
2022-03-15 19:25:08 +01:00
Using `--remove` with any rule command, you can remove it from configuration.
2022-03-20 18:37:32 +01:00
```bash
2022-03-20 18:31:25 +01:00
wp secure block-access xmlrpc --remove
2022-03-15 19:25:08 +01:00
```
2022-03-15 19:32:00 +01:00
### Get the output instead of writing in configuration files
2022-03-15 19:25:08 +01:00
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.
2022-03-20 18:37:32 +01:00
```bash
2022-03-20 18:31:25 +01:00
wp secure block-access htaccess --output
wp secure block-access htaccess --output --server=nginx
2022-03-15 19:25:08 +01:00
```
2022-03-15 19:32:00 +01:00
### Specify server type
2022-03-15 19:25:08 +01:00
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.
2022-03-20 18:37:32 +01:00
```bash
2022-03-20 18:31:25 +01:00
wp secure block-access htaccess --server=nginx
2022-03-15 19:25:08 +01:00
```
2022-03-15 19:32:00 +01:00
### Specify custom file path
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.
2022-03-15 19:25:08 +01:00
However, you can specify a custom file path that is going to be used for storing security rules.
```
2022-03-20 18:31:25 +01:00
wp secure block-access htaccess --file-path=/home/user/mysite.com/.htaccess
2022-03-15 19:25:08 +01:00
```
## Important Note for nginx users
2022-03-20 22:59:20 +01:00
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:
2022-03-15 19:25:08 +01:00
```
systemctl restart nginx
```
WIth each rule deploy or removal, you also need to restart nginx server.
## Installing
To install the latest version of this package over what's included in WP-CLI, run:
```
wp package install git@github .com:igorhrcek/wp-cli-secure-command.git
```
2022-03-20 07:53:07 +01:00
## Development and testing
You need to set up two working WordPress installations on Apache and nginx. Usage of Docker containers is highly recommended - you can use the official WordPress Docker containers, BitNami or bootstrap your environment using [ddev ](https://ddev.readthedocs.io/en/stable/users/cli-usage/#wordpress-quickstart ).
For testing you need to create `.env` file with the following content:
```
WORDPRESS_NGINX_PATH=wp/nginx
WORDPRESS_NGINX_URL=https://wpnginx.ddev.site
WORDPRESS_APACHE_PATH=wp/apache
WORDPRESS_APACHE_URL=https://wpapache.ddev.site
```
These paths and URLs are going to be used during tests, so make sure that they are accessible.
## Contributing
We appreciate you taking the initiative to contribute to this project.
2022-03-21 11:10:32 +01:00
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.