wordpress中的/wp-json/wp/v2/users/漏洞修复

来源:小凡日期:2024年11月28日

通过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访问用户信息。

相关文章
微信咨询微信咨询
二维码
微信扫码 立即咨询
销售热线咨询热线
15202181997
立即咨询立即咨询