I was wondering what the safest way of a cookie login is? If you just store the pass(encrypted with salt) and username in the cookie and validate it against the user table, a potential attacker can steal a cookie and login. People normally don't check there 'last time online'.
So is there a better way for the 'remember me cookie'? IP isn't a good option, is it? (Some machines change IP all the time).
Most popular ways:
Many scripts uses some kind of session tracking. When the user first visits the website, it generates a unique random ID for the user and store session information in the server and the ID in the cookie. The server then identifies the user using the unique ID (called session ID). The information associated with the session ID can be seen only by the server. PHP uses this by default,
Some stores the user data in the cookie itself, but with a HMAC signature using a secret string as a key. The script discards the cookie if the signature doesn't match. This way, the server does not have to keep the session data on the server. The user sees what's in the session by looking at the cookie, so you should not store sensitive data in it. Just the user ID (and possibly login time and cookie expiry time) should be enough. Though the user can see what's in the session info, the signature in the cookie makes sure that the user cannot modify the session data himself.
These ways provide some security, that the user can't tamper with the session data, but it doesn't protect the user from eavesdropper. They can always use a packet sniffer and steal a session from any open WiFi network. Some apps do rely on the user IP but it doesn't matter if the attacker is in the same network. Some apps rely on User-Agent, but there will be problems when the user updates their browser or import data from another browser.
If you are really concerned about security, then use HTTPS.
Also read this article, especially the section called How do website operators fix the problem?
I think I've found a clever solution!
Advantages of this (complicated?) script:
I've made a table in the database with the following information:
The remember me cookie will have this setup:
Session
will be a string of 40 (sha1) characters.Token
will be a string of 32 (md5) characters.Userhash
in the cookie will be a string of 32 (md5 of username) characters.Username
in the database will be the normal username.Expire
will be now + 60 days.The script:
Advantages of the script:
Reference: http://jaspan.com/improved_persistent_login_cookie_best_practice
Such a “remember me” feature is always an additional security risk.
Because just like in a session you only have one identifier that suffices not just to identify a user (Who is it?) but also to authenticate that user (Is it really him/her?) without doing an actual authentication.
But in opposite to a session that has (or should have) just a short life time (mostly less than an hour) and the identifier is (or should be) changed periodically (time based and on necessity due to authenticity/authority state changes), a “remember me” identifier is valid for days if not even for months or years! And this long validity period poses an additional security risk.
So before asking how to implement such a “remember me” feature, you should ask yourself if you really want that additional security risk. That mainly depends on the assets that your application has and what purpose the authentication has and if you want to take the risk of impersonations/identity thefts that the “remember me” feature poses.
If so, make sure to provide basic security by using HTTPS and set the HTTPOnly flag and the secure flag in your cookies. Then you could do the following to build such a “remember me” feature:
Authentication request
If the user authenticated via HTTPS and set the “remember me” option, generate a random remember me token, store it on the server side in a “remember me” database, and set the remember me cookie with the secure flag with that value. Then start a new session and set a remember me flag.
Any other request
With this the authentication is secured via HTTPS, both the initial authentication and the “remember me” authentication. And the user is only authentic during the current session; if it expires, the user has to re-authenticate either by using the remember me token or providing his/her login credentials. And as the remember me tokens are stored in the database, the user can invalidate any existing remember me token.