Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
359 lines (340 sloc) 17.2 KB
---
layout: post
title: 'Rough thoughts on Login Systems'
permalink: '/login_systems/'
tags: ['web login', 'security', 'engineering']
---
<div class="lead">
<p>
A few notes about login systems, some day I might turn this page into a better written document.
The context is web applications, but the same principals might be applicable to other domains.
</p>
<p>
A login system is the piece of code which deals with user authentication. The base case is logging a
user into a website when they supply their username and password. There are however many other
cases that need to be handled: resetting a password, detecting account hijacking, remediation, etc.
</p>
<p>
It's interesting to note that a well designed login system is not going to mitigate other security
issues (i.e. a SQL injection can lead to a full compromise). However,
security issues in the login layer can fog the audit trail; everything will look as if the
legitimate account holder took certain actions when that might not be the case.
</p>
<p>
I encourage you to send me questions or suggestions, I will keep updating this page as required.
</p>
</div>
<section>
<div class="page-header"><h3>A few assumptions...</h3></div>
<ul>
<li>
I'm going to assume the login system works with three pieces of information: an account id, a password and a contact information. The
contact information is usually an email address or a phone number, but it could something else too.
</li>
<li>
I'm going to assume that we have some crypto functions (e.g. Encr(key, data) and Decr(key, data)). Let's ignore the key distribution, rotation, revocation issues for now. The ideas here can be used with symmetric or asymmetric crypto.
</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Compromise vectors</h3></div>
<ul>
<li>Weak passwords.</li>
<li>Password sharing across many sites.</li>
<li>Malicious employee forging a session.</li>
<li>Malicious employee adding a backdoor (e.g. adding a security question).</li>
<li>Password database leak.</li>
<li>Compromised email address &rarr; password reset flow.</li>
<li>Phishing sites.</li>
<li>Malware (in the browser &rarr; malicous plugin, OS &rarr; virus, physical &rarr; keylogger)</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Sign up flow</h3></div>
<p>
Before a user can log in, they need to create an account.
<ol>
<li>Many tradeoffs are possible:
<ul>
<li>ask for the contact information (email or phone number) + password, require the user to confirm ownership of the contact information (via email link or sms).</li>
<li>ask for the contact information (email or phone number) + password, let them confirm ownership of the contact information at a later point in time (e.g. within 7 days)</li>
<li>ask for the contact information, let them create the password at a later point (using a password reset flow).</li>
</ul>
</li>
<li>
Generate a new unique account id. It could be fully random (e.g. increment sequential number, hash it, start over if it's not unique), or sequential. I recommend randomly distributed in the account id space.
</li>
<li>
At sign up time, the system should only ever store salted + hashed passwords. Password hash functions are typically: scrypt, bcrypt or pbkdf2.
</li>
<li>
Make sure that browser's password manager saves the signup password and re-uses it for the login flow.
</li>
<li>For phone number + password:
<ul>
<li>Check ability to send text messages to all phone numbers.</li>
<li>Check ability to normalize phone numbers, i.e. +1 650 123 4567 is the same as 1 650 123 4567, but 650 123 4567
is context dependant!</li>
</ul>
</li>
<li>
Normally, you want the sign up flow to end with a session. I.e. don't require the user to login right after
they created an account.
</li>
<li>
If the user fails one piece of the registration flows, sites often require the user to re-enter the password. This
can be very annoying if the user only failed the captcha (which are sometimes hard to pass on the first try).
Various fixes are possible: do not re-request the password if the captcha failed, do validation in javascript/ajax
so that the page does not go through a reload, etc.
</ol>
</p>
</section>
<section>
<div class="page-header"><h3>Login flow</h3></div>
<p>
The base case is prety simple.
<ol>
<li>Given the contact information &rarr; lookup account id &rarr; lookup salt &rarr; compute hash &rarr; compare hashes.</li>
<li>Do you "leak" at login time if the contact information exists or not? (you will most likely leak that in the registration flow?)</li>
<li>Brute force protection:
<ul>
<li>per account id counter.</li>
<li>use a "same ip" or "same machine cookie" check to reduce legitimate user lock off (i.e. layered counters).</li>
<li>have different thresholds and use Captcha to reduce automated lock out.</li>
</ul>
</li>
<li>Automatically fix typos:
<ul>
<li>case inversion in password.</li>
<li>first letter case inversion in password.</li>
<li>domain level correction, e.g. @gmail.con &rarr; @gmail.com (because there is no MX record for gmail.con).</li>
</ul>
</li>
</ol>
</p>
<p>If the login fails, there are ways to help users:</p>
<ol>
<li>Show some public information from the account (e.g. profile picture) to confirm they are trying to log into the right account.</li>
<li>Replace password input field with a text field, so they can see what they are typing.</li>
<li>Provide link to the password reset flow.</li>
</ol>
</section>
<section>
<div class="page-header"><h3>Session Creation</h3></div>
<ol>
<li>you can make the session == account_id + Encr(site_key, account id), but then you can't revoke a session.</li>
<li>you can make the session == account_id + Encr(account_key, account id), and revoke all the sessions at once (by changing the account_key).</li>
<li>you can make the session == account_id + database backed token. You can revoke individual sessions. Limit max number of simultaneous sessions?</li>
<li>
session vs persistent cookies: if you want the user to remain logged-in when they close their browser and return, you need
to set an expiry date.
</li>
<li>
cookie refreshing: you get the value back of persistent cookies, but not the expiry value. If
you make the session == timestamp + account_id + ..., you can refresh the session before it expires.
</li>
<li>
There is no reason to let javascript read the session cookie, so it's worth setting the secure flag.
</li>
<li>
At some point, Internet Explorer had a bug. User logs in &rarr; browses site &rarr; logs out &rarr; hits back button a few times &rarr; browser
re-submits login credentials. This bug could lead to account compromise in shared computer settings. Work around:
<ul>
<li>Use an intermediate page and redirect to different url works?</li>
<li>Use a token in the login form, detect form re-submit.</li>
</li>
</ol>
</section>
<section>
<div class="page-header"><h3>Session termination flow</h3></div>
<ol>
<li>If you have persistent cookies, you need a termination flow.</li>
<li>Make sure session is purged from database + cookie is deleted in browser.</li>
<li>Do you terminate all sessions or only current session? Some services implement "single sign on", but not "single sign off" :/</li>
<li>Make sure back button doesn't give you back a session (cache invalidation). Make sure back button to login page doesn't submit
the login form (does the IE bug + double redirect hack still apply?).
<li>Password change should trigger session termination. Alternatively, there should be a way to view active sessions and terminate "other" sessions.</li>
</ol>
</section>
<section>
<div class="page-header"><h3>Password reset flow</h3></div>
<ol>
<li>
Given a contact information &rarr; send a reset code / reset link. You want the code to be one time use, limited in time:
account_id + database backed token.
</li>
<li>
The user gets to set a new password by visiting the reset link (or manual reset code entry).
</li>
<li>
Normally, you want the reset flow to end with a session. I.e. don't require the user to login right after
they reset their password.
</li>
<li>
Should changing the email address on the account invalidate password reset links sent to other addresses?
</li>
<li>
Should changing the password on the account invalidate the password reset links?
</li>
</ol>
</section>
<section>
<div class="page-header"><h3>Handling account information change</h3></div>
<p>
People sometimes forget to log off from public computers (school, library, internet cafe, etc.).
</p>
<ul>
<li>Adding or removing a contact information
as well as password change should require a re-authentication to prevent account hijacking.</li>
<li>Such changes should trigger an email/sms notification.</li>
<li>Such changes should be reversable (e.g. link or flow to revert the change).</li>
<li>The re-auth flow should have the same rate limiting check as a normal login flow.</li>
</p>
</section>
<section>
<div class="page-header"><h3>Advanced recovery</h3></div>
<p>
There are cases where the password reset flow falls short, e.g. if a user uses the same password for their gmail and your service,
both accounts will get compromised at the same time!
</p>
<ul>
<li>Security question. If you do fuzzy matching, you need to store the security answer in plaintext :(</li>
<li>Require multiple codes, i.e. two factor authentication + email reset link.</li>
<li>Have pre-vetted friends (aka guardians) receive a piece of the reset code. (see ssss).</li>
<li>Micro transactions of random amounts, e.g. $0.02 &amp; $0.42 on user's credit card / bank account.</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Machine cookie</h3></div>
<p>A persistent cookie that is set only once for a given browser.</p>
<ul>
<li>Login notification.</li>
<li>Can be used to decide when to trigger two factor authentication.</li>
<li>
cookie refreshing: you get the machine cookie, but not the expiry value. Make the machine cookie == timestamp + id, you can
refresh the cookie before it expires.
</li>
<li>
Knowing that multiple people share a computer &rarr; don't give persistent cookie (or remember higher risk of compromise).
</li>
<li>
Potential privacy issue: ability to tell that two accounts share a computer. In the backend you can store Hash(machine cookie,
account id) to make it a little harder to find which accounts share a computer.
</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Two factor authentication</h3></div>
<p>Two factor authentication (2FA) adds an extra layer of security. There are many ways to implement 2FA, and you can
require it on every login, or only when something is suspicious.</p>
<ul>
<li>SMS code. Not reliable if SMS ends up in email inbox!</li>
<li>Push notification</li>
<li>Touch token</li>
<li>Phone call</li>
<li>Soft tokens (Google Authenticator)</li>
<li>Hard tokens (RSA, Vasco, Feitian, etc.)</li>
<li>Printed one time codes</li>
<li>Printed grids</li>
<li>Client side certificates.</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Detecting (potential) compromise</h3></div>
<p>Some of these provide weak signals, so consider using them as inputs to a machine learning system.</p>
<ul>
<li>Machine cookie.</li>
<li>IP address &rarr; geo position. Fails for people who travel a lot (pilots, drivers, etc.)</li>
<li>IP address &rarr; ASN.</li>
<li>TCP stack fingerprinting.</li>
<li>Browser fingerprinting.</li>
<li>Public password dumps &rarr; offline or login flow &rarr; detect matches. You can do this even if the passwords are hashed
in different ways, since the plaintext exists in the login flow.</li>
<li>Spam or other activity on the account.</li>
<li>User reports.</li>
<li>Login attempt with previous password &rarr; password might have been compromised and then changed.</li>
<li>Time of day / day of week usage analysis.</li>
<li>
When a session ends, mark it as ended in the backend. If activity continues, the session might have been compromised
by malware or a man-in-the-middle.
</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Handling compromise</h3></div>
<ol>
<li>Use a Captcha to avoid bots.</li>
<li>Provide the user with a way to reset their password.</li>
<li>Provide the user with a way to review / terminate sessions.</li>
<li>Provide the user with a way to revert any account changes.</li>
<li>
Message the user what happened (i.e. what triggered the compromise handling flow). This can be hard
if the decision comes from a "magic blackbox".
</li>
<li>Handle compromised machines (malware removal).</li>
</section>
<section>
<div class="page-header"><h3>Prevent employee-forged sessions</h3></div>
<ul>
<li>Easier if employees don't have access to production databases. Doable but harder otherwise.</li>
<li>Need to make sure that employees can't override a password hash.</li>
<li>Need to make sure that employees can't add an email address, phone number, security question, etc.</li>
<li>
Need to make sure that employees can't forge or discover password reset code. Hard to do in practice, how
do you let engineers debug any email or sms delivery issue without preventing them from reading the data?
</li>
</ul>
</section>
<section>
<div class="page-header"><h3>Conclusion</h3></div>
<p>
Using third party authentication services (OAuth, OpenID, Facebook Connect, etc.) is not only about getting social
context (user's name, age, friend list, etc.) but also avoids having to build and maintain the login system itself.
</p>
<p>
I recommend studying past security bugs in this area.
</p>
<p>
In some situations (e.g. supporting legacy protocols like SMTP/IMAP/XMPP) you need to provide the ability to set
secondary weak passwords. Need to isolate what can be done with such passwords.
</p>
</section>
<section>
<h3>Links</h3>
<ul>
<li><a href="https://www.owasp.org/index.php/Authentication_Cheat_Sheet" class="external">OWASP Authentication Cheat Sheet</a></li>
<li><a href="https://www.cs.auckland.ac.nz/~pgut001/pubs/book.pdf" class="external">Engineering Security by Peter Gutmann, see page 498</a></li>
<li><a href="http://www.oauthsecurity.com/" class="external">OAuth security</a></li>
<li>Password storage
<ul>
<li><a href="http://www.tarsnap.com/scrypt.html" class="external">scrypt</a></li>
<li><a href="http://en.wikipedia.org/wiki/PBKDF2" class="external">PBKDF2</a></li>
<li><a href="http://en.wikipedia.org/wiki/Bcrypt" class="external">bcrypt</a></li>
<li><a href="http://www.opine.me/a-better-way-to-store-password-hashes/" class="external">Password storage without user association / Blind Hashing</a> (patented)</li>
</ul>
</li>
<li>Two factor authentication
<ul>
<li><a href="https://www.duosecurity.com/duo-push" class="external">Duo push</a></li>
<li><a href="http://www.yubico.com/products/yubikey-hardware/yubikey/technical-description/" class="external">Yubikey</a></li>
<li><a href="http://tools.ietf.org/html/rfc6238" class="external">TOTP RFC</a></li>
<li><a href="https://blog.twitter.com/2013/login-verification-on-twitter-for-iphone-and-android" class="external">Login verification on Twitter for iPhone and Android</a></li>
<li><a href="http://blog.authy.com/twitter" class="external">Thoughts on Twitter's new Two-Factor Authentication</a></li>
</ul>
</li>
<li>Past security issues
<ul>
<li><a href="https://www.duosecurity.com/blog/duo-security-researchers-uncover-bypass-of-paypal-s-two-factor-authentication" class="external">PayPal Two-Factor Authentication Bypass</a></li>
<li><a href="http://securityaffairs.co/wordpress/19892/hacking/hacking-google-gmail.html" class="external">Google Password Reset Flaw</a></li>
<li><a href="http://dazzlepod.com/linkedin/" class="external">Linked-In unsalted password hashes leak</a></li>
</ul>
</li>
<li>Facebook Blog
<ul>
<li><a href="https://blog.facebook.com/blog.php?blog_id=company&blogger=536181839" class="external">Fighting the Battle Against Money Scams</a></li>
<li><a href="https://www.facebook.com/notes/facebook-security/malware-checkpoint-for-facebook/10150902333195766" class="external">Malware Checkpoint for Facebook</a></li>
<li><a href="https://www.facebook.com/notes/facebook-engineering/introducing-login-approvals/10150172618258920" class="external">Introducing Login Approvals</a></li>
</ul>
</li>
</ul>
</section>