77
Dip Your Toes in the Sea of Security James Titcumb php[MiNDS] Meetup - January 2016

Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Embed Size (px)

Citation preview

Page 1: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Dip Your Toesin the Sea of Security

James Titcumbphp[MiNDS] Meetup - January 2016

Page 3: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Some simple code...

<?php

$a = (int)filter_var($_GET['a'], FILTER_SANITIZE_NUMBER_INT);

$b = (int)filter_var($_GET['b'], FILTER_SANITIZE_NUMBER_INT);

$result = $a + $b;

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

Page 4: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Page 5: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

The Golden Rules

Page 6: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

The Golden Rules(my made up golden rules)

Page 7: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

1. Keep it simple

Page 8: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

2. Know the risks

Page 9: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

3. Fail securely

Page 10: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

4. Don’t reinvent the wheel

Page 11: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

5. Never trust anything

Page 12: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

OWASP& the OWASP Top 10

https://www.owasp.org/

Page 13: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Application Security(mainly PHP applications)

Page 14: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Always remember…

Filter InputEscape Output

Page 15: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

© 2003 Disney/Pixar. All Rights Reserved.

SQL Injection (#1)

Page 16: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

SQL Injection (#1)

http://xkcd.com/327/

Page 17: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

SQL Injection (#1)

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

Page 18: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

SQL Injection (#1)<?php

// user_id=1; DROP TABLE users; --

$user_id = $_GET['user_id'];

$sql = "

SELECT * FROM users

WHERE user_id = {$user_id}";

$db->execute($sql); ✘

Page 19: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

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 20: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

© 2003 Disney/Pixar. All Rights Reserved.

Page 23: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Cross-Site Scripting / XSS (#3)© 2003 Disney/Pixar. All Rights Reserved.

Page 24: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

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 25: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Cross-Site Request Forgery / CSRF (#8)

http://www.factzoo.com/invertebrates/cuttlefish-chameleon-of-the-sea.html

Page 26: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

<?php

if (!$isPost) {

$csrfToken = base64_encode(random_bytes(32)));

$_SESSION['csrf_token'] = $csrfToken;

// ... output the form ...

echo '<input type="hidden" name="csrf_token" value="'.$csrfToken.'" />';

} else if ($isPost) {

if (hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {

die("Token invalid...");

}

// ... handle the form ...

}

Cross-Site Request Forgery / CSRF (#8)

Page 27: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

<?php

if (!$isPost) {

$csrfToken = base64_encode(random_bytes(32)));

$_SESSION['csrf_token'] = $csrfToken;

// ... output the form ...

echo '<input type="hidden" name="csrf_token" value="'.$csrfToken.'" />';

} else if ($isPost) {

if (hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {

die("Token invalid...");

}

// ... handle the form ...

}

Cross-Site Request Forgery / CSRF (#8)

Page 28: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Errors, Exceptions & Logging (#6)

© 2003 Disney/Pixar. All Rights Reserved.

Page 29: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Errors, Exceptions & Logging (#6)

Page 30: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

© 2003 Disney/Pixar. All Rights Reserved.

Page 31: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

curl + https<?php

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Page 32: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

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 33: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

© 2003 Disney/Pixar. All Rights Reserved.

Page 34: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

WordPress PluginsAudit third party plugins carefully.

Page 35: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

WordPress PluginsAudit third party plugins carefully.

ANY THIRD PARTY CODE

Page 36: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

WordPress PluginsAudit third party plugins carefully.

ANY THIRD PARTY CODE

github.com/ /SecurityAdvisories

Page 37: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Page 38: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

We are not allsecurity experts!

Page 39: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

We are not allsecurity experts!

… but we CAN write secure code

Page 40: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Be the threat

Think Differently

Page 41: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

What do you want?

Think Differently

Page 42: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

How do you get it?

Think Differently

Page 43: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat ModellingD.R.E.A.D.

© Buena Vista Pictures

Page 44: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat Modelling

DamageREAD

© Buena Vista Pictures

Page 45: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat Modelling

DamageReproducibilityEAD

© Buena Vista Pictures

Page 46: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat Modelling

DamageReproducibilityExploitabilityAD

© Buena Vista Pictures

Page 47: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat Modelling

DamageReproducibilityExploitabilityAffected usersD

© Buena Vista Pictures

Page 48: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Threat Modelling

DamageReproducibilityExploitabilityAffected usersDiscoverability

© Buena Vista Pictures

Page 49: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Authentication& Authorization

Page 50: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

AuthenticationVerifying Identity

Page 51: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Case Study: Custom Authentication

We thought about doing this…

Page 52: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Case Study: Custom Authentication

We thought about doing this…

Page 53: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Case Study: Custom Authentication

We thought about doing this…

Page 54: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Password Hashingpassword_hash()

Page 55: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

AuthorizationVerifying Access

Page 56: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

CRYPTOGRAPHYIS

HARD

Page 57: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Page 58: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

CRYPTOGRAPHYIS

HARDNEVER EVER “ROLL YOUR OWN”

Page 59: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

CRYPTOGRAPHYIS

HARDNEVER EVER “ROLL YOUR OWN”

EVER!!!

Page 60: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

How to encrypt then?

Page 61: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

I’ve got some great ideas for encryption...

Image: The Guardian (http://goo.gl/pUkyvO)

Page 62: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

How to encrypt then?libsodium PECL package

Page 63: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Linux Server Security

Page 64: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Create an SSH Fortress

Page 65: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Firewalls

Page 66: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

iptables#!/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

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

# Outbound traffic

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

$IPT -A OUTPUT -p tcp --dport 443 -j ACCEPT

$IPT -A OUTPUT -p udp --dport 53 -m state --state NEW -j ACCEPT

Page 67: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

ufwsudo ufw enable

sudo ufw allow 22

sudo ufw allow 80

Page 68: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Mitigate Brute Force Attacks

Page 69: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Install OnlyWhat You Need

Page 70: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

© 2003 Disney/Pixar. All Rights Reserved.

Page 71: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

+

Page 72: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Case Study: Be Minimal

Internets

Postfix

Squid Proxy(badly configured)

hacker

spam

Page 74: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

The Golden Rules

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

Page 75: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

If you follow all this, you get...

Page 76: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

If you follow all this, you get...

Page 77: Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)

Any questions? :)

https://joind.in/talk/0ad74James Titcumb @asgrim