Skip to content.

Scott Arciszewski

Software, Privacy, Security, Innovation

Timing Attack to Object Injection

Posted on: May 12, 2014
Thanks to: Defuse Security
Full View (text/plain; charset=UTF-8)

Last July, I suddenly found myself unemployed. Like most adults, I have my own bills (and restitution) to pay, so I immediately began what can be succinctly described as a month of frenzied job searching.

During this time, I saw a plethora of job opportunities that demanded X years of experience with specific frameworks, like CodeIgniter, Symfony, Laravel, Zend, et al.

Once things stabilized, I revisited my notes from this time and casually read through the PHP code for many of these frameworks. I discovered a few weaknesses that I wasn't quite sure how best to exploit until I shared my findings with Defuse.

Since then, I explained these weaknesses and theoretical exploitations at BSides Orlando, hoping to find the time to write a PoC and share it with the infosec professors I know (e.g. Sam Bowne, Tim Starr, Owen Redwood). However, a month has already passed, and I have neither the time nor the energy to develop this further.

To its credit, CodeIgniter already had the patch committed for its 3.0 release, whenever that will be. To its credit, Kohana's developers immediately recognized what my pull request did and politely requested I re-submit it in accordance to their contribution guidelines.

Long story short, if you're comparing cryptographic hashes with the == or === operators, and you allow users to attempt to guess the same hash (e.g. there's no nonce used) without significant penalty (e.g. after 5 tries, IP ban), you are susceptible to a remote timing attack.

Read the full-text if you want the details. This is just the back-story.

  1. Vulnerability:
  2. 1. Remote timing attack
  3. 2. PHP Object Injection
  4. 3. Possibly, as a result of 2, remote code execution
  5. Affects:
  6. - CodeIgniter (<= 2.1.4)
  7. - Kohana (<= 3.2.3, 3.3.2)
  8. Some PHP frameworks (CodeIgniter and Kohana, for sure), give you the option of
  9. storing $_SESSION data in a cookie, then appending a hash of the session data
  10. and an unknown key.
  11. For example:
  12. a:2:{s:1:"a";s:10:"douchelord";s:8:"is_admin";b:0;}5c2d88368e2c154398e77269c3af3edd
  13. Vulnerability 1 - Remote Timing Attack
  16. If you wanted to alter the above serialized blob to read:
  17. a:2:{s:1:"a";s:5:"admin";s:8:"is_admin";b:1;}
  18. ... in theory, you would need to either know the encryption key stored in the framework's
  19. configuration. And remotely brute-forcing an md5/sha1 hash isn't really attractive.
  20. Luckily, the behavior of standard string comparison operators (== and ===) is to return false
  21. as soon as two bytes do not match. Thus we could discover that...
  22. a:2:{s:1:"a";s:5:"admin";s:8:"is_admin";b:1;}ef000000000000000000000000000000
  23. ...takes a little longer than...
  24. a:2:{s:1:"a";s:5:"admin";s:8:"is_admin";b:1;}00000000000000000000000000000000
  25. ... and then ...
  26. a:2:{s:1:"a";s:5:"admin";s:8:"is_admin";b:1;}ef870000000000000000000000000000
  27. ... will take slightly longer.
  28. Theoretically, if we need 100 samples per possible hexit to guess the correct value through
  29. this strategy, and there are 32 hexits in a hash, at 10 request per second it will take
  30. 51200 samples to crack an md5 hash. If you can guess 50 per second without bringing the
  31. target server to its knees, that's about 18 minutes (worst case) to get the end result:
  32. a:2:{s:1:"a";s:5:"admin";s:8:"is_admin";b:1;}ef87978c40e2c2871ba58ab55b50854e
  33. Vulnerability 2 - PHP Object Injection
  34. If you can convince PHP to pass arbitrary data to unserialize(), which is the automatic next
  35. step when the timing attack succeeds, then you can inject objects into the code. From what I
  36. understand, this can also lead to remote code execution.
  37. CodeIgniter already patched this in its 3.0/develop branch.
  38. Kohana accepted my pull request for its 3.3/develop branch:
  39. Note: Kohana uses SHA1, not MD5, so add another 20% to my estimate.
  • Categories:
  • Advisory
  • , Cryptography
  • , Open Source
  • , PHP
  • , Security

Want to hire Scott Arciszewski as a technology consultant? Need help securing your applications? Need help with secure data encryption in PHP?

Contact Paragon Initiative Enterprises and request Scott be assigned to your project.