This question already has an answer here:

What is the most simple and effective way to Prevent the Timing attacks for the programmers who use PHP?

I'm a high school student, I'm new to programming and I've been learning Security since few weeks. I know Blind and Time based SQL Injection attacks and I know how are they performed and How we can prevent them. But, in this page of PHP docs explains that comparison of passwords is also vulnerable to timing attacks (when using '==' operator).

I considered that anything written in the backend of the server with PHP is safe from Client Side Perspective but here is the case totally different. The hackers can take advantage of indirect methods and guess the backend and try to exploit it. Since there are not many tutorials, stuff, information to read about how timing attacks can be performed against the PHP script which performs the comparison of 2 strings, I want to know the things to keep in mind to prevent those kinds of attacks.

I want you to answer these questions.

  1. If I compare hashes with md5, then will it be applicable? (practical perspective).
  2. If Yes, then Are there any other things which might be vulnerable apart from string comparison? (examples please)
  3. What is the best thing here I can do to prevent it?
  4. Are these attacks common/sensitive?
  5. A short tutorial/script to demonstrate that how timing attacks work.

This Question doesn't ask about what are timing attack, but How they are performed and How they can be prevented? (for servers on PHP only)

thank you :)

marked as duplicate by Conor Mancone, forest, this.josh, kasperd, Daisetsu Nov 5 '18 at 1:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    First off, there is no such thing as a distinct Blind SQL Injection attack. There is just an SQL injection vulnerability which, once present, could be exploited in many ways, including blind, timed or whatever else SQL injection attacks. But you don't prevent Blind SQL Injections specifically. You just protect from SQL injections. Once for all. – Your Common Sense Oct 18 '18 at 8:12
  • @YourCommonSense While I love the way you explained the thing to me, also, I appreciate your effort to help me if there is already an answer somewhere. But, I must say that the question you linked me is about "Timing Attacks" while my question is focussed on "Where can this attack be performed?" and "How can I prevent it?" which might be related to your question but is different in meaning. :) – Abhas Kumar Sinha Oct 18 '18 at 8:17
  • There is enough information in the linked question and links provided in it. In a nutshell, "It's very hard or impossible to exploit this vulnerability over the network". So there is practically no way for this attack to be performed. For me it is clear enough. – Your Common Sense Oct 18 '18 at 10:02
  • @YourCommonSense Then, the only comparison of passwords are vulnerable to it? Are there examples of other things which might be vulnerable to this? like a hacker can make an indirect guess of the working of server-side scripts – Abhas Kumar Sinha Oct 18 '18 at 14:36
  • The attack on comparison of passwords is practically impossible. – Your Common Sense Oct 18 '18 at 14:39

1) An MD5 timing attack is equal to the difficulty of a plaintext timing attack, plus an MD5 rainbow table attack. Just use a normal timing attack to figure out the first 8-10 bits of the MD5 (Generating strings whose MD5s have a specific 8-10 bit sequence is easy). Then rainbow table match on those first 8-10 bits to get a small set of passwords. Then bruteforce those passwords.

2) Anything where checking is related to time. An example would be a complex SQL query. If you use an SQL DB to check the first input, and then another query to check the second input, then the attacker could use a timing attack to check the validity of the first input before guessing the second input.

3) Your for loop should loop over all of the characters of the correct answer, without exiting if an incorrect character or input length is found. Compilers might optimize this so you might want to make sure side effects are generated from this comparison that the compiler can't optimize.

4) No, not at all. If your PHP script does any system call at all, it will immediately make a string comparison negligibly short. Where the variance in string comparison times is hundreds or thousands of times less than any other variance. Just write random numbers to a file at the end of the script and a timing attack is impossible. Also, if it over the internet, a timing attack is incredibly infeasible. Internet times span hundreds of milliseconds and sometimes even seconds. String comparisons are in the low microseconds if not nanoseconds.



correct = pwsd.length == ans.length;
for(int i = 0; i < pswd.length && i < ans.length && correct; i++ ) {
    sleep(1); // 1ms
    correct &= pswd[i] == ans[i];


time = 0ms
guess = 0
while ( time < 0.9ms )
    guess = "A" * randomInt(1, 100)
    time = timeit(execute("Server.exe", guess))
log("LENGTH: ", guess.length)
time = 1ms
while ( time < 1.9ms )
    guess[0] = "A" + randomInt(0, 26)
    time = timeit(execute("Server.exe", guess))
log("First Character: ", guess[0])
time = 2ms
while ( time < 2.9ms )
    guess[1] = "A" + randomInt(0, 26)
    time = timeit(execute("Server.exe", guess))
log("Second Character: ", guess[1])

You can of course put the time = Xms, while, log("Xth character") bit in a function and then for loop over guess.length. This is most useful when you're on the same system and attacking a linux binary that's meant for admins, since you have excellent timing precision. A famous example of timing attacks were Meltdown and Spectre, which use CPU cache timings to exploit data (Correct guesses are instant, while cache misses take a bit).

  • For PHP, I've found a better alternative for string comparison. hash_equals() function - php.net/manual/en/function.hash-equals.php ;) – Abhas Kumar Sinha Oct 19 '18 at 7:48
  • @AbhasKumarSinha It uses a similar process that's independent of length using bitwise calculations. PHP has a nice string xor function so it's real easy. Take Input ^ Answer, and then check if each character is equal to 0. It doesn't have anything to do with hashes so idk why they named it that. Maybe for hashing the input (To guarantee same length)? – Nicholas Pipitone Oct 19 '18 at 16:24
  • Yes, that's what I was about to ask, there was nothing related to hashes what it does! – Abhas Kumar Sinha Oct 20 '18 at 14:00
  • @AbhasKumarSinha It's probably for speed. It'd be insensible for hash_equals to hash the input every time you compare it, since you can prehash it and give the prehashed value every time. – Nicholas Pipitone Oct 22 '18 at 16:35
  • may be.......... – Abhas Kumar Sinha Oct 23 '18 at 15:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.