Skip to content
Account takeover Write-up Intermediate

How I Gained Access To A Finance Company’s Accounts (Session Hijacking)

The API generated session tokens with low entropy, making them predictable. An attacker could brute-force valid session tokens and gain access to other users' accounts.

Talha Karakumru Follow

Published on medium.com

In a web application security test, I gained access to hundreds of accounts that have belonged to a finance company. The vulnerability occurred due to an insecure session token generation.

Talha Karakumru, on medium.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. 6 min read.

Context

During a web application security test against a finance company, Talha Karakumru found a session hijacking vulnerability that let him reach hundreds of accounts belonging to the company. The weakness was not in the login logic itself but in how the API generated session tokens: the tokens had a low entropy value, meaning they could be predicted by analysing many values produced by the same function. The author has anonymised the write-up for the company's privacy, so the host, paths and service names shown here are placeholders.

The feature involved is the login endpoint. The API accepted a JSON body containing a service object and, inside it, params with a username and a password. On a successful login the API generated a context, which is the session token, and returned it in the HTTP response.

Setup

The researcher had been doing web application security research and penetration testing for three years and had tested hundreds of web applications and APIs, with a particular interest in authentication and authorisation flaws. For this test he was given a test user account. The main tool named in the report is Burp Suite, and its Repeater was used to resend the login request manually multiple times. No versions are given.

The attack relies on being able to log in to obtain a token and to send requests with an arbitrary context value.

Finding it

At first the researcher did not notice that the session tokens were not actually random. Looking at a few tokens, he assumed they were hash values of some data and skipped the analysis. Later he went back to the token generation process and used Burp Suite's Repeater to resend the login request many times by hand. That is when the pattern appeared: some parts of the tokens stayed the same, while the other parts were either incremental or drawn from a small set of values.

Exploitation

Step 1: Look at the login request

The login request sends a JSON body with a context field and a service object whose params carry the credentials:

HTTP
POST /xyz/abc/service HTTP/1.1
Host: vulnerable.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.7,tr;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Content-Length: 166
Connection: close

{
  "context": "0000-0000-0000-0000",
  "service": {
    -- snippet --
    "params": {
      "username": "99999999",
      "password": "12345678",
      -- snippet --
    }
  }
}

The body carries a context field, a service object, and inside it params with the username and password. The response to a successful login carries the generated context:

The API Response Of The Login Request From the original on medium.com

The API response of the login request.

Step 2: Collect many tokens and split them

By logging in thousands of times the researcher obtained a large set of tokens. A sample of the output looks like this:

Text
obtained a lot of session tokens by logging in thousands of times...
51000 1L3IR 1V0B 01
51000 1L3IR 1UYR 00
51000 1L3IR 1UYU 00
52000 1L3IR 1V0S 00
52000 1L3IR 1V0V 00
51000 1L3IR 1V0B 00
51000 1L3IR 1UZK 00
52000 1L3IR 1V05 00
52000 1L3IR 1V01 00
51000 1L3IR 1V0M 00
52000 1L3IR 1V02 00
51000 1L3IR 1UZJ 00
52000 1L3IR 1V0W 00
52000 1L3IR 1V0A 00
52000 1L3IR 1V04 00
51000 1L3IR 1V0E 00
51000 1L3IR 1V1A 01
51000 1L3IR 1V1A 00
52000 1L3IR 1V24 00
51000 1L3IR 1V3V 00
52000 1L3IR 1V3U 00
52000 1L3IR 1V4F 00
51000 1L3IR 1V4X 00
51000 1L3IR 1V6J 01
51000 1L3IR 1V50 00
52000 1L3IR 1V4M 00
51000 1L3IR 1V6D 00
51000 1L3IR 1V6E 00
51000 1L3IR 1V61 00
52000 1L3IR 1V5D 00
51000 1L3IR 1V6C 00
52000 1L3IR 1V5W 00
52000 1L3IR 1V5M 00
52000 1L3IR 1V6D 00
51000 1L3IR 1V6J 00
52000 1L3IR 1V6G 00
52000 1L3IR 1V6J 00
51000 1L3IR 1V7X 00
...
52000 1L3IQ ZLF9 00
52000 1L3IQ ZLEY 00...

Each token splits into four parts. Taking 51000 1L3IR 1V0B 01 as the example:

  1. Part #1: 51000
  2. Part #2: 1L3IR
  3. Part #3: 1V0B
  4. Part #4: 01

The behaviour of each part:

