避免用戶枚舉

避免Users Enumeration。

.htaccess

避免用戶名單被https://example.com/?author=x一個個列舉出來。

.htaccess加入以下程式碼:

Stop User Enumeration in WordPress

https://perishablepress.com/stop-user-enumeration-wordpress/
# Block User ID Phishing Requests
<IfModule mod_rewrite.c>
	RewriteCond %{QUERY_STRING} ^author=([0-9]*)
	RewriteRule .* http://example.com/? [L,R=302]
</IfModule>

SITEMAP

官方添加這功能將用戶表列出來真的有點莫名奇妙,排除掉wp-sitemap-users-1.xml

於function.php加入:

How to remove author sitemaps from WordPress

https://duaneblake.co.uk/wordpress/how-to-remove-author-sitemaps-from-wordpress/
function remove_author_category_pages_from_sitemap($provider, $name)
{
    if ('users' === $name) {
        return false;
    }
    return $provider;
}
add_filter('wp_sitemaps_add_provider', 'remove_author_category_pages_from_sitemap', 10, 2);

REST API

控制REST API對外連通功能。

6 ways to enumerate WordPress Users

https://www.gosecure.net/blog/2021/03/16/6-ways-to-enumerate-wordpress-users/

上方的引用羅列了REST API的用戶名漏洞。

洩漏用戶名:

  • https://example.com/wp-json/wp/v2/users
  • https://example.com/?rest_route=/wp/v2/users

網路上很多是直接禁止REST API全部功能,不過WordPress不知道哪裡就調用了REST API,所以很多地方都會跳出錯誤。

1)失敗方案

就本站而言下述方式已知會妨礙古騰堡編輯器以及LiteSpeed Cache獲取Domain Key、圖片最佳化。

網路上流傳的限制REST API的程式碼基本結構都是如下:

Disable default WordPress REST Endpoints #2338

https://github.com/WP-API/WP-API/issues/2338#issuecomment-348385938

在function.php加入以下程式碼:

function rest_only_for_authorized_users($wp_rest_server) {
    if ( !is_user_logged_in() ) {
        wp_die( 'UNAUTHORIZED ACCESS' );
    }
}
add_filter( 'rest_api_init', 'rest_only_for_authorized_users', 99 );

完全禁止非註冊用戶接觸REST API,但如上述所說會造成許多功能的缺失。

2)測試方案一

REST API主要是顯示出來的slug部分會洩漏user_nicename,而user_nicename又預設是小寫化的user_login,造成登入名稱外漏。

所以在資料庫將user_nicename更改為與user_login不同的字串。

不懂程式,不知道這個想法正確與否。

3)測試方案二

加入判斷僅允許白名單連線的版本:

Prevent External Access to WordPress Rest API

https://www.jucra.com/whmcs/knowledgebase/183/Prevent-External-Access-to-Wordpress-Rest-API.html

4)測試方案三

Disable WordPress REST API Without a Plugin

https://wp-tutorials.tech/optimise-wordpress/disable-wordpress-rest-api-without-a-plugin/

此方案似乎可以靈活地解決這個問題。

QUIC.cloud

如果是QUIC.cloud的用戶,.htaccess和REST API這兩項可以分別透過:

  • 「Block Author Scan」
  • 「Block WP API User List」

來達成,CDN會自動轉向ERROR 403。

附言:「Block WP API User List」似乎無法阻止「https://example.com/?rest_route=/wp/v2/users」方式的用戶枚舉。