Cracking Encrypted Credit Card Numbers Exposed By API
A GraphQL API exposed encrypted credit card numbers for logged-in users, and because the encryption used no per-user salt, identical card numbers produced identical ciphertexts. An attacker with access to a victim's account could build a lookup table to recover full card numbers, though the CV2 was missing.
Published on craighays.com
I found an API that exposed encrypted credit card numbers. Here’s how I cracked them to reveal the full card details.
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.
While hacking on a private bug bounty program, I found a GraphQL endpoint that exposed far more information about logged-in users than it should have done. By playing with the 'about me' GraphQL API request I was able to guess and retrieve all the logged-in user's stored values present in the database, not just those disclosed by the web app.
Using these guessed parameters, I was able to retrieve all credit cards added to a user's account. This included the cardholder's name, their registered address, the expiry date of the card, the last 4 digits of the card, the type of card, and an encrypted version of the full credit card number.
The application allowed users to store up to 10 payment cards on their account for easier checkout when buying products and services. For the company to use them in future, I knew they must be stored in a decryptable format. The challenge I set myself before submitting my report was to crack the encryption algorithm and view the raw card numbers.
Context
The target was a private bug bounty program. The product let logged-in users store up to 10 payment cards on their account so that checkout for products and services was quicker. The feature I abused was the 'about me' GraphQL query, which returns profile data for the logged-in user.
Credit card numbers, briefly
Credit and debit card numbers follow an internationally recognised standard. A Payment Card Number (PCN) can be anywhere from 8 to 19 digits long, and the first 6 to 8 digits identify the issuing bank. This is the Issuer identification number (IIN). All American Express cards start with 34 or 37, all Visa cards start with a 4, and so on.
There is also a range of card numbers reserved for testing that will never be assigned to real customers. (That is another bug you can test for while bug bounty hunting: can I make a purchase with a test-only card?) My personal favourite is the Visa number 4111 1111 1111 1111. Stripe has a list of test cards you can use anywhere, and a quick search for "test payment card numbers" finds more.
Finally, the majority of credit card numbers include a validity verification number using the Luhn algorithm. With this, the final digit of the card number is a check digit for all of the preceding numbers. For 4111 1111 1111 111X the check digit value is 1. For 4111 1111 1111 112X the check digit value is 9. Anyone can generate all possible card numbers for a given bank provided they know the prefix, the length, and whether the number uses the Luhn algorithm. That is why you also need the card expiry date and CV2 numbers to make purchases.
Setup
I created a new user account and added my favourite Visa credit card number, 4111 1111 1111 1111.
Finding it
When I queried the API I received a 16 digit numerical, encrypted version of my card. I deleted the card, then added it again. When I queried the API again I received the same encrypted value. Now I knew the results were statically generated using a fixed algorithm.
Exploitation
Attempt 1: reversing the algorithm
The API allowed me to submit up to 10 card numbers in a single request. I uploaded 10 numbers in a row starting from 4111 1111 1111 1129, 4111 1111 1111 1137, and so on, and tried to spot a pattern. I could see the signs of a pattern emerging, but I could not get my head around it.
The last 4 digits of the response were always the plaintext of the original: 1111, 1129, 1137, and so on. The first 2 digits were always 54 for Visa, 53 for American Express, and so on, so it looked like the first digit of the card IIN became the second digit of the response, and the responses always started with a 5.
Because I knew the card type from the API response and also the last 4 digits, I already had 6 of the 16 digits required to rebuild any card number uploaded to the application server.
I kept pushing it for another couple of hours, trying more and more combinations, trying to get as close to 0000 0000 0000 0000 as possible, but only Luhn validating card numbers worked and I still could not get my head around the pattern. I was close, but not close enough.
Attempt 2: rainbow tables
At 3 am I woke up with an idea. If the same encrypted value is generated for all users who submit the same card number, I do not need to reverse the algorithm, I just need a rainbow table of all of the possible encrypted values mapped to raw card numbers.
A rainbow table is a lookup table that contains encrypted values and their plaintext counterparts. These are usually created for hashes such as MD5s, but in this case I needed one for a 2-way encrypted string, one that could be encrypted and decrypted with a custom algorithm.
The next morning I created a second user account and entered my go-to card number. When I queried it via the API, I got a match. Both users received the exact same encrypted 16 digit string.
This means that the card numbers are encrypted without the use of an encryption salt. In cryptography, salt is random data that is added to an input to prevent duplicate inputs from producing duplicate cryptographic outputs. Without adding salt, if someone knows the input for a given output, any matching outputs for other users can be mapped back to the known input. With a different salt for each user, the outputs will always be different, even for the same card numbers.
User 1 with salt 5000: 4111 1111 1111 1111 + 5000 -> 5477 2356 1143 1111
User 2 with salt 7992: 4111 1111 1111 1111 + 7992 -> 5436 4343 8711 1111
Putting it all together
Now I knew:
- The application stores credit card numbers in a 2-way encrypted 16 digit string
- The application uses the same algorithm for all users
- The application does not use a different cryptographic salt for each user, so two user accounts with the same card number will generate the same encrypted number
- I can upload 10 card numbers at a time and then query all of the encrypted card numbers
I then wrote a Python script that calculated all the valid card numbers for each bank, then used the application server to generate the cryptographic outputs for each one. I turned their application server into my own payment card number encryption tool.
Submitting my generated credit card numbers 10 at a time, I then issued the 'about me' GraphQL query to pull back the encrypted values. The tool would iterate through all possible combinations per bank, building up a rainbow table as it went. As this was only a proof of concept, I killed the script after a few hundred cycles to not waste resources on the application server. (Operations teams do not like it when you crash things and fill up log space.)
With my rainbow table proof of concept ready, I was able to decrypt any credit card number stored inside the application database for any logged-in user. As the 'about me' query also stored the card expiry date, registered owner's name, and registered address, the only thing missing to make real payments was the CV2 number.
Root cause
The API returned an encrypted version of the full card number to the end user, and that encryption was deterministic across all users: the same card number always produced the same 16 digit ciphertext, with no per-user salt. Because the ciphertext space was small and predictable (valid card numbers for a given bank can be enumerated from the IIN, the length and the Luhn check digit), the application itself could be used as an encryption oracle to build a lookup table.
Impact
The biggest hurdle to declaring this a P1 or Critical vulnerability was the fact that it only worked for logged-in users. To exploit it I needed to take over the victim's account. The site had less-than-perfect authentication protections in place and no MFA was possible at the time of testing. A brute force attack, leaked shared password attack, or phishing attack would all have been viable to gain access, but it was one step too far for this bug bounty program. Instead, we agreed on a lesser, but still nice severity level.
Fix
The easiest thing to do was to simply stop returning the encrypted card numbers as part of the API query response. Adding a per-user salt was a nice-to-have, but once the encrypted number is no longer exposed to end users, the risk is effectively mitigated.
Variants to test
- Over-returning GraphQL fields. Send the 'about me' style query with guessed field names for sensitive stored values (payment methods, addresses, identifiers). A positive result is any field returning data the web app never displays.
- Deterministic ciphertext across accounts. Store the same test value (for example
4111 1111 1111 1111) on two accounts and compare the returned encrypted strings. Identical output means no per-user salt and the value is a candidate for a rainbow table. - Encryption oracle via bulk input. Submit the maximum number of values the API accepts per request and read back the ciphertexts. A positive result is a stable, repeatable mapping between input and output that can be enumerated offline.
- Test-only card numbers. Try purchasing with a reserved test card number. A positive result is a successful checkout that should have been rejected.
What happened, in plain words
A security researcher was taking part in a private bug bounty programme. A bug bounty programme is an arrangement where a company invites independent testers to look for weaknesses in its systems and pays them for reporting what they find, instead of the company being caught out by criminals. "Private" means only invited testers can take part.
The product he was testing let people who had an account save their payment cards, so that buying things later took fewer steps. The researcher found that one of the ways the site's software asked the server for the user's own profile information, a request called "about me", was returning far more than the web page ever showed. By guessing the names of extra pieces of information, he could pull back everything the database held about that user, including their saved cards.
Among that data was an encrypted copy of the full card number. Encrypted means scrambled with a mathematical recipe so that it looks like meaningless digits, and can be turned back into the original only by someone who knows the recipe. The researcher's goal was to unscramble it. He could not work out the pattern directly, so instead he used the site itself as a scrambling machine: he fed it card numbers he already knew, noted the scrambled result each time, and built a giant lookup list. Once the list was big enough, any scrambled card number he saw could be matched back to the real number behind it.
This article walks through the whole story from the beginning, explaining each idea as it comes up. You only need to be comfortable using a browser.
The setup: what the product did
The target was a private bug bounty programme. The product let logged-in users store up to 10 payment cards on their account so that checkout for products and services was quicker. The feature the researcher abused was the "about me" GraphQL query, which returns profile data for the logged-in user.
Two terms there need unpacking.
API. An API (Application Programming Interface) is the set of doors a website's own pages use to talk to its server. When you click "my profile", your browser does not fetch a finished page from scratch; it sends a small request to the server asking for your details, and the server sends the details back, usually as structured text. Think of a restaurant: the menu and the waiter are the API. You do not walk into the kitchen yourself; you ask through the waiter, and the kitchen decides what to send back. If the waiter will fetch anything on the menu, including dishes the restaurant never meant to offer you, that is the kind of problem here.
GraphQL. GraphQL is a particular style of API. Instead of having a different address for every kind of request, GraphQL has one address, and the request itself says which pieces of information you want, written out by name. It is like a form where you tick boxes: name, address, email. The server is supposed to return only the boxes you ticked. The danger is that if the server has more boxes available than the web page ever shows, a curious user can tick boxes the page never offered.
Credit card numbers, briefly
Before the attack makes sense, you need to know how card numbers are built, because the researcher's method depends entirely on that structure.
Credit and debit card numbers follow an internationally recognised standard. A Payment Card Number (PCN) can be anywhere from 8 to 19 digits long, and the first 6 to 8 digits identify the issuing bank. This is the Issuer identification number (IIN). All American Express cards start with 34 or 37, all Visa cards start with a 4, and so on.
In plain terms: the opening digits tell you which bank and which card brand the number belongs to. That is not a secret; it is published. So if you know a number is a Visa, you already know it starts with a 4.
There is also a range of card numbers reserved for testing that will never be assigned to real customers. (That is another bug you can test for while bug bounty hunting: can I make a purchase with a test-only card?) My personal favourite is the Visa number 4111 1111 1111 1111. Stripe has a list of test cards you can use anywhere, and a quick search for "test payment card numbers" finds more.
Test numbers exist so that developers can try their checkout without moving real money. If a live shop accepts one, that is itself a flaw worth reporting, because it means the shop is not checking whether the card is real.
Finally, the majority of credit card numbers include a validity verification number using the Luhn algorithm. With this, the final digit of the card number is a check digit for all of the preceding numbers. For 4111 1111 1111 111X the check digit value is 1. For 4111 1111 1111 112X the check digit value is 9.
The Luhn algorithm is a simple arithmetic check. You run a small calculation over all the digits except the last one, and the answer tells you what the last digit must be. It catches typing mistakes, in the same way that a checksum on a parcel tracking number catches a mistyped digit. It is not a security measure: anyone can compute it. That is why you also need the card expiry date and CV2 numbers to make purchases. The CV2 is the short code printed on the card itself, which is not stored in the same place as the number.
The consequence, and this is the key point for the whole attack: anyone can generate all possible card numbers for a given bank provided they know the prefix, the length, and whether the number uses the Luhn algorithm. The set of possible numbers is large but finite and completely predictable.
Setup: creating an account
The researcher created a new user account and added his favourite Visa credit card number, 4111 1111 1111 1111.
That is worth pausing on. He did not need special tools, a stolen password or access to the server room. He needed an ordinary account, the same kind any customer can make.
Finding it: the same input, the same output
When he queried the API he received a 16 digit numerical, encrypted version of his card. He deleted the card, then added it again. When he queried the API again he received the same encrypted value. Now he knew the results were statically generated using a fixed algorithm.
"Statically generated" means the result does not change between attempts. If you put the same card number in twice, you get the same scrambled digits out twice. That is a strong hint about how the scrambling works, and it is the first crack in the problem. A salted scrambling method would produce a different-looking result each time, even for the same input, so that an observer could not compare two results and learn anything.
Exploitation
Attempt 1: reversing the algorithm
The API allowed him to submit up to 10 card numbers in a single request. He uploaded 10 numbers in a row starting from 4111 1111 1111 1129, 4111 1111 1111 1137, and so on, and tried to spot a pattern. He could see the signs of a pattern emerging, but he could not get his head around it.
The idea here is straightforward: if you feed in a series of numbers that differ by a small, known amount, and you look at the scrambled results, any regularity in the results tells you something about the recipe. It is like listening to a locked music box play ten notes and trying to work out which lever moves which note.
The last 4 digits of the response were always the plaintext of the original: 1111, 1129, 1137, and so on. The first 2 digits were always 54 for Visa, 53 for American Express, and so on, so it looked like the first digit of the card IIN became the second digit of the response, and the responses always started with a 5.
"Plaintext" means the original, readable form of the data, as opposed to the scrambled form. So the scrambled output was leaking the tail end of the real card number in the clear.
Because he knew the card type from the API response and also the last 4 digits, he already had 6 of the 16 digits required to rebuild any card number uploaded to the application server.
Six out of sixteen is not the whole number, but it is a large head start. Combined with the fact that the first digits are predictable from the card brand, the unknown middle shrank considerably.
He kept pushing it for another couple of hours, trying more and more combinations, trying to get as close to 0000 0000 0000 0000 as possible, but only Luhn validating card numbers worked and he still could not get his head around the pattern. He was close, but not close enough.
"Luhn validating" means the number passes the check-digit test described earlier. Only Luhn-validating card numbers worked, and the underlying pattern stayed out of reach.
Attempt 2: rainbow tables
At 3 am he woke up with an idea. If the same encrypted value is generated for all users who submit the same card number, he did not need to reverse the algorithm; he just needed a rainbow table of all of the possible encrypted values mapped to raw card numbers.
A rainbow table is a lookup table that contains encrypted values and their plaintext counterparts. These are usually created for hashes such as MD5s, but in this case he needed one for a 2-way encrypted string, one that could be encrypted and decrypted with a custom algorithm.
Two ideas are being contrasted here.
A hash is a one-way scramble: you can turn a password into a jumble, but you cannot turn the jumble back into the password. To attack a hash you pre-compute the jumbles of millions of likely passwords and store them in a table, then look up the jumble you found. That table is a rainbow table.
Two-way encryption is different: the data is meant to be recoverable, because the company needs to read the card number later to charge it. So the recipe can run in both directions. The researcher's insight was that even though he did not have the recipe, he could build the same kind of lookup table, because the site would happily scramble numbers for him.
The next morning he created a second user account and entered his go-to card number. When he queried it via the API, he got a match. Both users received the exact same encrypted 16 digit string.
This is the decisive observation. Two separate accounts, two separate people, same card number, same scrambled output. It means the scrambling does not take the user into account at all.
This means that the card numbers are encrypted without the use of an encryption salt. In cryptography, salt is random data that is added to an input to prevent duplicate inputs from producing duplicate cryptographic outputs. Without adding salt, if someone knows the input for a given output, any matching outputs for other users can be mapped back to the known input. With a different salt for each user, the outputs will always be different, even for the same card numbers.
A useful comparison: salt is like adding a pinch of a different coloured dye to each batch of paint. Even if two batches start from the same base colour, the dyed results look different, so you cannot tell by looking that they came from the same source. Without the dye, identical inputs give identical outputs, and anyone who knows one input can recognise its output anywhere else.
User 1 with salt 5000: 4111 1111 1111 1111 + 5000 -> 5477 2356 1143 1111
User 2 with salt 7992: 4111 1111 1111 1111 + 7992 -> 5436 4343 8711 1111
Line by line:
- The first line shows what would happen if each user had their own salt. User 1's card number is combined with the salt value 5000, and the result is one particular scrambled string.
- The second line shows User 2 with the same card number but a different salt, 7992, producing a completely different scrambled string.
- The point is that with per-user salt, the two outputs cannot be compared. The researcher would have had no way to know that both users held the same card, and no way to build a lookup table that worked across accounts.
In the real application, no such salt was in use, which is why both accounts produced the same output.
Putting it all together
Now he knew:
- The application stores credit card numbers in a 2-way encrypted 16 digit string
- The application uses the same algorithm for all users
- The application does not use a different cryptographic salt for each user, so two user accounts with the same card number will generate the same encrypted number
- He can upload 10 card numbers at a time and then query all of the encrypted card numbers
Each of those four facts is a piece of the puzzle. The first says the data is recoverable in principle. The second and third say the scrambling is predictable and shared. The fourth says the site will do the scrambling for him, in bulk, on demand.
He then wrote a Python script that calculated all the valid card numbers for each bank, then used the application server to generate the cryptographic outputs for each one. He turned their application server into his own payment card number encryption tool.
Python is a programming language commonly used for quick automation. The script did two jobs: generate candidate card numbers, and ask the site to scramble each one. The site was doing the hard work; the script was just a very fast, very patient assistant.
Submitting his generated credit card numbers 10 at a time, he then issued the "about me" GraphQL query to pull back the encrypted values. The tool would iterate through all possible combinations per bank, building up a rainbow table as it went. As this was only a proof of concept, he killed the script after a few hundred cycles to not waste resources on the application server. (Operations teams do not like it when you crash things and fill up log space.)
"Proof of concept" means a small demonstration that shows the problem is real, without going all the way to causing damage. Stopping early is a matter of professional courtesy: the aim is to prove the weakness, not to overload someone else's systems or fill their logs with noise.
With his rainbow table proof of concept ready, he was able to decrypt any credit card number stored inside the application database for any logged-in user. As the "about me" query also stored the card expiry date, registered owner's name, and registered address, the only thing missing to make real payments was the CV2 number.
That last sentence is the impact in one line: the only thing missing to make real payments was the CV2 number. The CV2, printed on the physical card, was the one piece the database did not hand over.
Root cause
The API returned an encrypted version of the full card number to the end user, and that encryption was deterministic across all users: the same card number always produced the same 16 digit ciphertext, with no per-user salt. Because the ciphertext space was small and predictable (valid card numbers for a given bank can be enumerated from the IIN, the length and the Luhn check digit), the application itself could be used as an encryption oracle to build a lookup table.
"Deterministic" simply means the same input always gives the same output. "Ciphertext" is the scrambled form of the data. An "encryption oracle" is any system that will scramble or unscramble things for you on request; if it does so predictably, you can use it as a tool rather than needing to know the recipe yourself.
Impact
The biggest hurdle to declaring this a P1 or Critical vulnerability was the fact that it only worked for logged-in users. To exploit it he needed to take over the victim's account. The site had less-than-perfect authentication protections in place and no MFA was possible at the time of testing. A brute force attack, leaked shared password attack, or phishing attack would all have been viable to gain access, but it was one step too far for this bug bounty programme. Instead, we agreed on a lesser, but still nice severity level.
Severity ratings like P1 or Critical are how bug bounty programmes rank findings, from minor annoyances up to emergencies. Here the finding was serious but not top of the scale, because it required an attacker to already be inside someone's account.
Some of those terms deserve a plain explanation:
- Authentication is how a site checks that you are who you say you are, usually with a password.
- MFA (multi-factor authentication) means asking for a second proof, such as a code from your phone, in addition to the password. With MFA in place, a stolen password alone is not enough to get in.
- Brute force means trying huge numbers of password guesses automatically.
- Phishing means tricking someone into handing over their password, often with a fake login page.
So the attack was real, but it needed a foothold first. That is why the programme settled on a lower severity than the researcher might have hoped for.
Fix
The easiest thing to do was to simply stop returning the encrypted card numbers as part of the API query response. Adding a per-user salt was a nice-to-have, but once the encrypted number is no longer exposed to end users, the risk is effectively mitigated.
In other words: the simplest cure was to stop handing the scrambled card number to the browser at all. If the data never leaves the server, there is nothing for an attacker to collect and match. Adding salt would have been a good extra layer, but it was not the essential fix.
Variants to test
If you are learning to hunt for bugs, these are the same ideas generalised so you can look for them elsewhere.
- Over-returning GraphQL fields. Send the "about me" style query with guessed field names for sensitive stored values (payment methods, addresses, identifiers). A positive result is any field returning data the web app never displays. In everyday terms: tick boxes on the form that the page never showed you, and see whether the server still fills them in.
- Deterministic ciphertext across accounts. Store the same test value (for example
4111 1111 1111 1111) on two accounts and compare the returned encrypted strings. Identical output means no per-user salt and the value is a candidate for a rainbow table. - Encryption oracle via bulk input. Submit the maximum number of values the API accepts per request and read back the ciphertexts. A positive result is a stable, repeatable mapping between input and output that can be enumerated offline. If the site will scramble things for you reliably, you can build your lookup list at your own pace.
- Test-only card numbers. Try purchasing with a reserved test card number. A positive result is a successful checkout that should have been rejected. This checks whether the shop is validating cards at all.
Glossary
- API: the set of requests a website's pages use to talk to its server; the waiter between you and the kitchen.
- Authentication: the process of proving you are who you claim to be, usually with a password.
- Brute force attack: trying enormous numbers of guesses automatically until one works.
- Bug bounty programme: an arrangement where a company invites testers to find and report weaknesses in exchange for payment.
- Check digit: a final digit calculated from the others so that typing mistakes can be detected.
- Ciphertext: data in its scrambled, unreadable form.
- CV2: the short security code printed on a payment card, used as an extra check when paying.
- Deterministic: always producing the same output for the same input.
- Encryption: scrambling data with a mathematical recipe so that only someone with the right knowledge can read it.
- Encryption oracle: any system that will scramble or unscramble data for you on request, which can be abused to build lookup tables.
- GraphQL: a style of API where the request names the exact pieces of data it wants.
- Hash: a one-way scramble that cannot be reversed, used to store things like passwords.
- IIN (Issuer identification number): the leading digits of a card number that identify the issuing bank and brand.
- Luhn algorithm: the arithmetic check that produces a card number's final check digit.
- MFA (multi-factor authentication): requiring a second proof, such as a phone code, in addition to a password.
- PCN (Payment Card Number): the full number printed on a payment card, 8 to 19 digits long.
- Phishing: tricking someone into revealing credentials, often with a fake login page.
- Plaintext: data in its original, readable form.
- Proof of concept: a small demonstration that a weakness is real, without causing harm.
- Python: a programming language often used for quick automation scripts.
- Rainbow table: a pre-built lookup list mapping scrambled values back to their originals.
- Salt: random data added to an input so that identical inputs produce different scrambled outputs.
- Severity rating (P1, Critical): a programme's ranking of how serious a finding is.
- Two-way encryption: scrambling that is designed to be reversible, because the data must be read again later.