Part Observed values Behaviour
#1 51000 or 52000 one of two values
#2 1L3IR, 1L3IQ incremental, changes infrequently
#3 1V0B, 1UYR, ZLF9 incremental, changes every time
#4 00 or 01 one of two values

Because part #2 changed so rarely, there was no need to brute force it. Part #3 changed on every token, but its length was not enough to prevent brute force. Part #3 has four characters, and each character can take 36 values (10 digits plus 26 letters), giving 36 x 36 x 36 x 36 = 36⁴ = 1,679,616 permutations. The researcher then asked whether it was really necessary to brute force from 0000 to ZZZZ. If a token such as 52000 1L3IQ ZLKI 00 is obtained, one can guess that 52000 1L3IQ ZLKH 00 has already been taken by another user, because ZLKH comes before ZLKI. The limit is that one cannot know how many users logged in after the token was obtained, though hundreds and even millions may have logged in before it.

Step 3: Validate tokens without a service

The researcher sent a context with an empty service object:

HTTP
POST /xyz/abc/service HTTP/1.1
Host: vulnerable.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.7,tr;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Content-Length: 43
Connection: close

{
  "context": "520001L3ITZS1C00",
  "service": {}
}

The API's answer distinguishes the two cases. A valid context without any service returns UNDEFINED_SERVICE. An invalid context returns INVALID_SESSION. That difference distinguishes a valid context from an invalid one.

Logging In With A Valid Context From the original on medium.com

Logging in with a valid context.

Logging In With An Invalid Context From the original on medium.com

Logging in with an invalid context.

Step 4: Write the exploit

The exploit has four parts.

  1. Obtain a session token, using the test user provided for the security test.
The Method That Attempts To Log In. Then, Returns The Obtained Context (Session Token) From the original on medium.com

The method that attempts to log in and then returns the obtained context (session token).

  1. Split the token into four parts.
From the original on medium.com
  1. Find the indexes of each character in part #3.
From the original on medium.com
  1. Brute force the session tokens.
The Method That Checks If A Context is Valid or Invalid From the original on medium.com

The method that checks whether a context is valid or invalid.

The Method That Handles The Brute Force Attack From the original on medium.com

The method that handles the brute force attack.

Step 5: Run it

With the oracle in place, the brute force found multiple session tokens in just a few seconds.

The Output Of The Exploit From the original on medium.com

The output of the exploit.

The Final Exploit Code From the original on medium.com

The final exploit code.

Root cause

The session token was generated with low entropy. The token is assembled from parts that are either fixed, drawn from a two-value set, or incremented sequentially, so a token issued to one user can be predicted from tokens issued to others. The API also returned different responses for a valid and an invalid context, UNDEFINED_SERVICE versus INVALID_SESSION, which lets an attacker tell whether a guessed token is valid. The report does not describe any rate limiting, token expiry or other control that would have slowed the brute force down.

Impact

By guessing session tokens, an attacker can obtain valid tokens and access the accounts they belong to. In this test the researcher gained access to hundreds of accounts belonging to the finance company. The report does not state what data or actions those accounts exposed, nor whether the company paid a bounty.

Fix

Variants to test

  1. Log in several times and compare tokens character by character. A positive result is any position that stays constant, cycles between a small set of values, or increments between requests.
  2. Split tokens into blocks and check each block for incrementing sequences. A positive result is a block that advances by one between consecutive logins.
  3. Send a known-valid token with an empty service object and an invalid token with the same shape. A positive result is two different error codes that reveal validity.
  4. Brute force the incremental block around a known token, testing values just below it. A positive result is a valid token for another account returned within a short run.
  5. Repeat the collection with a second account and compare the two token streams. A positive result is streams that overlap or interleave, showing a shared sequence rather than per-user randomness.

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. 13 min read.

What happened, in plain words

A security researcher named Talha Karakumru tested a web application belonging to a finance company. During that test he found a way to get into hundreds of accounts that belonged to the company. The problem was the piece of text the server hands out after you log in, the session token. That text was not random enough, so it could be guessed.

To follow the rest of this article you only need to know how to use a browser. Everything else is explained as it comes up.

