Published on Nov 10, 2023Olivia WilliamsRead time: 15m3.1K viewer
OTP Verification: What to Check Before the Code Goes Out

OTP Verification: What to Check Before the Code Goes Out

Introduction

Most account security advice treats the one-time password as a finish line. You add a code step, attackers move on, done. The published numbers say something narrower and more useful than that.

Google spent a year studying hijacking attempts against its own accounts with researchers from NYU and UCSD. An SMS code sent to a recovery phone number blocked every automated bot in the sample and 96% of bulk phishing. Against targeted attacks it blocked 76%. The same study found that 38% of users did not have access to their phone when challenged, which is the rest of the story: a control that stops an attack and a slice of your own customers is not free, and the friction lands on support.

Those figures set the agenda here. An OTP is a strong filter for bots and copy-paste phishing, a weak one against someone who has decided to target a specific person, and an operational headache when the message never arrives. The fixes live in three places: what you check before you send a code, how you verify the code you sent, and what you do with the connection that asked for it.

"An SMS code sent to a recovery phone number helped block 100% of automated bots, 96% of bulk phishing attacks, and 76% of targeted attacks." Google Security Blog, New research: How effective is basic account hygiene at preventing hijacking, May 2019.

What an OTP actually proves

A one-time password proves that whoever typed the code can read messages sent to one channel at that moment. That is possession of a channel, not identity. The distinction matters because every serious attack on OTP is an attack on the channel or on the flow around it, and almost none of them are an attack on the code itself.

Six digits is a million combinations. Nobody guesses that at human speed. What attackers do instead is borrow the channel: they port the number, forward the messages, phish the code out of the user, or lean on a support agent who reads it back over the phone. If your threat model is "someone with a leaked password tries my login page", the code does its job. If your threat model includes a person who wants this specific account, the code is one layer among several and has to be treated that way.

The generation methods, and where each one fails

The label "OTP" hides five different mechanisms. HOTP and TOTP share a secret with the server and differ in what advances the code, while the message-based methods move the code across a network and inherit that network's problems. Each one fails in its own way, and the failure decides how much you can rely on it.

HOTP derives a code from a shared secret and a counter that increments with each use. It needs a hardware token or an app, so it cannot be phished by a fake login page in the same way, but it drifts out of sync when codes are generated without being used and it needs a resynchronisation window that widens your acceptance criteria.

TOTP uses a shared secret plus the current time, usually in 30 to 60 second windows. It is the default for authenticator apps and the most common second factor for developer accounts. Its weak point is enrollment: the QR code or seed string is a bearer secret, and anyone who photographs it owns every future code.

SMS codes arrive on the SIM. They are the easiest to use, the hardest to phish in bulk, and the most exposed to telecom level attacks. This is the channel behind the 96% bulk phishing figure, and also the one that fails when the user is roaming, has changed numbers, or is on a carrier with slow delivery.

Email codes are cheap and universal, and they inherit every weakness of the mailbox. If the email account is compromised, the OTP step adds a redirect, not a barrier.

Push approval moves the decision to a device prompt. It is stronger than SMS against phishing, and it created a new attack in notification fatigue, where an attacker spams prompts until the user taps approve to make it stop. Treat push as a different control with a different threat model, not as a faster SMS.

Where SMS codes break: the channel, not the code

The attacks that get through SMS are worth naming, because each one has a different tell.

SIM swap moves the victim's number to a SIM the attacker controls, after which every future code is theirs. The account takeover happens at the carrier, days or hours before your login page sees anything unusual.

Call forwarding and voicemail tricks make the code reachable without touching the SIM. A forwarding rule set through a carrier portal, or a voicemail inbox with a weak PIN, gives an attacker a live feed of every code sent to that number.

SS7 and intercarrier weaknesses let well-resourced actors pull messages in transit. This is rare and expensive, but it is the reason high risk flows should not depend on SMS alone.

Notification previews are the boring one. A code that renders on a locked screen can be read by anyone holding the phone, and a code that appears in a smartwatch notification extends the same exposure.

None of these are visible in your application logs. What is visible is the connection that requests the code and the number it is being sent to, which is why the pre-send checks in the next sections matter more than another round of password complexity rules.

OTP bombing: when the flood is the attack

OTP bombing is an attacker requesting codes for a victim's number over and over, either to bury a legitimate message in noise or to push the victim into approving something to make it stop. It doubles as a fraud cost problem: every request you accept costs money at your SMS provider, and a script that triggers thousands of sends against invalid numbers is a bill, not a login attempt. The economics of that abuse are covered in our guide to SMS pumping fraud, and the mitigation is the same: rate limit by number and by source, then refuse to send to numbers that fail basic checks.

