6 ms·
It's All About Time – Remote timing attacks in PHP
- lucb1e 12y agoI've looked into timing attacks in the past but failed to exploit one. Even running php locally from the command line, eliminating any possibility of network randomness, I couldn't get even a few bytes with any amount of certainty. Not saying it's not an issue, but I'm not so sure it's a big deal either.
- aj-code 12y agoI wrote a tool a while ago for testing network based timing attacks. Getting the measurement right is really hard, just taking the average doesn't generally work while the 10th percentile measurement is much better. I did find PHP fairly hard to exploit though, but I was trying over a network which much more difficult than locally. The code for the tool (and a presentation pdf) is here if you're interested: https://github.com/aj-code/TimingIntrusionTool5000 https://github.com/aj-code/TimingIntrusionTool5000
- TheLoneWolfling 12y agoEven taking the straight minimum may be better.
- aj-code 12y agoYou'd think so! But that's not what I found, I found the minimum, max, and average were all crap. Even the median wasn't very good compared to the 10th percentile (which I got the idea from for here http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf)
- lucb1e 12y ago> just taking the average doesn't generally work Oh, well yeah that's what I did. Guess I'll have to look further. > the 10th percentile measurement is much better That sounds like something to try, thanks!
- rosenjon 12y agoThis is also what I think. Start adding in database latency and other functions that execute alongside your string comparison, and this seems incredibly unreliable. It's not like most calls to a page only execute a single string comparison. Good to know, but seems low on the attack vector priority list.
- umsm 12y ago> NOTE In general, it's not possible to prevent length leaks. So it's OK to leak the length. I would imagine that you can prevent length leaks by looping through the characters of the known value and then returning that comparison with an additional check of length. function timingSafeEquals($safe, $user) { $safeLen = strlen($safe); $userLen = strlen($user); $result = 0; for ($i = 0; $i < $userLen; $i++) { $result |= (ord($safe[$i]) ^ ord($user[$i])); } return $result === 0 && $userLen === $safeLen; }
- totony 12y agoThe for loop duration here will vary depending on the length of the string
- umsm 12y agoThat was a quick copy/paste. But imagine the loop would be the string we know (i.e. the password). Looping through the 10-character pass should be identical every time, regardless of what the user entered.
- totony 12y agoI imagine the strlen() function is not constant-timed, so I'd remove it, but it might work yes (but you'd have to check PHP's source code and have a deep understanding of it to really be sure).
- mappu 12y agolibc `strlen()` is O(n), but PHP strings are binary safe (i.e. http://3v4l.org/47iaP http://3v4l.org/47iaP ) so it must be storing a length as part of the string zval. So PHP strlen should be constant time. +1 for recommending checking the PHP source code to be sure, though
- TazeTSchnitzel 12y agostrlen() in PHP is constant-time O(1), we don't use C strings. (Well, we do, but we store length information and reference count them) See: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_builtin_functions.c#524 http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_builtin_function...
- totony 12y agoInteresting article, I never took the time to really think about this. For optimal security, OP mentions that we should fix the underlying code, but I kind of disagree. Understanding all the underlying code is a lot of work and, except for extremely secure application, is not really needed. Also, hindering performance for security is not always the way to go (he mentions returning only at the end). If you have ultra-secure tasks to do, you might want to do something like calculating the execution time of the function and sleep()'ing to add padding. Example code (doesn't work): function login(){ execstart = utime() // whatever code execduration = utime() - execstart; sleep( execduration % 100 ); // pad on at 100 boundary } You'd have to carefully choose your padding so the execution time is (normally) not revealed in the code.
- totony 12y agoI don't know how to do [code] tags
- hnmcs 12y agoAt this time, these few options are available: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc >Text after a blank line that is indented by two or more spaces is reproduced verbatim. (This is intended for code.)
- totony 12y agoThank you!
- AgentConundrum 12y agoI've been on this site for something like five years, but... I never knew that this functionality existed
- mifreewil 12y agoI'm not quite sure I understand what you are saying here. I don't think anyone is saying that you need to understand all the underlying code, but this is just something you should be aware of as you are building network-connected applications. Just like a web developer needs to be aware of CSRF, XSS, etc. Every language that I know of short-circuits string comparisons like this. You should know that you need to seek out a constant-time compare though when comparing hashes or other secrets. Your sleep hack is probably less secure. I remember reading somewhere that doing random sleep() calls is flawed and doesn't completely mitigate the attack, but does increase the number of requests needed to get a good statistical analysis.
- pjbrunet 12y agoHow viable is this in the real world? I could imagine it working on a lone, quiet server at 4am with no other people on it. The average server with 100s of accounts, 1000s of users all doing various stuff, that's going to introduce enough random delay to throw off your analysis. Maybe you could try to calculate averages but that's going to increase the number of requests. Would a good hosting company block an attack like this at the hardware level? I'd guess yes, but I don't know.
- aj-code 12y agoI think the reality is that this style of attack is difficult enough that it would be something of a last resort, and probably only tried on high value targets. It'd take a long time (you'd probably need 100s of millions of requests) and could be easily noticed. That said, while it's not instant game over, you don't really want this vulnerability if you can avoid it. Especially in things like authentication libraries.
- Eridrus 12y ago> How viable is this in the real world? Not really viable against a native code memcmp. Potentially viable against a comparison implemented in PHP or Java. Here's a video looking at actually implementing such attacks over LAN and internet: http://rdist.root.org/2010/11/09/blackhat-2010-video-on-remote-timing-attacks/ http://rdist.root.org/2010/11/09/blackhat-2010-video-on-remo... > Would a good hosting company block an attack like this at the hardware level? No, there's no generic way to defeat this. The only real limit is how granular your attacker can measure. If you're on shared hosting, you're probably the most vulnerable you can get to this (but also the most screwed already, so you probably don't cate).
- userbinator 12y agoShared hosting might actually add more noise, since all the traffic to the other sites on the server will affect timings too.
- bascule 12y agoPretty viable when you do statistical analysis across a large number of requests: http://matasano.com/research/TimeTrial.pdf http://matasano.com/research/TimeTrial.pdf https://github.com/dmayer/time_trial https://github.com/dmayer/time_trial
- invisible 12y agoI think the title on HN is a bit misleading. This issue applies to ALL languages and PHP has specific functions (since version 5.6) to avoid timing issues for you: hash_equals and password_verify. PHP is specifically trying to address poor authentication mechanisms by adding the (ingenious) password module to PHP's core.
- sarciszewski 12y agoI don't really find it misleading. It's about all sorts of remote timing attacks (including cache-timing attacks), not just passwords. I'm the author of the pull request that prompted the discussion, though, so I'm probably biased. :)
- invisible 12y agoIt's misleading because it implies this is a unique problem scoped in PHP. That is factually incorrect and hence misleading any reader to assume this article is about PHP's poor security against timing attacks. It'd simply be nice if the scope of the discussion was about how all languages have more or less the same flaws. Anyone writing code with security in mind should understand how to avoid those flaws. There are many ways to alleviate the problem specified in the pull request that may be better. Pecl is one route that was mentioned on internals. Also, possibly just cache the hex value in memory and avoid the bin2hex conversion outright (with randomization of your cold cache times). Edit: As an aside, I want to also greatly express that the requirement to attack the bin2hex vulnerability is so high for most cases the attacker would require complete, private, 1-hop, uninterrupted access to a single target machine. Usually this implies the attack not being remote but I do understand the mental exercise.
- sarciszewski 12y agoThe second you say "install this PECL extension" people hop on the fast train to nopesville. That is the problem I ran into getting people to adopt libsodium :\
- 12y ago
- contingencies 12y agoI suppose preg_match(), previously popularly opposed on performance grounds, it now arguably better than a straight string comparison in security terms due to probable timing attack resistance. :)
- Xylakant 12y agoRegular expressions will exhibit the same behavior. They will also short circuit if no match is possible/ In essence the behavior that causes the problem is a desirable performance optimization for the general case that you just need to prevent here.
- contingencies 12y agoI seriously doubt regex with grouping, greediness and repetition exhibit the temporal properties of purely linear character-by-character matches...
- coldtea 12y agoDoesn't have to exhibit "the temporal properties of purely linear character-by-character matches" and noone said that strawman. It just has to exhibit the "correct matching strings return faster" property, whatever more complicated search it does.
- contingencies 12y agoDoesn't it have to exhibit "correctly matching strings return faster ... on a per-character or other tiny chunk basis" in order to work the match towards correctness?
- coldtea 12y agoYes, and it will indeed try by all means to exhibit that behavior -- unless if it's some contrived regex designed to counter this. The key word here is "short cirtuit" the match, and any regex engine worth its salt will try to do that as much as possible, tranforming the regex to the faster FSM it can.
- homakov 12y agoTiming attacks are not working in reality because of many things. You know how many times hackers actually used it? I bet 0. Just little bit faster than bruteforce. But that's how you can make them faster http://homakov.blogspot.com/2014/07/timing-attack-666-faster.html http://homakov.blogspot.com/2014/07/timing-attack-666-faster...
- homakov 12y agoAnd if you have a PHP app, timing attack is the very last thing to worry about... go prevent XSS first :) Re http://klikki.fi/adv/wordpress.html#details http://klikki.fi/adv/wordpress.html#details