A few words first, because they appear constantly:

  • An API is the part of a website that answers requests from programs rather than drawing pages for people. Think of a restaurant: the dining room is the website you see, the kitchen is the API, and the waiter carries your order back and forth. When your browser loads a page, it is often the API that supplies the data behind it.
  • A request is a message a program sends to a server, asking it to do something. A response is the server's reply.
  • A session token (called a context in this report) is a short string of characters the server gives you after you log in. Your browser sends it back with every later request so the server knows who you are, without asking for your password again. It works like the wristband you get at a festival: show it at the gate and you get back in without buying another ticket.
  • Entropy means unpredictability. A token with high entropy is like a lottery number nobody can guess. A token with low entropy is like a queue ticket: the numbers come out in order, so anyone holding ticket 41 can work out that ticket 42 exists and probably belongs to someone.
  • Session hijacking is what happens when an attacker obtains someone else's session token and uses it to act as that person. No password is needed, because the token is the proof of identity.
  • Brute force means trying many possibilities one after another until one works, like spinning the dials on a combination lock through every combination.
  • Burp Suite is a tool security testers use to watch and modify the traffic between a browser and a server. Its Repeater feature lets you take one captured request and send it again and again, changing small details each time.
Photo by FLY:D on Unsplash From the original on medium.com

Photo by FLY:D on Unsplash

The author introduces himself: he has spent three years doing web application security research and penetration testing, has tested hundreds of web applications and APIs, and particularly enjoys finding flaws in how an application handles authentication (proving who you are) and authorisation (deciding what you are allowed to do).

In this test he found a session hijacking vulnerability caused by insecure token generation. The company's name and the real addresses are hidden, so the host and paths you will see are placeholders.

Let's Talk About The Vulnerability

When a user logs in, the API gives that user a session token. In this application the token had a low entropy value. Low entropy means the value can be predicted by looking at thousands of values produced by the same function. Going back to the queue ticket: if every ticket is one higher than the last, then seeing your own ticket tells you a great deal about everyone else's.

At first the researcher did not notice that the tokens were not actually random. He looked at a few of them and assumed they were hash values of some data. Hashes look random, which is exactly why a predictable token can pass for one at a glance. He skipped the analysis at that moment.

Later he went back to the token generation process. He used Burp Suite's Repeater to resend the login request many times by hand. That is when the pattern appeared: some parts of the tokens stayed the same, and the other parts were either just incremental (each one one step higher than the last) or had only a few different values.

The write-up has been anonymised for the company's privacy.

Analysing The Login

Including the login, the API required service data in a JSON format. JSON is a way of writing data as text using curly braces, colons and commas, so that both sides can read it unambiguously. The login also needed params containing a username and a password.

HTTP
POST /xyz/abc/service HTTP/1.1
Host: vulnerable.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.7,tr;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Content-Length: 166
Connection: close

{
  "context": "0000-0000-0000-0000",
  "service": {
    -- snippet --
    "params": {
      "username": "99999999",
      "password": "12345678",
      -- snippet --
    }
  }
}

Line by line, in plain words:

  • POST /xyz/abc/service HTTP/1.1 is the first line of the request. POST is the method, meaning "I am sending data for you to act on". /xyz/abc/service is the address on the server being asked. HTTP/1.1 is the version of the language the request is written in.
  • Host: vulnerable.com says which website the request is for. The real name has been replaced.
  • Accept, Accept-Language, Accept-Encoding tell the server what kind of reply the client can handle: JSON or text, which languages, and whether compressed data is acceptable.
  • Content-Type: application/json;charset=utf-8 says the body of the request is JSON text.
  • X-Requested-With: XMLHttpRequest is a header many web applications add to mark a request as coming from their own page's scripts rather than from a typed address.
  • Content-Length: 166 says how many bytes the body contains, so the server knows where the body ends.
  • Connection: close asks the server to close the connection after replying.
  • The blank line separates the headers from the body.
  • The body is the JSON. context is the field where the session token goes; here it holds a placeholder value. service selects which operation is being asked for, and inside it params carries the username and password. The -- snippet -- lines mark parts of the real request that were removed from the write-up.

Then the API generated a context, which was a session token, and sent the token back in an HTTP response.

The API Response Of The Login Request From the original on medium.com

The API Response Of The Login Request

The screenshot shows the reply to the login request, with the token the server returned.

Digging The Token Generation Process

After obtaining and analysing a lot of session tokens, the researcher realised a few things:

