r/linuxquestions • • Jun 02 '21

Resolved Why does checking a wrong password take longer than accepting the right password?

During login, or when using sudo my password usually gets accepted instantly, if I type it correctly. If I type in a wrong password though, my PC will take 1-2 seconds until it says it's wrong.

Why is that?

247 Upvotes

67 comments sorted by

250

u/aioeu Jun 02 '21 edited Jun 02 '21

The delay is deliberately added by the underlying PAM module that's doing the password check to make it harder to brute-force login attempts.

It would be nice to not have the delay on a GUI login, and to provide some other rate-limiting tactic (e.g. a 10 second timeout every three invalid password attempts)... but unfortunately that's not how it works.


Edit: Looking in to this a bit further... a GUI login manager (e.g. GDM or SDDM) could override the standard PAM failure delay logic and handle the delay itself in some "smarter" fashion. PAM allows the application to register its own delay function by setting the PAM_FAIL_DELAY PAM item.

81

u/quiet0n3 Jun 02 '21

Yeah the 2 second delay or whatever it is makes a huge difference. Goes from hours to 100's of years.

45

u/[deleted] Jun 02 '21

Novell Netware had a nice trick. After getting the password wrong past the limit, each guess would be wrong even if the right password is guessed. The password guesser had to stop for a time to get that to reset. An improvement on that would be that for anyone but root the lockout never resets (It has to automatically reset for root or else root could get locked out with no one to unlock them)

18

u/raptir1 Jun 02 '21

I feel like that only works if no one knows about it, right?

45

u/Hayden2332 Jun 02 '21

And would be annoying as hell for people who just accidentally guessed there own password wrong before getting it right and losing their minds

8

u/[deleted] Jun 02 '21

u/Hayden2332 They would have to guess it wrong a number of times. The system I ran had it set to 7 times. And authorized users would know about it and call the help desk to get unlocked.

u/raptir1 - why? I suppose if a bot got it wrong once it could back off for the unlock time before trying again, but then we have the same situation as we have now - long waits on wrong passwords.

3

u/raptir1 Jun 02 '21

Well, I was thinking of it from the "enhanced security" perspective, not the "less inconvenience" perspective. In the best case scenario this could be much more resistant to brute force than a simple delay on an incorrect password.

6

u/nekoexmachina Jun 02 '21

it would take much shorter time to bruteforce a password one have considered secure in 90's today due to hardware progression. forced delay eliminates this.

1

u/NormanClegg Jun 02 '21

Happens 10,000,000 times per day I am sure.

1

u/ManInBlack829 Jun 02 '21

Am I starting to realize why no one uses Novell anymore?

0

u/[deleted] Jun 02 '21

[deleted]

2

u/TurnkeyLurker Jun 03 '21

MS Teams uses the "Something went wrong." error message, along with a random piece of clip art (bicycle, bird, mountain, whatever).

Why are they showing me random clip art? It's just dumb.

1

u/NormanClegg Jun 02 '21

Are you saying that security via obscurity is a false ?

3

u/d0ubs Jun 02 '21

Openmediavault has the same trick

2

u/Phoenix591 Jun 02 '21

there are pam modules that do that more or less.depending how you configure it.

1

u/[deleted] Jun 02 '21

Indeed - that sounds interesting.

1

u/132ikl Jun 02 '21

I'm pretty sure PAM already has this and it might even be default at least on Arch Linux

1

u/JustLemonJuice Jun 02 '21

Thanks! I was already theorizing about the computer trying out multiple hashing algorithms, etc...

6

u/Sol33t303 Jun 02 '21

Could this be worked around by starting a bunch of instances of sudo and rotating through them?

Say you have one instance of sudo that takes 2 seconds as a timeout. What if you start up say 5 instances of sudo and rotate through them? You could guess a password on instance 1, then while waiting for that guess a password on instance 2, then while waiting on 2 guess a password on instance 3 until you get to 5. Then just make sure you timed it properly so right as you guess a password for instance 5 instance 1 hits two seconds and is ready for a new password, then repeat until you hit a password.

Go from 5 instances to thousands or millions or billions and you would be back to effectivelly full speed in your cracking attempts. Or does sudo have some kind of defence against this as well?

3

u/SlinkyAvenger Jun 02 '21

I believe PAM runs serially for authentication calls and blocks remaining calls while it does so.

5

u/aioeu Jun 02 '21 edited Jun 02 '21

No, they're completely different PAM sessions in completely different processes. There is no global, system-wide locking โ€” I think that would be a DoS waiting to happen.

3

u/aioeu Jun 02 '21

Yes, the PAM fail delay logic is effectively useless against multiple parallel password attempts.

But that's kind of tricky to do for a login screen, for instance.

1

