Skip to content
CORS Write-up Intermediate

How to Programmatically Harvest the OTP

A CORS misconfiguration on an API allowed a malicious website to add an attacker-controlled phone number to a victim's account and complete OTP verification automatically. The attacker could then link their own phone number to the victim's account, potentially enabling account takeover or bypassing OTP-based protections.

Greg Gibson Follow

Published on infosecwriteups.com

When I submit a vulnerability on a Bug Bounty program, I typically seek to build an exploit that automates as much of the Proof of Concept as possible to:

Greg Gibson, on infosecwriteups.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

The target was an API that let a user add a phone number to their account. The flow is the familiar one: the user submits a phone number, the application texts a six digit One Time Password (OTP) to that number, and the user enters the OTP into the website to prove the number belongs to them.

That flow does two jobs. It validates that users enter a number they control, and it makes a Cross-Site Request Forgery (CSRF) attack harder, because an attacker has to make two distinct API calls where the second needs a dynamic value that is produced by the first and delivered out of band.

The researcher had already identified a Cross Origin Resource Sharing (CORS) vulnerability on that API. The write-up is about turning that CORS weakness into a working, automated CSRF exploit that survives the phone validation step.

Setup

The exploit needs a few pieces of infrastructure:

  1. A phone number the attacker controls that can receive the OTP. The researcher used a Google Voice number, with all incoming SMS forwarded to a Twilio number. Google Voice itself needed to validate the Twilio number by sending it an OTP, which the researcher harvested through the Twilio API to complete the forwarding. Google's own instructions for setting up SMS forwarding are at https://support.google.com/voice/answer/9182115.
  2. A third, attacker-controlled API that can retrieve the OTP from Twilio. The researcher built this on AWS Lambda behind API Gateway, using the Twilio REST API rather than the Python SDK.
  3. A page to host the malicious client-side JavaScript. The researcher simply hosted it in S3 as an effectively blank webpage containing only the script.
  4. A trial account with Twilio, and familiarity with AWS API Gateway, to reproduce the setup.

Before settling on Google Voice, the researcher spent a few hours trying to receive the OTP on AWS Pinpoint and Twilio long codes (a normal 10 digit US phone number) without success. The assumption is that certain checks limit this B2B type messaging, so that only actual mobile numbers are engaged. The target application had no issue sending the OTP to a Google Voice number.

Finding it

The starting point was the CORS vulnerability on the phone-number API. The interesting question was whether it could be turned into a full CSRF exploit despite the OTP step. The OTP is delivered out of band, so a naive CSRF attempt stalls at the second call: the attacker does not know the code.

The researcher's approach was to make the victim's browser drive the whole flow against the attacker's own phone number, and to retrieve the OTP programmatically from the attacker's telephony setup quickly enough to submit it in the second call. The motivation for automating as much of the proof of concept as possible was threefold: to make it 100% clear the exploit can do what is claimed, to simplify reproduction for both the triage team and the company (if reproduction only requires navigating to a supplied URL, there is no confusion), and because building things is a good way to learn new tech.

Exploitation

The architecture has the victim's browser run the malicious script, which talks to the target application and to the attacker's API; the attacker's API talks to Twilio, which receives the forwarded SMS from Google Voice.

From the original on infosecwriteups.com

The flow is:

  1. The victim visits a webpage running the malicious script.
  2. JavaScript sends an HTTPS POST request with a JSON body including the attacker's phone number to the target application. This triggers a preflight OPTIONS request but is ultimately allowed via the Access-Control-Allow-Origin headers echoing the request origin.
  3. The target application sends an OTP to the attacker's Google Voice number, which then forwards that SMS to Twilio.
  4. JavaScript sends an HTTPS GET request to the attacker's API, which triggers Lambda to harvest the code from Twilio and return it in the HTTPS response.
  5. JavaScript sends the final HTTPS POST request to the target application with the attacker's phone number and OTP to validate.

Step 1: Victim loads the malicious page

The client-side code was hosted in S3 as an effectively blank webpage containing only the script. The victim only has to visit that page.

Step 2: Add the attacker's phone number

The cross-origin JSON request triggers a preflight OPTIONS request, but it is ultimately allowed because the Access-Control-Allow-Origin headers echo the request origin. That echoing is the CORS vulnerability: the attacker's page is permitted to make the call and read the response.

Step 3: OTP is delivered to the attacker

The target application sends an OTP to the attacker's Google Voice number, which forwards the SMS to Twilio.

Step 4: Harvest the OTP

From the original on infosecwriteups.com

