r/programminghorror • • Sep 22 '26

PHP A website with millions of users was generating their "secret" password reset codes like this for two decades. The algorithm was open to the public due to a failed open-source effort the company launched in the early 2010s. Fixed a few days ago.

Post image
2.1k Upvotes

135 comments sorted by

464

u/Kinexity Sep 22 '26

What's the problem exactly? Is it about the range of rand? Or is time in microseconds not coarse enough?

581

u/anto2554 Sep 22 '26

I don't think time in microseconds is considered cryptographically secure. It is probably an attack vector that is pretty hard to use in most cases, but there is a reason we have very specific libraries for generating "random" used for security stuff.

Edit: If you know pretty accurately when the command will run, you can narrow down the possible passwords a lot. You can also test the exact latency by seeing what your own OTP is, so it doesn't seem that hard actually

264

u/rtharston Sep 22 '26

Yep. There’s actually a story of a guy that lost the private key to his bitcoin wallet so he got some help recovering it. They figured out the version of the software that generated his key and then started trying all the time stamps around the time he remembered making the wallet.

They eventually got him back in.

75

u/memes_gbc Sep 22 '26

it worked because the password generator had a flaw that used the current time as the seed for the random number generator

66

u/rtharston Sep 22 '26

Exactly, just like the code snippet shared here.

16

u/memes_gbc Sep 22 '26

i would argue it's not exactly the same because the snippet used the time as input to generate an md5 hash

18

u/dotcarmen Sep 23 '26

The md5 hash doesn’t add any layers of randomness though. It’s functionally the same as ASCII encoding the numbers generated from the timestamp. Sure, you can’t reverse-calculate the input numbers, but you can’t reverse-calculate the timestamp from the random numbers anyways without similar complexity

4

u/simiaki Sep 22 '26

Bit there’s a bijection between the timestamp and the hash. It’s practically the same thing

11

u/WhiteEvilBro Sep 22 '26

There's no bijection, but the point stands

1

u/Lolllz_01 Sep 24 '26

64 bit timecode and 128 bit digest, on average exactly one collision, so pretty likely to be a bijection with the amount actially produced, even if its harder to invert

7

u/Amarandus Sep 24 '26

The sizes of the sets are not the same, so it can't be a bijection by definition. Injectivity is not guaranteed, surjectivity is not given (due to size difference).

11

u/rtharston Sep 22 '26

You’re right that the output isn’t the same. I hope the wallet wasn’t using MD5 hashes. 😂

Perhaps “just like” was a bit too vague. Thanks for calling that out.

I was referring to how the time is used as the seed for the random number generator. I haven’t seen many examples in actual code, but this is what I’ve seen when people say they use the time to get a random number.

I’m not sure what the code looks like in the wallet I referred to but I’m guessing the random number generator was seeded like this.

20

u/AtlanticPortal Sep 22 '26

You know it because as an attacker you are actually triggering the reset since all you need is the email address. Then you already calculated enough MD5s to try them all. At some point you manage to get the correct one.

10

u/SadKris4 Sep 22 '26

Irrelevant. Epoch is unneeded, you can just iterate over the 10,000 possible inputs for the MD5 function to derive what the evcode is

4

u/BiedermannS Sep 24 '26

That's why I regularly randomize the system time on all my servers

1

u/prehensilemullet Sep 22 '26 edited Sep 22 '26

Huh, I guess if a MITM could dispatch a separate reset password request at the exact same time as the user they’re trying to attack, they could get much closer to the same microsecond time.  Searching outward from their initial guess of the server’s time and applying this calculation, they could determine the exact time the server generated the code they received.  Then by gradually trying codes generated with increasing offset from this time, they could probably get better than 1/10000th chance of matching the code.

But, any MITM interception I can think of would probably mean they’ve already compromised the platform enough to do what they want, though who knows.

2

u/whocodes Sep 23 '26

the attacker would be the one requesting the code, so they would know almost exactly when it was generated

1

u/binarywork8087 Sep 25 '26

well someone would only do it on a Elon Musk bank account

1

u/binarywork8087 Sep 25 '26

from my point of view nothing is  cryptographically secure enough my 5 cents

69

u/thethappy Sep 22 '26

It's not random enough, basically you would also be able to regenerate the same reset codes with (relatively) simple brute forcing.

38

u/-PM_me_your_recipes Sep 22 '26

