7 ms·
This is great. One thing that's worth remembering about randomly generated tokens is that it's important to always use safe comparison methods when comparing t
by simonw 5y ago
This is great.
One thing that's worth remembering about randomly generated tokens is that it's important to always use safe comparison methods when comparing them to the stored one - otherwise you could be vulnerable to timing attacks.
In Python you can use secrets.compare_digest(a, b) for this: https://docs.python.org/3/library/secrets.html#secrets.compare_digest https://docs.python.org/3/library/secrets.html#secrets.compa...
- SilverRed 5y agoCould you elaborate on what you mean by timing attacks? What would go wrong if you did a basic params[:token] == dbvalue ?
- pests 5y agoMost naive string comparison methods (including language expressions like above) will usually compare one letter at a time until a mismatch is found. This allows an attacker to build up the correct password one character at a time. The correct letter will take longer to check then the incorrect.
- quelltext 5y agoBut in the "real world" wouldn't any such time difference be very small and fluctuations in network delay and measurement error so large in comparison that this isn't typically exploitable? Sure, better be safe and use a safe comparison method and that's simple enough, but still, how realistic is such an attack over the public internet? I'm aware of "practical" results: http://crypto.stanford.edu/~dabo/papers/ssl-timing.pdf http://crypto.stanford.edu/~dabo/papers/ssl-timing.pdf But the experiment setup was on a LAN, with AFAICT a single server, no other traffic to that server, and other such conditions.
- simonw 5y agoThe trick with timing attacks is that you don't measure the time taken for a single request. You send the same request thousands of times and take an average of the response time, which lets you pick away at the secret one character at a time.
- quelltext 5y agoSure, that's what you'd attempt to address errors, but that still doesn't convince me. That only gets you so far. My intuition says that you won't be able to distill any meaningful results in practice across long distances and requests being routed to different backend servers under varying load. You are trying to isolate a very specific duration in the order of microseconds. Of course, I'm not in the field and am happy to learn more. However, my intuition also tells me we should have seen more realistic experiments and results over the years to get a clear picture of the extent at which such an attack is actually feasible and when it seizes to be.
- flyinglizard 5y agoThe difference is probably below 1 microsecond. A modern CPU running at 3GHz roughly performs 3 instructions every 1 nanosecond. It varies a lot but character by character comparison, especially one that’s happening often and thus cached is one of the most trivial usecases so let’s assume this nominal throughput. I’d estimate a naive comparison loop to be around 20-30 cycles, for compare and control? And let’s wrap it in 250 cycles more because someone decided to use Python. 300 cycles, then, are about 100ns (tenth of a microsecond). Not saying it can’t be done with a timing attack over the internet but you’d need a huge sample size.
- kbknapp 5y agoA real attacker would try to minimize all those constraints by being collocated with the target as close as possible such as in the same datacenter or same rack if possible.
- deleted 5y ago[deleted]
- csnweb 5y agoI am wondering how that relates to searching for the token in a database (index). Does it still matter? I would assume the time might depend on where or even wether the token is in the index or not, not sure how much though and what to do about it.
- ezekg 5y agoRun the token through a secure HMAC function before storing it in the DB. Problem solved. KISS. :)
- iudqnolq 5y agoWhy would something derived from a random string have better comparison properties than the random string?
- ezekg 5y agoYou can’t perform a timing attack for a token “foo” in SELECT WHERE token = :token if the token stored in the DB is the HMAC of “foo”. E.g. trying “f” and then “fo” produce 2 entirely different, random tokens from the query’s POV. The attacker could never deduce that the correct token is “foo.”
- simonw 5y agoYes, it matters - timing attacks could absolutely work against an indexed column in a database. The trick I've used for this is to have tokens that look like this: "234523:7a561002f780e23853bdfbd89ae79bf2" Then you have database entries like this: id = 234523 token = 7a561002f780e23853bdfbd89ae79bf2 When a token comes in you use the bit before the : to look up the database record by its indexed ID, then perform a safe comparison between the rest of the token and the value you retrieved from the database.
- bpicolo 5y agoSerious question - what makes the database serialization not vulnerable to timing attacks in the same vein? I wouldn't expect those to be purely constant time implementations.