- Feature Articles
-
CodeSOD
- Most Recent Articles
- Crossly Joined
- My Identification
- Mr Number
- intint
- Empty Reasoning
- Zero Competence
- One Month
- A Little Extra Padding
-
Error'd
- Most Recent Articles
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- Tangled Up In Blue
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Are the frist number not pseudorandom generated?
Admin
This is a big RTFM as well as a WTF.
The documentation for uniqid tells you not to use it if you want the id to be unguessable, and what to use instead.
Admin
what, no ROT13, posers..
Admin
Also, the default arguments to
mt_rand()
already are0
andmt_getrandmax()
.That said, I've used something like (only) the first loop myself - but in that case, the requirements did not involve security and did involve guaranteed, checkable uniqueness. They were stored in a database and used to identify certain devices easily and with some typo resistance (i.e., much larger keyspace than needed). For that, this method is fine. For session IDs? Not so much.
Admin
It's not cryptographically secure but it meets the need for "a unique session ID value that can't easily be guessed".
It took an awful long time for an MD5 collision to be discovered in real life, and it was generated on purpose. 2^128 is an awfully big space for collisions to happen accidentally.
If it's an ID for a database you can generate one, see if it exists, then generate another one if it does. I'd bet a LOT of money that it never would though (assuming there was a way for me to collect that bet after a certain time limit).
Edit Admin
I note also the
mt_rand(0, mt_getrandmax()
which is exactly equivalent tomt_rand()
...And yes, this one is a fairly high level of WTF.
Edit Admin
And the documentation for
mt_rand()
is pretty clear on the same subjects.Admin
"A random number generation algorithm chosen at random is likely to be crap."
Not sure where I got this from, but I've known it since the 1970s.
Edit Admin
Honestly the
md5()
isn't really that terrible of a thing here. Sure, there are absolutely better choices, and adversaries can construct collisions with chosen prefixes, but those attacks don't appear to a concern here since there's no external user input being consumed in this function.The rest of the function is indeed a steaming pile of WTF.
Edit Admin
@FTB ref:
In general I'm on your side here. But it depends heavily on what "guessed" means. And on who came up with that requirement & why. If the guesser to defend against is a bored high schooler or clerk in your firm that's one thing. if it's a "malicious state actor" or whatever euphemism DoD prefers for "China" these days, that's rather another.
The usual problem with these kinds of requirements is the PHB that thinks of them is thinking about Sarah in accounting. Meanwhile that annoying guy in your IT security department (or who is your IT security department), is thinking "malicious state actor" is the minimum adversary to defend against.
Reality is probably in the middle. Incompetence, however, is everywhere.
Edit Admin
Knuth, in (I think) Volume 2 of Art of Programming. Classic
Edit Admin
It doesn't help, but it looks like it helps, so it's bad because it makes people feel safe.
For example, let's say generation one of some session is mechanism is simply an increasing integer. The site gets hacked. The programmer simply MD5's the sessionid and says "There, that's unguessable now". An adversary with more than two brain cells will recognize an MD5 hash and then simply add a "and then MD5 hash it" as the last step of their process that guesses session ids. All you did was make a script kiddie add a line of code to his hack script.
Admin
In Donald Knuth's "The Art of Computer Programming", the section on Random Number starts with the quote from John Von Neumann, "Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."
He then goes through different algorithms for this...
Admin
On the bright side, this won't be easily guessed because attackers won't be expecting something this dumb.
Admin
Would it be possible that the MD5 Operation is there to disguise the result?
Admin
TRWTF is reimplementing sessions when PHP already has that built in
Edit Admin
Yes. However, any security that adds an obfuscation layer has basically admitted that it need to be obfuscated and therefore isn't "secure enough for purpose" as is. Unfortunately, obfuscation only requires one person to figure out what obfuscation technique is being used and then it becomes worthless. This is the thought process that leads to Kerchoff's principal (https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle): "A cryptosystem should be secure, even if everything about the system, except the key, is public knowledge"
Admin
hmm.. I actually wonder if it may be the other way around: the PHB is thinking "malicious state actor" but the IT guy knows that it isn't exposed publicly and doesn't need that level of security, so this is all security theater to convince the boss that it's "extra secure" by using md5 to "randomize the bits" even more. (not that doing it the right way is all that hard either; so it's not like the security theater saves them anything, besides accommodating their malicious compliance)
Edit Admin
Reality is that your run-of-the-mill bad actor looking to get into your system is probably using tools and techniques developed for state actors five years ago. No one trying to forge a session token/cookie is actually guessing with their brain and typing by hand. As long as you are doing something that is reasonably cryptographically sound and your components are getting patched, the technical angle isn't going to work and they will switch to attacking the weaknesses in your process rather than your technology. Most likely, they'll try to use any chat help that you make available to try to get you to reset credentials for them. They'll try chat first because no can sniff out a mismatch between a person's last name and their accent over chat.
Admin
Aside from the good id or not, the first thing I find odd is that even though the conversion will work they know they are dealing with strings, the initialization is the number zero.
Admin
Personally, I like the line "$id = md5(uniqid($id, true));" where $id starts as a "random" number string, then becomes a "unique" ID, and ends up as a MD5 hash. So the $id is the hash of a unique number string $id. Of course that's nonsense, it's like saying "$hamburger = cow; $hamburger = fry(defrost(unpack(freeze(mince($hamburger, true))));".