srand is setting the seed for the random number generator. This makes the output predictable. What makes it dangerous is that this code was able to viewed by anyone, so they can reproduce the steps to find the temp password.

You could request a temp password via "forgot my password" for a random person's account and record the moment when you did it. Then you can can narrow the range of potential passwords by something that can be cracked in a few seconds at most.

This feels like something my coworker would do.... brb, need to check our temp password generation code.

-1

u/DaRadioman Sep 22 '26

Lol this is literally how OTP token re-sync works since they work on a similar mechanism.

Of course I'm that scenario the seed/secret is cryptographically secure so it works fine 😂

7

u/GRex2595 Sep 22 '26

OTP is also usually a second factor, not your account reset. So it doesn't matter if you can generate my OTP if you can't get my password.

12

u/FlourishingFlowerFan Sep 22 '26

This is a fun read where someone reverse engineered how Twitter generated IDs and was able to post tweet referencing itself: https://oisinmoran.com/quinetweet

Bottom line is if people (or LLMs) have enogh willpower, they can practically break a theoretical flaw in a system. Question is how much harm they could do with it. So just follow crypto best practices. 

13

u/isaycongrats Sep 22 '26

Its very easy to reproduce and thus other users might lose their accounts, in this case if you have your own implementation you can generate possible codes based on the time of your request and the rand part should be deterministic based on that seed so you have a real chance of generating a correct code

5

u/AtlanticPortal Sep 22 '26 • Bravo

It’s the predictability of the value. You don’t have to try all MD5 values. Only the ones that are the image of the really small group of strings generated by that function.

11

u/UntitledRedditUser Sep 22 '26

If you know the time a key was generated you could just generate it yourself, and reset the password.

They aren't using microseconds, they are using seconds.

Also php directly states that srand is not suitable for this type of use: https://www.php.net/manual/en/function.srand.php

11

u/Rek9876boss Sep 22 '26 edited Sep 22 '26

I'm pretty sure they are using microseconds. microtime() with no arguments returns a sting with time in seconds since last second accurate to the microsecond, a space, then integer number of seconds since unix epoch. Casting to double keeps the first part and gets rid of the second, so multiplying by a million just gives the number of microseconds since the last second.

Granted, you can check latency yourself to find a time window of a couple milliseconds when the code was generated, so you only have to check a few thousand codes, so it's definitely not secure.

3

u/Environmental-Ear391 Sep 22 '26 edited Sep 22 '26

Absolutely this and modern multi-core systems with open access to GPU float processing through abuse of shaders in 3D geometry operations makes the implications of time between the initial data and generated possibles for OTP hacking a forced reset of your main password significantly trivial in time required.

even with the data to process as formatted strings...

I am in the niche case of having FPGA hardware as part of my desktops motherboard for two machines I personally own, so making use of hardware like that makes it even quicker for usable results....

My own systems primary delay would be time between packets for network query/response operations. the actual time spent setting up the packets to send is trivial in comparison to network delays

0

u/UntitledRedditUser Sep 22 '26

Php is so cursed yeah, it's actually worse than I thought.

It's not the current time in microseconds, but a floating point number of seconds passed since the last seconds.

After the little math expression, it essentially goes from 0-999000, but since the precision is only 3 digits, it only has 1000 effective values.

How did this not generate a million duplicate keys?? Or maybe I'm wrong? I just tested it in an online runner.

3

u/Rek9876boss Sep 22 '26

Sorry, it multiplies by a million, not a thousand. My bad. I'm pretty sure it should go from 0 to 999999

-3

u/groumly Sep 22 '26

What the actual fuck?!? PHP needs to be sued for shit like that…

I went from “ok, it’s predictable, but the range of possibilities is still big enough that a brute force attempt should be easy to spot, this isn’t too bad” to “wait, what, so there’s a one thousand possible codes, total?!? Actually, less since md5 can collide the 1k inputs”…

2

u/UntitledRedditUser Sep 22 '26

Also rand is not cryptographically secure.

2

u/OrangeNood Sep 22 '26

srand() sets the random seed. And it uses microtime(), which is current unix time in micro-second (1/1000000 s). That's the only randomness. The rest is 100% predictable.

2

u/TheReycko Sep 22 '26

md5 is fine but this makes only 10000 possible values, plus md5 is not cryptographically secure, in fact it's only O(n) which makes bruteforcing very easy and fast for 4 bytes, I'm not good with time complexity but I'm pretty sure bruteforcing would be O(4*10000) which on a fast pc is doable in seconds if optimized My phone could calculate everything in 4m30 of real time via a single threaded bash script for instance

