harness the power of http headers to secure your web apps

58
© Zühlke 2016 Harness the power of HTTP headers to secure your web Apps Drag picture to placeholder or click icon to add Slide 1

Upload: daniel-gartmann

Post on 07-Feb-2017

290 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Harness the power of http headers to secure your web apps

© Zühlke 2016

Harness the power of HTTP headers to secure your web Apps

Drag picture to placeholder or click icon to add

Slide 1

Page 2: Harness the power of http headers to secure your web apps

© Zühlke 2016

whoami

Daniel GartmannSenior Software Engineer @ Zuhlke Engineering LtdMy job: Software Engineering ∩ SecurityTwitter: @daniel_gartmann Email: [email protected]

Slide 2

Page 3: Harness the power of http headers to secure your web apps

© Zühlke 2016

Agenda

• What’s wrong with the Web?• HTTP Strict Transport Security (HSTS) • HTTP Public Key Pinning (HPKP) • Content Security Policy (CSP)• Conclusion

Slide 3

Page 4: Harness the power of http headers to secure your web apps

© Zühlke 2016

What’s wrong with the Web?

• It’s the most popular app-delivery platform we ever had• Core communication protocols are insecure• Web security is often misunderstood

Slide 4

Page 5: Harness the power of http headers to secure your web apps

© Zühlke 2016

What’s wrong with the Web?

Slide 5

1) Request some resources

2) Deliver some code3) Code is executed on the fly by the browser!

Browsers Remote Code Execution engines

Page 6: Harness the power of http headers to secure your web apps

© Zühlke 2016

What’s wrong with the Web?

Slide 6

Page 7: Harness the power of http headers to secure your web apps

© Zühlke 2016

Origin matters…

What are HTTPS security guarantees?• Authenticity verifying identities• Confidentiality keeping secret• Integrity safe transport

Slide 7

Page 8: Harness the power of http headers to secure your web apps

© Zühlke 2016

How to get certs for free?

Slide 8https://letsencrypt.org/

Page 9: Harness the power of http headers to secure your web apps

© Zühlke 2016

How to automate?

Slide 9

https://certbot.eff.org/

Page 10: Harness the power of http headers to secure your web apps

© Zühlke 2016

How to test/debug your config?

Slide 10

https://www.ssllabs.com/ssltest/

Page 11: Harness the power of http headers to secure your web apps

© Zühlke 2016

HTTP Strict Transport Security (HSTS)

Mitigate following UA TLS implementation weaknesses:• [no scheme]foo.com defaults to http://foo.com and not https://foo.com

( SSL Stripping)• Possibility to click through certificate errors

(expired, wrong host, self-signed, untrusted root,…, MITM)• Allow mixed content (to some extent – active vs passive)• Cookies not subject to SOP (redirect attacks – no secure flag)

HSTS Rewrite all HTTP requests to HTTPSHSTS All certificate errors are considered fatal

Slide 11

Page 12: Harness the power of http headers to secure your web apps

© Zühlke 2016

Without HSTS

Slide 12

Page 13: Harness the power of http headers to secure your web apps

© Zühlke 2016Slide 13

Page 14: Harness the power of http headers to secure your web apps

© Zühlke 2016

With HSTS

Slide 14

Page 15: Harness the power of http headers to secure your web apps

© Zühlke 2016

How does HSTS work?

Slide 15

1) Request a resource over HTTP

2) Redirect (302) to HTTPS

5) Cached by UA

6) Subsequent requests are all secure!

3) Execute redirect to HTTPS

4) Sets HSTS header: Strict-Transport-Security: max-age=31536000

Page 16: Harness the power of http headers to secure your web apps

© Zühlke 2016

HTTP request on HSTS enabled website

Slide 16

2) HTTP request

4) HTTPS request

3) Local redirect (307)

5) Response

1) HSTS cached by UA

Page 17: Harness the power of http headers to secure your web apps

© Zühlke 2016

Secure all your subdomains (if you can!)

Slide 17

a.foo.com

foo.com

