WordPress是大多数个人建站者使用的平台,技术好的用各种高科技手段攻击/破解你的网站,本文就给管理员登录成功或短时间内多次登录失败加一个发邮件通知站长的功能
能起到基本的预警效果,先看看我测试的邮件。原版只有成功和失败就发邮件,增加了只有管理员账号成功才发邮件、登录失败增加了尝试失败次数及时间间隔(代码中可以自己设定)
连续3次登录失败

登录成功

把下面代码放到主题的functions.php中即可,我是子比主题,放在func.php里。
登录成功发送邮件代码
function wp_login_notify()
{
date_default_timezone_set('PRC');
$admin_email = get_bloginfo ('admin_email');
$to = $admin_email;
$subject = '您的网站登录提醒';
$message = '<p>您好!您的网站【' . get_option("blogname") . '】有登录!</p>' .
'<p>请确定是您自己的登录,以防别人攻击!登录信息如下:</p>' .
'<p>登 录 名:' . $_POST['username'] . '<p>' .
//下面这行是密码,怕泄密可以删除这行
'<p>登录密码:' . $_POST['password'] . '<p>' .
'<p>登录时间:' . date("Y-m-d H:i:s") . '<p>' .
'<p>登 录 IP:' . $_SERVER['REMOTE_ADDR'] . '<p>';
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
//下面填管理员账号,如果所有账号登录都发邮件就把if这行去掉
if ($_POST['username']=='管理员账号'){
wp_mail( $to, $subject, $message, $headers );
}
}
add_action('wp_login', 'wp_login_notify');
//管理员登录发邮件通知站长结束
指定时间间隔内多次登录失败发送邮件
function wp_login_failed_notify()
{
date_default_timezone_set('PRC');
// 定义允许的尝试次数和时间间隔
$maxAttempts = 3;
$timeInterval=60;//间隔60秒
// 获取当前时间戳
$currentTime = time();
// 获取当前IP地址
$ip = $_SERVER['REMOTE_ADDR'];
// 更新IP地址的尝试次数和最后尝试时间,同时获取设定的间隔时间
updateAttempts($ip, $currentTime,$timeInterval);
$attempts = getAttempts($ip);
$admin_email = get_bloginfo ('admin_email');
$to = $admin_email;
$subject = '您的网站登录错误警告';
$message = '<p>您好!您的网站【' . get_option("blogname") . '】有登录错误!</p>' .
'<p>请确定是您自己的登录失误,以防别人攻击!登录信息如下:</p>' .
'<p>登 录 名:' . $_POST['username'] . '<p>' .
'<p>登录密码:' . $_POST['password'] . '<p>' .
'<p>登录时间:' . date("Y-m-d H:i:s") . '<p>' .
'<p>登 录 IP:' . $ip . '<p>'.
'<p>尝试次数:'.$attempts.'<p>';
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
// 检查是否可以再次尝试
if ($attempts >= $maxAttempts and $_POST['username']=='管理员账号') {
// 如果尝试次数超过最大值且是管理员账号,则发送邮件提醒
wp_mail( $to, $subject, $message, $headers );
} else {
//这里不做任何处理
}
}
add_action('wp_login_failed', 'wp_login_failed_notify');
// 更新IP地址的尝试次数、最后尝试时间
function updateAttempts($ip, $currentTime,$timeInterval) {
$attempts = file_get_contents("attempts.txt");
if ($attempts === false) {
$attempts = [];
} else {
$attempts = json_decode($attempts, true);
}
if (!isset($attempts[$ip])) {
$attempts[$ip] = [
'count' => 1, // 如果不存在该IP的记录,则初始化为1
'last_attempt' => $currentTime
];
} else {
if (($currentTime - $attempts[$ip]['last_attempt']) <= $timeInterval) {
$attempts[$ip]['count']++; // 在时间间隔内,增加尝试次数
$attempts[$ip]['last_attempt'] = $currentTime; // 更新最后尝试时间
} else {
// 超过时间间隔,重置尝试次数和最后尝试时间
$attempts[$ip] = [
'count' => 1,
'last_attempt' => $currentTime
];
}
}
file_put_contents("attempts.txt", json_encode($attempts));
}
// 获取IP地址的尝试次数
function getAttempts($ip) {
$attempts = file_get_contents("attempts.txt");
if ($attempts === false) {
return 0;
}
$attempts = json_decode($attempts, true);
if (!isset($attempts[$ip])) {
return 0;
}
return $attempts[$ip]['count'];
}
//管理员账号指定时间内登录错误达指定次数后发邮件通知站长结束