1

u/Neither_Berry_100 Sep 22 '26

Run it on your own account to get an idea of the delay. Then keep trying random times around that delay on new accounts. Automate this and you will break into many accounts.

1

u/No_Complex_18 Sep 23 '26

Rand(1,9999) limits the possible values of evcode to 10000 possibilities. Potentially further reducible by estimating the microsecond time and therefore the rand seed.

With ratelimit this is probably not exploitable for one specific user, but if you try thousands of accounts you will crack some.

1

u/yahluc Sep 23 '26

Yeah, all you really need is a list of emails from any kind of leak (doesn't have to be for this specific website even) and you're almost 100% guaranteed to get access to a few accounts in almost an instant.

1

u/binarywork8087 Sep 25 '26

let me check this... this is not very well explained....

1

u/enzodr Sep 26 '26

There are also not enough microseconds for this to be secure. About 10^13 per year. This sounds like a lot, but in terms of cryptography it’s small enough to take advantage of

1

u/Rough_Priority_9294 Sep 26 '26

It's both. The PRNG is initialized with a number that could technically be predicted. Even if this would get fixed, the rand range is extremely small so it basically takes only a 1000 tries to bruteforce the code.

-7

u/hot10010 Sep 22 '26

md5 is cryptographically broken

19

u/Kinexity Sep 22 '26

I don't think that's relevant in this case.

13

u/CzechFortuneCookie Sep 22 '26

MD5 being „cryptographically broken“ has got nothing to do with the shitty implementation of the recovery code generating algorithm. It would still be broken and „predictable“ no matter if you used MD5, SHA256, 512, 1000000000.

3

u/groumly Sep 22 '26

That doesn’t matter at all here.

We’re not trying to generate a collision, we’re trying to find the number that `rand()` returns. And given the seed, it’s not particularly hard.

327

u/lurebat Sep 22 '26 edited Sep 22 '26

Mmm, one code per microsecond So if you attack someone by requesting a reset on their email, and the response comes back in 30ms, it means there are only 30000 options of passwords.

Let's be charitable and say the give you only 5 attempts per code, that still means 1/6 chance of guessing correctly. Infinite attempts.

247

u/SirBackrooms Sep 22 '26

they give you an unlimited number of attempts per code

54

u/ernandziri Sep 22 '26

In that case, does it even matter how they generate the secret?

45

u/SirBackrooms Sep 22 '26

it does. there were only 10^4 options before, which i could bruteforce in under an hour. now it seems to be an actually cryptographically random 256-bit one, not something you can just bruteforce by sending requests to the server.

3

u/NooneAtAll3 Sep 22 '26

why would 104 take an hour?

8

u/SirBackrooms Sep 22 '26

because i was sending requests to the server. when i knew the code, checking all possible integers and their associated potential codes took like a second

11

u/ernandziri Sep 22 '26

I was thinking of 2fa codes for some reason. Makes sense!

3

u/sage-longhorn Sep 22 '26

Nope not with only 4 digits

134

u/lurebat Sep 22 '26

Of course

4

u/flooronthefour Sep 22 '26

how sweet of them

1

u/dontdoxme33 Sep 28 '26

So the flaw isn't in this code itself (though it's a bit strange) it's in the fact that they don't invalidate the code once a certain number of attempts are attempted.

I'm assuming this code gets stored in a database somewhere to be checked for validaty later correct?

(And by "code" in the second paragraph, I mean the actual code that gets sent to the user)

13

u/SirBackrooms Sep 22 '26

per microsecond, but yes

4

u/Ectrian Sep 22 '26

Does this matter? They're still only rolling a random between 0-9999, what they're seeding is kind of irrelevant, that's easily brute forceable on its own.

3

u/SirBackrooms Sep 22 '26

It ultimately doesn't, because of that, but even if they were rolling a bigger random number, this code would be flawed due to this aspect.

Also note that the comment I was responding to originally said "millisecond" and not microsecond.

2

u/lurebat Sep 22 '26 edited Sep 22 '26

They multiply microtime by 1 million, making it a millisecond, no?

Edit: oops I got it mixed up with divide

1

u/SirBackrooms Sep 22 '26

one might think

2

u/NullOfSpace Sep 22 '26

The accuracy would still be down to the microsecond

4