b.foo.com

Strict-Transport-Security: max-age=31536000; includeSubDomains

Page 18: Harness the power of http headers to secure your web apps

© Zühlke 2016

How to (partially) revoke HSTS?

Slide 18

Strict-Transport-Security: max-age=0

chrome://net-internals/#hsts

Page 19: Harness the power of http headers to secure your web apps

© Zühlke 2016

Trust On First Use (TOFU)

• First request is at risk• Mitigation hardcode HSTS into UAs

• https://hstspreload.appspot.com/

Slide 19

Strict-Transport-Security: max-age=10886400; includeSubDomains; preload

Page 20: Harness the power of http headers to secure your web apps

© Zühlke 2016

HSTS deployment best practices

Low risk approach:• Configure redirection of all HTTP traffic to HTTPS • Add HSTS header with a low max-age (e.g. 1min) value• Extend policy with includeSubDomains (if possible!) • Ensure that all works fine and increase progressively the max-age (e.g.

to 12 months?)• Submit domain to be preloaded into UAs

Slide 20

Page 21: Harness the power of http headers to secure your web apps

© Zühlke 2016

HTTP Strict Transport Security (HSTS)

http://caniuse.com/#search=HSTS

Slide 21

Page 22: Harness the power of http headers to secure your web apps

© Zühlke 2016

HTTP Public Key Pinning (HPKP)

Protect against:• Rogue Certificate Authorities (CA) – Put everyone at risk• Compromised CAs – Put everyone at risk• MITM attacks

HPKP Limit your circle your trust!

Slide 22

Page 23: Harness the power of http headers to secure your web apps

© Zühlke 2016

How is trust established on the Web?

Slide 23

Certificate Authority

1) Identification “Alice”

2) PublicKeyAlice

3) thumbprintAlice = Sign(Alice || PublicKeyAlice, PrivateKeyCA)

4) CertificateAlice = [Alice, PublicKeyAlice, thumbprintAlice]

[PrivateKeyAlice, PublicKeyAlice]

[PrivateKeyCA, PublicKeyCA]

Verify(Alice || PublicKeyAlice, thumbprintAlice, PublicKeyCA)

1) CertificateAlice

Page 24: Harness the power of http headers to secure your web apps

© Zühlke 2016

Do you trust them all?

Slide 24

Page 25: Harness the power of http headers to secure your web apps

© Zühlke 2016

How bulletproof is the web trust model?

• Millions of certs are issued every year• Commercial Security (CAs profit, UAs vendors marketshare) • Any CA can issue certs for any domain (without asking permission)• The security is as good as the weakest link

Slide 25

Page 26: Harness the power of http headers to secure your web apps

© Zühlke 2016

How does HPKP work?

Slide 26

1) HTTPS request

3) Pins cached by UA

2) Sets HPKP header

Public-Key-Pins: max-age=5184000;pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=";pin-sha256="K87oWBWM9UZfyddvDfoxL+8lpNyoUB2ptGtn0fv6G2Q=";includeSubDomains

Next connection/handshake – UA validates the certs using the cached pins!

Page 27: Harness the power of http headers to secure your web apps

© Zühlke 2016

What to pin? pin = base64(sha256(SPKI))

Slide 27

Page 28: Harness the power of http headers to secure your web apps

© Zühlke 2016

How to pin?

Slide 28

Page 29: Harness the power of http headers to secure your web apps

© Zühlke 2016

Where to pin?

Slide 29

No pinning accept all certs signed by all CAs present in the trust store

Pin the root cert accept all certs signed by a given CA

Pin the intermediary cert accept all certs signed by the given intermediate cert

Pin the leaf cert accept only a single cert – your cert!

Page 30: Harness the power of http headers to secure your web apps

© Zühlke 2016

Report pin-validation failures

Public-Key-Pins: max-age=2592000;pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="; pin-sha256="LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="; report-uri="http://example.com/pkp-report"

Slide 30

Page 31: Harness the power of http headers to secure your web apps

© Zühlke 2016

Deployment without enforcement

