true, 'severity' => 'medium', 'rule' => 'xmlrpc_multicall', 'action' => 'blocked', 'status_code' => 403, 'message' => __('XML-RPC system.multicall request blocked', 'all-in-one-wp-content-security')));
self::deny_request('xmlrpc_multicall', null);
}
}
/**
* Whether the XML-RPC request body calls system.multicall (only the method name is read)
*/
public static function is_multicall()
{
$body = @file_get_contents('php://input', false, null, 0, 8192);
return is_string($body) && (bool) preg_match('#\s*system\.multicall\s*#i', $body);
}
/**
* Stop the request with HTTP 403 (never cached; no theme or plugin output)
*/
public static function deny_request($reason, $block)
{
if (!defined('DONOTCACHEPAGE')) {
define('DONOTCACHEPAGE', true);
}
$ip = AOWPCS_IP::client_ip();
$metrics = array(array('blocked', ''), array('blocked_rule', $reason));
// Refused before the request monitor runs, so count it as an XML-RPC request here
if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST && AOWPCS_Settings::get('traffic_monitoring') && !AOWPCS_Request_Monitor::was_counted()) {
$metrics[] = array('xmlrpc', '');
}
AOWPCS_Event_Log::count_metrics($metrics);
// XML-RPC refusals have their own event; blocked addresses and bot filters are logged here
$reasons = array(
'scanner_user_agent' => __('Request from a known attack tool refused', 'all-in-one-wp-content-security'),
'crawler_spoofed' => __('Request from a fake search engine crawler refused', 'all-in-one-wp-content-security'),
);
if ($block || isset($reasons[$reason])) {
AOWPCS_Event_Log::log_throttled('request_blocked', AOWPCS_IP::hash($ip) . '|' . $reason, 1, 600, array(
'request' => true,
'severity' => 'low',
'rule' => $reason,
'action' => 'blocked',
'status_code' => 403,
/* translators: %s: why the request was refused */
'message' => sprintf(__('%s (further requests in the next 10 minutes are only counted)', 'all-in-one-wp-content-security'), $block ? __('Request from a blocked address refused', 'all-in-one-wp-content-security') : $reasons[$reason]),
));
}
$reference = strtoupper(substr(hash('sha256', $ip . '|' . gmdate('Y-m-d')), 0, 8));
$until = ($block && !empty($block['expires'])) ? wp_date(get_option('time_format') . ' (' . get_option('date_format') . ')', $block['expires']) : '';
if (!headers_sent()) {
status_header(403);
nocache_headers();
header('X-Robots-Tag: noindex');
}
if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) {
if (!headers_sent()) {
header('Content-Type: text/xml; charset=UTF-8');
}
echo 'faultCode403faultStringForbidden';
exit;
}
$uri = isset($_SERVER['REQUEST_URI']) ? wp_unslash($_SERVER['REQUEST_URI']) : '';
if (strpos($uri, '/' . rest_get_url_prefix() . '/') !== false || isset($_GET['rest_route']) || (isset($_SERVER['HTTP_ACCEPT']) && strpos(wp_unslash($_SERVER['HTTP_ACCEPT']), 'application/json') === 0)) {
if (!headers_sent()) {
header('Content-Type: application/json; charset=UTF-8');
}
echo wp_json_encode(array('code' => 'aowpcs_forbidden', 'message' => __('Access temporarily blocked by the site\'s security rules.', 'all-in-one-wp-content-security'), 'data' => array('status' => 403, 'reference' => $reference)));
exit;
}
if (!headers_sent()) {
header('Content-Type: text/html; charset=UTF-8');
}
$message = $until !== ''
/* translators: %s: until */
? sprintf(__('Your requests were temporarily blocked by this site\'s security rules until %s.', 'all-in-one-wp-content-security'), $until)
: __('Your request was blocked by this site\'s security rules.', 'all-in-one-wp-content-security');
echo '
' . esc_html__('Access blocked', 'all-in-one-wp-content-security') . ''
. ''
. '' . esc_html__('Access temporarily blocked', 'all-in-one-wp-content-security') . '
'
. '' . esc_html($message) . '
'
. '' . esc_html__('If you think this is a mistake, contact the site owner and mention the reference below.', 'all-in-one-wp-content-security') . '
'
. '' . esc_html__('Reference:', 'all-in-one-wp-content-security') . ' ' . esc_html($reference) . '
'
. '';
exit;
}
}
/**
* Login protection: failed login tracking, temporary lockouts and generic error messages
*/
class AOWPCS_Login_Guard
{
public static function init()
{
add_action('wp_login_failed', array(__CLASS__, 'failed'), 10, 2);
add_action('wp_login', array(__CLASS__, 'succeeded'), 10, 2);
if (AOWPCS_Plugin::safe_mode()) {
return;
}
add_filter('authenticate', array(__CLASS__, 'authenticate'), 99, 3);
if (AOWPCS_Settings::get('login_generic_errors')) {
add_filter('authenticate', array(__CLASS__, 'generic_errors'), 100, 3);
}
}
private static function key($ip)
{
return 'login:' . AOWPCS_IP::hash(AOWPCS_IP::block_key($ip));
}
public static function failed($username, $error = null)
{
if (AOWPCS_Loopback::is_own_request()) {
return;
}
$ip = AOWPCS_IP::client_ip();
$user = is_string($username) && $username !== '' ? (get_user_by('login', $username) ?: get_user_by('email', $username)) : false;
if (AOWPCS_Settings::get('traffic_monitoring')) {
AOWPCS_Event_Log::count_metrics(array(array('login_failed', '')));
AOWPCS_Event_Log::log_throttled('login_failed', AOWPCS_IP::hash($ip), 30, HOUR_IN_SECONDS, array(
'request' => true,
'severity' => 'low',
'user_id' => $user ? $user->ID : 0,
'rule' => 'login_failed',
'status_code' => 200,
// The submitted user name is never stored (users sometimes type their password into it)
'message' => $user ? __('Failed login for an existing account', 'all-in-one-wp-content-security') : __('Failed login for an unknown user name', 'all-in-one-wp-content-security'),
'data' => array('existing_account' => (bool) $user, 'via' => (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ? 'xmlrpc' : ((defined('REST_REQUEST') && REST_REQUEST) ? 'rest' : 'login form')),
));
}
// A login form on another site can make any visitor's browser submit failed logins here;
// such attempts are logged but must not get the visitor blocked
if (!AOWPCS_Settings::get('login_protection') || AOWPCS_Plugin::safe_mode() || AOWPCS_Request_Monitor::browser_cross_site()) {
return;
}
$window = (int) AOWPCS_Settings::get('login_window_minutes') * MINUTE_IN_SECONDS;
$count = AOWPCS_Rate_Limiter::hit(self::key($ip), $window);
if ($count >= (int) AOWPCS_Settings::get('login_max_failures')) {
/* translators: 1: count, 2: window / 60 */
$result = AOWPCS_Block_List::block($ip, (int) AOWPCS_Settings::get('login_block_minutes'), sprintf(__('%1$d failed logins within %2$d minutes', 'all-in-one-wp-content-security'), $count, $window / 60), 'login_lockout', AOWPCS_Settings::get('block_scope'));
if (!is_wp_error($result)) {
AOWPCS_Rate_Limiter::reset(self::key($ip));
/* translators: %d: count */
AOWPCS_Event_Log::log('login_lockout', array('request' => true, 'severity' => 'medium', 'rule' => 'login_lockout', 'action' => 'ip_blocked', 'message' => sprintf(__('Address blocked after %d failed logins', 'all-in-one-wp-content-security'), $count)));
}
}
}
public static function succeeded($user_login, $user)
{
$ip = AOWPCS_IP::client_ip();
AOWPCS_Rate_Limiter::reset(self::key($ip));
if (AOWPCS_Settings::get('log_admin_logins') && $user instanceof WP_User && user_can($user, 'manage_options')) {
AOWPCS_Event_Log::log('admin_login', array('request' => true, 'severity' => 'low', 'user_id' => $user->ID, 'message' => __('Administrator logged in', 'all-in-one-wp-content-security')));
}
}
/**
* Refuse authentication from blocked addresses (covers XML-RPC and application passwords)
*/
public static function authenticate($user, $username, $password)
{
$ip = AOWPCS_IP::client_ip();
if (AOWPCS_Block_List::find($ip) && !AOWPCS_Block_List::is_allowlisted($ip)) {
return new WP_Error('aowpcs_blocked', __('Error: Too many failed login attempts. Please try again later.', 'all-in-one-wp-content-security'));
}
return $user;
}
/**
* Same message for an unknown user name and a wrong password (no account enumeration)
*/
public static function generic_errors($user, $username, $password)
{
if (is_wp_error($user) && array_intersect($user->get_error_codes(), array('invalid_username', 'invalid_email', 'incorrect_password'))) {
return new WP_Error('aowpcs_invalid_login', __('Error: The user name, e-mail address or password is incorrect.', 'all-in-one-wp-content-security'));
}
return $user;
}
}
/**
* XML-RPC control: off, disable pingbacks, disable (PHP level, any server) or server-level block
* (the original "Disable XML-RPC" .htaccess rule plus the PHP-level block as a fallback)
*/
class AOWPCS_Xmlrpc_Guard
{
public static function init()
{
if (AOWPCS_Plugin::safe_mode()) {
return;
}
$mode = AOWPCS_Settings::get('xmlrpc_mode');
if ($mode === 'pingbacks' || $mode === 'disable' || $mode === 'server') {
add_filter('xmlrpc_methods', array(__CLASS__, 'remove_pingback_methods'));
add_filter('wp_headers', array(__CLASS__, 'remove_pingback_header'));
add_filter('pings_open', '__return_false', 9999);
}
if ($mode === 'disable' || $mode === 'server') {
add_filter('xmlrpc_enabled', '__return_false');
add_action('init', array(__CLASS__, 'block'), 2);
remove_action('wp_head', 'rsd_link');
}
}
public static function remove_pingback_methods($methods)
{
unset($methods['pingback.ping'], $methods['pingback.extensions.getPingbacks']);
return $methods;
}
public static function remove_pingback_header($headers)
{
unset($headers['X-Pingback']);
return $headers;
}
public static function block()
{
if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST && !AOWPCS_Loopback::is_own_request()) {
AOWPCS_Event_Log::log_throttled('xmlrpc_blocked', AOWPCS_IP::hash(AOWPCS_IP::client_ip()), 5, 600, array('request' => true, 'severity' => 'low', 'rule' => 'xmlrpc_disabled', 'action' => 'blocked', 'status_code' => 403, 'message' => __('XML-RPC is disabled', 'all-in-one-wp-content-security')));
AOWPCS_Firewall::deny_request('xmlrpc_disabled', null);
}
}
/**
* Apply a mode change: keep the original option key and the .htaccess rule in sync.
* Returns true or WP_Error (the mode is still applied at PHP level).
*/
public static function apply_mode($mode)
{
AOWPCS_Settings::update(array('xmlrpc_mode' => $mode));
AOWPCS_Settings::update_legacy('disable_xmlrpc', $mode === 'server');
$file = AOWPCS_Htaccess::root_file();
$body = AOWPCS_Htaccess::read_block($file, AOWPCS_Htaccess::XMLRPC);
if ($body !== null && !AOWPCS_Htaccess::is_own_xmlrpc_block($body)) {
return new WP_Error('aowpcs_xmlrpc_foreign', __('An "XMLRPC Restriction" block that was not written by this plugin exists in .htaccess; it was left unchanged.', 'all-in-one-wp-content-security'));
}
if ($mode === 'server') {
if (!AOWPCS_Environment::uses_htaccess()) {
return new WP_Error('aowpcs_no_htaccess', __('This server does not read .htaccess files, so XML-RPC is blocked at PHP level only (which is effective on every server).', 'all-in-one-wp-content-security'));
}
$result = AOWPCS_Htaccess::write_validated($file, AOWPCS_Htaccess::XMLRPC, AOWPCS_Htaccess::xmlrpc_lines(), false, 'XML-RPC server-level block');
} else {
$result = true;
foreach (AOWPCS_Htaccess::root_files() as $root) {
$existing = AOWPCS_Htaccess::read_block($root, AOWPCS_Htaccess::XMLRPC);
if ($existing !== null && AOWPCS_Htaccess::is_own_xmlrpc_block($existing)) {
$result = AOWPCS_Htaccess::remove_block($root, AOWPCS_Htaccess::XMLRPC, 'XML-RPC server-level block removed');
}
}
}
if (!is_wp_error($result)) {
AOWPCS_Check_Fs_Walk::note_own_write($file);
}
return $result;
}
}
/**
* Optional restrictions for anonymous visitors: user enumeration and the generator tag
*/
class AOWPCS_Rest_Guard
{
public static function init()
{
if (AOWPCS_Plugin::safe_mode()) {
return;
}
if (AOWPCS_Settings::get('rest_user_enumeration')) {
add_filter('rest_pre_dispatch', array(__CLASS__, 'users_endpoint'), 10, 3);
add_action('template_redirect', array(__CLASS__, 'author_enumeration'), 0);
add_filter('wp_sitemaps_add_provider', array(__CLASS__, 'sitemap_users'), 10, 2);
}
if (AOWPCS_Settings::get('hide_generator')) {
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_empty_string');
}
}
public static function users_endpoint($result, $server, $request)
{
if (!is_user_logged_in() && preg_match('#^/wp/v2/users(/|$)#', $request->get_route())) {
return new WP_Error('rest_user_cannot_view', __('Sorry, you are not allowed to list users.', 'all-in-one-wp-content-security'), array('status' => rest_authorization_required_code()));
}
return $result;
}
public static function author_enumeration()
{
if (!is_user_logged_in() && isset($_GET['author']) && preg_match('/^\d+$/', (string) wp_unslash($_GET['author']))) {
wp_safe_redirect(home_url('/'), 301);
exit;
}
}
public static function sitemap_users($provider, $name)
{
return $name === 'users' ? false : $provider;
}
}