2013 - Mark story - Avoiding the Owasp

60
AVOIDING THE OWASP Top 10 security exploits Saturday, 5 October, 13

description

PHP Conference Argentina 2013

Transcript of 2013 - Mark story - Avoiding the Owasp

Page 1: 2013 - Mark story - Avoiding the Owasp

AVOIDING THE OWASP Top 10 security exploits

Saturday, 5 October, 13

Page 2: 2013 - Mark story - Avoiding the Owasp

ME

Illustrator turned developer

PHP developer for 8 years

Architect/Developer at FreshBooks

Lead developer of CakePHP

Saturday, 5 October, 13

Page 3: 2013 - Mark story - Avoiding the Owasp

SECURITY

Saturday, 5 October, 13

Page 4: 2013 - Mark story - Avoiding the Owasp

SECURITY CONTINUUM

( )unusable unrestricted

Saturday, 5 October, 13

Page 5: 2013 - Mark story - Avoiding the Owasp

OWASPOpen Web Application Security Project

Saturday, 5 October, 13

Page 6: 2013 - Mark story - Avoiding the Owasp

OWASP TOP 10

Saturday, 5 October, 13

Page 7: 2013 - Mark story - Avoiding the Owasp

INJECTION‘ OR 1=1 ‘--1

Saturday, 5 October, 13

Page 8: 2013 - Mark story - Avoiding the Owasp

RISKS

Command - Permits arbitrary shell commands.

SQL - Permits query manipulation, and arbitrary SQL.

Bad guys can run arbitrary code/queries.

Saturday, 5 October, 13

Page 9: 2013 - Mark story - Avoiding the Owasp

$username = $_POST[‘username’];$password = $_POST[‘password’];

$query = “SELECT * FROM userWHERE username = ‘$username’AND password = ‘$password’”;

$user = $db->query($query);

SQL INJECTION EXAMPLE

Saturday, 5 October, 13

Page 10: 2013 - Mark story - Avoiding the Owasp

$username = “root”;$password = “‘ OR 1 = 1 --”;

USER INPUT

Saturday, 5 October, 13

Page 11: 2013 - Mark story - Avoiding the Owasp

FINAL QUERY

$query = “SELECT * FROM userWHERE username = ‘root’AND password = ‘‘ OR 1 = 1 --”;

Saturday, 5 October, 13

Page 12: 2013 - Mark story - Avoiding the Owasp

FINAL QUERY

$query = “SELECT * FROM userWHERE username = ‘root’AND password = ‘‘ OR 1 = 1 --”;

Saturday, 5 October, 13

Page 13: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Use an ORM or Database abstraction layer that provides escaping. Doctrine, Zend\Table, and CakePHP all do this.

Use PDO and prepared statements.

Never interpolate user data into a query.

Never use regular expressions, magic quotes, or addslashes()

Saturday, 5 October, 13

Page 14: 2013 - Mark story - Avoiding the Owasp

EXAMPLE (PDO)

$query = “SELECT * FROM userWHERE username = ?AND password = ?”;

$stmt = $db->prepare($query);$stmt->bindValue($username);$stmt->bindValue($password);$result = $db->execute();

Saturday, 5 October, 13

Page 15: 2013 - Mark story - Avoiding the Owasp

COMMAND INJECTION

$file = $_POST[‘file’];

$res = file_get_contents($file);

echo $res;

Saturday, 5 October, 13

Page 16: 2013 - Mark story - Avoiding the Owasp

$f = “../../../../../../etc/passwd”;

USER INPUT

Saturday, 5 October, 13

Page 17: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Escape and validate input.

Check for ..

Check for ;

Ensure the realpath resolves to a file that is allowed.

Saturday, 5 October, 13

Page 18: 2013 - Mark story - Avoiding the Owasp

2BROKEN AUTHENTICATION & SESSION MANAGEMENT

/index.php?PHPSESSID=pwned

Saturday, 5 October, 13

Page 19: 2013 - Mark story - Avoiding the Owasp