Public-Key-Pins-Report-Only: max-age=2592000;pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="; pin-sha256="LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="; report-uri="http://example.com/pkp-report"

Slide 31

Page 32: Harness the power of http headers to secure your web apps

© Zühlke 2016

Awesome tools out there – use them!https://report-uri.io/

Slide 32

Page 33: Harness the power of http headers to secure your web apps

© Zühlke 2016

HPKP deployment best practices

Low risk approach:• Define your pins (at least 2 – pin of current cert + backup cert), max-age,

report-uri, includeSubDomains …• Deploy HPKP using report only• Ensure that all works fine and remove the “-Report-Only”

Slide 33

Page 34: Harness the power of http headers to secure your web apps

© Zühlke 2016

HTTP Public Key Pinning (HPKP)

http://caniuse.com/#search=HPKP Slide 34

Page 35: Harness the power of http headers to secure your web apps

© Zühlke 2016

Content Security Policy (CSP)

CSP gives you the power to define a policy that restricts how your web apps get executed.

Protects against:• Code injection attacks - XSS• Clickjacking• Malicious content sources• More…

Slide 35

Page 36: Harness the power of http headers to secure your web apps

© Zühlke 2016

How does CSP work?

Slide 36

1) Request

2) Response with CSP header Content-Security-

Policy: policy3) CSP compliant

UAenforces policy on the fly

Page 37: Harness the power of http headers to secure your web apps

© Zühlke 2016

How does a CSP policy look like?

Content-Security-Policy: default-src 'none'; base-uri 'self'; block-all-mixed-content; child-src render.githubusercontent.com; connect-src 'self' uploads.github.com status.github.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com wss://live.github.com; font-src assets-cdn.github.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; frame-src render.githubusercontent.com; img-src 'self' data: assets-cdn.github.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com *.githubusercontent.com; media-src 'none'; object-src assets-cdn.github.com; plugin-types application/x-shockwave-flash; script-src assets-cdn.github.com; style-src 'unsafe-inline' assets-cdn.github.com Slide 37

Page 38: Harness the power of http headers to secure your web apps

© Zühlke 2016

Whitelist trusted sources

default-src directive defines the default list of trusted sources / schemes for the app being executed by the UA.

Slide 38

Content-Security-Policy: default-src 'self' ; Same origin policyContent-Security-Policy: default-src 'none' ;Content-Security-Policy: default-src http://foo.com;Content-Security-Policy: default-src http://foo.com http://boo.com ;Content-Security-Policy: default-src * ; Please don’t do that!

Page 39: Harness the power of http headers to secure your web apps

© Zühlke 2016

Whitelist trusted sources

default-src applies the defined policy to following directives (that can be overwritten for more specific behaviour):• script-src <script> • connect-src XMLHttpRequest, WebSocket and

EventSource• child-src <frame> and <iframe>. • font-src @font-face• img-src images and favicons• media-src <audio> and <video> • object-src <object>, <embed> and <applet> • style-src Stylesheets

Slide 39

Page 40: Harness the power of http headers to secure your web apps

© Zühlke 2016

Example 1/2

Slide 40

Page 41: Harness the power of http headers to secure your web apps

© Zühlke 2016

Example 2/2

Slide 41

Page 42: Harness the power of http headers to secure your web apps

© Zühlke 2016

Why is XSS so scary?

XSS enables an attacker to execute malicious code in victims browser’s context in order to:

• Steal users credentials• Hijack user session• Steal, modify or delete user’s data• Arbitrary code execution

Slide 42

1) Link with XSS payload

2) Click the link

3) Request with XSS payload

4) XSS payload “Reflected”

5) XSS payload is executed

Page 43: Harness the power of http headers to secure your web apps

© Zühlke 2016

What can CSP do against XSS?Examples of malicious injected payloads:

Primary defence against XSS is to always output encode untrusted data.

Consider CSP only as an additional layer of defence against XSS Think about no/partially CSP compliant UAs.

