昨晚在研究评论结构时,网站右键查看源代码,无意间发现自己的管理员用户名被暴露了…

如上图,在博主的评论的 class 里看到的 test10 是我本地的测试网站的管理员的用户名!附:《使用 PhpStudy 2016 创建本地 PHP 测试环境及安装 WordPress 的方法》
网站已经使用了《如何将 WordPress 作者存档链接中的用户名改为用户昵称或 ID》这篇文章里的隐藏存档链接中管理员用户名的方法,没想到管理员用户名还能以另一种方式暴露了… 不过还好,非常隐蔽~~然后查看了下其它几个 wordpress 的博客,他们也全部中招了(话说,各位博主的管理员登录用户名真的好复杂啊!)!看来是 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;
if ( empty($comment_depth) )
$comment_depth = 1;
if ( empty($comment_thread_alt) )
$comment_thread_alt = 0;
if ( $comment_alt % 2 ) {
$classes[] = 'odd';
$classes[] = 'alt';
} else {
$classes[] = 'even';
}
$comment_alt++;
// Alt for top-level comments
if ( 1 == $comment_depth ) {
if ( $comment_thread_alt % 2 ) {
$classes[] = 'thread-odd';
$classes[] = 'thread-alt';
} else {
$classes[] = 'thread-even';
}
$comment_thread_alt++;
}
$classes[] = "depth-$comment_depth";
if ( !empty($class) ) {
if ( !is_array( $class ) )
$class = preg_split('#\s+#', $class);
$classes = array_merge($classes, $class);
}
$classes = array_map('esc_attr', $classes);
/**
* Filter the returned CSS classes for the current comment.
*
* @since 2.7.0
*
* @param array $classes An array of comment classes.
* @param string $class A comma-separated list of additional classes added to the list.
* @param int $comment_id The comment id.
* @param object $comment The comment
* @param int|WP_Post $post_id The post ID or WP_Post object.
*/
return apply_filters( 'comment_class', $classes, $class, $comment_id, $comment, $post_id );
}
我们的管理员用户名正是被其中的第 14 行暴露的… 在此,我们只需将这一行中的$user->user_nicename改为$user->user_id即可安全的隐藏管理员的登录名了~ 也隐藏了注册用户的登录用户名了!取而代之显示的是注册用户(包括管理员)的 ID。从此再也不用担心网页中会暴露诸位站长的登录用户名了~
友情提示:此方法是直接修改的 wordpress 的源程序,所以每次更新 wordpress 程序都得进行这样的修改。希望高手能提供更好的方法!
11 月 04 日补充更新:
经过张戈的提醒和龙砚庭博主文章的提示,得到了一个基本完美的解决方案:也就是将 comment_class()函数里输出的 comment-author-test10 这个 class 去掉,也将 body_class()函数里输出的 author-test10 这个类似的 class 去掉。因为这个是通过 functions.php 来解决的,所以不用担心 wordpress 程序升级的问题。方法是,将以下代码加入 functions.php 中,即可完事!
/**
*(全网独家)如何正确的避免你的 WordPress 管理员登录用户名被暴露 – 龙笑天下
* http://www.ilxtx.com/further-hide-your-wordpress-admin-username.html
* 说明:直接去掉函数 comment_class() 和 body_class() 中输出的 "comment-author-" 和 "author-"
*/
function lxtx_comment_body_class($content){
$pattern = "/(.*?)([^>]*)author-([^>]*)(.*?)/i";
$replacement = '$1$4';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('comment_class', 'lxtx_comment_body_class');
add_filter('body_class', 'lxtx_comment_body_class');
comment_class()和 body_class()过滤的结果分别是:
1、comment_class()过滤后的结果如下,去掉了原有 class 里的 comment-author-test10,请和上面的图 1 比较
class=“comment byuser bypostauthor odd alt thread-odd thread-alt depth-1”
2、body_class()过滤后的结果如下,去掉了原有 class 里的 author-test10 和 author-1,请和上面的图 2 比较
class=“archive author logged-in”
延深阅读
我们在上面看到了这个关键字 user_nicename,然而通过百度搜索,找到的准确的相关信息聊聊无几… 还是通过科学上网用谷歌搜索是找到了相关的文章:《users – user_login vs. user_nicename – WordPress Development Stack Exchange》。
(英语蹩脚,请见谅~)大意是说,user_login 是登录名,用来登录的;user_nicename 是在作者存档链接中显示的名称。通常,如果你的登录名中没有特殊的字符,则 user_nicename 和 user_login 登录名是一样的;但,如果你是使用电子邮箱注册的 wordpress,也就是说你的登录名是邮箱地址的话,则 user_nicename 和 user_login 是不同的!
比如,如果你的 user_login 登录名是 [email protected],那么你的 user_nicename 则是 userexample-com。
Boke112提示:
大家修复问题后,为了安全起见,可以考虑修改我们的管理员登录用户名,具体方法请移步:三种方法修改 WordPress 默认用户名 admin
经研究代码发现,这个暴露用户名的只是显示在 li 的 class 里,我们只需要给这个 li 赋予一个 class 值就能堵住这个漏洞。以 Nana 主题为例,打开主题文件 inc\functions\ comment-template.php,大概在第 10 行,找到以下代码:
$tag = 'li';
替换为:
$tag = 'li class="wu"';
即可堵住这个泄露管理员用户名的漏洞,这样就不用修改 WordPress 源程序了。

















桂公网安备 45010502000016号