54
Dive into Drupal Security @greggles Friday, May 18, 2012

Drupal Security Dive Into the Code

Embed Size (px)

DESCRIPTION

Drupal Security Dive Into the Code - this presentation looks at cross site scripting (xss), sql injection, and cross site request forgeries (csrf) in Drupal. The presentation was given at DrupalGovDays in Washington DC May 18, 2012.

Citation preview

Page 2: Drupal Security Dive Into the Code

Greg KnaddisonPair programmer

@gregglesAcquian

Drupal Security Team

Friday, May 18, 2012

Page 3: Drupal Security Dive Into the Code

US$15 on kindle, US$26 paperbackcrackingdrupal.com

Friday, May 18, 2012

Page 4: Drupal Security Dive Into the Code

Overview

Warm up

CSRF, XSS, SQLi code

Agenda

Friday, May 18, 2012

Page 5: Drupal Security Dive Into the Code

think like a diver

Friday, May 18, 2012

Page 6: Drupal Security Dive Into the Code

be the attacker

Say hello to $user_data

Friday, May 18, 2012

Page 7: Drupal Security Dive Into the Code

XSS Access Bypass CSRFAuthentication/Session Arbitrary Code Execution SQL InjectionOthers

48%

16%

10%

3%

4%

7%

12%

reported in core and contrib SAs from 6/1/2005 through 3/24/2010

Drupal vulnerabilities by type

Friday, May 18, 2012

Page 8: Drupal Security Dive Into the Code

Eddy Out: Definitions

A1 - Injection

A2 - XSS

A3 - Broken Authentication and Session Mgmt

A4 - Insecure Direct Object References

A5 - Cross Site Request Forgery

Friday, May 18, 2012

Page 9: Drupal Security Dive Into the Code

Eddy Out: Definitions

A6 - Security Misconfiguration

A7 - Insecure Cryptographic Storage

A8 - Failure to Restrict URL Access

A9 - Insufficient Transport Layer Protection

A10 - Unvalidated Redirects and Forwards

Friday, May 18, 2012

Page 10: Drupal Security Dive Into the Code

Eddy Out: Freebies

A3 - Broken Authentication and Session Mgmt

A7 - Insecure Cryptographic Storage

A9 - Insufficient Transport Layer Protection

But don’t stop at the top 10...or today’s 3

Friday, May 18, 2012

Page 11: Drupal Security Dive Into the Code

The basicsToes in the water

Friday, May 18, 2012

Page 12: Drupal Security Dive Into the Code

Security Review module

Free

Automated check of configurations

drupal.org/project/security_review

Demo

http://crackingdrupal.com/n/32

Friday, May 18, 2012

Page 13: Drupal Security Dive Into the Code

Captaining your ship

ssh or sftp, but never ftp

shared wifi? https if you can, vpn if you can’t

Least privilege

Audit roles

Friday, May 18, 2012

Page 14: Drupal Security Dive Into the Code

Stay up to date

Seriously

Friday, May 18, 2012

Page 15: Drupal Security Dive Into the Code

Modernize your vessel

Update module (can email you)

Mailing list

@drupalsecurity

rss: d.o/security/ d.o/security/contrib etc.

Friday, May 18, 2012

Page 16: Drupal Security Dive Into the Code

Head for the lifeboats

Have backups

Test them periodically

Be able to restore them

Sanitize before traveling with them

http://crackingdrupal.com/n/53

Friday, May 18, 2012

Page 17: Drupal Security Dive Into the Code

XSSaka: Cross Site Scripting

code in browser using your session

Friday, May 18, 2012

Page 18: Drupal Security Dive Into the Code

XSS

Code

Running in your browser

Using your cookies on your site

Requesting, sending, reading responses

Browser context

Does that sound familiar?

Friday, May 18, 2012

Page 19: Drupal Security Dive Into the Code

Ajax

DrupalHTML

JSUser

Friday, May 18, 2012

Page 20: Drupal Security Dive Into the Code

Cross Site Scripting

= Bad

DrupalAttacker JSHTML

JSVictim

Friday, May 18, 2012

Page 21: Drupal Security Dive Into the Code

Validate input

“Why would I ever want javascript in a node title?”

-developer who forgot to filter on output

Friday, May 18, 2012

Page 22: Drupal Security Dive Into the Code

Validate input

Is it an email?

Is it a nid (right type? that they have access to?)

Is this my beautiful wife?

Is this my beautiful house?

Validation is NOT filtering

Validation is “yes or no” - user fixes it

Friday, May 18, 2012

Page 23: Drupal Security Dive Into the Code

Filter on output

“output”

“filter”

“on”

Friday, May 18, 2012

Page 24: Drupal Security Dive Into the Code

Friday, May 18, 2012

Page 25: Drupal Security Dive Into the Code

Output Contexts

Mail context

Database context

Web context

Server context

