How I made LastPass give me all your passwords
The LastPass browser extension parsed URLs incorrectly, so a crafted URL could make the extension believe the current site was a different domain. An attacker could then steal stored passwords for that domain by tricking a user into visiting a malicious page.
Published on labs.detectify.com
TwitterLinkedIn
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
LastPass is a password manager, and the subject of this write-up is its browser extension. The extension injects HTML code into every page the user visits, and part of what it does is autofill: it parses the current URL to work out which domain the browser is on, then fills login forms on that page with the credentials stored for that domain.
That autofill path is where the bug lived. If the extension can be convinced that the page belongs to a different domain than the one the browser actually loaded, it will happily place the stored credentials for that other domain into a form controlled by the attacker.
Setup
The write-up describes no special accounts, roles or tooling. The researcher simply installed the LastPass browser extension and started looking at the HTML it added to pages. The only precondition for the attack is a victim who has the extension installed, is logged in, has stored credentials for some site, and visits a page the attacker controls. The author also notes that the attack would not work if multi factor authentication was enabled on the LastPass account.
Finding it
The starting observation was that the extension added HTML code to every page visited. Digging into how that worked led to the autofill functionality and to the URL parsing routine it relies on. The parsing code was flawed, and the flaw is what allowed stored passwords to be extracted.
Exploitation
Step 1: understand the URL parsing
The relevant code is the lpParseUri function, shown un-minified:
var fixedURL = URL.match(/^(.*://[^/]+/.*)@/);
fixedURL && (url = url.substring(0, fixedURL[1].length) + url.substring(fixedURL[1].length).replace(/@/g, "%40"));
Step 2: craft a URL that the browser and the extension read differently
The researcher browsed to this URL:
http://avlidienbrunn.se/@twitter.com/@hehe.php
The browser treats the current domain as avlidienbrunn.se, because that is the host component of the URL. The two components disagree about which site the user is on.
Step 3: collect the credentials
With the extension believing the page belongs to twitter.com, it filled the form on the attacker's page with the stored credentials for twitter.com. The researcher then repeated the same approach against other commonly used sites to extract credentials for those too.
Root cause
The mistake is in the URL parsing in lpParseUri. Because the extension's notion of the current domain is what drives credential selection for autofill, the mismatch lets an attacker-controlled page present itself as another domain to the extension.
Impact
An attacker who can get a victim to visit a crafted URL can have the extension autofill the victim's stored credentials for a chosen site into a form the attacker controls, and then read them. By iterating over commonly used sites, credentials for many accounts can be collected. The attack requires the victim to have the extension installed and to visit the attacker's page, and it does not work when multi factor authentication is enabled on the LastPass account.
Fix
The issue was reported through LastPass's responsible disclosure page and was resolved and pushed to LastPass users. The fix was pushed in less than a day, and the researcher received a bug bounty of $1,000. The write-up does not describe the code change itself.
The author's general advice is to consider disabling autofill, since this is not the first autofill bug seen, and to enable multi factor authentication, which would have prevented this attack.
Variants to test
- Multiple
@characters in the authority or path of a URL, as in the payload above. A positive result is the extension treating the page as a different domain than the browser does and filling credentials for that domain. - A positive result is the same domain mismatch between what the browser loads and what the extension believes.
- A positive result is stored credentials appearing in fields the attacker can read.
What happened, in plain words
LastPass is a password manager: a program that remembers your passwords for you so you do not have to type them every time. It comes in several forms, and this story is about its browser extension, the small add-on that lives inside your browser. One of the things the extension does is called autofill: when you open a login page, it types your saved username and password into the form for you.
To do that, the extension has to answer a simple question: which website am I on right now? It looks at the address in the address bar (the URL) and works out the domain, the site name such as twitter.com. Then it looks in its vault for the password saved for that site and fills it in.
The bug was in that first step, working out which site you are on. The extension read the address in a slightly different way from the browser. The researcher found an address that the browser and the extension disagreed about. The browser thought you were on one site; the extension thought you were on another. So the extension helpfully filled in the password for the other site, into a form on a page the attacker controlled. The attacker could then read the password.
By repeating this for many popular sites, an attacker could collect a victim's passwords for all of them, just by getting the victim to open a web page. The researcher reported it, LastPass fixed it in less than a day, and paid a $1,000 bug bounty.
Background: what the pieces are
Before the details, here are the ideas you need. None of them require any security knowledge.
- Browser extension: a small program you install into your browser (Chrome, Firefox and so on) that adds features. It can read and change the pages you visit, which is exactly what makes autofill possible, and also what made this bug serious.
- URL: the address of a page, like
http://example.com/login. It has parts. Thehttp://part is the scheme (how to talk to the server). Theexample.compart is the host, the name of the server. The/loginpart is the path, which page on that server. - Domain: the site name, such as
twitter.com. Password managers store passwords per domain, so getting the domain right is the whole basis of autofill. - Autofill: the feature that types your saved login into a form automatically.
- HTML: the language web pages are written in. Forms, buttons and text fields on a page are HTML.
- Credentials: your username and password for a site.
- Multi factor authentication (MFA): a second proof of identity on top of your password, such as a code from your phone. The author notes the attack would not work if MFA was enabled on the LastPass account.
- Bug bounty: a reward a company pays to someone who reports a security problem responsibly instead of abusing it.
Setting up
The write-up describes no special accounts, roles or tools. The researcher installed the LastPass browser extension and started looking at the HTML it added to pages. That is the whole setup.
The only precondition for the attack is a victim who:
- has the extension installed,
- is logged in to LastPass,
- has saved credentials for some site,
- visits a page the attacker controls.
Finding the bug
The starting observation was simple: the extension added some HTML code to every page visited. That is worth a look, because code being injected into pages you did not write is exactly where things can go wrong. Digging into how that worked led to the autofill functionality and to the routine it uses to read the URL. That routine was flawed, and the flaw is what allowed stored passwords to be extracted.
Step 1: understanding the URL parsing
The relevant code is the lpParseUri function, shown un-minified (minified means squashed and shortened to save space; un-minified is the readable version):
var fixedURL = URL.match(/^(.*://[^/]+/.*)@/);
fixedURL && (url = url.substring(0, fixedURL[1].length) + url.substring(fixedURL[1].length).replace(/@/g, "%40"));
Line by line, in plain words:
var fixedURL = URL.match(/^(.*://[^/]+/.*)@/);searches the URL for a pattern. The result is stored in a variable calledfixedURL. If there is no@, this is empty.fixedURL && (...)means: only do the next part if an@was found.- Inside the brackets, the code rebuilds the URL in two pieces. The first piece,
url.substring(0, fixedURL[1].length), is everything up to that last@, kept exactly as it was. The second piece,url.substring(fixedURL[1].length).replace(/@/g, "%40"), is everything after it, with every@replaced by%40.%40is simply the encoded form of the@character, the way some characters are written safely inside a URL.
The code assumes there is only one @ that matters, and that assumption is wrong.
Why does @ matter at all? In a URL, an @ can separate a username from the host, as in http://[redacted]/. Browsers know this rule.
Step 2: craft a URL that the browser and the extension read differently
The researcher browsed to this URL:
http://avlidienbrunn.se/@twitter.com/@hehe.php
What each side sees:
- The browser reads the host as
avlidienbrunn.se, because that is the part right afterhttp://and before the first/. Everything after that first/is the path, so@twitter.com/@hehe.phpis just a path on that server. The browser loads a page fromavlidienbrunn.se. - The extension, using the flawed parsing, treats the last
@as the separator and ends up believing the domain istwitter.com. The real domain ends up inside what the parser considers the username portion of the URL.
The two disagree about which site the user is on. That disagreement is the whole trick. The attacker only needs a page they control, here on avlidienbrunn.se, and an address shaped so the extension reads a different site name from it.
Step 3: collect the credentials
Because the extension believed the page belonged to twitter.com, it filled the form on the attacker's page with the stored credentials for twitter.com. The attacker's page can read what is typed into its own form, so the password is now in the attacker's hands.
The researcher then repeated the same approach against other commonly used sites, extracting credentials for those too. Each site needs its own crafted address, but the method is the same.
The screenshot shows the extension filling the researcher's form with the stored credentials for twitter.com.
Why it worked
The mistake is in the URL parsing in lpParseUri. A URL can contain more than one @. Since the extension's idea of the current domain decides which password gets filled in, an attacker-controlled page can present itself to the extension as another domain.
What an attacker could do with it
An attacker who can get a victim to visit a crafted URL can have the extension autofill the victim's stored credentials for a chosen site into a form the attacker controls, and then read them. By iterating over commonly used sites, credentials for many accounts can be collected. The attack requires the victim to have the extension installed and to visit the attacker's page, and it does not work when multi factor authentication is enabled on the LastPass account.
The fix and the reward
The issue was reported through LastPass's responsible disclosure page and was resolved and pushed to LastPass users. The fix was pushed in less than a day, and the researcher received a bug bounty of $1,000. The write-up does not describe the code change itself.
The author's general advice is to consider disabling autofill, since this is not the first autofill bug seen, and to enable multi factor authentication, which would have prevented this attack.
Other shapes worth testing
- Multiple
@characters in the authority or path of a URL, as in the payload above. A positive result is the extension treating the page as a different domain than the browser does and filling credentials for that domain. - Other URL forms where the browser and the extension's parser can disagree about the host, such as userinfo-like prefixes. A positive result is the same domain mismatch between what the browser loads and what the extension believes.
- Autofill on pages where the form is controlled by the attacker rather than the legitimate site. A positive result is stored credentials appearing in fields the attacker can read.
- Accounts with multi factor authentication enabled, to confirm the attack does not work under that condition, as the author states.
Glossary
- Browser extension: a small add-on program installed into a browser that can read and change the pages you visit.
- Password manager: a program that stores your passwords and fills them in for you.
- Autofill: the feature that types your saved username and password into a login form automatically.
- URL: the address of a web page, made of a scheme, a host and a path.
- Scheme: the first part of a URL, such as
http://, saying how to talk to the server. - Host: the server name inside a URL, such as
example.com. - Path: the part of a URL after the host, naming the page on that server.
- Domain: the site name, such as
twitter.com; password managers store passwords per domain. - HTML: the language web pages are written in, including forms and text fields.
- Credentials: a username and password for a site.
- URL parsing: the process of splitting a URL into its parts to work out, for example, which domain it points to.
- Regular expression: a pattern used to search text, here used to find the last
@in a URL. - Percent-encoding: writing a character in a safe form inside a URL, for example
%40for@. - Userinfo: the optional username part of a URL before an
@, as inhttp://[redacted]/. - Minified / un-minified: code squashed to save space, and the readable version of it.
- Bug bounty: a reward a company pays for a responsibly reported security problem.
- Responsible disclosure: reporting a security problem privately to the company so it can be fixed before it is made public.