Inside the Lambda, the code only needs to poll Twilio for any messages, parse them to see whether the OTP code has been received, and return an appropriate response (either 200 or 404) depending on the result. The researcher chose the Twilio REST API over the Python SDK because the integration is simple.

Two design decisions matter here. First, the Lambda deletes EVERY message EVERY time, because you cannot assume only OTP messages will be delivered to Twilio if forwarding is configured on an existing number. Second, deleting everything ensures subsequent exploit instances are not sorting through a huge message queue or finding old, formerly valid OTP codes and returning them as valid. At scale this would not work: with multiple victims exploited simultaneously you would need a better way to find the right OTP code, which is outside the scope of this proof of concept.

Step 5: Validate the number with the OTP

The final call submits the attacker's phone number and the harvested OTP to validate.

Making the calls run in order

The researcher is not a JavaScript developer and describes the code as hacked together well enough to work. Two keys made it work: forcing JavaScript to block and wait so the three API calls occur in order and only when ready, rather than in the asynchronous free-for-all typical JS allows; and recursion in the poll_twilio function to reattempt the call to the attacker API until the OTP code is successfully returned. The recursion lets the OTP work its way from the target application to Twilio and prevents the exploit from breaking if there are delays along the way.

Each API call was set up as a standalone function that calls the next function only when the request has completed successfully, so the first API call must complete before the second is triggered. The published snippets are the basic working code with some things removed and changed for simplicity and readability, and are meant as a baseline for anyone duplicating the effort.

Root cause

The underlying flaw is the CORS misconfiguration: the Access-Control-Allow-Origin headers echo the request origin, so the attacker's page is authorised to make the JSON POST and read the response. The OTP step is a defence against CSRF in the sense that the second call needs a dynamic value delivered out of band, but it only defends if the attacker cannot receive that value. By pointing the phone number at a Google Voice number that forwards to Twilio, and by harvesting the code through an attacker-controlled API, the researcher removed that obstacle. The phone validation therefore did not stop the attack; it only added steps.

Impact

With this automation, an attacker can add the attacker's phone number to a victim's account by getting the victim to visit a page. The write-up notes that other vulnerabilities are currently mitigated with the use of OTP and could be reliably exploited with this automation. The exploit depends on the victim visiting the attacker's page, on the target accepting the attacker's Google Voice number, and on the attacker's telephony and Lambda setup working quickly enough. The proof of concept is not built for simultaneous exploitation of multiple victims, as noted above.

Fix

The write-up does not describe a fix or a change made by the program. The defect to address is the CORS configuration that echoes the request origin and thereby allows arbitrary origins to make and read the phone-number API calls; the origin should be restricted to trusted values rather than reflected.

Variants to test

  1. Any API that echoes the request origin in Access-Control-Allow-Origin while accepting JSON POSTs with credentials. A positive result is a preflight OPTIONS response that reflects your origin and a subsequent POST whose response your page can read.
  2. Phone or email validation flows where the second call needs a code from the first. A positive result is that you can receive the code on a number or address you control (for example a forwarding service) and complete the second call from the victim's browser.
  3. Flows where the attacker controls the destination of the out-of-band message. A positive result is the code arriving somewhere you can poll programmatically, as with the Google Voice to Twilio forwarding here.
  4. Harvesting endpoints that poll a message queue. A positive result is a 200 with the code when it has arrived and a 404 otherwise, with old messages deleted so stale codes are not returned.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

A company ran a website where you could attach a phone number to your account. To prove the number was really yours, the site sent a six digit code by text message, and you typed that code back into the site. This is a very common pattern: it stops people from typing in someone else's number, and it also makes a certain kind of attack harder, because the attacker would need to know a code that arrives on a phone they do not own.

The researcher had already found a weakness in how that website's API handled requests coming from other websites. That weakness is called a CORS misconfiguration. On its own it lets a malicious page talk to the API. The question this write-up answers is whether that weakness could be turned into a full attack that also gets past the text message code. The answer was yes, by pointing the phone number at a number the attacker controls, and by fetching the code automatically, fast enough to type it back in.

This article walks through that attack from zero. You do not need to know any security jargon beforehand: every term is explained the first time it appears, and there is a glossary at the end.

The pieces you need to understand first

