SlideShare a Scribd company logo
@asgrim
Dip Your Toes
in the Sea of Security
James Titcumb
phpDay 2016
@asgrim
James Titcumb
www.jamestitcumb.com
www.roave.com
www.phphants.co.uk
www.phpsouthcoast.co.uk
Who is this guy?
@asgrim
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);
@asgrim
@asgrim
The Golden Rules
@asgrim
The Golden Rules
(my made up golden rules)
@asgrim
1. Keep it simple
@asgrim
2. Know the risks
@asgrim
3. Fail securely
@asgrim
4. Don’t reinvent the wheel
@asgrim
5. Never trust anything
@asgrim
OWASP
& the OWASP Top 10
https://www.owasp.org/
@asgrim
Application Security
(mainly PHP applications)
@asgrim
Always remember…
Filter Input
Escape Output
@asgrim
© 2003 Disney/Pixar. All Rights Reserved.
SQL Injection (#1)
@asgrim
SQL Injection (#1)
http://xkcd.com/327/
@asgrim
SQL Injection (#1)
1. Use PDO / mysqli
2. Use prepared / parameterized statements
@asgrim
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);
✘
@asgrim
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();
✓
@asgrim
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
exec($_GET)
https://github.com/search?q=exec%28%24_GET&ref=cmdform&type=Code
@asgrim
eval()
https://github.com/search?q=eval%28%24_GET&type=Code&ref=searchresults
@asgrim
Cross-Site Scripting / XSS (#3)
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
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');
@asgrim
Cross-Site Request
Forgery / CSRF (#8)
http://www.factzoo.com/invertebrates/cuttlefish-chameleon-of-the-sea.html
@asgrim
Cross-Site Request Forgery / CSRF (#8)
<?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 ...
}
@asgrim
<?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)
@asgrim
Cross-Site Request Forgery / CSRF (#8)
<?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 ...
}
@asgrim
Timing attacks
From zend_is_identical:
return (Z_STR_P(op1) == Z_STR_P(op2) ||
(Z_STRLEN_P(op1) == Z_STRLEN_P(op2) &&
memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0));
@asgrim
Timing attacks
Actual string: “foobar”
● a (0.00001)
● aa (0.00001)
● aaa (0.00001)
● aaaa (0.00001)
● aaaaa (0.00001)
● aaaaaa (0.00002) ← success!
● aaaaaaa (0.00001)
● aaaaaaaa (0.00001)
● aaaaaaaaa (0.00001)
@asgrim
Timing attacks
1 int memcmp(const void* s1, const void* s2,size_t n)
2 {
3 const unsigned char *p1 = s1, *p2 = s2;
4 while(n--)
5 if( *p1 != *p2 )
6 return *p1 - *p2;
7 else
8 p1++,p2++;
9 return 0;
10 }
http://clc-wiki.net/wiki/C_standard_library:string.h:memcmp#Implementation
@asgrim
Timing attacks
Actual string: “foobar”
● “aaaaaa” (0.00001)
● “baaaaa” (0.00001)
● …
● “faaaaa” (0.00002) ← success!
● “fbaaaa” (0.00002)
● “fcaaaa” (0.00002)
● …
● “foaaaa” (0.00003) ← success!
@asgrim
Sensitive Data Exposure (#6)
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
Sensitive Data Exposure (#6)
@asgrim
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
curl + https
<?php
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
✘
@asgrim
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");
✓
@asgrim
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
Third Party Code
@asgrim
Third Party Code
!!! WARNING !!!
@asgrim
Third Party Code
github.com/ /SecurityAdvisories
!!! WARNING !!!
@asgrim
@asgrim
We are not all
security experts!
@asgrim
We are not all
security experts!
… but we CAN write secure code
@asgrim
Hack your own system!
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
What do you want?
Think like a hacker
@asgrim
How do you get it?
Think Differently
@asgrim
Threat Modelling
D.R.E.A.D.
© Buena Vista Pictures
@asgrim
Threat Modelling
Damage
R
E
A
D
© Buena Vista Pictures
@asgrim
Threat Modelling
Damage
Reproducibility
E
A
D
© Buena Vista Pictures
@asgrim
Threat Modelling
Damage
Reproducibility
Exploitability
A
D
© Buena Vista Pictures
@asgrim
Threat Modelling
Damage
Reproducibility
Exploitability
Affected users
D
© Buena Vista Pictures
@asgrim
Threat Modelling
Damage
Reproducibility
Exploitability
Affected users
Discoverability
© Buena Vista Pictures
@asgrim
Rank them in order
And fix them!
© Buena Vista Pictures
@asgrim
Authentication
& Authorization
@asgrim
Authentication
Verifying Identity
@asgrim
Case Study: Custom Authentication
We thought about doing this…
@asgrim
Case Study: Custom Authentication
We thought about doing this…
@asgrim
Case Study: Custom Authentication
We thought about doing this…
✘
@asgrim
Password Hashing
password_hash()
@asgrim
Authorization
Verifying Access
@asgrim
CRYPTOGRAPHY
IS
HARD
@asgrim
@asgrim
CRYPTOGRAPHY
IS
HARD
NEVER EVER “ROLL YOUR OWN”
@asgrim
CRYPTOGRAPHY
IS
HARD
NEVER EVER “ROLL YOUR OWN”
EVER!!!
@asgrim
How to encrypt then?
@asgrim
I’ve got some
great ideas for
encryption...
Image: The Guardian (http://goo.gl/pUkyvO)
@asgrim
How to encrypt then?
libsodium PECL package
@asgrim
Linux Server Security
@asgrim
Create an SSH Fortress
@asgrim
Firewalls
@asgrim
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
@asgrim
ufw
sudo ufw enable
sudo ufw allow 22
sudo ufw allow 80
@asgrim
Mitigate Brute Force
Attacks
@asgrim
Install Only
What You Need
@asgrim
© 2003 Disney/Pixar. All Rights Reserved.
@asgrim
+
@asgrim
Case Study: Be Minimal
Internets
Postfix
Squid Proxy
(badly configured)
hacker
spam
@asgrim
Resources
● http://securingphp.com/
● https://www.owasp.org/
● http://blog.ircmaxell.com/
● https://github.com/paragonie/random_compat
● https://github.com/ircmaxell/password_compat
● https://paragonie.com/blog
● https://websec.io/resources.php
@asgrim
The Golden Rules
1. Keep it simple
2. Know the risks
3. Fail securely
4. Don’t reinvent the wheel
5. Never trust anything / anyone
@asgrim
If you follow all this, you get...
@asgrim
If you follow all this, you get...
@asgrim
Any questions? :)
https://joind.in/talk/fc2dc
James Titcumb

More Related Content

PDF
Dip Your Toes in the Sea of Security (IPC Fall 2017)
PDF
Dip Your Toes in the Sea of Security
PDF
Dip Your Toes In The Sea Of Security (PHPNW16)
PDF
Dip Your Toes in the Sea of Security (PHP UK 2016)
PDF
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
PDF
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
PDF
Play, Slick, play2-authの間で討死
PDF
WordPress Security: Be a Superhero - WordCamp Raleigh - May 2011
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes in the Sea of Security (PHP UK 2016)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
Play, Slick, play2-authの間で討死
WordPress Security: Be a Superhero - WordCamp Raleigh - May 2011

What's hot (8)

PDF
PHP Secure Programming
TXT
TXT
R57.Php
PDF
GCRC 2014 - The Dark Side of Ruby
PDF
Itsecteam shell
PDF
Speeding up Red Team engagements with carnivorall
PPTX
Coding Horrors
PDF
Fund performance-management
PHP Secure Programming
R57.Php
GCRC 2014 - The Dark Side of Ruby
Itsecteam shell
Speeding up Red Team engagements with carnivorall
Coding Horrors
Fund performance-management
Ad

Viewers also liked (20)

PDF
Diving into HHVM Extensions (php[tek] 2016)
PDF
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
PDF
Adding 1.21 Gigawatts to Applications with RabbitMQ (DPC 2015)
PDF
Mastering PHP Data Structure 102 - phpDay 2012 Verona
ODP
PHP applications/environments monitoring: APM & Pinba
PPTX
Company Profile of zhongshan xinxin display products.,ltd
PDF
Chang7
PDF
Portfolio_Done
DOC
Vermell4
PPTX
webTender 2.0
PPTX
Web Site and Rich Internet Applications
DOC
Taronja7
PDF
Php unit the-mostunknownparts
PPTX
Thesis Defense Final
PPT
Testing multithreaded java applications for synchronization problems
PPT
Regular Expressions 2007
PDF
PHP 7 – What changed internally?
DOCX
Seven strategies to teach students text comprehension
PDF
1 chemistry analytical_methods
KEY
Andrei's Regex Clinic
Diving into HHVM Extensions (php[tek] 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Adding 1.21 Gigawatts to Applications with RabbitMQ (DPC 2015)
Mastering PHP Data Structure 102 - phpDay 2012 Verona
PHP applications/environments monitoring: APM & Pinba
Company Profile of zhongshan xinxin display products.,ltd
Chang7
Portfolio_Done
Vermell4
webTender 2.0
Web Site and Rich Internet Applications
Taronja7
Php unit the-mostunknownparts
Thesis Defense Final
Testing multithreaded java applications for synchronization problems
Regular Expressions 2007
PHP 7 – What changed internally?
Seven strategies to teach students text comprehension
1 chemistry analytical_methods
Andrei's Regex Clinic
Ad

Similar to Dip Your Toes in the Sea of Security (phpDay 2016) (20)

PDF
Dip Your Toes in the Sea of Security (CoderCruise 2017)
PDF
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
PDF
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
PDF
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
PDF
Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)
PDF
Evolution Of Web Security
PPTX
Don't Pick the lock
PDF
Dip Your Toes in the Sea of Security (DPC 2015)
PDF
Security 202 - Are you sure your site is secure?
PPT
PHPUG Presentation
PDF
Security in PHP Applications: An absolute must!
ODP
2009 Barcamp Nashville Web Security 101
PPTX
Open source security
PDF
Dip Your Toes in the Sea of Security (PHP Cambridge)
PDF
Security Vulnerabilities: How to Defend Against Them
PDF
Security Theatre - AmsterdamPHP
PDF
Security Theatre - Confoo
PDF
Stop expecting magic fairy dust: Make apps secure by design
ODP
My app is secure... I think
PDF
Cryptography For The Average Developer - Sunshine PHP
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
Dip Your Toes in the Sea of Security (PHP Dorset, 2nd June 2014)
Evolution Of Web Security
Don't Pick the lock
Dip Your Toes in the Sea of Security (DPC 2015)
Security 202 - Are you sure your site is secure?
PHPUG Presentation
Security in PHP Applications: An absolute must!
2009 Barcamp Nashville Web Security 101
Open source security
Dip Your Toes in the Sea of Security (PHP Cambridge)
Security Vulnerabilities: How to Defend Against Them
Security Theatre - AmsterdamPHP
Security Theatre - Confoo
Stop expecting magic fairy dust: Make apps secure by design
My app is secure... I think
Cryptography For The Average Developer - Sunshine PHP

More from James Titcumb (20)

PDF
Living the Best Life on a Legacy Project (phpday 2022).pdf
PDF
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
PDF
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
PDF
Climbing the Abstract Syntax Tree (PHP Russia 2019)
PDF
Best practices for crafting high quality PHP apps - PHP UK 2019
PDF
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
PDF
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
PDF
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
PDF
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
Living the Best Life on a Legacy Project (phpday 2022).pdf
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
Best practices for crafting high quality PHP apps (php[world] 2019)
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Best practices for crafting high quality PHP apps - PHP UK 2019
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Crafting Quality PHP Applications (PHPkonf 2018)
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation

Dip Your Toes in the Sea of Security (phpDay 2016)