secure programming lecture 9: web application security ... · application security (intro,...

46
Secure Programming Lecture 9: Web Application Security (intro, authentication) David Aspinall 15th February 2018

Upload: others

Post on 01-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Secure Programming Lecture 9: WebApplication Security (intro,

authentication)

David Aspinall

15th February 2018

Page 2: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outline

Introduction

Basics: HTTP

Authentication

Cookies and sessions

Summary

Page 3: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

OWASP

The Open Web Application Security Project is a charitystarted in 2001, to promote mechanisms for securingweb apps in a non-proprietary way.

They have local chapters worldwide; the Scotlandchapter sometimes meets in Appleton Tower.

Like CERT and Mitre, OWASP produce taxonomies ofweaknesses and coding guidelines.

Their most well known output is the OWASP Top 10 list ofweaknesses in web applications.

Page 4: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

OWASP Top 10 list 2017

Page 5: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

OWASP Top 10 list 2017

É A1 InjectionÉ A2 Broken AuthenticationÉ A3 Sensitive Data ExposureÉ A4 XML External Entities (XXE)É A5 Broken Access ControlÉ A6 Security MisconfigurationÉ A7 Cross-Site Scripting (XSS)É A8 Insecure DeserializationÉ A9 Using Components with Known VulnerabilitiesÉ A10 Insufficient Logging & Monitoring

The list is compiled using surveys of application securitycompanies and an industry survey of 500 people,claiming to represent over 100k apps/APIs.

See https://www.owasp.org/index.php/Top_10-2017_Top_10

Page 6: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Changes since OWASP Top 10 list 2013

Web app programming changes:

É Growth of microservices, using APIs and RESTÉ architectural assumptions on old code violated

É Single Page Apps (Angular, React)É functionality moved from server to client

Top 10 changes suggested by data and community:

É XML handling, deserialisationÉ Logging

Moved down in priority: CSRF, unvalidated redirects.

Page 7: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Roadmap

É A1 Injection ØÉ A2 Broken AuthenticationÉ . . .

In the next few lectures and lab sessions we’ll look atweb app security and some of the main weaknesscategories.

Page 8: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Overview

We start with a quick review of the basics.

Whether you program web sites using a

É Microservice architecture (Node.js, Spring Boot)É Web Application Framework (Rails, Django, . . . )É Content Management System (Joomla, Drupal, . . . )É Wiki (MediaWiki, Confluence, . . . )É Blog (Wordpress, . . . )É . . . anything else

knowing what is happening underneath is important tounderstand how security provisions work (or don’t).

Similarly, we looked at assembler code and CPU execution for Capplications, to understand what was really going on “under the

bonnet”.

Page 9: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outline

Introduction

Basics: HTTP

Authentication

Cookies and sessions

Summary

Page 10: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP

HTTP = Hyper Text Transfer Protocol

É Protocol used for web browsingÉ and many other things by now (Q. Why?)

É Specifies messages exchangedÉ HTTP/1.1 specified in RFC 2616É request methods: GET, POST, PUT, DELETE

É Messages are text based, in lines (Unix: CR+LF)É Stateless client-side design

É quickly became a problem, hence cookies

É NB: HTTP is entirely separate from HTML!É HTTP headers not HTML <HEAD>É HTML is text format for web content

Page 11: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP communication

HTTP is a client-server protocol.

É Client initiates TCP connection (usually port 80)É Client sends HTTP request over connectionÉ Server responds

É may close connection (HTTP 1.0 default)É or keep it persistent for a wee while

É Server never initiates a connectionÉ except in newer HTML5 WebSocketsÉ WebSockets allow low-latency interactivityÉ Upgrade: websocket handshake & switch to WSÉ expect to see rise in use and security issues. . .

Page 12: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP GET message (simplified)

GET / HTTP/1.1Host: www.bbc.co.ukUser-Agent: Mozilla/5.0Accept: text/htmlAccept-Language: en-US,en;q=0.5

Page 13: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP GET message (full)

GET / HTTP/1.1Host: www.bbc.co.ukUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateDNT: 1Connection: keep-alivePragma: no-cacheCache-Control: no-cache

Page 14: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP Response (simplified)

HTTP/1.1 200 OKServer: ApacheContent-Type: text/html; charset=UTF-8Date: Wed, 19 Feb 2014 14:30:42 GMTConnection: keep-alive

<!DOCTYPE html> <html lang="en-GB" > <head> < !-- Barlesque 2.60.1 --><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="description" content="Explore the BBC, for latest news,sport and weather, TV &amp; radio schedules and highlights, withnature, food, comedy, children's programmes and much more" />...

Page 15: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

HTTP Response (full)

HTTP/1.1 200 OKServer: ApacheEtag: "c8f621dd5455eb03a12b0ad413ab566f"Content-Type: text/htmlTransfer-Encoding: chunkedDate: Wed, 19 Feb 2014 20:12:34 GMTConnection: keep-aliveSet-Cookie: BBC-UID=a583d...4929Mozilla/5.0; expires=Sun, 19-Feb-18 20:12:34 GMT; path=/; domain=.bbc.co.ukX-Cache-Action: HITX-Cache-Hits: 574X-Cache-Age: 50Cache-Control: private, max-age=0, must-revalidateX-LB-NoCache: trueVary: X-CDN

d1c<!DOCTYPE html>...

Note: cache fingerprint; chunked transfer; cookie; cache directives.

Page 16: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Client != Browser

[dice]da: telnet www.bbc.co.uk 80Trying 212.58.244.71...Connected to www.bbc.net.uk.Escape character is '^]'.GET / HTTP/1.0Host: www.bbc.co.ukAccept: text/html, text/plain, image/*Accept-Language: enUser-Agent: Handwritten in my terminal

Page 17: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Client != Browser

HTTP/1.1 200 OKServer: ApacheContent-Type: text/htmlDate: Wed, 19 Feb 2014 14:26:00 GMT...

Client-side security doesn’t existÉ Any program can conduct HTTP(S) communicationsÉ . . . URLs can be constructed arbitrarilyÉ . . . POST forms content alsoÉ In server-side context, there are no input validation

guarantees despite any client-side code.

Page 18: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Referer header

GET /news/ HTTP/1.1Host: www.ed.ac.ukUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateDNT: 1Referer: http://www.ed.ac.uk/homeConnection: keep-alive

Page 19: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Referer header

Question. What immediate security issue arises fromthis header?

Page 20: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Referer header

Page 21: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Referer header

GET /loggedin/secretfile.html HTTP/1.1Host: www.mycompany.comAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateDNT: 1Referer: http://www.mycompany.com/loggedin/

Don’t rely on Referer header for access decisions!É Flawed assumption made in old web apps:

user has navigated to a logged in area, thereforethey must be logged in

É But Referer is from client, cannot be trusted!É Also risky because of TOCTOUÉ and confuses authentication with authorization

Page 22: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Inputs via GET Request

http://www.shop.com/products.asp?name=Dining+Chair&material=Wood

É Input encoded into parameters in URLÉ Bad for several reasons:

É SEO optimisation: URL not canonicalÉ cache behaviour (although not relevant for login)

Question. What’s another reason this format is bad?

Page 23: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Inputs via GET Request

http://someplace.com/login.php?username=jdoe&password=BritneySpears

É URL above is visible in browser navigation bar!

Page 24: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

POST Request

POST /login.php HTTP/1.0Host: www.someplace.examplePragma: no-cache

Cache-Control: no-cacheUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5a)Referer: http://www.someplace.example/login.phpContent-type: application/x-www-form-urlencodedContent-length: 49

username=jdoe&password=BritneySpears

É URL in browser:http://www.someplace.example/login.php

Page 25: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

GET versus POST

É GET is a request for informationÉ can be (transparently) resent by browsersÉ also may be cached, bookmarked, kept in history

É POST is an update providing informationÉ gives impression that input is hiddenÉ browsers may treat differently

É neither provide confidentiality without HTTPS!É plain text, can be sniffed

É in practice, GET often changes state somewhereÉ user searches for something, gets recordedÉ user has navigated somewhere, gets recordedÉ so shouldn’t think GET implies functional

Page 26: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

When to use POST instead of GETÉ For sensitive data, always use POST

É helps with confidentiality but not enough alone

É For large data, use POSTÉ URLs should be short (e.g., <=2000 chars)É longer URLs cause problems in some software

É For actions with (major) side effects use POSTÉ mainly correctness; many early web apps wrong

These are general guidelines. There are sometimes more complextechnical issues to prefer GET.

Page 27: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outline

Introduction

Basics: HTTP

Authentication

Cookies and sessions

Summary

Page 28: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Authentication

You know this already:

É Something you haveÉ Something you knowÉ Something you are

Question. What else?

Question. What about machine-machineauthentication?

Page 29: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Is the application vulnerable?

É Credentials can be guessed or overwritten throughweak account management functionsÉ default passwordsÉ broken account creation/recovery

É Automated brute-force attacks possible (“credentialstuffing”)

É Passwords or other credentials are sent overunencrypted connections.

É Credentials aren’t protected when stored (stolenentries vulnerable to offline attack)

É Multi-factor (or recovery factor) broken/missing

Exercise. Explain/revise how each of these drawbackscan be addressed.

Page 30: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Modern password recommendations

É Avoid passwords if possibleÉ Don’t require rotation unless compromise signsÉ Recommend secure storage mechanismsÉ Check for weak passwords, use (sane) password

complexity rulesÉ Rate-limit logins to prevent automated attacksÉ Use multi-factor authentication for recovery

An example where recent practical academic researchhas had some nice impact. See

É the 2016 UK NCSC Password guidance: simplifyingyour approach

É the 2017 NIST 800-63B which has a comprehensiveproposal for Authentication Assurance Levels

Page 31: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outlook for web authentication/identity

We’re likely to see more shared facilities (and a battle):

É Interoperable schemes, e.g. OWASP ASVSÉ Perhaps using OAUTH, OpenIDÉ FIDO, an industry-led initiative to replace passwords

É Maybe Government identity verification, e.g.,GOV.UK Verify.

Page 32: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outline

Introduction

Basics: HTTP

Authentication

Cookies and sessions

Summary

Page 33: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Cookies: state in a stateless world

Some state is highly desirable between requests:

É remember user’s preferences, navigation point, . . .É web applications: user logged in

However, also the less desirable:

É advertising network tracking idsÉ may be shared between websitesÉ thus can profile user browsing behaviourÉ hence compromise privacyÉ also risk of theft

É if browser/machine compromised, orÉ if cookies passed in clear

Page 34: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Cookies and the law

See Sitebeam’s cheeky infographic

Page 35: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Cookies in HTTP headers

É Specified in RFC6265É Just ASCII plain text

É Sent by serverÉ Stored in client (database, filesystem, . . . )É Returned by client when visiting page again

É Cookies can be set by the server for a particularpath/domainÉ then sent for any page matching

É Multiple cookies may be set and returnedÉ Cookies may have a limited lifetime

É set by Expires or Max-Age

Page 36: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Setting cookies

Server -> User Agent

Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnlySet-Cookie: mylanguage=en-GB; Path=/; Domain=example.com

User Agent -> Server

Cookie: SID=31d4d96e407aad42; mylanguage=en-GB

Page 37: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Secure cookies?

RFC6265: The Secure attribute limits the scope of thecookie to “secure” channels (where “secure” isdefined by the user agent). When a cookie has theSecure attribute, the user agent will include the cookiein an HTTP request only if the request is transmittedover a secure channel (typically HTTP over TransportLayer Security (TLS) [RFC2818]).

É . . . provided browser obeys thisÉ still, no harm in using (defence in depth)

the HttpOnly attribute is similar, and forbids the browser fromallowing JavaScript access to the cookie, in principle.

Page 38: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Expiry dates

Server -> User Agent

Set-Cookie: mylanguage=en-US; Expires=Wed, 09 Jun 2024 10:18:14 GMT

User Agent -> Server

Cookie: SID=31d4d96e407aad42; mylanguage=en-US

É Of course, no guarantee cookie is kept for 6 years. . .

Page 39: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Removing cookiesRFC6265: To remove a cookie, the server returns aSet-Cookie header with an expiration date in the past.The server will be successful in removing the cookie onlyif the Path and the Domain attribute in the Set-Cookieheader match the values used when the cookie wascreated.

Server -> User Agent

Set-Cookie: lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT

User Agent -> Server

Cookie: SID=31d4d96e407aad42

É Again, no guarantee of what browser actually doesÉ . . . if indeed the same browser is being used

Page 40: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Session hijacking

Web apps use session IDs as a credential

É if an attacker steals a SID, she is logged in!

This is session hijacking.

Many possible theft mechanisms:

É XSS, sniffing, interceptionÉ or: calculate, guess, brute-forceÉ also session fixation

É using same SID from unauthenticated to logged inÉ attacker grabs/sets SID before user visits site

Page 41: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Session hijacking defences

Web apps (or frameworks) should implement defences,and discard SIDs if something suspicious happens.

É Link SID to IP address of clientÉ but problems if behind NAT, transparent proxiesÉ ISP proxy pools mean need to use subnet, not IPÉ subnet may be shared with attacker!

É Link SID to HTTP Headers, e.g. User-AgentÉ but can be trivially faked. . . and usually guessedÉ . . . or captured (trick victim to visit recording site)

Page 42: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

OWASP: Is the application vulnerable?

Poor Session ID (SIDs) management by:

É exposing SIDs in the URL (e.g., URL rewriting).É SIDs are vulnerable to session fixation attacks.É SIDs don’t timeout, or sessions/tokens aren’t

invalidated in logout.É SIDs are weak (small entropy, or predictable)É Session IDs aren’t rotated after a new login.

Page 43: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

OWASP: How do I do things correctly?

See the

É OWASP Session Management Cheat Sheet

Or use a framework in which there is a strong degree ofconfidence that things have been done properly.

General Secure Programming advice: reuse believed-to-be-securesolutions as far as possible. It may be tempting to use the latest

WhoJamig WebApp Framework but think twice unless you’re sure it iswell programmed for security, not just appearance

Page 44: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Outline

Introduction

Basics: HTTP

Authentication

Cookies and sessions

Summary

Page 45: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

Review questions

HTTP Headers

É Describe three possible vulnerabilities for a webapplication posed by an attacker who fabricatesHTTP headers rather than using the web app runningvia a reliable browser.

É Explain the reasons for using POST rather than GET.What security guarantees does it provide?

Cookies

É Consider an online grocery merchant that uses acookie to store the user’s shopping basket, includingthe list of product IDs and their prices, encryptedusing a secret key derived from the SID. Whatthreats might be posed and by whom?

Page 46: Secure Programming Lecture 9: Web Application Security ... · Application Security (intro, authentication) David Aspinall 15th February 2018. Outline Introduction ... É Microservice

References

Some examples were adapted from:

É Innocent Code: a security wake-up call for webprogrammers by Sverre H. Huseby, Wiley, 2004.

as well as the named RFCs and the OWASP resources.