Skip to content
VPNDetection

Step up risky logins

Ask for a second factor only when a login looks risky: from a VPN or Tor exit, a hosting network, or a country the account hasn't used.

Asking for a second factor on every login protects accounts but annoys the people who own them. Asking only when a login looks unusual keeps much of the protection and removes much of the friction. This guide scores each login by its address and asks for more only when the score calls for it.

How it works

After a person enters the right password, your server looks up the login's address and compares it with what you know about the account, such as the countries it usually logs in from. If nothing is unusual, the login completes. If something is, you ask for a second factor before the session starts.

Before you begin

  • Somewhere to store each account's recent login countries.
  • A second factor you can ask for, such as an authenticator code or an emailed code.

1. Score the login

Node.js
export async function loginRisk(account, ip) {
    const r = await client.lookup(ip);
    const country = r.location?.country;
    const reasons = [];
    if (r.isTor) {
        reasons.push('tor');
    }
    if (r.isVpn) {
        reasons.push('vpn');
    }
    if (r.asn?.type === 'hosting') {
        reasons.push('hosting network');
    }
    if (country && account.countries.length > 0 && !account.countries.includes(country)) {
        reasons.push('new country');
    }
    return { country: country, reasons: reasons };
}

2. Decide what to ask for

  • No reasons: complete the login.
  • One reason: ask for the account's second factor.
  • Tor, or a new country together with a VPN: ask for the second factor and email the account owner about the login.

After a login completes, add its country to the account's list, so the next login from that country isn't flagged.

3. Handle a failed lookup

If the lookup fails, apply the policy you use without VPNDetection rather than blocking the login. For most products that means completing the login, or asking for the second factor if the account has one. Record that the login wasn't scored, so you can review it if the account later reports a problem.

4. Avoid locking out travelers

A new country is often a trip, not an attack. Ask for a second factor rather than refusing the login, and keep the countries an account has used, so the same trip doesn't ask again on every login. If you show account holders their recent logins, include the country, so they can spot one they don't recognize.

5. Test it

Log in to a test account from a VPN in a different country. The login should ask for the second factor, and the email should arrive.

With a paid plan

Attackers who take over accounts often use residential and mobile proxies, because they look like ordinary home and phone connections. A paid plan flags both (is_resproxy, is_mobproxy), and adds the city, so you can compare regions instead of whole countries.

Next steps

To screen accounts when they're created, see Screen VPN and proxy sign-ups.