Before the steps make sense, here are the basic building blocks, in everyday terms.

  • A website and a browser. Your browser (Chrome, Firefox, Safari) downloads pages and runs small programs inside them. Those programs are written in a language called JavaScript. JavaScript running on a page can send requests to servers and read the replies, within limits the browser enforces.
  • A server and an API. A server is a computer somewhere that answers requests. An API is the set of addresses and rules a server offers so that programs, rather than people clicking buttons, can ask it to do things. Think of a restaurant: the menu is the API, the kitchen is the server, and your order is a request. The website's own pages use the same API that the researcher later abused.
  • A request and a response. When a browser (or a script) asks a server for something, that is a request. The server's answer is a response. Requests have methods: GET usually means "give me something", POST usually means "here is some data, do something with it".
  • JSON. A simple text format for structured data, used in the body of many requests. It looks like {"key": "value"}. The phone-number API accepted JSON in POST requests.
  • Origin. The combination of protocol, host and port that a page came from, for example https://example.com. Two pages with different origins are treated by the browser as strangers to each other, even if they look similar.
  • Cross-origin requests and CORS. By default a browser will not let a page from one origin read the response of a request to a different origin. That rule protects users. CORS (Cross Origin Resource Sharing) is the mechanism a server uses to say "actually, pages from these other origins are allowed to read my responses". The server does this with response headers, notably Access-Control-Allow-Origin. If a server echoes back whatever origin asked, instead of a fixed list of trusted ones, then any website on the internet is allowed in. That is the CORS vulnerability here.
  • Preflight. For certain requests, such as a POST carrying JSON to another origin, the browser first sends a small automatic request called a preflight, using the OPTIONS method, to ask the server whether the real request is allowed. If the server's answer permits it, the browser sends the real request. If the server echoes the asking origin in Access-Control-Allow-Origin, the preflight succeeds and the real request goes through.
  • CSRF. Cross-Site Request Forgery. The idea: a victim is logged in to a site, visits a malicious page, and that page makes the victim's browser send requests to the site. Because the browser attaches the victim's login automatically, the site thinks the request came from the victim. The attacker never sees the victim's password; they just borrow the browser's trust.
  • OTP. One Time Password: the six digit code sent by text message. It is valid once, and it arrives out of band, meaning through a different channel (the phone network) than the one the attack uses (the browser). That is exactly why it is a defence: an attacker sitting on the web side cannot see it.
  • Phone validation flow. The user submits a number, the site texts a code to that number, the user types the code back. Two separate API calls, where the second needs a value produced by the first.
  • Lambda and API Gateway. AWS Lambda is a service where you upload a small piece of code and it runs when called, without you managing a server. API Gateway is the front door that gives that code a web address. Together they make a small, cheap, attacker-controlled API.
  • Twilio and Google Voice. Twilio is a service that lets programs send and receive phone calls and text messages through an API. Google Voice is Google's phone service; one of its features is forwarding incoming text messages to another number. The researcher chained the two: Google Voice received the code, forwarded it as a text to the Twilio number, and Twilio's API let a program read it.
  • Long code. A normal ten digit US phone number, as opposed to a short code used for bulk messaging.
  • S3. Amazon's simple file storage service. You can put a file in it and get a web address for it, which makes it a quick way to host a page.
  • Recursion. A function that calls itself. Used here to keep retrying until the code arrives.
  • Asynchronous. JavaScript normally does not wait for a request to finish before running the next line; it starts the request and moves on. That is fine for most pages but useless for an attack that must do three things strictly in order.

Why automate the proof of concept at all

The researcher explains the motivation directly. When submitting a vulnerability to a bug bounty program (a scheme where companies pay people for reporting security flaws), the aim is to automate as much of the proof of concept as possible, for three reasons:

  1. It makes it 100% clear the exploit can do what is claimed.
  2. It simplifies reproduction for both the triage team (the people who first check a report) and the company. If reproducing only requires navigating to a URL you supply, there is no confusion about steps.
  3. Building things is a good way to learn new tech.

That third reason matters for a beginner: the researcher openly says they are not a JavaScript developer and hacked the code together well enough to work. The point is not elegant code, it is a demonstration that works.

The setup

The attack needs a few pieces of infrastructure before any code is written.

  1. A phone number the attacker controls that can receive the OTP. The researcher used a Google Voice number, with all incoming SMS forwarded to a Twilio number. Setting up that forwarding was itself a small puzzle: Google Voice wanted to validate the Twilio number by texting it a code, in the same way the target site wanted to validate the Google Voice number. The researcher harvested that code through the Twilio API to complete the forwarding. Google's own instructions are at https://support.google.com/voice/answer/9182115.
  2. A third, attacker-controlled API that can retrieve the OTP from Twilio. This was built on AWS Lambda behind API Gateway, using the Twilio REST API rather than the Python SDK, because the integration is simple.
  3. A page to host the malicious client-side JavaScript. This was simply hosted in S3 as an effectively blank webpage containing only the script.
  4. A trial account with Twilio, and familiarity with AWS API Gateway, to reproduce the setup.

