php - What is the most secure method of logging someone into a website? -
i trying ascertain best way keep logged website after have verified log in correct.
i tried have @ "keep me logged in" - best approach upvoted answer said should generate token , store token in database! surely wholly unsecure because takes database hack , cookie editing elses account?
could please provide me date secure way of doing this? thanks.
we posted blog secure authentication long-term persistence (a.k.a "remember me"), largest difference between blog post , ircmaxell's answer "keep me logged in" - best approach separation of lookup (which not constant-time) , validation (which constant-time).
in strategy outlined in our blog post, aren't storing tokens in database, you're storing sha-256 hash of token. if attacker leaks these values, has crack sha-256 hashes of strong random tokens. they're better off launching reverse shell lets them authenticate user (or proceed take on entire machine local kernel exploit).
logging in (simple , basic)
use bcrypt.
password_verify()
. don't generate own salts.if want go mile, consider bcrypt + aes library encrypt password hashes (which helpful if have database , webserver on separate hardware, since compromising database won't give them encryption key).
- rate-limit failed attempts. example: after 5 failures per ip or username, require captcha.
long-term persistence ("remember me")
when logging in:
- generate secure random token.
- generate secure random identifier.
- store identifier , token in
rememberme
cookie. - store sha256 hash of token in database.
- when user lands on page, if have
rememberme
cookie, grab identifier , database search. - if there authentication token identifier, grab sha256 hash.
- compare hash of token provided cookie sha256 hash in database using
hash_equals()
. - if succeeds, set session variable user's id. generate new token. if fails, delete entry database.
advantages of strategy
- if successful sql injection attack leads tokens being leaked, attacker has sha256 hash of various tokens. not helpful compromising accounts.
- database searches not constant-time. why identifier separated token.
- sequential identifiers leak activity level of application, many businesses wish keep confidential. random identifier obfuscates detail.
disadvantages
- (future stackoverflow authors should feel free populate list if discovered.)
Comments
Post a Comment