u/Godfatherr_xxx Jun 05 '21

This is interesting

5

u/Vlad_The_Impellor Jun 02 '21

Seems like a fixed delay is itself a vulnerability, since the hash time can be guessed from a fixed delay if the hardware is known.

usleep(PAM_FAIL_DELAY + (random() % 5000));

would be better. Or, overkill.

9

u/JDaxe Jun 02 '21

How would guessing the hash time help an attacker?

13

u/Vlad_The_Impellor Jun 02 '21

Knowing the hardware, hash time, and delay constant, you could theoretically simulate the comparison hash bytes, but not their order. If you have the bytes in any order, the hash can be brute forced on a system with no delays into a set of more-likely passwords to be tested.

I don't know of anyone succeeding with that, but why risk it? The proc is sleeping anyway and random isn't expensive. Volunteer nothing.

2

u/vontrapp42 Jun 03 '21

How is this worse than not doing any delay though?

4

u/Vlad_The_Impellor Jun 03 '21

Not doing a delay is very like having no passwords.

It turns out Pam effectively randomizes the auth failure, so it's moot. But, if the delay were a constant, you can make close guesses at the probable hash bytes being compared by CISC hardware. You can't guess the order, but that's a horizontally scalable problem.

It's not constant though, so the internal timing is hidden from the observer.

1

u/vontrapp42 Jun 03 '21

Wouldn't the hash compare be peanuts compared to the hash computation? How do you separate the two?

8

u/vikarjramun Jun 02 '21

Theoretical side-channel attack. Pretty sure modern hash functions are devised to make this irrelevant though.

5

u/aioeu Jun 02 '21

You're talking about timing attacks. The PAM fail delay is not intended to be resistant to timing attacks.

Moreover, simply adding a random number to the delay (which the PAM fail delay actually already does) does not make timing attacks harder anyway. Random numbers are uncorrelated, which means it's just noise that averages to zero over multiple runs.

Code that wants to be resistant to timing attacks needs to use constant-time algorithms. The underlying password hash function that PAM ends up calling might use this, but PAM itself does not have any constant-time algorithms in it.

2

u/moratnz Jun 02 '21

My understanding is that the delay is explicitly to stop the side channel attacks; the delay isn't a fixed amount of time after failure, it's a variable delay to make sure the failure returns st the same time regardless of how long the hash check takes.

3

u/aioeu Jun 02 '21

