本页介绍了如何使用 reCAPTCHA 检测和防范账号盗用 (ATO)。
当攻击者使用从数据泄露获得的凭据(也称为密码转储)发送登录 API 端点的请求时,通常会发生 ATO 攻击。即使密码转储来自一个不相关的网站,这种类型的攻击也可成功,因为人们倾向于在多个账号之间重用密码。这种类型的攻击不会影响采用适当的密码安全性(例如,使用密码管理器)的用户。
准备工作
检测和防范 ATO
借助 reCAPTCHA,您可以通过以下任一选项检测并阻止 ATO:
使用“我不是机器人”复选框
向您的网站添加“我不是机器人”复选框是在一定程度上防范 ATO 的最快速、最简单方法,且无需集成诸如短信或电子邮件验证之类的附加功能。攻击者必须付出一定的代价才能突破此保护,对于某些网站来说,这个方案就已足够。
在您的网页上添加“我不是机器人”复选框。
以下代码是一个受复选框保护的登录页面的实际示例:
function onSuccess(token) {
// The token is included in the POST data in the g-recaptcha-response
// parameter. The backend must create an Assessment with the token
// and verify the token is valid.
console.log(token);
}<form id="loginForm" action="?" method="POST">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<div class="g-recaptcha" data-sitekey="reCATCHA_sitekey"
data-action="account_login" data-callback="onSuccess"></div>
</form><script src="https://www.google.com/recaptcha/enterprise.js" async defer></script>
您可以通过单击代码窗口右上角的 <> 图标,在 JSFiddle 中尝试此代码。
<html>
<head>
<title>Account Login - Checkbox</title>
<script src="https://www.google.com/recaptcha/enterprise.js" async defer></script>
<script>
function onSuccess(token) {
// The token is included in the POST data in the g-recaptcha-response
// parameter. The backend must create an Assessment with the token
// and verify the token is valid.
console.log(token);
}
</script>
</head>
<body>
<form id="loginForm" action="?" method="POST">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<div class="g-recaptcha" data-sitekey="6LeAkOgUAAAAACcy3uY6N9H9SJMS27n3Zx2OOnYK"
data-action="account_login" data-callback="onSuccess"></div>
</form>
</body>
</html>使用得分和自定义挑战
如需防范 ATO,请使用基于 reCAPTCHA 得分的密钥,并使用多重身份验证 (MFA) 质询,例如采用一次性代码 (OTP) 的电子邮件和短信质询。
如需使用基于得分的键和自定义挑战,请考虑以下选项:
安装基于得分的密钥,以检测大规模 ATO 中的自动化操作。
如需在网页上安装基于得分的网站密钥,请参阅在网页上安装基于得分的网站密钥。
如需在移动应用上安装基于得分的密钥,请参阅将 reCAPTCHA 与 Android 应用集成或将 reCAPTCHA 与 iOS 应用集成。
使用 MFA 在登录时验证账号所有权或执行敏感操作。
如需了解详情,请参阅配置多重身份验证。
根据您的用例,您可以单独使用 MFA 或将其与基于得分的密钥一起使用。例如,您可能只想将 MFA 质询用于低于特定阈值的得分,以减少负面影响。
以下示例展示了如何在登录场景中集成基于得分的密钥。
function submitForm() {
grecaptcha.enterprise.ready(function() {
grecaptcha.enterprise.execute(
'reCAPTCHA_site_key', {action: 'account_login'}).then(function(token) {
document.getElementById("token").value = token;
document.getElementByID("loginForm").submit();
});
});
}<form id="loginForm" action="?" method="POST"> Username: <input type="text" name="username"/><br/> Password: <input type="password" name="password"/><br/> <input type="hidden" id="token" name="recaptcha_token"/> <button onclick="submitForm()">Login</button> </form>
<script src="https://www.google.com/recaptcha/enterprise.js" async defer></script>
您可以通过单击代码窗口右上角的 <> 图标,在 JSFiddle 中尝试此代码。
<html>
<head>
<title>Account Login - Score</title>
<script src="https://www.google.com/recaptcha/enterprise.js" async defer></script>
<script>
function submitForm() {
grecaptcha.enterprise.ready(function() {
grecaptcha.enterprise.execute(
'reCAPTCHA_site_key', {action: 'account_login'}).then(function(token) {
document.getElementById("token").value = token;
document.getElementByID("loginForm").submit();
});
});
}
</script>
</head>
<body>
<form id="loginForm" action="?" method="POST">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="hidden" id="token" name="recaptcha_token"/>
<button onclick="submitForm()">Login</button>
</form>
</body>
</html>后续步骤
- 如需了解其他账号保护功能,请参阅用户账号保护功能。