Hijacking Meteor Accounts by Sniffing DDP

Written by Pete Corey on Aug 23, 2015.

You’re in your neighborhood Starbucks scarfing down an Everything With Cheese Bagel while browsing the web, and you decide to visit your favorite Meteor application. You go to the website, type in your authentication credentials, and hit “Log In”, paying no mind that the application is running over http, not https.

Unbeknownst to you, sitting in a dimly lit corner closest to the restrooms, someone is sniffing the public wifi. As you hit “Log In”, they see your authentication credentials fly across the wire.

In its raw form, a login attempt over DDP using the account-password package looks like this:

["{\"msg\":\"method\",\"method\":\"login\",\"params\":[{\"user\":{\"email\":\"joe@schmoe.com\"},\"password\":{\"digest\":\"5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8\",\"algorithm\":\"sha-256\"}}],\"id\":\"9\"}"]

The attacker sees the website you’re connected to, your email address and a hash of the password you provided. Now that he has all of this information, hijacking your account is as simple as navigating to the Meteor application and running the following in his browser console:

Accounts.callLoginMethod({
    methodArguments: [{
      user: {email: "joe@schmoe.com"},
      password: {digest: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", algorithm: "sha-256"}
    }]
});

And just like that, someone was able to catch your login credentials as they flew unencrypted across the network, and use them to take control of your account.

Smells Like Hash

But how can this be? You know that when you call Meteor.loginWithPassword on the client, the password you provide is hashed before it’s sent to the server. How can an attacker log in without access to the actual password string?

The accounts-password package hashes the provided password before sending it across the network in an attempt to prevent attackers from being able to see the raw password. The server then compares the user-provided hash with the hash it keeps the users collection. If the two hashes match, the server assumes that the user provided the correct password, and logs them into the application.

This means that the hash is effectively being treated as a password. If you send the right hash, you will be logged into the associated account, regardless of whether you know what the actual password is. Because this hash, or pseudo-password, is being sent in plain text over the network, anyone who intercepts it can easily replay the message and send their own login request.

SSL To The Rescue

People often ask me if they should be using SSL/TLS (https) with their Meteor applications. My answer is always a resounding, “Yes!” At its core, DDP is a plain text protocol that offers no protection against inspection, tampering or replay over the network. This means that all of your users’ potentially private data is being broadcast to the slice of the world between the client and the server.

So how does SSL save the day? By adding and correctly configuring SSL and navigating to your Meteor application over https, you’re creating a secure connection between the client and the server. All network communications are completely encrypted and protected from replay attacks.

Using SSL is an easy way to ensure that private data stays private, even when it’s being shipped back and forth between the client and the server.

The Ecstasy of Testing

Written by Pete Corey on Aug 18, 2015.

describe("The ecstasy of testing", function() {

You dive in, equipped with nothing more than a creeping dissatisfaction and a passing test suite.

As the darkness of uncertainty begins to envelop you, the shining green light of your passing suite fills you with unbounded confidence. Fearlessly, you make your first refactor. Vast swaths of code fall away before you, subjected to the annals of git history. Intricate towers of loosely coupled components assemble themselves upon the foundations of your assumptions and beliefs.

In your gut, you feel that it’s ready. You hit save. Your waiting tests eagerly devour your creation, but something is wrong. The suite is failing. Exceptions smoulder red with roars of unmet assertions. You realize that you’ve missed an entire set of edge cases. Your assumptions were unfounded, and were it not for the meticulous diligence of your test suite, devastating. Cries of what could have been are drowned out by a torrent of mechanical clicks and clacks as your fingers deftly recover from your oversight.

Once again you set the suite into motion. Between the hushed sounds of your bated breath you can nearly hear the whirring cogs of your test harness as it churns through vast permutations of possibilities and potentialities. Finally, the gears come to rest.

The screen glows green in front of you.

Your tests are passing.

It’s finished.

});

DOS Your Meteor Application With Where

Written by Pete Corey on Aug 10, 2015.

If you’ve read my previous posts, you’ll know that I talk quite a bit about the dangers of not checking your method and publication arguments. These posts usually boil down to the dangers of letting users pass arbitrary data into your collection query objects. These types of vulnerabilities usually take the form of data leakage, or unauthorized data modifications (which are very serious issues), but it’s also possible to completely hang an application with a well-crafted query.

Let’s dig into an example to see how this can happen and what we can do to prevent it!

The Setup

Let’s pretend that you’re building a Meteor application. Within this application you have a very simple method called grabData. It expects you to pass in an ID, and it will return the corresponding item from the Data collection:

Meteor.methods({
  grabData: function(id) {
    return Data.findOne(id);
  }
});

Awesome, our method works! Now imagine what would happen if a malicious user ran the following method call in their browser console:

Meteor.call('grabData', {$where: "d = new Date; do {c = new Date;} while (c - d < 10000);"});

Uh oh… What is this $where operator? What’s all this Date and looping business? And why is my app completely unresponsive?

The Exploit

The $where operator will execute any valid Javascript string on each element in the collection over which you’re running your query. It’s intended use is to run complicated queries that may carry out some business logic on a document before deciding whether to include it in the result set. Unfortunately, $where will execute any Javascript passed into it - including Javascript designed to loop forever.

We were expecting the client to pass an ID into grabData, but our malicious user decided to get more creative. They passed in a selector object with a $where operator designed to spin your Mongo instance’s CPU at 100% for 10 seconds. By pegging the CPU of your Mongo instance (which may or may not be the same CPU used by your Meteor application), the malicious user has essentially DOS’d your application.

Check out a quick demonstration:

This is a particularly nasty vulnerability. In this case, our malicious user was kind enough to free the CPU after 10 seconds. In the real world, an attacker may peg your CPU indefinitely, forcing you to restart your Mongo instance.

The Fix

So how do we avoid this type of “NoSQL injection”? Wherever possible, don’t trust user input, and definitely don’t pass it directly into a collection query. Always check that your user arguments match your expectations, and be especially careful when using the $where operator.

A simple fix to our grabData method may look like this:

Meteor.methods({
  grabData: function(id) {
    check(id, String);
    return Data.findOne(id);
  }
});

You can use my new package, east5th:check-checker to see if you have any unchecked arguments in your application’s publications and methods.