隐藏 WordPress 管理员登录用户名,提高安全性

WordPress 提高后台安全的方法 小羿 发过「给 WordPress 后台登录添加验证码」和「自定义 WordPress 后台登录地址」,但是如果你的 管理员登录用户名 被暴露,同样也会有安全隐患,下面教大家如何隐藏 WordPress 管理员登录用户名。

网站右键查看源代码,无意间发现自己的管理员用户名被暴露了...

(图1:评论中暴露登录用户名)

方法1:直接修改Wordpress程序

① 然后,查了下代码,查到了这个函数comment_class(),进一步发现是被这个函数get_comment_class()(大约在wp-includes\comment-template.php 的419行)暴露管理员的登录用户名... 该函数内容如下:

function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
 global $comment_alt, $comment_depth, $comment_thread_alt;
 
 $comment = get_comment($comment_id);
 
 $classes = array();
 
 // Get the comment type (comment, trackback),
 $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
 
 // Add classes for comment authors that are registered users.
 if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) {
 $classes[] = 'byuser';
 $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
 // For comment authors who are the author of the post
 if ( $post = get_post($post_id) ) {
 if ( $comment->user_id === $post->post_author ) {
 $classes[] = 'bypostauthor';
 }
 }
 }
 
 if ( empty($comment_alt) )
 $comment_alt = 0;

我们的管理员用户名正是被其中的第14行暴露的... 在此,我们只需将这一行中的$user->user_nicename改为$user->user_id即可安全的隐藏管理员的登录名了~ 也隐藏了注册用户的登录用户名了!取而代之显示的是注册用户(包括管理员)的ID。从此再也不用担心网页中会暴露诸位站长的登录用户名了~

另外用户页面也有博主登录用户名,如下图:

(图2:用户页面中也暴露登录用户名)

然后查到了这个函数body_class(),进一步发现是被这个函数get_body_class()(大约在wp-includes\post-template.php 的634行)暴露管理员的登录用户名... 该函数部分内容如下:

elseif ( is_author() ) {
    $author = $wp_query->get_queried_object();
    $classes[] = 'author';
    if ( isset( $author->user_nicename ) ) {
        $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
 $classes[] = 'author-' . $author->ID;
    }
}

其中该函数的第5行也暴露了博主的登录名,将这一行删掉或注释掉即可成功解决~

方法2:过滤掉 "Comment-Author-" 和 "Author-"

「后台」→「外观」→「编辑」→ 「functions.php」文件,把下面的代码添加进去:

/**
 * 说明:直接去掉函数 comment_class() 和 body_class() 中输出的 "comment-author-" 和 "author-"
 */
function lxtx_remove_comment_body_author_class($content){ 
    $pattern = "/(.*?)([^>]*)author-([^>]*)(.*?)/i";
    $replacement = '$1$4';
    $content = preg_replace($pattern, $replacement, $content);  
    return $content;
}
add_filter('comment_class', 'lxtx_remove_comment_body_author_class');
add_filter('body_class', 'lxtx_remove_comment_body_author_class');

comment_class()和body_class()过滤的结果分别是:

// comment_class()过滤后的结果如下,去掉了原有class里的comment-author-test10,请和上面的图1比较
class="comment byuser  bypostauthor odd alt thread-odd thread-alt depth-1"
 
// body_class()过滤后的结果如下,去掉了原有class里的author-test10和author-1,请和上面的图2比较
class="archive author  logged-in"

另外其实老外早在2010年就发现了这个漏洞...

/**
 * 说明:直接去掉函数 comment_class() 和 body_class() 中输出的 "comment-author-" 和 "author-"
 */
function lxtx_remove_comment_body_author_class( $classes ) {
 foreach( $classes as $key => $class ) {
 if(strstr($class, "comment-author-")||strstr($class, "author-")) {
 unset( $classes[$key] );
 }
 }
 return $classes;
}
add_filter( 'comment_class' , 'lxtx_remove_comment_body_author_class' );
add_filter('body_class', 'lxtx_remove_comment_body_author_class');

 方法3:改为输出用户ID或用户昵称

/**
* 说明:comment_class() 和 body_class() 中输出的 nicename改为 userid 或者 username
* 20170527:优化原Injov版的输出昵称方法
*/
function lxtx_change_comment_or_body_classes($classes, $comment_id){
global $wp_query;
$comment = get_comment( $comment_id );
$user = get_userdata( $comment->user_id );
$comment_author = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
$author = $wp_query->get_queried_object();
$archive_author = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
$archive_author_id = 'author-' . $author->ID;
foreach( $classes as $key => $class ) {
switch( $class ) {
case $comment_author:
// $classes[$key] = 'comment-author-' . sanitize_html_class( $comment->comment_author, $comment->comment_author );
$classes[$key] = 'comment-author-' . sanitize_html_class( $comment->user_id );
break;
case $archive_author:
// $classes[$key] = 'author-' . sanitize_html_class( get_the_author_meta( 'display_name' ), get_the_author_meta( 'display_name' ) );
$classes[$key] = 'author-' . sanitize_html_class( $author->ID );
break;
case $archive_author_id:
$classes[$key] = '';
break;
}
}
    return $classes;
}
add_filter( 'comment_class', 'lxtx_change_comment_or_body_classes', 10, 4 );
add_filter( 'body_class', 'lxtx_change_comment_or_body_classes', 10, 4 );

注:注释的两行代码为替换成昵称,不了解可以不理会~~

# 更多WordPress技巧,请关注「WordPress专题

本文来源:ilxtx