通过wordpress中的/wp-json/wp/v2/users/获取后台用户名等相关信息,如何处理这个漏洞?
方法一:直接屏蔽REST API
如果你的网站不需要使用WordPress的REST API,可以直接屏蔽所有REST API接口。在functions.php
文件中添加以下代码:
add_filter('rest_authentication_errors', function($access) {
return new WP_Error('rest_cannot_access', 'REST API不再提供访问', ['status' => 403]);
});
这样,所有的REST API请求都会被拒绝访问。
方法二:屏蔽用户接口
如果你只需要屏蔽用户接口,可以在functions.php
文件中添加以下代码:
add_filter('rest_endpoints', function($endpoints) {
if (isset($endpoints['/wp/v2/users'])) {
unset($endpoints['/wp/v2/users']);
}
if (isset($endpoints['/wp/v2/users/(?P<id>[\d]+)'])) {
unset($endpoints['/wp/v2/users/(?P<id>[\d]+)']);
}
return $endpoints;
});
这段代码会从REST API中移除用户接口,防止通过wp-json/wp/v2/users
访问用户信息。