u/ComprehensiveWing542 Sep 22 '26

But let's be real that is mathematically possibly but there are many variables that make it impossible to execute or near impossible to if you will If an attempt to reset password would be done would also be done for someone worth the grain even as its something that not everyone can execute by his own

-2

u/dydhaw Sep 22 '26

Wrong, it's very practical. even ignoring the fact that the password space is only 10000 because of the following rand, which is completely broken, you could e.g. get a VPS on the same cloud provider and AZ, giving you low latency network access to that particular server (and possibly even a synchronized high resolution NTP). Then you could request a reset on your own account and deduce the clock offsets based on round trip request timing.

1

u/ComprehensiveWing542 Sep 24 '26

I'm sorry to say but I may have read your explanation 3-4 times and it doesn't make sense to me. The variables I'm talking about have to do with reset password number of times you can request in a given time. Nr of requests you can sent to that account resetting the password before being blocked. Nginx, web throttling bot detection and many other techniques which are standards in backend dev.

2

u/dydhaw Sep 24 '26

Why would you assume any of those are in place here, they are using a 4 digit number as a password and according to another comment did not have any attempt limits or lockouts. If you can determine the clock offset to the server in ms resolution and have a stable RTT you can realistically narrow it down to ~1000 seeds. But of course that's moot since the password's so weak anyway. Of course if proper techniques were in place that would make attacks harder (not impossible, mind you.)

2

u/ComprehensiveWing542 Sep 24 '26

Don't put words in my mouth my statement was: "that make it impossible to execute or near impossible to if you will"
I did check your methodology, and it does seem to make sense. It also seems quite possible, but again, only if they haven't implemented even the most basic security measures, which most frameworks provide by default nowadays.

2

u/dydhaw Sep 24 '26

I think this is a common, I'd call misconception, that devs have about the attacker mentality: the research gap (impossible->possible) is almost always the bottleneck for an attacker, the engineering gap (possible->practical) is secondary. defensive layers like the ones you mentioned are there to mitigate opportunistic attackers, but they can't stop dedicated ones.

With the gap between opportunistic and dedicated attackers rapidly closing (thanks to AI), I think it's important to understand that "almost good enough" is not good enough for security.

(I may be biased, over a decade in cybersecurity research)

2

u/ComprehensiveWing542 Sep 24 '26

No i definitely hear you and I was amazed (rabitt hole really) when researching a bit the method you talked about. I have used AI to find weak points on webapps I've developed and it really does help a lot in narrowing down paths one may take to infiltrate some system.

Yet i talk from what i see in my exp in web dev (where i mainly get to maintain as well as work on the security end of things) that as you said to at least make it harder for someone to get into these systems and having throttling is one of them.

Got to accept however hacking has never been easier (from a time when just getting into the basics took one years)

6

u/Sanae_ Sep 22 '26 edited Sep 22 '26

Even better than the 30k: rand(0,9999) means at most 10k passwords will be generated, for all users - the substr might further reduce that number.

Edit: brb, computing...

Edit2: at most 9997 passwords.
Not really surprising to lose losing 3 passwords, as MD5 is a hash, it tends to cover the whole space.

2

u/Wessel_89 Sep 23 '26

There are only 9999 options regardless if you know the time.

1

u/SarahC Sep 22 '26

But those 30,000 are 1 list from a list of 30 shurely?

So if it takes 30 milliseconds - but you call on ???????01 you know the next one would be ?????????31 and so on......

But if you call at ???????02 you're then getting numbers seeded every 30, but from 02.....

I figure I'm missing something with all your upvotes, and I'm not very smart - could you add a bit more detail?

5

u/lurebat Sep 22 '26

It goes through an md5 hash, so it's not sequential. But you can pre calculate them and then try them on.

1

u/iTrooz_ Sep 23 '26

Wouldn't it be 10000 passwords per microsecond, because of the rand() ?

2

u/lurebat Sep 23 '26

They seed the rand based on it, a seeded rand will always give the same results in the same order.

1

u/Ariquitaun Pronouns:This/Self Sep 26 '26

You'd be able to brute force that easily in seconds

62

u/Responsible_Royal_98 Sep 22 '26

I’ve seen worse. I know a site that just base64 encodes your email and uses that as the “authentication” token for the password reset link. If you realize what’s going on it removes all guesswork in taking over an arbitrary account.

22

u/BakuhatsuK Sep 22 '26