Before settling on Google Voice, the researcher spent a few hours trying to receive the OTP on AWS Pinpoint and Twilio long codes (a normal ten digit US phone number) without success. The assumption is that certain checks limit this B2B type messaging, so that only actual mobile numbers are engaged. The target application, by contrast, had no issue sending the OTP to a Google Voice number.

Finding it

The starting point was the CORS vulnerability on the phone-number API. The interesting question was whether it could be turned into a full CSRF exploit despite the OTP step. The OTP is delivered out of band, so a naive CSRF attempt stalls at the second call: the attacker does not know the code.

The researcher's approach was to make the victim's browser drive the whole flow against the attacker's own phone number, and to retrieve the OTP programmatically from the attacker's telephony setup quickly enough to submit it in the second call.

Exploitation

The architecture has the victim's browser run the malicious script, which talks to the target application and to the attacker's API; the attacker's API talks to Twilio, which receives the forwarded SMS from Google Voice.

From the original on infosecwriteups.com

The diagram shows that arrangement: the victim's browser on one side, the target application and the attacker's API in the middle, and Google Voice and Twilio on the attacker's side receiving the text message.

The flow is:

  1. The victim visits a webpage running the malicious script.
  2. JavaScript sends an HTTPS POST request with a JSON body including the attacker's phone number to the target application. This triggers a preflight OPTIONS request but is ultimately allowed via the Access-Control-Allow-Origin headers echoing the request origin.
  3. The target application sends an OTP to the attacker's Google Voice number, which then forwards that SMS to Twilio.
  4. JavaScript sends an HTTPS GET request to the attacker's API, which triggers Lambda to harvest the code from Twilio and return it in the HTTPS response.
  5. JavaScript sends the final HTTPS POST request to the target application with the attacker's phone number and OTP to validate.

Step 1: Victim loads the malicious page

The client-side code was hosted in S3 as an effectively blank webpage containing only the script. The victim only has to visit that page. Nothing is clicked, nothing is typed: loading the page is enough, because the script starts running on its own.

Step 2: Add the attacker's phone number

The cross-origin JSON request triggers a preflight OPTIONS request, but it is ultimately allowed because the Access-Control-Allow-Origin headers echo the request origin. That echoing is the CORS vulnerability: the attacker's page is permitted to make the call and read the response.

In plain words: the victim's browser, while sitting on the attacker's page, asks the target site to attach the attacker's phone number to the victim's account. The browser first asks permission (the preflight), the target site says yes to whoever asks, and the real request follows. The request is made from the victim's browser, which is what makes this a CSRF.

Step 3: OTP is delivered to the attacker

The target application sends an OTP to the attacker's Google Voice number, which forwards the SMS to Twilio. From the target site's point of view everything is normal: it texted the number it was given. The trick is that the number belongs to the attacker, and the attacker has arranged for the message to end up somewhere a program can read it.

Step 4: Harvest the OTP

From the original on infosecwriteups.com

Inside the Lambda, the code only needs to poll Twilio for any messages, parse them to see whether the OTP code has been received, and return an appropriate response (either 200 or 404) depending on the result. To poll means to ask repeatedly, on a timer, "has anything arrived yet?". The researcher chose the Twilio REST API over the Python SDK because the integration is simple.

Two design decisions matter here. First, the Lambda deletes EVERY message EVERY time, because you cannot assume only OTP messages will be delivered to Twilio if forwarding is configured on an existing number. Second, deleting everything ensures subsequent exploit instances are not sorting through a huge message queue or finding old, formerly valid OTP codes and returning them as valid. At scale this would not work: with multiple victims exploited simultaneously you would need a better way to find the right OTP code, which is outside the scope of this proof of concept.

Step 5: Validate the number with the OTP

The final call submits the attacker's phone number and the harvested OTP to validate.

Making the calls run in order

The researcher is not a JavaScript developer and describes the code as hacked together well enough to work. Two keys made it work: forcing JavaScript to block and wait so the three API calls occur in order and only when ready, rather than in the asynchronous free-for-all typical JS allows; and recursion in the poll_twilio function to reattempt the call to the attacker API until the OTP code is successfully returned. The recursion lets the OTP work its way from the target application to Twilio and prevents the exploit from breaking if there are delays along the way.

Each API call was set up as a standalone function that calls the next function only when the request has completed successfully, so the first API call must complete before the second is triggered. The published snippets are the basic working code with some things removed and changed for simplicity and readability, and are meant as a baseline for anyone duplicating the effort.