Rate limits belong on three counters, not one. Per phone number, per IP address, and per account. A single per-IP limit is trivially bypassed with a proxy pool, and a single per-number limit still lets an attacker spread requests across thousands of real numbers.

Verification bugs that turn a good code into a free pass

The code is usually fine. The check around it is where accounts fall open.

No attempt cap. If a six digit code can be submitted unlimited times, a script will find it inside the validity window. Cap attempts per code at three to five, then invalidate.

No single use. A code that still works after a successful login is a replayable password. Mark it consumed on first success.

No binding to the session or action. A code issued to reset a password should not satisfy a login, and a code issued to user A should not be accepted for user B. Store the code against the session, the user id, and the purpose.

Long expiry. Fifteen minutes is a long window for a bot with an attempt cap of three, and a short one for a user on a slow connection. Pick a value you can defend, commonly five to ten minutes, and expire the code on any new request.

Silent failure modes. If the endpoint returns different errors for "wrong code" and "expired code", you have handed an attacker an oracle. Return one generic failure and log the detail server side.

Unthrottled resend. Resend is a second send endpoint and needs its own limits, or the bombing problem returns through a different door.

Where the codes leak

Your logs, your URLs and your support desk are where codes show up outside the application, and all three are fixable this week.

Application logs that capture request bodies will capture the code, and so will error trackers and APM traces. A code in a URL parameter ends up in browser history, referrer headers and proxy logs. Support chat is the highest risk of all: an agent who asks a customer to read the code back has just taught every social engineer who calls next week that the trick works. Write the rule down and train to it: support never reads, accepts or confirms a one-time code, and the customer is told to start the flow again if they are stuck.

The takeover path that skips the code

Not every compromise goes through the challenge. Session token theft takes the account after login, where no code is requested. MFA fatigue pushes the user into approving a prompt they did not start. Adversary in the middle phishing proxies the real login page and relays the code as the victim types it. Our breakdown of MFA implementation and bypass attacks covers those flows in detail, and the short version is that an OTP layer needs an inventory of every login path before it means anything.

What to check before you send the code

This is the part you control, and it is cheaper than any additional factor. Every check below is a data scoring and validation call on a number, and four of them cover most of the value.

Number validity against the country you claim. A number that fails structure checks for the stated country will not receive the message, and the request is either a typo or automated noise.

Line type. Mobile numbers can receive SMS. VoIP and virtual numbers often can, but they are the standard tool for bulk account creation, because a virtual number can be bought in minutes and discarded after one SMS. Flagging virtual numbers at signup is one of the highest yield checks you can run, and our guide to spotting VoIP numbers at signup covers the tells.

Disposable and spam history. Numbers recycled from burner apps and previously used for fraud should cost more than a clean mobile number, whether that means a step up, a manual review, or a refusal.

Carrier and country consistency. If the number's carrier and country do not match the connection asking for the code, that is a signal. It is not proof of anything on its own, since people travel and roam, but it belongs in the score. Carrier lookup explains what the carrier field can and cannot tell you.

Number checks reduce the number of wasted sends and catch bulk signup patterns. They do not detect a SIM swapped an hour ago, and nothing in a single API response will tell you that history. Pair them with a carrier change monitor if SIM swap is in your threat model.

Reading the connection at challenge time

The request that asks for a code carries more context than the number does. On the same call you can look at the IP and decide whether this is a request worth paying for.

A datacenter or hosting ASN asking for an SMS code to a residential mobile number is a common bot pattern. So is a Tor exit node. The ASN record is what separates a hosting provider from a mobile carrier, and network intelligence is the lookup that returns it. A commercial VPN is weaker evidence, since plenty of legitimate users browse through one, but it does mean the country from the IP cannot be trusted as a match to the user's real location, which changes how much weight the number and IP consistency check should carry. The signal set is described on the VPN and proxy detection page, and the reasoning behind scoring a connection rather than blocking it is in our guide to risk based authentication with IP signals.

Putting the checks in code

Two calls do the work: one on the number, one on the connection. Both use the v2 endpoints with the key in the Authorization header.

curl -s "https://v2.greipapi.com/score/phone/+120****0143?country_code=US" \
  -H "Authorization: Bearer YOUR_KEY"

The response returns data.is_valid, data.is_disposable, data.carrier, data.risk_level and data.recommendation, where the recommendation is one of accept, review or reject, with data.reasons explaining anything above low risk. The field reference is in the phone scoring docs.