Text
obtained a lot of session tokens by logging in thousands of times...
51000 1L3IR 1V0B 01
51000 1L3IR 1UYR 00
51000 1L3IR 1UYU 00
52000 1L3IR 1V0S 00
52000 1L3IR 1V0V 00
51000 1L3IR 1V0B 00
51000 1L3IR 1UZK 00
52000 1L3IR 1V05 00
52000 1L3IR 1V01 00
51000 1L3IR 1V0M 00
52000 1L3IR 1V02 00
51000 1L3IR 1UZJ 00
52000 1L3IR 1V0W 00
52000 1L3IR 1V0A 00
52000 1L3IR 1V04 00
51000 1L3IR 1V0E 00
51000 1L3IR 1V1A 01
51000 1L3IR 1V1A 00
52000 1L3IR 1V24 00
51000 1L3IR 1V3V 00
52000 1L3IR 1V3U 00
52000 1L3IR 1V4F 00
51000 1L3IR 1V4X 00
51000 1L3IR 1V6J 01
51000 1L3IR 1V50 00
52000 1L3IR 1V4M 00
51000 1L3IR 1V6D 00
51000 1L3IR 1V6E 00
51000 1L3IR 1V61 00
52000 1L3IR 1V5D 00
51000 1L3IR 1V6C 00
52000 1L3IR 1V5W 00
52000 1L3IR 1V5M 00
52000 1L3IR 1V6D 00
51000 1L3IR 1V6J 00
52000 1L3IR 1V6G 00
52000 1L3IR 1V6J 00
51000 1L3IR 1V7X 00
...
52000 1L3IQ ZLF9 00
52000 1L3IQ ZLEY 00...

This block is the raw output of logging in thousands of times and writing down each token. Each line is one token, with spaces added between its parts so the pattern is easier to see. The ... marks lines that were left out.

First of all, let's split a token into 4 parts. Taking 51000 1L3IR 1V0B 01 as the example:

  1. Part #1: 51000
  2. Part #2: 1L3IR
  3. Part #3: 1V0B
  4. Part #4: 01

What each part did:

Part Values seen Behaviour
#1 51000 or 52000 one of two values
#2 1L3IR, 1L3IQ incremental, changes infrequently
#3 1V0B, 1UYR, ZLF9 incremental, changes every time
#4 00 or 01 one of two values

Part #1 was either 51000 or 52000. Part #2 was incremental and didn't change so often, so there was no need to brute force it. Part #3 was incremental and changed every time, but the length of the value wasn't enough to prevent brute force attacks. Part #4 was either 00 or 01.

You might think that brute forcing part #3 should have taken too long, because there are 10 digits and the English alphabet contains 26 characters. So each character in part #3 could have 36 different values. Considering that part #3 had 4 characters, there were 36 x 36 x 36 x 36 = 36⁴ = 1,679,616 permutations. A permutation here just means one possible arrangement of the characters. That is why length matters: every extra character multiplies the work by 36.

The question was "did we really have to brute force from 0000 to ZZZZ?".

Let's assume that we got a token like this: 52000 1L3IQ ZLKI 00. We could guess that 52000 1L3IQ ZLKH 00 has already been taken by another user, because ZLKH comes before ZLKI. In other words, if the tokens are handed out in order, then the token just below yours belongs to whoever logged in just before you. We could know that hundreds and even millions of users have already logged in, but we could not know how many users have logged in after we've done. That is the limit of the trick: it reaches backwards in the sequence, not forwards.

Validating Session Tokens

The researcher found that the server answered differently depending on whether a context was valid. When he sent a valid context (session token) without specifying any service, the API returned UNDEFINED_SERVICE as a response. But when he sent an invalid context, the API returned INVALID_SESSION as a response.

HTTP
POST /xyz/abc/service HTTP/1.1
Host: vulnerable.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.7,tr;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Content-Length: 43
Connection: close

{
  "context": "520001L3ITZS1C00",
  "service": {}
}

Line by line:

  • The first line and the headers do the same jobs as in the login request: POST sends data, Host names the site, the Accept headers describe the reply the client can handle, Content-Type says the body is JSON, X-Requested-With marks it as coming from the page's scripts, Content-Length: 43 gives the body size, and Connection: close ends the connection after the reply.
  • The body contains only two things. context holds the token being tested, here 520001L3ITZS1C00. service is an empty object, {}, meaning no operation is requested.

Why an empty service? Because the answer still depends on whether the token is real. A valid token with nothing to do produces UNDEFINED_SERVICE, while a token the server does not recognise produces INVALID_SESSION. That difference lets an attacker tell a valid token from an invalid one. An oracle, in security, is any response that answers a question the server did not mean to answer: here, "does this token exist?".

Logging In With A Valid Context From the original on medium.com

Logging In With A Valid Context

Logging In With An Invalid Context From the original on medium.com

Logging In With An Invalid Context

The two screenshots show the two different replies side by side: one for a token the server accepts, one for a token it rejects.

Writing The Exploit Code

An exploit is a program that carries out the attack automatically. It has four parts.