Once CSP is enabled it prevents execution of:• Inline scripts• Inline event handlers• Inline styles• Functions that generate code out of strings

Slide 43

Page 44: Harness the power of http headers to secure your web apps

© Zühlke 2016

CSP prevents inline JS / Event handlers

Slide 44

Injected payload

Page 45: Harness the power of http headers to secure your web apps

© Zühlke 2016

CSP prevents unsafe JS functions (string code)

Slide 45

Injected payload

Page 46: Harness the power of http headers to secure your web apps

© Zühlke 2016

Lax CSP

Slide 46

Avoid if possible!

Page 47: Harness the power of http headers to secure your web apps

© Zühlke 2016

What if you badly need an inline script?

Slide 47

1) hash = base64(sha256(alert(1);))2) add it to the script-src directive

Or use: https://report-uri.io/home/hash/

Page 48: Harness the power of http headers to secure your web apps

© Zühlke 2016

What if you badly need an inline style?

Slide 48

1) hash = base64(sha256(body {…}))2) add it to the style-src directive

Or use: https://report-uri.io/home/hash/

Page 49: Harness the power of http headers to secure your web apps

© Zühlke 2016

What can CSP do against XSS?

The reflected-xss directive configures the UA built in XSS filter against reflected attacks.Possible values:• Allow - Allows reflected XSS attacks• Filter - Filter the reflected XSS attack• Block - Block reflected XSS attacks Maximum security

Does not deprecate: X-XSS-Protection: 1; mode=block headerSlide 49

Content-Security-Policy: default-src 'self' ; reflected-xss block;

Page 50: Harness the power of http headers to secure your web apps

© Zühlke 2016

What can CSP do against Clickjacking?

Slide 50

Page 51: Harness the power of http headers to secure your web apps

© Zühlke 2016

What can CSP do against Clickjacking?

Mitigation Prevent malicious websites to frame your web app

Slide 51

Content-Security-Policy: default-src 'self' ; frame-ancestors * ;Content-Security-Policy: default-src 'self' ; frame-ancestors 'self' ;Content-Security-Policy: default-src 'self' ; frame-ancestors http://foo.com;Content-Security-Policy: default-src 'self' ; frame-ancestors 'none' ; Does not deprecate: X-Frame-Options: DENY | SAMEORIGIN |

ALLOW-FROM

Page 52: Harness the power of http headers to secure your web apps

© Zühlke 2016

CSP Reporting

Slide 52

Content-Security-Policy: default-src 'self' ; report-uri http://foo.com ;

Page 53: Harness the power of http headers to secure your web apps

© Zühlke 2016

Deploying CSP without breaking things

1) Define your policy e.g default-src 'self'2) Define your monitoring endpoint e.g report-uri http://foo.com3) Deploy in report only mode

Content-Security-Policy-Report-Only: default-src 'self' ; report-uri http://foo.com;

4) Ensure that all works fine5) Deploy without “-Report-Only”

Content-Security-Policy: default-src 'self' ; report-uri http://foo.com;6) Monitor when you get attacked

Content-Security-Policy:

Slide 53

Page 54: Harness the power of http headers to secure your web apps

© Zühlke 2016

Content Security Policy (CSP 1.0)

Slide 54

http://caniuse.com/#search=csp

Page 55: Harness the power of http headers to secure your web apps

© Zühlke 2016

Content Security Policy (CSP 2.0)

Slide 55http://caniuse.com/#search=csp

Page 56: Harness the power of http headers to secure your web apps

© Zühlke 2016

Content Security Policy (CSP 3.0)

Slide 56https://www.w3.org/TR/CSP3/

Page 57: Harness the power of http headers to secure your web apps

© Zühlke 2016

Specifications

Slide 57

Page 58: Harness the power of http headers to secure your web apps

© Zühlke 2016

Web platform is maturing towards a secure Web Platform

• HSTS, HPKP, CSP significantly reduce the attack surface• Use them with caution – don’t DoS yourself!• CSP is less risky than HSTS, HPKP• Security for FREE!• Start using them today!

Slide 58