RISKS

Identity theft.

Firesheep was an excellent example.

Saturday, 5 October, 13

Page 20: 2013 - Mark story - Avoiding the Owasp

SESSION FIXATION EXAMPLE

<?phpsession_start();if (isset($_GET[‘sessionid’]) {session_id($_GET[‘sessionid’]);

}

Saturday, 5 October, 13

Page 21: 2013 - Mark story - Avoiding the Owasp

SESSION FIXATION EXAMPLE

<?phpsession_start();if (isset($_GET[‘sessionid’]) {session_id($_GET[‘sessionid’]);

}

Saturday, 5 October, 13

Page 22: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Rotate session identifiers upon login/logout

Set the HttpOnly flag on session cookies.

Use well tested / mature libraries for authentication.

SSL is always a good idea.

Saturday, 5 October, 13

Page 23: 2013 - Mark story - Avoiding the Owasp

3 XSS<script>alert(‘cross site scripting’);</script>

Saturday, 5 October, 13

Page 24: 2013 - Mark story - Avoiding the Owasp

RISKS

Allows bad guys to do things as the person viewing a page.

Steal identities, passwords, credit cards, hijack pages and more.

Saturday, 5 October, 13

Page 25: 2013 - Mark story - Avoiding the Owasp

XSS EXAMPLE

<p><?php echo $user[‘bio’]; ?>

</p>

Saturday, 5 October, 13

Page 26: 2013 - Mark story - Avoiding the Owasp

XSS EXAMPLE

<p><?php echo $user[‘bio’]; ?>

</p>

Saturday, 5 October, 13

Page 27: 2013 - Mark story - Avoiding the Owasp

I know, I can use regular expressions!

Saturday, 5 October, 13

Page 28: 2013 - Mark story - Avoiding the Owasp

NOSaturday, 5 October, 13

Page 29: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Regular expressions and strip_tags leave you vulnerable.

The only robust solution is output encoding.

Saturday, 5 October, 13

Page 30: 2013 - Mark story - Avoiding the Owasp

EXAMPLE

<p><?php echo htmlentities($user[‘bio’],ENT_QUOTES,‘UTF-8’

); ?></p>

Saturday, 5 October, 13

Page 31: 2013 - Mark story - Avoiding the Owasp

DANGERS

Manually encoding is error prone, and you will make a mistake.

Using a template library like Twig that provides auto-escaping reduces the chances of screwing up.

Encoding is dependent on context.

Saturday, 5 October, 13

Page 32: 2013 - Mark story - Avoiding the Owasp

4INSECURE DIRECT OBJECT REFERENCE

Saturday, 5 October, 13

Page 33: 2013 - Mark story - Avoiding the Owasp

RISKS

Bad guys can access information they shouldn’t

Bad guys can modify data they shouldn’t.

Saturday, 5 October, 13

Page 34: 2013 - Mark story - Avoiding the Owasp

BROKEN PASSWORD UPDATE

<form action=”/user/update” method=”post”><input type=”hidden” name=”userid” value=”4654” /><input type=”text” name=”new_password” /><button type=”submit”>Save</button>

</form>

Saturday, 5 October, 13

Page 35: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Remember hidden inputs are not really hidden, and can be changed by users.

Validate access to all things, don’t depend on things being hidden/invisible.

If you need to refer to the current user, use session data not form inputs.

Whitelist properties any form can update.

Saturday, 5 October, 13

Page 36: 2013 - Mark story - Avoiding the Owasp

5SECURITY MISCONFIGURATION

Saturday, 5 October, 13

Page 37: 2013 - Mark story - Avoiding the Owasp

RISKS

Default settings can be insecure, and intended for development not production.

Attackers can use misconfigured software to gain knowledge and access.

Saturday, 5 October, 13

Page 38: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Know the tools you use, and configure them correctly.

Keep up to date on vulnerabilities in the tools you use.

Remove/disable any services/features you aren’t using.

Saturday, 5 October, 13

Page 39: 2013 - Mark story - Avoiding the Owasp

6SENSITIVE DATA EXPOSURE4012 8888 8888 1881

Saturday, 5 October, 13

Page 40: 2013 - Mark story - Avoiding the Owasp

RISKS

Bad guys get credit cards, personal identification, passwords or health records.

Your company could be fined or worse.

Saturday, 5 October, 13

Page 41: 2013 - Mark story - Avoiding the Owasp

ASSESSING RISK

Do you have sensitive data?

Is it in plaintext?

Any old/bad crypto in use?

Missing SSL?

Who can access sensitive data?

Saturday, 5 October, 13

Page 42: 2013 - Mark story - Avoiding the Owasp

7MISSING FUNCTION LEVELACCESS CONTROL

Saturday, 5 October, 13

Page 43: 2013 - Mark story - Avoiding the Owasp

RISKS

Anyone on the internet can request things.

Missing access control could mean bad guys can do things they shouldn’t be able to.

Saturday, 5 October, 13

Page 44: 2013 - Mark story - Avoiding the Owasp

PREVENTION

No simple solutions sadly.

Good automated tests help.

Saturday, 5 October, 13

Page 45: 2013 - Mark story - Avoiding the Owasp

8CROSS SITE REQUEST FORGERY

(CSRF)

Saturday, 5 October, 13

Page 46: 2013 - Mark story - Avoiding the Owasp

RISKS

Evil websites can perform actions for users logged into your site.

Side effects on GET can be performed via images or CSS files.

Remember the Gmail contact hack.

Saturday, 5 October, 13

Page 47: 2013 - Mark story - Avoiding the Owasp

CSRF EXAMPLE

Your app

Evil site

Saturday, 5 October, 13

Page 48: 2013 - Mark story - Avoiding the Owasp

CSRF EXAMPLE

Your app

Evil site

Login

Saturday, 5 October, 13

Page 49: 2013 - Mark story - Avoiding the Owasp

CSRF EXAMPLE

Your app

Evil site

Login

Accidentally visit

Saturday, 5 October, 13

Page 50: 2013 - Mark story - Avoiding the Owasp

CSRF EXAMPLE

Your app

Evil site

Login

Accidentally visit

Submit form for evil

Saturday, 5 October, 13

Page 51: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Add opaque expiring tokens to all forms.

Requests missing tokens or containing invalid tokens should be rejected.

Saturday, 5 October, 13

Page 52: 2013 - Mark story - Avoiding the Owasp

SAMPLE CSRF VALIDATION

<?phpif (!$this->validCsrfToken($data, ‘csrf’)) {throw new ForbiddenException();

}

Saturday, 5 October, 13

Page 53: 2013 - Mark story - Avoiding the Owasp

9USING COMPONENTS WITH KNOWN VULNERABILITIES

CVE bingo

Saturday, 5 October, 13

Page 54: 2013 - Mark story - Avoiding the Owasp

RISK

Using old busted software can expose you to documented issues.

CVE databases are filled with version numbers and matching exploits.

Saturday, 5 October, 13

Page 55: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Do routine upgrades. Keep up to date with all your software.

Read mailing lists and keep an eye out for security releases.

Saturday, 5 October, 13

Page 56: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Several vulnerability databases around.

https://cve.mitre.org/cve/

Saturday, 5 October, 13

Page 57: 2013 - Mark story - Avoiding the Owasp

10UNVALIDATED REDIRECTS & FORWARDS

Saturday, 5 October, 13

Page 58: 2013 - Mark story - Avoiding the Owasp

RISKS

Trusting user input for redirects opens phishing attacks.

Breach of trust with your users.

Saturday, 5 October, 13

Page 59: 2013 - Mark story - Avoiding the Owasp

PREVENTION

Don’t trust user data when handling redirects.

Saturday, 5 October, 13

Page 60: 2013 - Mark story - Avoiding the Owasp

THANK YOU

Saturday, 5 October, 13