-
Notifications
You must be signed in to change notification settings - Fork 620
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
WIP: www/nginx: add njs script to detect MitM attacks on TLS connections #1070
Merged
Merged
Changes from all commits
Commits
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
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,60 @@ | ||
var fs = require('fs'); | ||
var tls_fingerprints = JSON.parse(fs.readFileSync('/usr/local/etc/nginx/tls_fingerprints.json')); | ||
|
||
function customArrayIndexOf(haystack, needle) { | ||
var element, element_id; | ||
for (element_id in haystack) { | ||
element = haystack[element_id]; | ||
if (element == needle) return element_id; | ||
} | ||
return -1; | ||
} | ||
|
||
function check_cipher_array(r, browser_ciphers, fingerprint_ciphers, result) { | ||
if (result.status == 'Intercepted') { | ||
return; | ||
} | ||
if (browser_ciphers.length > fingerprint_ciphers.length) { | ||
// the proxy supports more cipers than the browser -> intercepted | ||
result.status = "Intercepted"; | ||
return; | ||
} | ||
var browser_cipher; | ||
var browser_cipher_index; | ||
var last_index = -1; | ||
var current_index; | ||
for (browser_cipher_index in browser_ciphers) { | ||
browser_cipher = browser_ciphers[browser_cipher_index]; | ||
current_index = customArrayIndexOf(fingerprint_ciphers, browser_cipher); | ||
if (current_index === -1 || current_index <= last_index) { | ||
// a cipher has been found, which is not supported by the browser | ||
// such a connection is definitly intercepted | ||
result.status = "Intercepted"; | ||
//result.status = JSON.stringify(fingerprint_ciphers[0].toBytes().toString('hex')); | ||
return; | ||
} | ||
last_index = current_index; | ||
} | ||
if (result.status == 'Unknown') { | ||
result.status = browser_ciphers.length === fingerprint_ciphers.length ? 'Original' : 'Hardened' | ||
} | ||
} | ||
|
||
function check_intercept(r) { | ||
var tls_result = {'status': 'Unknown'}; | ||
if (r.headersIn['User-Agent'] && r.variables.ssl_ciphers != '') { | ||
var ua = r.headersIn['User-Agent']; | ||
if (ua in tls_fingerprints) { | ||
var fp = tls_fingerprints[ua]; | ||
var browser_ciphers = r.variables.ssl_ciphers.split(':'); | ||
check_cipher_array(r, browser_ciphers, fp.ciphers, tls_result); | ||
if (r.variables.ssl_curves != '') | ||
{ | ||
var browser_curves = r.variables.ssl_curves.split(':'); | ||
check_cipher_array(r, browser_curves, fp.curves, tls_result); | ||
} | ||
} | ||
} | ||
return tls_result.status; | ||
} | ||
|
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
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
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.
sorry, this function is needed, Array.indexOf does not work for some reasons (don't know why)…
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.
ok, just for reference: which browser in particular?
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.
this is not a browser, this runs directly inside nginx (njs). It is strange that when I use the njs comand line util it works as expected but in nginx it is strange:
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 think the problem is fixed by this commit: nginx/njs@f0dc710
Could you build njs from master branch and test?
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.
@VBart I just tried indexOf on version nginx/njs@549e51e which I have built yesterday. It still returns
-1
nginx version: Nginx-1.14.0 (if it helps)
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.
Btw, since the problem is related to comparsion of UTF-8 and Byte strings internal representation, I think you can try to workaround it by using
toBytes()
ortoUTF8()
methods. Not sure which one will work in this particular case:or
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.
It's not so urgent as we are blocked here anyway (FreeBSD build issue):
opnsense/tools@634a086
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.
@fabianfrz jfyi, njs 0.2.7 has been released today with the fix.
As of FreeBSD build issue, that's strange, because njs works perfectly on FreeBSD. And njs module is a part of official nginx port for a long time: https://www.freshports.org/www/nginx
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.
@VBart thanks, so let's wait for inclusion in the FreeBSD ports tree so we can use it. @fichtner fixed the build by patching out a build flag:
opnsense/ports@d188643#diff-60d367a88ba4945c90d6504cc2f25228
It seems that an unsupported compiler flag caused the issue.
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.
On FreeBSD only nginx-full has the NJS option enabled normally. It could be due to our unsupported version of FreeBSD 11.1. I will re-evaluate once we are on 11.2.