-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Beats Management] Prevent timing attacks when checking auth tokens #19363
Merged
ycombinator
merged 4 commits into
elastic:feature/x-pack/management/beats
from
ycombinator:x-pack/management/beats/apis/security/constant-time-comparisons
May 25, 2018
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a95f1e0
Using crypto.timingSafeEqual() for comparing auth tokens
ycombinator ce0c080
Prevent subtler timing attack in token comparison function
ycombinator 7af9c2d
Introduce random delay after we try to find token in ES to mitigate t…
ycombinator beddea9
Remove random delay
ycombinator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/beats/server/lib/crypto/are_tokens_equal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { timingSafeEqual } from 'crypto'; | ||
|
||
const RANDOM_TOKEN_1 = 'b48c4bda384a40cb91c6eb9b8849e77f'; | ||
const RANDOM_TOKEN_2 = '80a3819e3cd64f4399f1d4886be7a08b'; | ||
|
||
export function areTokensEqual(token1, token2) { | ||
if ((typeof token1 !== 'string') || (typeof token2 !== 'string') || (token1.length !== token2.length)) { | ||
// This prevents a more subtle timing attack where we know already the tokens aren't going to | ||
// match but still we don't return fast. Instead we compare two pre-generated random tokens using | ||
// the same comparison algorithm that we would use to compare two equal-length tokens. | ||
return timingSafeEqual(Buffer.from(RANDOM_TOKEN_1, 'utf8'), Buffer.from(RANDOM_TOKEN_2, 'utf8')); | ||
} | ||
|
||
return timingSafeEqual(Buffer.from(token1, 'utf8'), Buffer.from(token2, 'utf8')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { areTokensEqual } from './are_tokens_equal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,14 @@ async function getEnrollmentToken(callWithInternalUser, enrollmentToken) { | |
}; | ||
|
||
const response = await callWithInternalUser('get', params); | ||
return get(response, '_source.enrollment_token', {}); | ||
const token = get(response, '_source.enrollment_token', {}); | ||
|
||
// Elasticsearch might return fast if the token is not found. OR it might return fast | ||
// if the token *is* found. Either way, an attacker could using a timing attack to figure | ||
// out whether a token is valid or not. So we introduce a random delay in returning from | ||
// this function to obscure the actual time it took for Elasticsearch to find the token. | ||
const randomDelayInMs = 25 + Math.round(Math.random() * 200); // between 25 and 225 ms | ||
return new Promise(resolve => setTimeout(() => resolve(token), randomDelayInMs)); | ||
} | ||
|
||
function deleteUsedEnrollmentToken(callWithInternalUser, enrollmentToken) { | ||
|
@@ -80,7 +87,7 @@ export function registerEnrollBeatRoute(server) { | |
try { | ||
const enrollmentToken = request.headers['kbn-beats-enrollment-token']; | ||
const { token, expires_on: expiresOn } = await getEnrollmentToken(callWithInternalUser, enrollmentToken); | ||
if (!token || token !== enrollmentToken) { | ||
if (!token) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the comparison here because it was unnecessary. The comparison was being done in ES when we tried to |
||
return reply({ message: 'Invalid enrollment token' }).code(400); | ||
} | ||
if (moment(expiresOn).isBefore(moment())) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment re. clamping. That's a lot harder to de-noise than this, see https://blog.ircmaxell.com/2014/11/its-all-about-time.html
This approach is basically spewing random numbers that even makes it reasonably easy to find the seed similar to this - https://franklinta.com/2014/08/31/predicting-the-next-math-random-in-java/
I don't think this timing sidechannel is a big deal (if the key isn't valid, it doesn't exist at this point?), but if we're going to remove it, I don't think random noise is the way :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm starting to think protecting against a timing attack just to determine existence of an enrollment token is probably overkill in this case. That's because a) these enrollment tokens are meant to be short-lived and one-time use and b) they have an expiration time as well. In other words, by the time a timing attack might determine an enrollment token, it might've been used or have expired already.
So now I'm inclined to remove the random noise bit I implemented in 7af9c2d. @alexbrasetvik sounds like you're on board with that. @kobelb WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree.
https://events.ccc.de/congress/2012/Fahrplan/attachments/2235_29c3-schinzel.pdf is also a good resource on this topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me!