No, the PAM fail delay logic is not intended to thwart timing attacks. It is not a constant-time algorithm. (The underlying password hash function might be constant-time, but that's not really part of PAM.)

1

u/moratnz Jun 02 '21

And now I'm slightly less ignorant. Thanks :)

1

u/Vlad_The_Impellor Jun 02 '21

It might very well be as you describe. It's been a long time since I looked at login. That strategy is much better than my solution.

1

u/[deleted] Jun 03 '21

You wouldn't like it for three failures right now anyway. The current upstream defaults completely lock the account after 3 wrong passwords, for 10 minutes, or less if you're already logged into it or another admin account and can faillock reset yourself before it times out. Or if you do like I did once and just ctrl-alt-del reboot the machine to dodge the timeout, before I remembered you can faillock reset yourself.

1

u/sBartfast42 Jun 03 '21

I think they also wanted a fixed delay so as not to give any clues as to how wrong/correct the password was

1

u/aioeu Jun 03 '21 edited Jun 03 '21

No, you've made exactly the same mistake a lot of other people have in this post.

PAM does not do anything to prevent timing attacks on the underlying password hash. The PAM fail delay (it isn't a fixed delay, but it might as well be) we've been discussing here does not hide the time it takes for the password algorithm to check the password. Nothing in PAM is dependent on the password or its length, so PAM shouldn't make things worse, but PAM does not go out of its way to make timing attacks harder.

The fail delay's sole purpose is to slow down brute force attacks.

1

u/sBartfast42 Jun 03 '21

Ah, OK. Thanks for the correction๐Ÿ‘.

4

u/TimurHu Jun 02 '21

If you use Gnome and its login manager, then you can hit the ESC key to make it stop spinning while it is 'checking' the wrong password.

3

u/JustLemonJuice Jun 02 '21

That's good to know, thanks!

Wouldn't this circumvent the anti-bruteforce functionality?

3

u/TimurHu Jun 02 '21

I'm not a security expert so can't answer that. All I know is that when I realize I mistyped my password, I hit ESC and then it lets me retry quicker.

2

u/Logan_MacGyver Jun 03 '21

Same experience with KDE

37

u/alex_ch_2018 Jun 02 '21

To make it a bit harder to an intruder to login into the system by e.g. iterating over a password dictionary.

0

u/[deleted] Jun 02 '21

[deleted]

2

u/istarian Jun 02 '21

The time complexity of most hash algorithms is probably known... And it's probably closely related to password length.

Combined with other pieces of information that aren't secret that might help you discover things that are.

0

u/[deleted] Jun 02 '21

[deleted]

0

u/[deleted] Jun 02 '21

[deleted]

0

u/[deleted] Jun 02 '21

[deleted]

0

u/[deleted] Jun 02 '21

[deleted]

2

u/VulcansAreSpaceElves Jun 02 '21

You could do that or, with much fewer lines of code you could add a random delay between 0 and 100,000 microseconds to each password failure and get totally effective protection that doesn't even register as different to a human from the 2 second delay for the prevention of brute force attacks.

1

u/AnAirMagic Jun 02 '21

The "correct" way to do that is to use a constant time algorithm (as the grandparent comment alluded to). Here are some examples. They essentially boil down to looking at the entire string buffer before making the decision, no matter the input size, or when the first mismatch occurs.

14

u/the88shrimp Jun 02 '21

More than likely a brute force countermeasure.

11

u/PageFault Debian Jun 02 '21

I saw an article some years ago where people were able to determine how close they were to a password based on how long it took to compare.

This is likely irrelevant now with the prevalence of hashing, but the idea is that checking correct password takes longer than checking an incorrect password since it can fail on the first incorrect character.

So, say my password is hunter2

Checking with strcmp( passwd, "abc" ) takes less time to check than strcmp( passwd, "hun" ) since it can fail on first incorrect character. Using this information could shorten the time needed to brute force the password. Adding a small, slightly randomized delay would simultaneously prevent this, and reduce the number of possible attempts per time period on same tty.

12

u/LOLTROLDUDES Jun 02 '21

To prevent bots from trying passwords a lot.

For example, magical 0 ms delay, infinite passwords per second, they're in.

Three second delay, they can only guess one password every three seconds.

4

u/thenebular Jun 02 '21

It's a security feature. The delay is to make brute forcing the password through that input take so long it's not feasible.

6

u/apocalypsedg Jun 02 '21

It's dumb I know but it's too prevent attackers from bruteforcing it. Really annoys me when trying to login, it should be 0.5 to 1 second not like 3 or 5 seconds or whatever ridiculous length they set it as, it makes the whole logging in experience feel slow and clunky.

0

u/[deleted] Jun 02 '21

[deleted]

2

u/rw3iss Jun 02 '21

Thank you! ๐ŸŽ‰

0

u/[deleted] Jun 02 '21

[deleted]

1

u/rw3iss Jun 02 '21

Yeah, thanks. Not doing anything serious and am usually home anyway, keep laptop close.

2

u/Kriss3d Jun 02 '21

But a good system should let you do say 5 fast password attempts then double up with 1 second per guess as delay. This way you can get a few attempts and the only way to reset it is either no guessing for say an hour or if you get the right login.

1

u/[deleted] Jun 02 '21

[removed] โ€” view removed comment

1

u/ManInBlack829 Jun 02 '21

A lot of Android phones do 5 attempts per minute. Not saying it's ideal but it's a way of looking at it.

1

u/[deleted] Jun 02 '21

To stop brute-force attempts.

1

u/michaelpaoli Jun 03 '21

Why does checking a wrong password take longer

It doesn't. A failed login authentication has been attempted. Most of the time after that an intentional delay is added, to slow down any brute force attempts - e.g. trying lots of passwords in quick succession.

1

u/atsuko_24 Jun 02 '21 edited Jun 02 '21

Key stretching. It makes attacks take much longer, whether it's a shitty acquaintance or snoopy SO who has a few ideas what your password may be, or randos from a dozen countries hitting your server with dictionaries.

-1

u/AssumeACanOpener Jun 02 '21

Ha, call me crazy, but I set up all my local accounts to just automatically log on. I mean, if you get physical access to my machines then I'm already done for. Or sure, I can and do encrypt things, but still, I like convenience for the most part.

4

u/krisvek Jun 02 '21

Call me crazy, but I close and lock the doors to my house at night. ๐Ÿ˜›

0

u/[deleted] Jun 02 '21

[removed] โ€” view removed comment

2

u/kcl97 Jun 02 '21

If the /etc is not encrypted, you can boot in through USB and fudge around the /etc/passed and /etc/group files to regain access. In the old days, one can just set the root to no password, but these days, you probably have to be more clever, like granting sudo to a backup account. Obviously, make sure your computer is physically locked down or private data encrypted if your /etc is not.

1

u/Michaelmrose Jun 02 '21

If its unencrypted you can simply modify the boot settings to boot directly into a shell and change the password. This can be locked down but this wouldn't be much protection by itself.

If the system is encrypted as any laptop should be then you try really hard to remember or cry.