#1: Obtain a session token. (The researcher was given a test user during the security test.)

The Method That Attempts To Log In. Then, Returns The Obtained Context (Session Token) From the original on medium.com

The Method That Attempts To Log In. Then, Returns The Obtained Context (Session Token)

The screenshot shows the piece of code that logs in with the test account and hands back the token it receives.

#2: Split the token into 4 parts.

From the original on medium.com

The screenshot shows the code that cuts the token into the four blocks described earlier, so each block can be handled differently.

#3: Find the indexes of each character in part #3.

From the original on medium.com

An index here means the position of a character in the list of allowed characters (the ten digits and twenty-six letters). The screenshot shows the code that converts each character of part #3 into its position.

#4: Brute force the session tokens.

The Method That Checks If A Context is Valid or Invalid From the original on medium.com

The Method That Checks If A Context is Valid or Invalid

The screenshot shows the code that sends a candidate token with an empty service and reads the reply, using the UNDEFINED_SERVICE versus INVALID_SESSION difference as the yes or no answer.

The Method That Handles The Brute Force Attack From the original on medium.com

The Method That Handles The Brute Force Attack

The Result & The Final Exploit Code

I've found multiple session tokens in just a few seconds.

The Output Of The Exploit From the original on medium.com

The Output Of The Exploit

The output of the exploit.

The Final Exploit Code From the original on medium.com

The Final Exploit Code

The screenshot shows the complete exploit in one piece.

Why it worked

The session token was generated with low entropy. The token is assembled from parts that are either fixed, drawn from a two-value set, or incremented sequentially, so a token issued to one user can be predicted from tokens issued to others. The API also returned different responses for a valid and an invalid context, UNDEFINED_SERVICE versus INVALID_SESSION, which lets an attacker tell whether a guessed token is valid. The report does not describe any rate limiting (a cap on how many requests a client may make in a given time), token expiry or other control that would have slowed the brute force down.

What it meant

By guessing session tokens, an attacker can obtain valid tokens and access the accounts they belong to. In this test the researcher gained access to hundreds of accounts belonging to the finance company. The report does not state what data or actions those accounts exposed, nor whether the company paid a bounty.

How to fix it

Variants to test

If you ever test a login of your own, these are the checks the finding suggests:

  1. Log in several times and compare tokens character by character. A positive result is any position that stays constant, cycles between a small set of values, or increments between requests.
  2. Split tokens into blocks and check each block for incrementing sequences. A positive result is a block that advances by one between consecutive logins.
  3. Send a known-valid token with an empty service object and an invalid token with the same shape. A positive result is two different error codes that reveal validity.
  4. Brute force the incremental block around a known token, testing values just below it. A positive result is a valid token for another account returned within a short run.
  5. Repeat the collection with a second account and compare the two token streams. A positive result is streams that overlap or interleave, showing a shared sequence rather than per-user randomness.

Glossary

  • API: the part of a service that answers requests from programs, as opposed to the pages people see in a browser.
  • Authentication: proving who you are, usually with a username and password.
  • Authorisation: deciding what an authenticated user is allowed to do.
  • Brute force: trying many possible values one after another until one works.
  • Burp Suite: a tool for watching, editing and resending the traffic between a browser and a server.
  • Context: the name this API uses for its session token.
  • Entropy: unpredictability; high entropy means hard to guess, low entropy means predictable.
  • Exploit: a program or set of steps that carries out an attack automatically.
  • Hash: a fixed-length fingerprint computed from data, designed to be impossible to reverse.
  • Header: a line in a request or response carrying metadata, such as the content type or the host.
  • HTTP: the language browsers and servers use to exchange requests and responses.
  • Index: the position of a character within an ordered list of allowed characters.
  • JSON: a text format for structured data using braces, colons and commas.
  • Oracle: any server response that reveals information it was not meant to reveal, such as whether a token is valid.
  • Permutation: one possible arrangement of a set of characters; here, one candidate token value.
  • Rate limiting: a cap on how many requests a client may make in a given period.
  • Repeater: the Burp Suite feature that resends a captured request, optionally modified, on demand.
  • Request: a message a client sends to a server asking it to do something.
  • Response: the server's reply to a request.
  • Session hijacking: taking over someone's logged-in session by obtaining their session token.
  • Session token: a short string issued after login that the client sends back to prove who it is.
  • Username and password: the credentials a user supplies to log in.

Similar reports

View all

Same bug class and techniques, clearest first.

Account takeover

The lesson behind this report.

Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.