I always base64 decode any string I see that starts with ey or ends with an = sign. Every single one

1

u/Lolllz_01 Sep 24 '26

Whats the ey marker?

5

u/marl6894 Sep 25 '26

Left curly bracket, usually gonna be JSON

97

u/DenyCasio Sep 22 '26

I exploited a similar feature of some gambling bots in oldschool runescape. They would output their calculation time in the chat and the number you "rolled" when playing. So i wrote a program that would tell me when to accept a trade to get a win. It worked about 80 percent of the time. I couldn't tell if it was lag, or seeded losses to ensure the house didn't lose too much money.

They changed their bots shortly after to not output verbose info.

5

u/Hottage [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 22 '26

Okay it looks gross and deterministic but is it actually an issue?

You'd have to know the exact microsecond that the request was made (including any minute time variance on the server) to be able to exploit this.

The most egregious part of this code would be the keyspace only being 10,000 wide.

As an example:

$microtime = microtime(); // 0.70375600 1790086788
$seed = (double)$microtime * 1_000_000; // 703756
srand($seed);
$val = rand(0, 9999); // 4997
$hash = md5($val); // ccf8111910291ba472b385e9c5f59099
$code = substr($hash, 2, 6); // f81119

If you miscalculate the attack time by even one microsecond:

$microtime = microtime(); // 0.70375700 1790086788
$seed = (double)$microtime * 1_000_000; // 703757
srand($seed);
$val = rand(0, 9999); // 8045
$hash = md5($val); // 704cddc91e28d1a5517518b2f12bc321
$code = substr($hash, 2, 6); // 4cddc9

If anything the issue these days would be that you could throw a few hundred requests at the validation endpoint in a second or two to try and brute force it.

22

u/SirBackrooms Sep 22 '26

you could bruteforce it since there were only 10^4 options for the code. i know this since did it myself on my own account.

however, using the microsecond is an issue in itself. say you know it ran within a certain 30ms window. that means there are only 30000 options, which can also just be bruteforced.

8

u/Hottage [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 22 '26

But to narrow the keyspace for the 30ms window, you still have to know the exact clock time of the request on the server, which is affected by many external factors such as clock drift, request duration and network latency.

There are only 10^4 options per 30ms window, however that only assumes you know the exact microsecond the server generated the seed, for all intents and purposes there are still 26^6 outside extremely controlled conditions.

Let me be clear, it is not a good implementation. It is an adequate one for anything not mission critical.

3

u/SirBackrooms Sep 22 '26

seems like you're overthinking something. first, there were 10^4 options in total, since the int between 0 and 9999. so you could just bruteforce them, which is what i did. now, even if they didn't do that capping, there would only be 3*10^4 options if you knew the 30ms window, since the pseudo-RNG is deterministic for a specific seed. still very much bruteforceable.

3

u/Hottage [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 22 '26

I guess the knowledge that you only have to dictionary attack the substr(md5($x), 2, 6) of the range $x = 0 to $x = 9999 is the most egregious part of this,

7

u/WhatImKnownAs Sep 22 '26

As you say, there are only 10000 options: That rand(0, 9999) will always return one of the integers 0...9999. The seeding, as bad as that is, only affects which one of them it gave.

I suppose 30000 sequential seed values might not result in all the 10000 possible rand() values, but it's unlikely to be significantly less. It's only if you could estimate the time to better than 10 ms, that you could use the seed values to restrict the set of possible reset codes further.

10000 is already bad, especially if they really allowed infinite retries as you stated.

1

u/Blockpair Sep 26 '26

Ignore the timing at the start as its not relevant. There is a possible 10,000 MD5 hashes of numbers. Then, among those hashes are 6 character sub-strings over their values (starting at offset 2.) Okay now, this is where it gets much worse for 2 reason:

the output of MD5 here is in hex
its MD5 which is cryptographically weak

So for any 6 characters you're saying that the possibility isn't between 0 and 255. It's only between 0 - 9 and A - F or 16 characters. That greatly reduces the entropy even more. It's possible but unlikely too that some of the 10,000 inputs to MD5 produce the same substrings due to the way hash functions work.

So it ends up resulting in secrets that are incredibly easy to brute force: 16 ** 6 -- 16,777,216. If its external HTTP brute force its going to take a while but hash-based would be broken in seconds.

18

u/NightmareJoker2 Sep 22 '26

Well, at least it’s not as bad as Facebook. They generate a code and store it until it is used or revoked, and simply resend the same code if you make a reset request again.
I receive multiple requests daily, because someone keeps wanting to steal my username.

7

u/EdelWhite Sep 22 '26

Meh it's probably fine. Definitely random enough to deter script kiddies. Would do nothing against a targeted attack tho.

3

u/Wessel_89 Sep 23 '26

A script kiddie can easily brute force 9999 passwords

1

u/EdelWhite Sep 24 '26

It's a reset code, not a password. 

2

u/Wessel_89 Sep 24 '26

They can also easily brute force 9999 reset codes

4

u/Single-Virus4935 Sep 23 '26

Two things would make this secure:

  1. Use a actually cryptographic RNG.
  2. Use a salt or pepper. 

Best to combine both so even if one of the two is broken/leaked it is still not brutforceable. 

7

u/darksteelsteed Sep 22 '26

So the first bit of the srand being based off timestamp in usec. You request a password reset email. You look at your own clocks time. You then apply the algorithm using different seeds by taking your own clock back or forth microsecond at a time until you get a match. Your search space will be maybe a 5 min window in either direction. So that is 600 million md5 combinations to try. Trivial on your gpu to brute force ... or you just use rainbow tables because its md5. Then you sync your clock with that info to match the servers clock. Then you can issue password recovery requests and since you now have the same time as the server your search space is radically reduced to only a couple of million tries. You can even Then go further and fingerprint the servers timing to narrow this further until you can literally generate password reset codes without needing to receive the emails at all. That is why this code is not secure

1

u/TenThousandArabs Sep 22 '26

Rainbow table of what? All validation is on the server I don’t see how you’re able to brute force this locally 

1

u/darksteelsteed Sep 23 '26 edited Sep 23 '26

https://en.wikipedia.org/wiki/Rainbow_table Because your search space for the input to the md5 hash is only 10000 fixed combinations, using rainbow tables can greatly accelerate your cracking time as you only have a space of 10000 to work with, and instead of calculating the md5s you just look up the answer from a dictionary. Honestly its theoretical. Considering there is only 10000 combinations rainbow tables aren't even needed.

The harder part is synchronizing the timing between your request and the server processing it. However as I described above you first try sync your clock to the servers to decrease your search space. Then you go further by fingerprinting the timing of the server, and handling your latency right you can predict the generated pin to complete the password reset for any account.

Its not all local. You still have to repeatedly probe the target server for this. However http1.1 and http2.0 let's you stream multiple requests without closing the connection, so you use this too to your advantage. Unless they have a woke security team and waf or hide behind cloudflare it would take time but you would be able to get it right to password reset anyones acc eventually.

1

u/ElectricalPrice3189 Sep 27 '26

Only 10k possible codes, or am I reading things incorrectly?

1

u/Yekyaa Sep 27 '26

Isn't MD5 hex? Should be like 164 - 1, or 65535

5

u/SirBackrooms Sep 27 '26

You're missing that it's hashing an integer that is always between 0 and 9999.

0

u/Gammelkebab Sep 22 '26

So am i reading this code correctly? row 1 does nothing, row 2 takes a rnd 4 digit nr and the 3rd row reduces the resulting String to the 2nd to 6th digit. But the nr ends after 4 digits? i m confus

5

u/SirBackrooms Sep 22 '26

row 1 seeds the pseudo-RNG with the current time in microseconds (poor practice, since the time is roughly known by both parties)
row 2 uses md5 to hash a "random" four digit number (deterministic, based on the seed) into an a string
row 3 uses 6 hexadecimal digits (indices 2-7) of that string as the "secret" code

php's substring works differently to javascript's. the second int is the length and not the upper index.

11

u/AdorableZeppelin Sep 22 '26

srand seeds the random number generator. The second line hashes a "random" number (it's no longer random because of the seed). The third line takes a subset of the hash and that's the reset code.

The problem is 1. using a reproducible seeding mechanism (time) and 2. using a substring of a hash since a hash will always produce the same output given the same input.

In short if you know roughly when the code was created you could brute force it.

2

u/devrelm Sep 22 '26

srand seeds the random number generator.

Pass the same number to srand, and the next call to rand(0,9999) will always be the same, even on different systems.

So if you can guess the time that the random number was generated, then you can narrow down which random number will be produced. A few runs on a test account could help narrow it down by comparing the actual produced code against a rainbow table of expected codes to figure out a range of latency between the time the request is made and the time the code is generated.

Further, there's only 10000 options which in itself may be easily guessed or brute-forced.

1

u/efari_ Sep 22 '26 edited Sep 22 '26

I don’t know anything about python (or whatever language this) but I guess the first line seeds the RNG with current time in microseconds

Then line 2 takes the md5 hash of a random 4-digit number (given the seed on line 1)
Which is a hex string of 32 character long.

Line 3 takes 4 characters thereof

3

u/Genmutant Sep 22 '26

That looks like PHP.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 23 '26

What does multiplying by 1 million accomplish?

1

u/Weird-Boat3953 Sep 23 '26

integer. srand want integer number. This whole pattern is in PHP docs

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 23 '26

So why not multiply by 1 or something? I saw a comment mentioning it, and that's probably why they were doing that, but I didn't see much explanation as to why.

1

u/Weird-Boat3953 Sep 24 '26

Because microtime returns seconds(integer) + microseconds(beyond 1)

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Sep 24 '26

I don't what that means, so I looked it up. It looks like it returns a string, unless you pass true, then it returns a double. The OP code doesn't pass anything. I don't remember the string to int conversion rules, but it says the format is "msec sec", so my guess is it ignores stuff after the space.

1

u/hypnokev Sep 23 '26

I love this. I can’t even tell you about the worst one I found on an IoT pentest. Suffice to say I defeated their AES-128 encryption solution that day, and it took just under 1,000,000 guesses. Impressed this one requires fewer.

1

u/DT-Sodium Sep 23 '26

Even if you forget how the rand is seeded, what's still only 10k passwords to brute-force through.

1

u/JerryAtom Sep 23 '26

PHP programmers, it explains a lot

1

u/theWildBananas Sep 24 '26

Fixed by using an actual programming language

1

u/M4elstr0m__ Sep 24 '26

I see two issues: the code, and the PHP lang itself (just kidding dont hit me)

1

u/AmazingGrinder Sep 24 '26

"A website" and it's Wikidot, current hosting of multiple popular international projects like SCP Foundation.

1

u/CondiMesmer Sep 25 '26

I mean it's probably good enough for most real world stuff. It'd be pretty hard to exploit, even if it's deterministic.

1

u/ferrybig Sep 29 '26

If you send a request to their servers from close to their datacenter, it is easy to guess the time on the server, especially if that web application also exposes dates in resources you make or the HTTP date header.

1

u/binarywork8087 Sep 25 '26

sorry but if it works dont touch it and if it is very well developed it is just ok

1

u/binarywork8087 Sep 25 '26

this is that obscurity problem now I got it

1

u/binarywork8087 Sep 25 '26

the problem is md5, is it?, too flawed these days....

1

u/Crazo7924 Oct 20 '26

I think no one asked the real question:

Which website was affected?

1

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: Sep 22 '26

Now that is a horror.

1

u/AdditionalTerm732 Sep 22 '26

Nobody: ... Absolutely nobody: ... The dev: Forgets the admin password, deletes the entire website in frustration, completely forgetting the password was literally open-source on GitHub for 10 years.

1

u/Memw1 Sep 22 '26

First you gotta know if in the second you choose someone decided to reset their password, then you need to test every microsecond possibly getting you ratelimited.

1

u/Medium-Head5014 Sep 23 '26

Better i would not say but a huge improvement over the previous one, that one was making a mess, i think its on par with fable 5.1 from what Ive seen today

0

u/empwilli Sep 22 '26

Is the fact that its not really seeded at random really an issue? I feel that without proper rate limiting or similar the 17895680 options can easily be brute forced. Even simpler if I focus on the 2 or 3 char long codes.

1

u/capfsb Sep 23 '26

rand(0,9999) -> only 10 000 unique keys. If they allow only 3 attemps per code per email per day you could ask "reset code" for 3333 accounts and probability of open 1 account of them is about 100%

0

u/empwilli Sep 28 '26

However, its only 10000 unique Keys, If you manage to factor in the timestamp. Otherwise, the seed changes these 10k.

1

u/capfsb Sep 29 '26

Check yourself

2

u/obeythelobster Sep 23 '26

Would it be secure with a fixed seed at start-up for all requests + a salt?

0

u/[deleted] Sep 22 '26

[deleted] — view deleted comment

1

u/Oakchris1955 Sep 22 '26

It's Wikidot, under grabrys/wikidot on Github