http://acko.net/blog/safe-string-theory-for-the-web

Friday, May 18, 2012

Page 26: Drupal Security Dive Into the Code

Filtering XSS

Input untrusted data

Output browser appropriate data

check_plain, check_markup

filter_xss, filter_xss_admin

free: l(), t() @ and %, drupal_set_title

Friday, May 18, 2012

Page 27: Drupal Security Dive Into the Code

Friday, May 18, 2012

Page 28: Drupal Security Dive Into the Code

htmlhtmlblahhtml

<? print $node_title ?>html

Friday, May 18, 2012

Page 29: Drupal Security Dive Into the Code

htmlhtmlblahhtml

<script>alert(‘xss’);

<script>html

Friday, May 18, 2012

Page 30: Drupal Security Dive Into the Code

htmlhtmlblahhtml

&lt;script&gt;alert(‘xss’);

&lt;/script&gt;html

htmlhtmlblahhtml

alert(‘xss’);html

Friday, May 18, 2012

Page 31: Drupal Security Dive Into the Code

Are you my XSS?

drupal_set_message($user_data);

$output .= $node->title;

FAPI checkboxes, radios, descriptions, etc.

Friday, May 18, 2012

Page 32: Drupal Security Dive Into the Code

Identifying XSS

<script>alert(‘xss’);</script>

<img src=”asdf.png” onerror=”alert(‘xss’)”>

Friday, May 18, 2012

Page 33: Drupal Security Dive Into the Code

Deep Dive on XSSFriday, May 18, 2012

Page 34: Drupal Security Dive Into the Code

XSS Resources

http://drupalscout.com/tags/xss

Friday, May 18, 2012

Page 35: Drupal Security Dive Into the Code

SQL Injection

Friday, May 18, 2012

Page 36: Drupal Security Dive Into the Code

User modified data

Included into a query

Without filtering

Friday, May 18, 2012

Page 37: Drupal Security Dive Into the Code

phpphp

sql $user_dataphpphp

Friday, May 18, 2012

Page 38: Drupal Security Dive Into the Code

phpphp

sql ‘’;delete from users;phpphp

Friday, May 18, 2012

Page 39: Drupal Security Dive Into the Code

Fixing SQL Injection

“Use Drupal’s database API”

Placeholders

DBTNG, ORM, Methods (not that complex)

Friday, May 18, 2012

Page 40: Drupal Security Dive Into the Code

Dive on SQL InjectionFriday, May 18, 2012

Page 41: Drupal Security Dive Into the Code

CSRFCross Site Request Forgery

Taking action without confirming intent.

Friday, May 18, 2012

Page 42: Drupal Security Dive Into the Code

Taking action without confirming intent.

How do we confirm intent?

WTF is intent?

Friday, May 18, 2012

Page 43: Drupal Security Dive Into the Code

<a href=”/delete/user/1”>Delete user 1</a>

Friday, May 18, 2012

Page 44: Drupal Security Dive Into the Code

<a href=”/delete/1”>Delete user 1</a>

<img src=”/delete/1”>

Friday, May 18, 2012

Page 45: Drupal Security Dive Into the Code

CSRF Flow

Drupal

/user

Victim

html

cookie

Friday, May 18, 2012

Page 46: Drupal Security Dive Into the Code

CSRF Flow

Drupal

node/1

Victim

html

Friday, May 18, 2012

Page 47: Drupal Security Dive Into the Code

CSRF Flow

Drupal

node/1

Victim

html

js

jquery.js

css

foo.css

etc.

delete/1object deleted

in db

cookie

Friday, May 18, 2012

Page 48: Drupal Security Dive Into the Code

How do you exploit it?

URL Shorteners

<img src=”http://example.com/delete/2”>

Send a message to a site admin

What is my email address or twitter?

Friday, May 18, 2012

Page 49: Drupal Security Dive Into the Code

Are you my CSRF?

menu call back with an action verb and not drupal_get_form

directly use $_POST, $_GET, arg(), menu object

not using form_submit OR drupal_get_token

Friday, May 18, 2012

Page 50: Drupal Security Dive Into the Code

Tokens (aka nonce)

Form API includes tokens by default

do form, form_validate, form_submit

don’t $_POST

OR: drupal_get_token, drupal_valid_token

Friday, May 18, 2012

Page 51: Drupal Security Dive Into the Code

Deep Dive on CSRFFriday, May 18, 2012

Page 52: Drupal Security Dive Into the Code

CSRF Resources

http://drupalscout.com/tags/csrf

Friday, May 18, 2012

Page 53: Drupal Security Dive Into the Code

Resources

drupal.org/security

groups.drupal.org/best-practices-drupal-security

drupalscout.com

acquia.com

crackingdrupal.com

Friday, May 18, 2012

Page 54: Drupal Security Dive Into the Code

Thanks!questions?

contact?@greggles

[email protected]

Friday, May 18, 2012