curl -s "https://v2.greipapi.com/lookup/ip/203.0.113.42?params=security,location" \
  -H "Authorization: Bearer YOUR_KEY"

That response includes security.vpn_or_proxy, security.hosting, security.tor and security.is_blacklisted, plus geo.country_code for the consistency check against the number. See the IP lookup docs for the full response shape.

A workable decision order for a signup or login flow:

  • Reject when the number is invalid for the stated country, or already blacklisted.
  • Send the code without friction when the number is a valid mobile, risk is low, and the IP is not hosting or Tor.
  • Send the code but mark the session for review when the number is virtual or disposable, or when the IP is a commercial VPN and the countries disagree.
  • Refuse the send and offer an alternative channel when the connection is a datacenter or Tor exit and the account is new.

Rules you can tune per route live in the dashboard as custom rules, which keeps the list out of your application code as it grows.

A rollout order that does not lock out real users

Ship this in stages, because every check that refuses a send is a support ticket if it is wrong.

Start in log-only mode. Score every request, write the decision to your events table, and send the code as you do today. After a week you have your own false positive rate instead of someone else's benchmark.

Then refuse only the cheapest cases: invalid numbers and blacklists. These almost never belong to a real customer.

Then add the review tier for virtual numbers and country mismatches. Watch the review queue, not the block count.

Finally, add the connection checks to new accounts first, where a refusal costs an abandoned signup rather than a locked-out paying customer.

Keep a break glass path. A verified customer who cannot receive a code needs a documented alternative that does not involve support reading a code aloud, typically a timed identity check or a callback to the number on file.

How to measure whether it worked

Track the funnel per stage: codes requested, codes delivered, codes verified, and logins completed. Delivery failure and verification failure move for different reasons, and mixing them hides the problem.

On the fraud side, count the abuse you stop rather than the traffic you block: wasted sends to invalid numbers, accounts created per number, and challenges triggered per IP range. If your SMS bill drops and your verification rate holds, the checks are working. If verification drops with the bill, you have started refusing real users and the thresholds need a second look.

Conclusion

An OTP is a possession check on a channel, and its strength depends on who is attacking. It stops bots and bulk phishing almost completely, as the Google study shows, and it lets a quarter of targeted attacks through. The remaining risk sits in the channel, the verification logic and the support flow, not in the six digits.

The cheapest improvement is upstream: know what the number is before you pay to send to it, and know what the connection looks like before you trust the answer. A valid mobile on a clean residential connection deserves a code and no friction. A virtual number from a datacenter IP deserves a review, and the difference is one call you can make before the SMS goes out.

FAQs

What is OTP verification?

OTP verification is the step where a service sends a short, single use code to a channel the user controls and asks them to type it back. It proves control of that channel at that moment, which is why it works well against automated attacks and less well against a targeted attacker who can reach the same channel.

Is SMS OTP still safe?

It is still effective against bots and bulk phishing, blocking 100% and 96% of those attacks in Google's published research. Against targeted attacks the same study measured 76%, so SMS is a reasonable default for low risk flows and a weak sole factor for high value accounts. Add a stronger factor for admin, payment and recovery paths.

What is OTP bombing and how do you stop it?

OTP bombing is repeatedly requesting codes for someone else's number to flood their messages or push them into approving a prompt. Stop it with rate limits per number, per IP and per account, by refusing sends to numbers that fail validity and line type checks, and by alerting on send volume that spikes without a matching rise in completed logins.

How do I check a phone number before sending an OTP?

Call a phone scoring endpoint with the number and its country code, then read validity, line type, disposable status, carrier and the risk recommendation. Reject invalid and blacklisted numbers, and route virtual or disposable numbers to review rather than sending a code you expect to be abused.

Can phone number scoring detect a SIM swap?

No single lookup can see that history. A scoring call tells you the current carrier, line type and risk for the number, which helps when the request contradicts it, but detecting a recent swap needs a carrier change record over time. Treat the lookup as one input and monitor for carrier changes separately if SIM swap is in your threat model.

Will IP checks block legitimate users on VPNs?

They can, if you block on the VPN flag alone. Most VPN users are ordinary customers, and the useful signal is the combination: a commercial VPN with a matching number and country is different from a datacenter ASN with a mismatched country. Score the pair and reserve refusals for hosting and Tor connections on new accounts.

Get started

Start protecting your business today

Our service is trusted by thousands of businesses worldwide.

  • 1,000 requests during trial
  • Cancel anytime

Related Articles