Top Banner
Testing Node Security by @jmortegac NOV 18-19 · 2016
73

Testing NodeJS Security

Jan 07, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing NodeJS Security

Testing Node Security by @jmortegac

NOV 18-19 · 2016

Page 2: Testing NodeJS Security

Agenda

Introduction nodejS security

Npm security packages

Node Goat project

Tools

Page 3: Testing NodeJS Security

nodeJS introduction

JavaScript in the backend

Built on Chrome´s Javascript runtime(V8)

NodeJs is based on event loop

Designed to be asynchronous

Single Thread

Concurrent requests.

Page 4: Testing NodeJS Security

Security updates

Page 5: Testing NodeJS Security

Security updates

Page 6: Testing NodeJS Security

Find nodeJS vulnerabilities http://cve.mitre.org/find/

Page 7: Testing NodeJS Security

Last vulnerabilities https://nodesecurity.io/advisories

Page 8: Testing NodeJS Security

NPM modules install

Page 9: Testing NodeJS Security

Npm security packages

Helmet

express-session / cookie-session

csurf

express-validator

bcrypt-node

express-enforces-ssl

Page 10: Testing NodeJS Security

Security HTTP Headers

Strict-Transport-Security

X-Frame-Options

X-XSS-Protection

X-Content-Type-Options

Content-Security-Policy

Page 11: Testing NodeJS Security

Helmet module https://www.npmjs.com/package/helmet

Page 12: Testing NodeJS Security

Helmet module https://github.com/helmetjs/helmet

Page 13: Testing NodeJS Security

Helmet module

CSPContent-Security-Policy header

hidePoweredBydeletes X-Powered-by header

Hpkpprotection MITM

Hstsforces https connections

noCachedesactive client cache

Frameguardprotection clickjacking

xssFilterprotection XSS

Page 14: Testing NodeJS Security

Helmet module

Page 15: Testing NodeJS Security

Check headers security http://cyh.herokuapp.com/cyh

https://securityheaders.io/

Page 16: Testing NodeJS Security

Express versions

https://www.shodan.io/search?query=express

Page 17: Testing NodeJS Security

Disable x-powered-by

Avoid framework fingerprinting

Page 18: Testing NodeJS Security

Disable x-powered-by

Use Helmet and use “hide-powered-by” plugin

Page 20: Testing NodeJS Security

httpOnly & secure:true

Page 21: Testing NodeJS Security

Delete cookies from cache browser

// Set cache control header to eliminate cookies from cache

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

res.header('Cache-Control', 'no-cache="Set-Cookie, Set-Cookie2"');

next();

});

Page 22: Testing NodeJS Security

XSS attacks

An attacker can exploit XSS vulnerability to:

Steal session cookies/Sesion hijacking

Redirect user to malicious sites

Defacing and content manipulation

Cross Site Request forgery

Page 24: Testing NodeJS Security

CSRF

<form action="/process" method="POST">

<input type="hidden" name="_csrf" value="{{csrfToken}}">

<button type="submit">Submit</button>

</form>

app.use(function (request, response, next) {

response.locals.csrftoken = request.csrfToken();

next();

});

Page 25: Testing NodeJS Security

CSRF

Page 27: Testing NodeJS Security

Validator

Page 28: Testing NodeJS Security

Validator

Page 29: Testing NodeJS Security

Validator

Page 30: Testing NodeJS Security

Validator with reg exp

Page 31: Testing NodeJS Security

Regular expressions

https://www.npmjs.com/package/safe-regex

Detect vulnerable regular

expressions that can cause DoS

Page 32: Testing NodeJS Security

NodeJS Crypto

http://nodejs.org/api/crypto.html Use require(‘crypto’) to access this module

The crypto module requires OpenSSL

require("crypto")

.createHash("sha1") //algorithm

.update(“cOdEmOtiOn") //text

.digest("hex"); //hexadecimal result

Page 33: Testing NodeJS Security

Bcrypt-node

https://github.com/kelektiv/node.bcrypt.js

Page 34: Testing NodeJS Security

Bcrypt-node

Page 35: Testing NodeJS Security

Bcrypt-node

Page 36: Testing NodeJS Security

Bcrypt-node

Page 37: Testing NodeJS Security

Building a secure HTTPS server

Page 39: Testing NodeJS Security

Building a secure HTTPS server

Page 40: Testing NodeJS Security

Building a secure HTTPS server

var helmet = require("helmet");

var ms = require("ms");

app.use(helmet.hsts({

maxAge: ms("1 year"),

includeSubdomains: true

}));

Send hsts header for all requests

Page 41: Testing NodeJS Security

Node Goat http://nodegoat.herokuapp.com/tutorial

Page 42: Testing NodeJS Security

Node Goat https://github.com/OWASP/NodeGoat

Page 43: Testing NodeJS Security

EVAL()

Page 44: Testing NodeJS Security

EVAL() on github

Page 45: Testing NodeJS Security

EVAL() ATTACKS

res.end(require('fs').readdirSync('.').toString())

res.end(require('fs').readdirSync('..').toString())

Page 46: Testing NodeJS Security

Insecure Direct Object References

Use session instead of request param

var userId = req.session.userId;

Page 47: Testing NodeJS Security

Tools

NSP

Require Safe

David

KrakenJS / Lusca middleware

Retire

snyk.io

Page 48: Testing NodeJS Security

NSP https://github.com/nodesecurity/nsp

npm install -g nsp

Analyze package.json

nsp check --output summary

Page 49: Testing NodeJS Security

NSP with Grunt

npm install –g grunt-nsp-package

Page 50: Testing NodeJS Security

Nsp execution

Page 51: Testing NodeJS Security

Nsp execution

Page 53: Testing NodeJS Security

Project dependences

Page 54: Testing NodeJS Security

Project dependences npm install –g david

Page 55: Testing NodeJS Security

https://snyk.io

Page 56: Testing NodeJS Security

http://krakenjs.com/

Page 58: Testing NodeJS Security

Retire.js

http://retirejs.github.io/retire.js

Detecting components and js libraries

with known vulnerabilities

Page 59: Testing NodeJS Security

Retire.js

Page 60: Testing NodeJS Security

Retire.js

Page 61: Testing NodeJS Security

Retire.js

Page 62: Testing NodeJS Security

Retire.js https://raw.githubusercontent.com/bekk/retire.js/master/repository/jsrepository.json

Page 63: Testing NodeJS Security

Retire.js execution

Page 64: Testing NodeJS Security

NodeJsScan

https://github.com/ajinabraham/NodeJsScan

python NodeJsScan.py -d <dir>

Page 65: Testing NodeJS Security

NodeJsScan https://github.com/jmortega/NodeJsScan/blob/master/rules.xml

Page 66: Testing NodeJS Security

NodeJsScan

Page 67: Testing NodeJS Security

Passport

Page 68: Testing NodeJS Security

Passport

Page 69: Testing NodeJS Security

https://github.com/jmortega/testing_nodejs_security

Page 71: Testing NodeJS Security

References

https://blog.risingstack.com/node-js-security-checklist/

https://blog.risingstack.com/node-js-security-tips/

https://groups.google.com/forum/#!forum/nodejs-sec

https://nodejs.org/en/blog/vulnerability/september-2016-

security-releases/

https://expressjs.com/en/advanced/security-updates.html

http://opensecurity.in/nodejsscan/

http://stackabuse.com/securing-your-node-js-app/

Page 73: Testing NodeJS Security

Books