dip your toes in the sea of security (php dorset, 2nd june 2014)

53
Dip Your Toes in the Sea of Security James Titcumb PHP Dorset 2nd June 2014

Upload: james-titcumb

Post on 08-May-2015

2.011 views

Category:

Software


4 download

DESCRIPTION

Security is an enormous topic, and it’s really, really complicated. If you’re not careful, you’ll find yourself vulnerable to any number of attacks which you definitely don’t want to be on the receiving end of. This talk will give you just a taster of the vast array of things there is to know about security in modern web applications, such as writing secure PHP web applications and securing a Linux server. Whether you are writing anything beyond a basic brochure website, or even developing a complicated business web application, this talk will give you insights to some of the things you need to be aware of.

TRANSCRIPT

Page 1: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Dip Your Toesin the Sea of Security

James TitcumbPHP Dorset

2nd June 2014

Page 2: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim

Who is this guy?

Page 3: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Who are you?

https://www.flickr.com/photos/akrabat/10168019755/

Page 4: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Some simple code...

<?php

$a = (int)$_GET['a'];

$b = (int)$_GET['b'];

$result = $a + $b;

printf('The answer is %d', $result);

Page 5: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

Page 6: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple

Page 7: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple2. Know the risks

Page 8: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple2. Know the risks3. Fail securely

Page 9: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple2. Know the risks3. Fail securely4. Don’t reinvent the wheel

Page 10: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple2. Know the risks3. Fail securely4. Don’t reinvent the wheel5. Never trust anything / anyone

Page 11: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

OWASP& the OWASP Top 10

https://www.owasp.org/

Page 12: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Application Security(mainly PHP applications)

Page 13: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Always remember…

Filter InputEscape Output

Page 14: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

SQL Injection (#1)

http://xkcd.com/327/

Page 15: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

SQL Injection (#1)

1. Use PDO / mysqli2. Use prepared / parameterized statements

Page 16: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

SQL Injection (#1)<?php

$user_id = $_GET['user_id'];

$sql = "

SELECT * FROM users

WHERE user_id = {$user_id}";

$db->execute($sql); ✘

Page 17: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

SQL Injection (#1)<?php

$user_id = $_GET['user_id'];

$sql = "

SELECT * FROM users

WHERE user_id = :userid";

$stmt = $db->prepare($sql);

$stmt->bind('userid', $user_id);

$stmt->execute();✔

Page 18: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Cross-Site Scripting / XSS (#3)

● Escape output

Page 19: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Cross-Site Scripting / XSS (#3)

● Escape output<?php

$unfilteredInput = '<script type="text/javascript">...</script>';

// Unescaped - JS will run :'(

echo $unfilteredInput;

// Escaped - JS will not run :)

echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

Page 20: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Cross-Site Request Forgery / CSRF (#8)

● HTTP request● e.g. submit a POST request to login form

Page 21: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Errors, Exceptions & Logging (#6)

Sensitive Information exposurecustom Exception & Error handling - filter what the end user sees - Handle API responses in a unified way

Page 22: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

curl + https<?php

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Page 23: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

curl + https<?php

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($ch, CURLOPT_CAINFO, "/path/to/certificate");

Page 26: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

WordPress

Page 27: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

WordPress

Urgh.

Page 28: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

We are not security experts!

We CAN write secure code

Page 29: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

We are not security experts!

We CAN write secure code

● Learn more● Keep it simple● Think about attack vectors● Prioritise vulnerabilities

Page 30: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

● Be the “threat”● What do you want?

○ Personal data (name, address, DOB)○ Sensitive data (credit cards, bank accounts)○ Cause disruption (downtime)

● How would you do it?

Think Differently

Page 31: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Threat Modelling

● Damage● Reproducibility● Exploitability● Affected users● Discoverability

Page 32: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Authentication& Authorization

Page 33: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Authentication

== Verifying IDENTITY

● Password● Delegated password (LDAP etc.)● One-time code (SMS, Yubi, Google 2FA

etc.)● Biometrics (fingerprints, iris, DNA etc.)● Keycards / USB sticks etc.

Page 34: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

CRYPTOGRAPHYIS

HARD

Page 35: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

CRYPTOGRAPHYIS

HARDNEVER EVER “ROLL YOUR OWN”

Page 36: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

CRYPTOGRAPHYIS

HARDNEVER EVER “ROLL YOUR OWN”

EVER!!!

Page 37: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Case Study: Custom Authentication

We thought about doing this…

Page 38: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Case Study: Custom Authentication

We thought about doing this…

Page 39: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Case Study: Custom Authentication

We thought about doing this…

...but the we realised we wereovercomplicating things.

Page 40: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Password Hashing

● Never store plain text passwords● Never encrypt passwords● Always hash passwords● Use password_hash / password_verify

○ >= PHP 5.5○ < PHP 5.5, use ircmaxell/password-compat

Page 41: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Authorization

== Verifying ACCESS

● Access lists● Role-based● Attribute-based

Page 42: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Linux Server Security

Page 43: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

SSH Fortress

● Passwordless● Rootless● Lock down IP● Run on different port

Page 44: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Firewalls#!/bin/bash

IPT="/sbin/iptables"

$IPT --flush

$IPT --delete-chain

$IPT -P INPUT DROP

$IPT -P FORWARD DROP

$IPT -P OUTPUT DROP

# Loopback

$IPT -A INPUT -i lo -j ACCEPT

$IPT -A OUTPUT -o lo -j ACCEPT

# Inbound traffic

$IPT -A INPUT -p tcp --dport ssh -j

ACCEPT

$IPT -A INPUT -p tcp --dport 80 -j ACCEPT

# Outbound traffic

$IPT -A OUTPUT -p tcp --dport 80 -j

ACCEPT

$IPT -A OUTPUT -p udp --dport 53 -m state

--state NEW -j ACCEPT

Page 45: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Brute Force Attacks

● iptables (complex)● fail2ban● DenyHosts

Page 46: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Install Only What You Need

● Audit Regularly● Use provisioning● Don’t install cruft

Page 47: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Case Study: Be Minimal

Internets

Postfix

Squid Proxy(badly configured)

hacker

spam

Page 48: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Resources

● http://securingphp.com/● https://www.owasp.org/● http://blog.ircmaxell.com/

Page 49: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

The Golden Rules

1. Keep it simple2. Know the risks3. Fail securely4. Don’t reinvent the wheel5. Never trust anything / anyone

Page 50: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

If you follow all this, you get...

Page 51: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

If you follow all this, you get...

Page 52: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

Questions?

Page 53: Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)

James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim

Thanks for watching!