Cookie Poisoning by Line
Cookie Poisoning by Line
Summary
Cookie poisoning is a known technique mainly for achieving impersonation and breach of
privacy through manipulation of session cookies, which maintain the identity of the client. By
forging these cookies, an attacker can impersonate a valid client, and thus gain information and
perform actions on behalf of the victim. The ability to forge such session cookies (or more
generally, session tokens) stems from the fact that the tokens are not generated in a secure way.
In this paper, we explain why session management (and session management security) is a
complex task (which is why it is usually left for commercial products). We describe how the
tokens are generated for two commercial application engines. We then analyze the strength of
each mechanism, explain its weakness, and demonstrate how such weakness can be exploited to
execute an impersonation/privacy breach attack. We discuss the feasibility of the attack. Finally,
we recommend an approach to session management which separates the security from the
functionality – the latter is carried out by application engines, while the former should be
provided by a dedicated application security product.
• It should never be possible for one client to be able to predict the token another client
received, or is in the process of receiving, or will receive. This is obviously a ‘must have’
in order to prevent impersonation attacks and consequently breach of privacy.
• Furthermore, it is desirable that a client will not be able to predict the next token he/she
will get when accessing the site. This is useful in minimizing the damage of stealing the
token while it travels (in the clear) to and fro, and while it is stored on disk at the client.
• Any token should have a reasonable expiration period – again, to minimize the damage of
it being stolen.
As can be seen, it is not very easy to fulfill all these requirements, especially if the session
mechanism is developed ad-hoc. The more intricate security requirements are definitely
something developers, especially ones not versed in security, may easily miss.
One recent example is the cookie mechanism that was employed by Verizon Wireless
(www.verizonwireless.com and www.app.airtouch.com). The security problem is mentioned in
the press (http://www.newsbytes.com/news/01/169781.html), and in technical resources
(http://online.securityfocus.com/archive/1/211520 - a report by Marc Slemko, dated September
1st, 2001). To quote from the latter:
Cell phone bills are often very interesting things, since they contain
names, addresses, and a complete record of calls placed and received,
along with the approximate location the user was when the call was
made. I'm sure I'm not alone in expecting my provider to provide a
reasonable level of privacy for this data.
https://www.app.airtouch.com/jstage/plsql/ec_navigation_wrapper.nav_frame_display?p_se
ssion_id=3346178&p_host=ACTION
As we see here, the problem is simple: the Verizon Wireless site assigns a token (in this case, it
appears as a parameter named p_session_id) to each logged-in visitor in the site. This token is
used to identify the visitor. The token value is sequentially incremented per each new visitor,
hence if you’re a visitor and your p_session_id is N, the next visitor will be assigned N+1. This
is an extremely predictable mechanism, and it completely violates all security requirements
(although it probably fulfills all the functional requirements).
¤2002 Sanctum, Inc. 2 www.SanctumInc.com
Many other examples of insufficient security in tokens are demonstrated in the work of MIT
Laboratory for Computer Science (“Dos and Don’ts of Client Authentication on the Web” by
Kevin Fu, Emil Sit, Kendra Smith and Nick Feamster)
http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf
So we see that it is difficult to come out with a good session management solution, let alone a
secure session management solution. This is one of the reasons why application servers are so
popular.
The upside of application engines is the fact that they completely relieve the programmer from
worrying about session management. All functionality aspects of session management are taken
care of, usually much better than an in house programmer could have achieved.
The downside of application engines is the fact that they seem to relieve the programmer from
worrying about the security of the token, yet we can show that the harsh reality is far from that.
In fact, some very popular application engines do not provide secure tokens. As a result, the
programmer obtains a false sense of security.
We examined the tokens generated by two popular application servers. In both cases, we were
able to demonstrate that the token is not as random as it seems, and that it is possible (in one
case, with ease), to predict the values of the token for the next sessions (of a different client).
The target of this attack is a very popular commercial application engine. The product uses two
cookies to identify a session. The pair formed by the two cookies identifies the session. The first
cookie is merely a counter, incremented once per new session. It probably ensures that no two
pairs are ever identical. The second cookie is the token cookie, apparently intended to secure the
pair by being “unpredictable”. Since it is very easy to predict the first cookie, we focus on the
second cookie, which we’ll denote as “TOKEN”.
At first glance, TOKEN seems to be a sequence of random 8 decimal digits. The entropy
(amount of randomness) here is 108 = 226.57 which may be considered sufficient, considering that
it’s quite unfeasible to try such amounts of requests (100 million) against a site without
triggering some kind of alarm and human attention.
But, a closer look reveals that in fact, TOKEN obeys the following equation:
Let us denote by t the GMT time, in seconds, since 01/01/1970 00:00, as set on the
application server.
Let us denote by m the milliseconds portion of the tick counter on the application server.
Then:
TOKEN= ( 31415821 * (t + m) + 1 ) mod 100000000
It is interesting to note that t can be extracted from the HTTP Date header the server
sends back to the client together with the first time the cookies are set.
This means that the TOKEN cookie is quite predictable. In fact, if one knows a range of time T ≤
t < T+∆T (in seconds) in which a cookie was generated, one can infer that TOKEN has one of
∆T+1000 values, which is a rather short list of values. Testing a bit more than a thousand values
against the server may take few minutes, in which the victim session is likely to remain active.
The outline of an attack algorithm is as following:
Obtain a first pair (id1, TOKEN1). Record t1 – the server time (from the Date HTTP
header)
Wait ∆T seconds.
Obtain a second pair (id2, TOKEN2). Record t2 – the server time (from the Date HTTP
header)
if (id2 > id1 +1)
begin
// we have a victim session interjected here.
for (x= t1 ; x < t2 +1000 ; x++) // which is ∆T+1000 iterations
begin
Try the pair (id1 +1, ( 31415821 * x + 1 ) mod 100000000)
end
end
The attack described above enables the attacker to impersonate a victim, provided that such
victim was assigned a cookie between the two samples the attacker made of the site cookies.
Since the attacker can repeat the algorithm as many times as he/she would like, it is possible for
him/her to obtain these cookies for all clients, at a price of sampling the site (say, one request
every minute), and additionally some 1060 requests per any new client discovered. Again, as
hinted above, it is possible to sample at closer intervals (once a second) and exploit the
granularity problem of the clock ticks, in which case it is probably possible to arrive at 100
requests per new client.
It is likely that if an attempt to impersonate a client is performed while the site is loaded with
traffic, then the additional hundreds/thousands of request would go unnoticed, at least
momentarily.
In this example, we deal with a still popular (yet a bit outdated) application engine. This engine
generates a single cookie for each new session. This cookie (which we shall name ID) comprises
of 3 mandatory fields (F1, F2 and F3), and one optional (server configuration dependent) field
(F4, preceded by a dot), concatenated. The fields are as following:
F3 = 3 characters (A-Z0-9) – session count in this 2 second time slice, represented in base
36.
As can be seen, F4 (if it exists) is constant, and hence trivially predictable. F2 is simply the
server time (in seconds) divided by 2, modulo 46656, which is quite predictable, and F3 is not
too obscure as well – as it is sequentially incremented in the 2 seconds time slice (always begins
at one).
The only interesting field is therefore F1. Apparently, it holds enough entropy to secure the
system, since it can assume 366 values (=231.0). Yet again, what seems secure at first sight appears
not so secure when performing a full analysis. Explanation on how and why F1 can be predicted
is provided in Appendix A, since it is too long for inclusion here. The problem we exploited with
F1 is the fact that it uses a PRNG (Pseudo Random Number Generator), which in itself is
predictable. So knowing several values of F1 suffices to fully predict the PRNG, and hence
future (and past) values of F1.
Preparation:
Obtain three IDs, in the shortest time intervals possible.
Extract the PRNG internal state (as explained in Appendix A).
Interception Cycle
Obtain an ID, and record the server time, t. For simplicity, assume t is even.
Find the PRNG internal state that was used to generate this ID (as explained in Appendix
A)
Wait ∆T seconds (where ∆T is even)
Obtain a new ID.
Advance the PRNG, and record all internal states between the PRNG state of the old ID
and the PRNG state that generated this ID (As explained in Appendix A). Let the list of internal
values be L
// ∆T/2 iterations:
for (T=t; T<t+∆T; T+=2)
begin
for each internal PRNG state L, i.
begin
Try an ID cookie consisting of:
As can be seen, it is feasible, although not trivial, to predict some ID cookies. For feasibility, it is
required that the time interval (∆T) be short (with respect to the expected usage of the server), in
order to minimize the length of L (the list of possible internal PRNG states). If these intervals are
indeed very short (less than two seconds), it may be possible, with correct timing, to tell whether
a new session was interjected at the current 2 second time slice, which makes the attack more
effective (since it requires launching the additional requests only when it is known that a new
victim session was indeed created). It should also be mentioned that in order not to lose
synchronization (of the PRNG internal state) with the site, it is necessary to keep requesting a
new ID from time to time, in order to advance the attacker’s PRNG internal state to the new
value. It should be remembered that the PRNG is likely to be used for many purposes, not just
the creation of sessions. This means that the site may use the PRNG intensively, thus causing a
quick de-synchronization (to counter which it is necessary to re-sync at close time intervals, e.g.
every few minutes). On the other hand, it may be possible to get a clearer glimpse of the internal
PRNG state by inspecting other random values that may be used in the site. This may offer a
shortcut saving a lot of computation power.
Vendor 2 acknowledged the weakness yet wrote us “session cookies are -NOT- a replacement
for authentication tokens. A session cookie in conjunction with a random auth token or auth
login validation is both reasonable mechanisms. This should be true in designing session based
scripts - even where the session tokens are 'trusted' today.” – thus laying the responsibility in the
hands of the developers.
The two vendors, while technically acknowledging the problem, dismissed it as a non-security
issue. That is, both vendors assume their customers implement their own session security tokens,
not relying on the vendor tokens. The vendors, therefore, claim that their tokens are used (or
should be used) solely to better differentiate between different users, and not as a security
measure. In the documentation, we did not find any warning against using the token as a secure
session identifier. Furthermore, Vendor 1’s documentation uses phrases that lead one to believe
that this token is secure. And in reality, of course, most sites use the tokens issued by vendors as
a secure session identifier, oblivious to the fact that it is weak.
In a sense, the application developer is back to square one: he/she cannot trust the built-in
session identification mechanism, and thus is forced to write his/her own such mechanism, with
best effort to fulfill all the requirements mentioned above and to avoid the delicate pitfalls of
cryptography.
Conclusion
We see session security falls between the cracks –vendors don’t do it right, don’t care for it, or
delegate the responsibility for it to the developers, while in-house development is error-prone,
and requires a deep understanding of security.
In this paper, we provided real life examples for both insecure tokens in commercial application
engines, as well as in home grown applications.
Our solution is simple – the world of web applications should consist of three components:
• The application (which is developed in house, and expresses the business logic, as well as
the novelty and specialty of the company/site).
In all the above cases, a web application firewall would have fortify the tokens generated by the
application engines (or by the in house developed application) transparently (the developer
needn’t even be aware of this), and ensure, through using strong cryptography and security tested
mechanisms, that the tokens sent to the application are indeed genuine, and not forged.
The ID generation mechanism concatenates two consecutive samples to form a 64 bit integer,
which may be negative (if the most significant bit is 1). Then, absolute value of this number is
taken, and mod 366 is applied to yield F1.
And now to some mathematics: we want to be able to predict the values sampled from this
PRNG.
We do get a direct glimpse at the state bits. To understand why, let us consider the mathematical
representation of F1. Let the two samples needed for F1’s generation be denoted S1 and S2.
Then:
N= S1 * 232 + S2
if (N ≥ 263) N=264-N //make sure N is “positive” – i.e. most significant bit is 0.
F1=N mod 366
We see, therefore, that F1 mod 212 provides us with two options for the 12 least significant bits
of S2, which, in turn are the bits 16-27 of state (denoting the least significant bit as 0 and the
most significant bit as 47).
Now, we can guess the 16 least significant bits of state, and together we’ll have the 28 least
significant bits of state. We have 217 guesses (216 for the 16 least significant bits of state, and 2
for the original sign of N).
The number of guesses can be easily reduced by taking another sample, as close as possible (i.e.
with as few samples of PRNG in between), and verifying against the 11 bits of information (12
bits minus the sign bit). If it is possible to achieve two IDs with less than (say) 16 advances of
the PRNG in between, then with a calculation of 217*16 we can reduce the number of guesses we
Finally, we can also guess the 20 most significant bits, and we can easily verify them using the
ID’s we have, because once all the bits of state are known, it is possible to calculate F1
accurately.
In order to check all guesses at this phase, we need to perform few million calculations.
The above can be easily performed using a standard PC (Pentium-III or Pentium-4) in few
minutes or less.
After this, the full state of the PRNG becomes known. This enables to accurately follow the
PRNG to the future and to the past.
For example, if one has the current value of the PRNG, and an ID that was produced from the
PRNG after some advances have taken place, it is possible to find the PRNG state associated
with the ID, as well as all PRNG states in between (and the ID that may have been produced for
them) via simply advancing the PRNG and generating the ID, until the ID generated matches the
one obtained from the server. This provides both a list of possible IDs between the time the
PRNG was at the known state and the time the ID was obtained, as well as the current state of
the PRNG (the one matching the obtained ID).
It should be noted that an ID is obtained from sampling two consecutive states of the PRNG. But
since it is impossible to know how the PRNG is used, we must check every possibility for having
two consecutive pairs. So if the PRNG states are A, B, C and D we must list the IDs formed from
(A,B), (B,C) and (C,D).