避免暴露你的 WordPress 管理员登录用户名
昨晚在研究评论结构时,网站右键查看源代码,无意间发现自己的管理员用户名被暴露了…
图1 评论中暴露登录用户名
图2 用户页面中也暴露登录用户名
方法1:将作者存档链接中的用户名更改为用户ID
详细方法可以参看之前的文章:https://pdbn.top/1897.html
龙笑天下网已经通过这个方法隐藏存档链接中管理员用户名,没想到管理员用户名还是以另一种方式暴露了… 不过还好,非常隐蔽~~然后查看了下其它几个wordpress的博客,他们也全部中招了(话说,各位博主的管理员登录用户名真的好复杂啊!)!看来是 wordpress的通病了!大家赶紧自查下哦~所以看下步骤2。
注:虽然我们还可以将作者归档链接中的用户名改为用户昵称,但是由于我们更多地使用中文作为昵称,会导致链接地址可能出现一些问题,所以不推荐。
方法2:过滤掉 "Comment-Author-" 和 "Author-"
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()过滤的结果分别是:
// 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"
20161122:才发现其实老外早在2010年就发现了这个漏洞...
/**
*(全网独家)如何正确的避免你的 WordPress 管理员登录用户名被暴露 - 龙笑天下
* http://www.ilxtx.com/further-hide-your-wordpress-admin-username.html
* 说明:直接去掉函数 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');
代码改自:《Small Security Hole in WordPress Comments》
方法2的两段代码二选一哈~ 效果一样!
方法 3:改为输出用户 ID 或用户昵称
11 月 18 日发现inlojv的这个方法也不错,但在改为输出昵称的时候有点错误,对此,我已经优化好了,请放心使用:
/**
*(全网独家)如何正确的避免你的 WordPress 管理员登录用户名被暴露 - 龙笑天下
* https://www.ilxtx.com/further-hide-your-wordpress-admin-username.html
* 说明: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 );
注:注释的两行代码为替换成昵称,不了解可以不理会~~
我现在的策略是,非管理员显示的是用户的 ID,管理员的话,则显示的是......千万不要去查看哦~我现在采用的是方法 2,直接过滤掉了~
延深阅读
我们在上面看到了这个关键字user_nicename,然而通过百度搜索,找到的准确的相关信息聊聊无几... 还是通过科学上网用谷歌搜索是找到了相关的文章:《users - user_login vs. user_nicename - WordPress Development Stack Exchange》。内容如下图:
原文:http://www.ilxtx.com/further-hide-your-wordpress-admin-username.html



