july 7th phillip chaffeefiles.meetup.com/16676252/node.js security.pdf · either fork and deploy...

17
Phillip Chaffee July 7 th , 2015 Security for web servers written in Javascript Node.js Security

Upload: others

Post on 24-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Phillip ChaffeeJuly 7th, 2015

Security for web servers written in Javascript

Node.js Security

Page 2: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Javascript interpreter for writing servers

Built on Google Chrome’s Javascript Runtime – V8

Event driven

Non-blocking I/O model

Written in C

Node.js Overview

Page 3: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Either Fork and deploy (Heroku)

Fork, clone, and run locally

Open the source code

Follow the walkthrough

Source code comments are very helpful

How to use it

Node Goat is a purposefully flawed application developed using Node.js.

Made to be hacked/fixed to learn the OWASP top 10 for Node.js.

What is it?

OWASP Node Goat

Page 4: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

ExpressJSNode.js framework

Node is very low level

Needs a framework to sit on top of it to handle routing

MongoDBA NoSQL database that uses a Javascript console

Stores data in JSON objects

SwigA front end framework for injecting Javascript into the browser

Main Libraries Used in Node Goat

Page 5: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

OWASP Top 10

Page 6: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

A1 - Injection

Page 7: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

A2 – Broken Auth

Password field needs encryptions

NPM to the rescue

Bcrypt

Page 8: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Not much different then in a non Node.js app

Validate all inputs

Use correct output encoding

HTTP only on session cookies not needed by JS

Use built in Express middleware session managementapp.use(express.session({});

A3 - XSS

Page 9: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

A4 – Insecure Direct Object References

Use session instead of request parameters

AlsoCheck access

Use indirect session/user object references

Page 10: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Node.js configurations tips Use the latest and most stable versions of node.js and all major

packages being used

Lock all npm packages versions

Use HTTP request body limiting middleware

Never run an application with root privileges

HelmetHelmet is a great node package that bundles together a lot of the

security configuration you will need

A5 - Misconfiguration

Page 11: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Use secure HTTPS protocol https.createserver()

Encrypt all sensitive data var crypto = require(“crypto”);

Don’t store sensitive data longer than you need to

Verify algorithms strength

Disable autocomplete

A6 – Sensitive Data

Page 12: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Verify that the current user has sufficient rights to view restricted areas

ExpressJS middleware

A7 – Mission Function Level Access Control

Page 13: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Malicious web pages

ExpressJS provides middleware specifically for thisapp.use(express.csrf());

app.use(function(req, res, next) {

res.locals.csrftoken = req.csrftoken();

next();

});

A8 – Cross-Site Request Forgery

Page 14: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Do not run any modules with root privileges

Use the Node Security Project npm install nsp –g

Nsp package

This scans the package.json file and alerts you to any packages with known vulnerabilities

A9 - Using Components with Known Vulnerabilities

Page 15: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Avoid using redirects and forwards altogether

If used, don’t include user parameters

Otherwise, validate destination and current user

A10-Unvalidated Redirects and Forwards

Page 16: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

ZAProxy still works great for testing

It has it’s own npm package npm install zaproxy

var options = { proxy : ‘http://localhost:4000’ };

var ZapClient = require(‘zaproxy’);

var zaproxy = new ZapClient(options);

Node.js is very low level, so security can be built as middleware

Further reading/information https://github.com/PhillipChaffee/nodejs-

security/blob/master/reading.md

Conclusion

Page 17: July 7th Phillip Chaffeefiles.meetup.com/16676252/Node.js Security.pdf · Either Fork and deploy (Heroku) Fork, clone, and run locally Open the source code Follow the walkthrough

Questions & Discussion