125 points · by philfreo · 16y ago · 36 comments
You should know that the exact same problem applies (moreso, in fact) to encrypted strings. Developer laziness insulates most apps from raw MD5 digests (most devs use the hex digest function, which returns human readable [and safe] output). But the exact same "iterate 1 bit at a time until you hit a jackpot block" trick works with almost every application that uses AES.
It's not just SQL, either; I've used it to get XSS out of corrupted AES decryptions as well.
And, of course, the same trick works in the opposite direction. If you encrypt a string with a quoting domain (say, where the character ';' needs to be quoted '\;' to preserve an encoded tuple), attackers can pad inputs to get the quote and the metacharacter to span blocks, then use block corruption to kill the quote but not the metacharacter.
Have I mentioned today how you shouldn't be building crypto code? DON'T DATE ROBOTS.
The interesting thing about this report is how fast you can brute-force calculate this in order to find a magic byte sequence.
How would you go about such a thing? My initial thought is to simply permute over the controllable bytes and see what comes out, looking for specific characters that get you what you need, e.g. a quote to break out of an HTML attribute, but I think I might be missing an easier path.
It's really not rocket science.
Sounds like a fabricated situation just for the game to me, versus something trying to resemble real life situations. Not saying its not possible, but the type of coders that would make a SQL injection oversight with something like that would also not care to provide a non-default raw_output value to the function for the heck of it
The French team came up with the solution quicker as they worked out that any '=' would work - their keyspace search was thus an order of magnitude quicker:
To get an idea of what can be exposed if you don't salt, and thus use predictable hashes, have a look here: http://www.google.com/#q=inurl:c4ca4238a0b923820dcc509a6f758...
md5(1) = c4ca4238...
Also concatenating "foo" to the end of /every/ password is not salting your hash. You need to have a suitably random string for it be considered salt. Also you want to have a different random string for every password in your database.
[+1 to tptacek for constantly saying don't write crypto code :-p]
While this was a clever challenge, in general you should sanitize all of your input using filters.
You should escape output.
In this case there isn't anything wrong with the input, and you can't say that any byte of MD5 is wrong or dangerous.
It's just data that is used improperly.
Binary data in SQL queries is especially tricky, because strings may be expected to be in specific character encoding (e.g. UTF-8), so you either need to use prepared statements and pass argument explicitly as binary, or use string-safe escaping (e.g. hex).
Input sanitisation has been solved thousands of times by people smarter than you. Do not reinvent the wheel (poorly).
Sorry, if your own personal experience suggests that most software is rigorous with SQL and input validation, I think I have to assert a broader and more accurate perspective on the issue. It feels statistically improbable to me that we're just missing all the "good" software.
instead of '||'[1-9]
one could have searched for
'!=' or '<>' thus opening up the search space