Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit...

43
Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac

Transcript of Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit...

Page 1: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Hacking NodeJS

applications for fun

and profit

Testing NodeJS Security

by @jmortegac

Page 2: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Agenda

▪ Introduction nodejS security▪Npm security packages▪Node Goat project▪ Tools

Page 3: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Node JS

▪ JavaScript in the backend▪ Built on Chrome´s Javascript runtime(V8)▪ NodeJs is based on event loop▪ Designed to be asynchronous▪ Single Thread▪ Node.js is resilient to flooding attacks since

there’s no limit on the number of concurrent requests.

Page 4: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Security updates

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

Page 5: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Package vulnerabilities

https://www.npmjs.com/advisories

Page 6: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Npm security packages

▪ Helmet▪ express-session▪ cookie-session▪ csurf▪ express-validator▪ bcrypt-node▪ express-enforces-ssl

Page 7: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Security HTTP Headers ▪ Strict-Transport-Security

▪ X-Frame-Options▪ X-XSS-Protection▪ X-Content-Type-Options▪ Content-Security-Policy

Page 8: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

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

/helmet

Page 9: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

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

Page 10: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Helmet module▪ hidePoweredBy▪ Hpkp→protection MITM▪ Hsts→forces https

connections▪ noCache→desactive client

cache▪ Frameguard→protection

clickjacking▪ xssFilter→protection XSS

Page 11: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Helmet CSP

Page 12: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Check headers security

▪ http://cyh.herokuapp.com/cyh▪ https://securityheaders.io/

Page 13: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Express versions

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

Page 14: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Disable x-powered-by

Page 15: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Disable x-powered-by

▪ Avoid framework fingerprinting

Page 16: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Disable x-powered-by

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

Page 17: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Sessions management

▪ secure▪ httpOnly▪ domain▪ path▪ expires

▪ https://www.npmjs.com/package/cookie-session

Page 18: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

httpOnly & secure:true

Page 19: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm 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 20: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

CSRF attacks

Page 21: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

https://www.npmjs.com/package/csurf

Page 22: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm 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 23: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

CSRF

Page 24: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Filter/sanitize user input

▪ Fixing XSS attacks▪ https://www.npmjs.com/package/sanitizer

▪ Module express-validator▪ https://www.npmjs.com/package/express-validator

Page 25: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

ExpressValidator

Page 26: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security
Page 27: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Bcrypt-node

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

Page 28: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security
Page 29: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security
Page 30: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

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

/tutorial

Page 31: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

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

Goat

Page 32: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

EVAL() ATTACKS

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

Page 33: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Insecure Direct Object References

▪ Use session instead of request param

▪ var userId = req.session.userId;

Page 34: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Tools

▪ KrakenJS▪ Lusca

middleware▪ NodeJsScan

Page 35: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

http://krakenjs.com/

Page 36: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

https://github.com/krakenjs/lusca

Page 37: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

NodeJsScan▪ https://github.com/ajinabra

ham/NodeJsScan

Page 39: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

NodeJsScan

Page 40: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

GitHub repositories

▪ https://github.com/jmortega/testing_nodejs_security▪ https://github.com/cr0hn/vulnerable-node▪ https://github.com/rdegges/svcc-auth▪ https://github.com/strongloop/loopback-getting-start

ed-intermediate▪ https://github.com/Feeld/strong-node

Page 41: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Node security learning

▪ https://www.udemy.com/nodejs-security-pentesting-and-exploitation/

Page 42: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

Books

Page 43: Hacking NodeJS applications for fun - FOSDEM · Hacking NodeJS applications for fun and profit Testing NodeJS Security by @jmortegac. Agenda Introduction nodejS security Npm security

References

▪ https://blog.risingstack.com/node-js-security-checklist/▪ https://blog.risingstack.com/node-js-security-tips/▪ https://www.npmjs.com/package/helmet▪ https://expressjs.com/en/advanced/best-practice-security.html▪ https://expressjs.com/en/advanced/security-updates.html▪ http://nodegoat.herokuapp.com/tutorial▪ https://www.owasp.org/index.php/Projects/OWASP_Node_js_Goa

t_Project