Why does order matter so much? Because each step depends on the one before it. You cannot ask for the code before the site has sent it, and you cannot submit the code before you have it. In ordinary JavaScript all three requests would start at once and the second and third would fail. Blocking and waiting, plus retrying, turns three racing requests into a reliable sequence.

Root cause

The underlying flaw is the CORS misconfiguration: the Access-Control-Allow-Origin headers echo the request origin, so the attacker's page is authorised to make the JSON POST and read the response. The OTP step is a defence against CSRF in the sense that the second call needs a dynamic value delivered out of band, but it only defends if the attacker cannot receive that value. By pointing the phone number at a Google Voice number that forwards to Twilio, and by harvesting the code through an attacker-controlled API, the researcher removed that obstacle. The phone validation therefore did not stop the attack; it only added steps.

Impact

With this automation, an attacker can add the attacker's phone number to a victim's account by getting the victim to visit a page. The write-up notes that other vulnerabilities are currently mitigated with the use of OTP and could be reliably exploited with this automation. The exploit depends on the victim visiting the attacker's page, on the target accepting the attacker's Google Voice number, and on the attacker's telephony and Lambda setup working quickly enough. The proof of concept is not built for simultaneous exploitation of multiple victims, as noted above.

Fix

The write-up does not describe a fix or a change made by the program. The defect to address is the CORS configuration that echoes the request origin and thereby allows arbitrary origins to make and read the phone-number API calls; the origin should be restricted to trusted values rather than reflected.

Variants to test

If you want to look for similar issues yourself, these are the shapes to look for. Each one is described with what a positive result looks like.

  1. Any API that echoes the request origin in Access-Control-Allow-Origin while accepting JSON POSTs with credentials. A positive result is a preflight OPTIONS response that reflects your origin and a subsequent POST whose response your page can read.
  2. Phone or email validation flows where the second call needs a code from the first. A positive result is that you can receive the code on a number or address you control (for example a forwarding service) and complete the second call from the victim's browser.
  3. Flows where the attacker controls the destination of the out-of-band message. A positive result is the code arriving somewhere you can poll programmatically, as with the Google Voice to Twilio forwarding here.
  4. Harvesting endpoints that poll a message queue. A positive result is a 200 with the code when it has arrived and a 404 otherwise, with old messages deleted so stale codes are not returned.

Glossary

  • API: the set of addresses and rules a server offers so programs can ask it to do things, like a menu in a restaurant.
  • API Gateway: an AWS service that gives a piece of code a web address and forwards requests to it.
  • Asynchronous: JavaScript's default behaviour of starting a request and moving on without waiting for the answer.
  • Bug bounty program: a scheme where companies pay people for reporting security flaws.
  • CORS (Cross Origin Resource Sharing): the mechanism, using response headers, by which a server tells the browser which other origins may read its responses.
  • Cross-origin request: a request from a page on one origin to a server on a different origin.
  • CSRF (Cross-Site Request Forgery): tricking a logged-in victim's browser into sending requests to a site, so the site thinks the victim made them.
  • GET: a request method meaning "give me something".
  • Google Voice: Google's phone service, used here to receive the OTP and forward it as a text message.
  • JSON: a simple text format for structured data, written as key and value pairs.
  • Lambda: an AWS service that runs a small piece of uploaded code when it is called, with no server to manage.
  • OPTIONS: the request method used for a preflight, asking the server whether a cross-origin request is allowed.
  • Origin: the combination of protocol, host and port a page came from; pages with different origins are treated as strangers.
  • OTP (One Time Password): a code valid once, here six digits sent by text message.
  • Out of band: delivered through a different channel than the one under attack, here the phone network rather than the browser.
  • POST: a request method meaning "here is some data, do something with it".
  • Preflight: the automatic OPTIONS request a browser sends before certain cross-origin requests, to check permission.
  • Recursion: a function calling itself, used here to retry until the code arrives.
  • Request / response: what a browser or script asks a server, and the server's answer.
  • S3: Amazon's simple file storage service, used here to host the malicious page.
  • Triage team: the people who first check a bug bounty report before it reaches the company.
  • Twilio: a service whose API lets programs send and receive text messages and calls.
  • 200 / 404: HTTP status codes meaning "here is your result" and "not found", used here to signal whether the code had arrived.
  • Access-Control-Allow-Origin: the response header that tells the browser which origin may read the response; echoing the asking origin is the flaw here.

More by Greg Gibson

View all

3 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

CORS misconfiguration

The lesson behind this report.

A permissive cross-origin policy lets another site read responses with the user's credentials.