SlideShare a Scribd company logo
@asgrim
Dip Your Toes in the
(Caribbean) Sea of Security
James Titcumb
CoderCruise 2017
$ whoami
James Titcumb
www.jamestitcumb.com
www.roave.com
@asgrim
@asgrim
@asgrim
Some simple PHP 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)
@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)
<?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
Dependencies Disappearing
@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()
(basically, bcrypt with proper salt)
@asgrim
Two Factor Authentication
@asgrim
@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: IBTimes (http://goo.gl/zPVeo0)
@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
iptables
https://twitter.com/sadserver/status/615988393198026752
@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
● https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04
● https://www.kali.org/
@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...
Any questions?
James Titcumb
@asgrim

More Related Content

PDF
Get Started with RabbitMQ (CoderCruise 2017)
PDF
Dip Your Toes in the Sea of Security
PDF
Dip Your Toes in the Sea of Security (IPC Fall 2017)
PDF
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
PDF
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
PDF
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
PDF
PHP Secure Programming
PDF
Diving into HHVM Extensions (php[tek] 2016)
Get Started with RabbitMQ (CoderCruise 2017)
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
Dip Your Toes in the Sea of Security (PHP MiNDS January Meetup 2016)
PHP Secure Programming
Diving into HHVM Extensions (php[tek] 2016)

What's hot (20)

PDF
Questioning the status quo
KEY
FizzBuzzではじめるテスト
PDF
Php web backdoor obfuscation
PDF
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
PDF
Two scoops of Django - Security Best Practices
PDF
Top 10 php classic traps php serbia
PDF
Code obfuscation, php shells & more
PDF
PHP Backdoor: The rise of the vuln
PDF
Practical django secuirty
PDF
Top 10 php classic traps confoo
PPTX
Mitigating CSRF with two lines of codes
PPTX
Django Web Application Security
PDF
HTTP For the Good or the Bad - FSEC Edition
PPTX
Angular js security
PDF
Making JavaScript Libraries More Approachable
PPT
Dealing with Legacy Perl Code - Peter Scott
PDF
Trading with opensource tools, two years later
PDF
Head in the Clouds: Testing Infra as Code - Config Management 2020
ODP
My app is secure... I think
PDF
Barely Legal Xxx Perl Presentation
Questioning the status quo
FizzBuzzではじめるテスト
Php web backdoor obfuscation
Dip Your Toes in the Sea of Security (PHP Berkshire Nov 2015)
Two scoops of Django - Security Best Practices
Top 10 php classic traps php serbia
Code obfuscation, php shells & more
PHP Backdoor: The rise of the vuln
Practical django secuirty
Top 10 php classic traps confoo
Mitigating CSRF with two lines of codes
Django Web Application Security
HTTP For the Good or the Bad - FSEC Edition
Angular js security
Making JavaScript Libraries More Approachable
Dealing with Legacy Perl Code - Peter Scott
Trading with opensource tools, two years later
Head in the Clouds: Testing Infra as Code - Config Management 2020
My app is secure... I think
Barely Legal Xxx Perl Presentation
Ad

Similar to Dip Your Toes in the Sea of Security (CoderCruise 2017) (20)

PDF
Dip Your Toes In The Sea Of Security (PHPNW16)
PDF
Dip Your Toes in the Sea of Security (phpDay 2016)
PDF
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
PDF
Dip Your Toes in the Sea of Security (PHP UK 2016)
PDF
Dip Your Toes in the Sea of Security (DPC 2015)
PDF
Dip Your Toes in the Sea of Security (PHP Cambridge)
PDF
Proposed PHP function: is_literal()
PDF
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Symfony & Javascript. Combining the best of two worlds
PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications (PHP Benelux 2018)
PDF
Bringing modern PHP development to IBM i (ZendCon 2016)
PDF
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
PDF
Crafting Quality PHP Applications (ConFoo YVR 2017)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
TXT
R57.Php
ODP
Concern of Web Application Security
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes in the Sea of Security (phpDay 2016)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (PHP UK 2016)
Dip Your Toes in the Sea of Security (DPC 2015)
Dip Your Toes in the Sea of Security (PHP Cambridge)
Proposed PHP function: is_literal()
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Symfony & Javascript. Combining the best of two worlds
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications (PHP Benelux 2018)
Bringing modern PHP development to IBM i (ZendCon 2016)
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
Crafting Quality PHP Applications (ConFoo YVR 2017)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
R57.Php
Concern of Web Application Security
Ad

More from James Titcumb (18)

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
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
Kicking off with Zend Expressive and Doctrine ORM (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
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
PDF
Climbing the Abstract Syntax Tree (Forum PHP 2017)
PDF
Climbing the Abstract Syntax Tree (IPC Fall 2017)
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)
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)
Kicking off with Zend Expressive and Doctrine ORM (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)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
A Presentation on Artificial Intelligence
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx

Dip Your Toes in the Sea of Security (CoderCruise 2017)