SlideShare a Scribd company logo
Security in PHP
Session Fixation
• Session Fixation. This is where an attacker
explicitly sets the session identifier of a
session for a user. Typically in PHP it's done by
giving them a url like
http://www.example.com/index...?session_na
me=sessionid . Once the attacker gives the url
to the client, the attack is the same as
a session hijacking attack.
Session Fixation
• This is where an attacker explicitly sets the
session identifier of a session for a user.
Typically in PHP it's done by giving them a url
like http://www.example.com/index...?session
_name=sessionid. Once the attacker gives the
url to the client, the attack is the same as a
session hijacking attack.
• There are a few ways to prevent session fixation (do
all of them):
• Set session.use_trans_sid = 0 in your php.ini file.
This will tell PHP not to include the identifier in the
URL, and not to read the URL for identifiers.
• Set session.use_only_cookies = 1 in your php.ini file.
This will tell PHP to never use URLs with session
identifiers.
• Regenerate the session ID anytime the session's
status changes. That means any of the following:
– User authentication
– Storing sensitive info in the session
– Changing anything about the session
– etc...
• Preventing fixation
• URL-based session handling appends the session URL to
every request. This method is not preferred because it
creates unattractive URLs with an appended GET
parameter and it makes URL-based caching more tricky.
Furthermore this is a potential way of starting session
fixation by distributing such links to unsuspecting users
with a preset valid session ID.
• http://mydomain.com/some/page?PHP_SESSID=xxx
• Cookie based session handling creates a cookie with the
session ID. This method is preferred because it does not
append anything to the URL while the cookie provides
good options for controlling the client-side session
lifetime. You can make the cookie expire when the user
closes the browser window, or define any time-based
cookie lifetime you prefer. Remember to delete redundant
session data on the server as well, if applicable.
Session Hijacking
• This is where an attacker gets a hold of a
session identifier and is able to send requests
as if they were that user. That means that
since the attacker has the identifier, they are
all but indistinguishable from the valid user
with respect to the server.
Session Hijacking
• Preventing hijacking
• You can do two things to effectively fight hijacking attempts.
Change the session ID on every request so an attacker cannot
continue with an exposed session ID even if the attacker knows
the current session identifier’s value.
• // Change the session ID on every request
session_regenerate_id();
• The second defense is adding some security checks to your
session handler to make sure the client is the same that started
the session. It is suggested that you check the client’s browser
and IP address. Notice that whatever information you use in
such checks can potentially be spoofed by the attacker, thus
providing only a limited help for security. Furthermore beware
that IP addresses for sessions can change for valid reasons,
which should be considered in the check.
Session Hijacking
• Additional checking could be done by adding
another cookie with a value that changes on every
request. Thereby not only the session ID has to be
valid together with the browser and IP coming
from the same device, but another secret value
also has to be presented by the client in order to
be trusted as the correct session owner.
Session Hijacking
• You cannot directly prevent session hijacking. You can
however put steps in to make it very difficult and harder to
use.
• Use a strong session hash
identifier: session.hash_function in php.ini. If PHP < 5.3, set
it to session.hash_function = 1 for SHA1. If PHP >= 5.3, set it
to session.hash_function =
sha256 or session.hash_function = sha512.
• Send a strong
hash: session.hash_bits_per_character in php.ini. Set this
to session.hash_bits_per_character = 5. While this doesn't
make it any harder to crack, it does make a difference when
the attacker tries to guess the session identifier. The ID will
be shorter, but uses more characters.
Session Hijacking
• Set an additional entropy
with session.entropy_file and session.entropy_length in your php.ini file. Set
the former to session.entropy_file = /dev/urandom and the latter to the
number of bytes that will be read from the entropy file, for
example session.entropy_length = 256.
• Change the name of the session from the default PHPSESSID. This is
accomplished by calling session_name() with your own identifier name as the
first parameter prior to calling session_start.
• If you're really paranoid you could rotate the session name too, but beware
that all sessions will automatically be invalidated if you change this (for
example, if you make it dependent on the time). But depending on your use-
case, it may be an option...
• Rotate your session identifier often. I wouldn't do this every request (unless
you really need that level of security), but at a random interval. You want to
change this often since if an attacker does hijack a session you don't want them
to be able to use it for too long.
• Include the user agent from $_SERVER['HTTP_USER_AGENT'] in the session.
Basically, when the session starts, store it in something
like $_SESSION['user_agent']. Then, on each subsequent request check that it
matches. Note that this can be faked so it's not 100% reliable, but it's better
than not.
Form Spoofing in php
• As a php developer you create lots of form in
your application.
• But how do you track that the form submitted is
submitted from your website?
• This is how you spoof a form submission:
Lets assume we have following code located at
http://www.yourdomain.com/form.php
Form Spoofing in php
<form action="submit.php" method="post">
<select name="myvar">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit">
</form>
• From the above code we notice that the value
of $_POST[‘myvar’] is either 1 or 2.
• Now if some one saves this form from their browser
in their desktop, they can change action attribute to
the full URL of the from .They can even replace select
tag to textbox with the name ‘myvar’.
Form Spoofing in php
• Now the modified form will be like this
<form action="http://yourdomain.com/submit.php"
method="post">
<input type=”text” name=”myvar”
value=”333333”>
<input type="submit">
</form>
• Now this person can submit anything as the value
of $_POST['myvar'].
Form Spoofing in php
• The solution for this is to have a Shared secret . You
can create a Secret key everytime the form loads
and keep that key in a session. When you are
submitting it you can also pass the session key as
hidden variable. At the receiving end you can check
if the hidden secret variable is same as the session
variable .
$secret = md5(uniqid(rand(), true));
$_SESSION['secret'] = $secret;
<input type="hidden" name="secret" value="<?php
echo $_SESSION[‘secret’];?>">
PHP Filters
• Validating data = Determine if the data is in
proper form.
• Sanitizing data = Remove any illegal character
from the data.
• PHP filters are used to validate and sanitize
external input.
• The PHP filter extension has many of the
functions needed for checking user input, and is
designed to make data validation easier and
quicker.
• The filter_list() function can be used to list what
the PHP filter extension offers:
PHP Filters
• <table>
<tr>
<td>Filter Name</td>
<td>Filter ID</td>
</tr>
<?php
foreach (filter_list() as $id =>$filter) {
echo '<tr><td>' . $filter . '</td><td>' .
filter_id($filter) . '</td></tr>';
}
?>
</table>
Why Use Filters?
• Many web applications receive external input.
External input/data can be:
• User input from a form
• Web services data
• Server variables
• Anything from $_GET, $_POST, $_REQUEST
• Cookies ($_COOKIES)
• Files
• Some server variables (e.g.
$_SERVER[‘SERVER_NAME’])
• Environment variables
• Database query results
Why Use Filters?
• You should always validate external data!
Invalid submitted data can lead to security
problems and break your webpage!
By using PHP filters you can be sure your
application gets the correct input!
PHP filter_var() Function
• The filter_var() function both validate and
sanitize data.
• The filter_var() function filters a single variable
with a specified filter. It takes two pieces of
data:
• The variable you want to check
• The type of check to use
Sanitize a String
• <?php
$str = "<h1>Hello World!</h1>";
$newstr=filter_var($str,FILTER_SANITIZE_STRING);
echo $newstr;
?>
Validate an Integer
• The following example uses the filter_var() function to
check if the variable $int is an integer.
• If $int is an integer, the output of the code above will be:
"Integer is valid". If $int is not an integer, the output will
be: "Integer is not valid":
<?php
$int = 100;
if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
echo("Integer is valid");
} else {
echo("Integer is not valid");
}
?>
Validate an Integer
• <?php
$int = 0;
if (filter_var($int, FILTER_VALIDATE_INT) === 0 ||
!filter_var($int, FILTER_VALIDATE_INT) === false) {
echo("Integer is valid");
} else {
echo("Integer is not valid");
}
?>
Validate an IP Address
<?php
$ip = "127.0.0.1";
if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
echo("$ip is a valid IP address");
} else {
echo("$ip is not a valid IP address");
}
?>

More Related Content

What's hot

Apache Web Server Setup 3
Apache Web Server Setup 3Apache Web Server Setup 3
Apache Web Server Setup 3
Information Technology
 
Apache Web server Complete Guide
Apache Web server Complete GuideApache Web server Complete Guide
Apache Web server Complete Guidewebhostingguy
 
Web application security
Web application securityWeb application security
Web application security
Ravi Raj
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
Jagat Kothari
 
Configuring the Apache Web Server
Configuring the Apache Web ServerConfiguring the Apache Web Server
Configuring the Apache Web Serverwebhostingguy
 
Apache web server
Apache web serverApache web server
Apache web server
Rishabh Bahukhandi
 
Whats new in ASP.NET 4.0
Whats new in ASP.NET 4.0Whats new in ASP.NET 4.0
Whats new in ASP.NET 4.0
py_sunil
 
Apache Web Server Setup 2
Apache Web Server Setup 2Apache Web Server Setup 2
Apache Web Server Setup 2
Information Technology
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarniwebhostingguy
 
Apache server configuration & optimization
Apache server configuration & optimizationApache server configuration & optimization
Apache server configuration & optimizationGokul Muralidharan
 
Apache error
Apache errorApache error
Apache error
Rishabh Bahukhandi
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
Mandakini Kumari
 
Running the Apache Web Server
Running the Apache Web ServerRunning the Apache Web Server
Running the Apache Web Serverwebhostingguy
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
danwrong
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & sessionJamshid Hashimi
 
PHP And Web Services: Perfect Partners
PHP And Web Services: Perfect PartnersPHP And Web Services: Perfect Partners
PHP And Web Services: Perfect Partners
Lorna Mitchell
 
Linux System Administration - Web Server and squid setup
Linux System Administration - Web Server and squid setupLinux System Administration - Web Server and squid setup
Linux System Administration - Web Server and squid setup
Sreenatha Reddy K R
 

What's hot (19)

Apache Web Server Setup 3
Apache Web Server Setup 3Apache Web Server Setup 3
Apache Web Server Setup 3
 
Apache Web server Complete Guide
Apache Web server Complete GuideApache Web server Complete Guide
Apache Web server Complete Guide
 
Web application security
Web application securityWeb application security
Web application security
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
 
Css
CssCss
Css
 
Configuring the Apache Web Server
Configuring the Apache Web ServerConfiguring the Apache Web Server
Configuring the Apache Web Server
 
Apache web server
Apache web serverApache web server
Apache web server
 
Whats new in ASP.NET 4.0
Whats new in ASP.NET 4.0Whats new in ASP.NET 4.0
Whats new in ASP.NET 4.0
 
Apache Web Server Setup 2
Apache Web Server Setup 2Apache Web Server Setup 2
Apache Web Server Setup 2
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarni
 
Apache server configuration & optimization
Apache server configuration & optimizationApache server configuration & optimization
Apache server configuration & optimization
 
Apache error
Apache errorApache error
Apache error
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
Running the Apache Web Server
Running the Apache Web ServerRunning the Apache Web Server
Running the Apache Web Server
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & session
 
Php intro
Php introPhp intro
Php intro
 
PHP And Web Services: Perfect Partners
PHP And Web Services: Perfect PartnersPHP And Web Services: Perfect Partners
PHP And Web Services: Perfect Partners
 
Linux System Administration - Web Server and squid setup
Linux System Administration - Web Server and squid setupLinux System Administration - Web Server and squid setup
Linux System Administration - Web Server and squid setup
 

Viewers also liked

Introducing WPFand XAML
Introducing WPFand XAMLIntroducing WPFand XAML
Introducing WPFand XAML
Mindfire Solutions
 
PHP Security
PHP SecurityPHP Security
PHP Security
Mindfire Solutions
 
Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1jimbojsb
 
GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7jimbojsb
 
Austin NoSQL 2011-07-06
Austin NoSQL 2011-07-06Austin NoSQL 2011-07-06
Austin NoSQL 2011-07-06jimbojsb
 
Fall 2011 PHP Class - Session 2
Fall 2011 PHP Class - Session 2Fall 2011 PHP Class - Session 2
Fall 2011 PHP Class - Session 2jimbojsb
 
Geek Austin PHP Class - Session 2
Geek Austin PHP Class - Session 2Geek Austin PHP Class - Session 2
Geek Austin PHP Class - Session 2jimbojsb
 
GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6jimbojsb
 
Geek Austin PHP Class - Session 1
Geek Austin PHP Class - Session 1Geek Austin PHP Class - Session 1
Geek Austin PHP Class - Session 1jimbojsb
 
Geek Austin PHP Class - Session 3
Geek Austin PHP Class - Session 3Geek Austin PHP Class - Session 3
Geek Austin PHP Class - Session 3jimbojsb
 
Geek Austin PHP Class - Session 4
Geek Austin PHP Class - Session 4Geek Austin PHP Class - Session 4
Geek Austin PHP Class - Session 4jimbojsb
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionsUdaAs PaNchi
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
Firdaus Adib
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
jimbojsb
 
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
Lviv Startup Club
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
Programmer Blog
 
PHP Security
PHP SecurityPHP Security
PHP Security
manugoel2003
 
Session php
Session phpSession php
Session php
200Hussain
 
Utah PHP Users Group - 2012
Utah PHP Users Group - 2012Utah PHP Users Group - 2012
Utah PHP Users Group - 2012
Randy Secrist
 

Viewers also liked (20)

Introducing WPFand XAML
Introducing WPFand XAMLIntroducing WPFand XAML
Introducing WPFand XAML
 
PHP Security
PHP SecurityPHP Security
PHP Security
 
Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1
 
GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7
 
Austin NoSQL 2011-07-06
Austin NoSQL 2011-07-06Austin NoSQL 2011-07-06
Austin NoSQL 2011-07-06
 
Fall 2011 PHP Class - Session 2
Fall 2011 PHP Class - Session 2Fall 2011 PHP Class - Session 2
Fall 2011 PHP Class - Session 2
 
Geek Austin PHP Class - Session 2
Geek Austin PHP Class - Session 2Geek Austin PHP Class - Session 2
Geek Austin PHP Class - Session 2
 
GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6
 
Geek Austin PHP Class - Session 1
Geek Austin PHP Class - Session 1Geek Austin PHP Class - Session 1
Geek Austin PHP Class - Session 1
 
Geek Austin PHP Class - Session 3
Geek Austin PHP Class - Session 3Geek Austin PHP Class - Session 3
Geek Austin PHP Class - Session 3
 
Php Security
Php SecurityPhp Security
Php Security
 
Geek Austin PHP Class - Session 4
Geek Austin PHP Class - Session 4Geek Austin PHP Class - Session 4
Geek Austin PHP Class - Session 4
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
Андрій Ждань “Фрілансер наважився взяти проект під ключ, що його чекає”
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
PHP Security
PHP SecurityPHP Security
PHP Security
 
Session php
Session phpSession php
Session php
 
Utah PHP Users Group - 2012
Utah PHP Users Group - 2012Utah PHP Users Group - 2012
Utah PHP Users Group - 2012
 

Similar to Security in php

Ruby on Rails Security Guide
Ruby on Rails Security GuideRuby on Rails Security Guide
Ruby on Rails Security Guide
ihji
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
kunjan shah
 
Penetration Testing Report
Penetration Testing ReportPenetration Testing Report
Penetration Testing Report
Aman Srivastava
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
Mudasir Syed
 
Evolution Of Web Security
Evolution Of Web SecurityEvolution Of Web Security
Evolution Of Web Security
Chris Shiflett
 
S8-Session Managment
S8-Session ManagmentS8-Session Managment
S8-Session Managment
zakieh alizadeh
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
sana mateen
 
Php web app security (eng)
Php web app security (eng)Php web app security (eng)
Php web app security (eng)
Anatoliy Okhotnikov
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
SreejithVP7
 
Secure programming with php
Secure programming with phpSecure programming with php
Secure programming with php
Mohmad Feroz
 
Web Exploitation Security
Web Exploitation SecurityWeb Exploitation Security
Web Exploitation Security
Aman Singh
 
Why Browser Debugger is a Developer's Best Friend
Why Browser Debugger is a Developer's Best FriendWhy Browser Debugger is a Developer's Best Friend
Why Browser Debugger is a Developer's Best Friend
Odoo
 
LAMP security practices
LAMP security practicesLAMP security practices
LAMP security practicesAmit Kejriwal
 
Rest API Security
Rest API SecurityRest API Security
Rest API Security
Stormpath
 
Logical Attacks(Vulnerability Research)
Logical Attacks(Vulnerability Research)Logical Attacks(Vulnerability Research)
Logical Attacks(Vulnerability Research)
Ajay Negi
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptx
ShitalGhotekar
 
Top 10 Web Application vulnerabilities
Top 10 Web Application vulnerabilitiesTop 10 Web Application vulnerabilities
Top 10 Web Application vulnerabilities
Terrance Medina
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 

Similar to Security in php (20)

Ruby on Rails Security Guide
Ruby on Rails Security GuideRuby on Rails Security Guide
Ruby on Rails Security Guide
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
Penetration Testing Report
Penetration Testing ReportPenetration Testing Report
Penetration Testing Report
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
Rails Security
Rails SecurityRails Security
Rails Security
 
Evolution Of Web Security
Evolution Of Web SecurityEvolution Of Web Security
Evolution Of Web Security
 
S8-Session Managment
S8-Session ManagmentS8-Session Managment
S8-Session Managment
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
 
Php web app security (eng)
Php web app security (eng)Php web app security (eng)
Php web app security (eng)
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
Secure programming with php
Secure programming with phpSecure programming with php
Secure programming with php
 
Web Exploitation Security
Web Exploitation SecurityWeb Exploitation Security
Web Exploitation Security
 
Why Browser Debugger is a Developer's Best Friend
Why Browser Debugger is a Developer's Best FriendWhy Browser Debugger is a Developer's Best Friend
Why Browser Debugger is a Developer's Best Friend
 
LAMP security practices
LAMP security practicesLAMP security practices
LAMP security practices
 
Rest API Security
Rest API SecurityRest API Security
Rest API Security
 
Logical Attacks(Vulnerability Research)
Logical Attacks(Vulnerability Research)Logical Attacks(Vulnerability Research)
Logical Attacks(Vulnerability Research)
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptx
 
Top 10 Web Application vulnerabilities
Top 10 Web Application vulnerabilitiesTop 10 Web Application vulnerabilities
Top 10 Web Application vulnerabilities
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 

More from Jalpesh Vasa

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
Jalpesh Vasa
 
5. HTML5
5. HTML55. HTML5
5. HTML5
Jalpesh Vasa
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
Jalpesh Vasa
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex example
Jalpesh Vasa
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
Jalpesh Vasa
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
Jalpesh Vasa
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
Jalpesh Vasa
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
Jalpesh Vasa
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android development
Jalpesh Vasa
 

More from Jalpesh Vasa (14)

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
5. HTML5
5. HTML55. HTML5
5. HTML5
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex example
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android development
 

Recently uploaded

DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 

Recently uploaded (20)

DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 

Security in php

  • 2. Session Fixation • Session Fixation. This is where an attacker explicitly sets the session identifier of a session for a user. Typically in PHP it's done by giving them a url like http://www.example.com/index...?session_na me=sessionid . Once the attacker gives the url to the client, the attack is the same as a session hijacking attack.
  • 3. Session Fixation • This is where an attacker explicitly sets the session identifier of a session for a user. Typically in PHP it's done by giving them a url like http://www.example.com/index...?session _name=sessionid. Once the attacker gives the url to the client, the attack is the same as a session hijacking attack.
  • 4. • There are a few ways to prevent session fixation (do all of them): • Set session.use_trans_sid = 0 in your php.ini file. This will tell PHP not to include the identifier in the URL, and not to read the URL for identifiers. • Set session.use_only_cookies = 1 in your php.ini file. This will tell PHP to never use URLs with session identifiers. • Regenerate the session ID anytime the session's status changes. That means any of the following: – User authentication – Storing sensitive info in the session – Changing anything about the session – etc...
  • 5. • Preventing fixation • URL-based session handling appends the session URL to every request. This method is not preferred because it creates unattractive URLs with an appended GET parameter and it makes URL-based caching more tricky. Furthermore this is a potential way of starting session fixation by distributing such links to unsuspecting users with a preset valid session ID. • http://mydomain.com/some/page?PHP_SESSID=xxx • Cookie based session handling creates a cookie with the session ID. This method is preferred because it does not append anything to the URL while the cookie provides good options for controlling the client-side session lifetime. You can make the cookie expire when the user closes the browser window, or define any time-based cookie lifetime you prefer. Remember to delete redundant session data on the server as well, if applicable.
  • 6.
  • 7. Session Hijacking • This is where an attacker gets a hold of a session identifier and is able to send requests as if they were that user. That means that since the attacker has the identifier, they are all but indistinguishable from the valid user with respect to the server.
  • 8. Session Hijacking • Preventing hijacking • You can do two things to effectively fight hijacking attempts. Change the session ID on every request so an attacker cannot continue with an exposed session ID even if the attacker knows the current session identifier’s value. • // Change the session ID on every request session_regenerate_id(); • The second defense is adding some security checks to your session handler to make sure the client is the same that started the session. It is suggested that you check the client’s browser and IP address. Notice that whatever information you use in such checks can potentially be spoofed by the attacker, thus providing only a limited help for security. Furthermore beware that IP addresses for sessions can change for valid reasons, which should be considered in the check.
  • 9. Session Hijacking • Additional checking could be done by adding another cookie with a value that changes on every request. Thereby not only the session ID has to be valid together with the browser and IP coming from the same device, but another secret value also has to be presented by the client in order to be trusted as the correct session owner.
  • 10. Session Hijacking • You cannot directly prevent session hijacking. You can however put steps in to make it very difficult and harder to use. • Use a strong session hash identifier: session.hash_function in php.ini. If PHP < 5.3, set it to session.hash_function = 1 for SHA1. If PHP >= 5.3, set it to session.hash_function = sha256 or session.hash_function = sha512. • Send a strong hash: session.hash_bits_per_character in php.ini. Set this to session.hash_bits_per_character = 5. While this doesn't make it any harder to crack, it does make a difference when the attacker tries to guess the session identifier. The ID will be shorter, but uses more characters.
  • 11. Session Hijacking • Set an additional entropy with session.entropy_file and session.entropy_length in your php.ini file. Set the former to session.entropy_file = /dev/urandom and the latter to the number of bytes that will be read from the entropy file, for example session.entropy_length = 256. • Change the name of the session from the default PHPSESSID. This is accomplished by calling session_name() with your own identifier name as the first parameter prior to calling session_start. • If you're really paranoid you could rotate the session name too, but beware that all sessions will automatically be invalidated if you change this (for example, if you make it dependent on the time). But depending on your use- case, it may be an option... • Rotate your session identifier often. I wouldn't do this every request (unless you really need that level of security), but at a random interval. You want to change this often since if an attacker does hijack a session you don't want them to be able to use it for too long. • Include the user agent from $_SERVER['HTTP_USER_AGENT'] in the session. Basically, when the session starts, store it in something like $_SESSION['user_agent']. Then, on each subsequent request check that it matches. Note that this can be faked so it's not 100% reliable, but it's better than not.
  • 12. Form Spoofing in php • As a php developer you create lots of form in your application. • But how do you track that the form submitted is submitted from your website? • This is how you spoof a form submission: Lets assume we have following code located at http://www.yourdomain.com/form.php
  • 13. Form Spoofing in php <form action="submit.php" method="post"> <select name="myvar"> <option value="1">1</option> <option value="2">2</option> </select> <input type="submit"> </form> • From the above code we notice that the value of $_POST[‘myvar’] is either 1 or 2. • Now if some one saves this form from their browser in their desktop, they can change action attribute to the full URL of the from .They can even replace select tag to textbox with the name ‘myvar’.
  • 14. Form Spoofing in php • Now the modified form will be like this <form action="http://yourdomain.com/submit.php" method="post"> <input type=”text” name=”myvar” value=”333333”> <input type="submit"> </form> • Now this person can submit anything as the value of $_POST['myvar'].
  • 15. Form Spoofing in php • The solution for this is to have a Shared secret . You can create a Secret key everytime the form loads and keep that key in a session. When you are submitting it you can also pass the session key as hidden variable. At the receiving end you can check if the hidden secret variable is same as the session variable . $secret = md5(uniqid(rand(), true)); $_SESSION['secret'] = $secret; <input type="hidden" name="secret" value="<?php echo $_SESSION[‘secret’];?>">
  • 16. PHP Filters • Validating data = Determine if the data is in proper form. • Sanitizing data = Remove any illegal character from the data. • PHP filters are used to validate and sanitize external input. • The PHP filter extension has many of the functions needed for checking user input, and is designed to make data validation easier and quicker. • The filter_list() function can be used to list what the PHP filter extension offers:
  • 17. PHP Filters • <table> <tr> <td>Filter Name</td> <td>Filter ID</td> </tr> <?php foreach (filter_list() as $id =>$filter) { echo '<tr><td>' . $filter . '</td><td>' . filter_id($filter) . '</td></tr>'; } ?> </table>
  • 18. Why Use Filters? • Many web applications receive external input. External input/data can be: • User input from a form • Web services data • Server variables • Anything from $_GET, $_POST, $_REQUEST • Cookies ($_COOKIES) • Files • Some server variables (e.g. $_SERVER[‘SERVER_NAME’]) • Environment variables • Database query results
  • 19. Why Use Filters? • You should always validate external data! Invalid submitted data can lead to security problems and break your webpage! By using PHP filters you can be sure your application gets the correct input!
  • 20. PHP filter_var() Function • The filter_var() function both validate and sanitize data. • The filter_var() function filters a single variable with a specified filter. It takes two pieces of data: • The variable you want to check • The type of check to use
  • 21. Sanitize a String • <?php $str = "<h1>Hello World!</h1>"; $newstr=filter_var($str,FILTER_SANITIZE_STRING); echo $newstr; ?>
  • 22. Validate an Integer • The following example uses the filter_var() function to check if the variable $int is an integer. • If $int is an integer, the output of the code above will be: "Integer is valid". If $int is not an integer, the output will be: "Integer is not valid": <?php $int = 100; if (!filter_var($int, FILTER_VALIDATE_INT) === false) { echo("Integer is valid"); } else { echo("Integer is not valid"); } ?>
  • 23. Validate an Integer • <?php $int = 0; if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) { echo("Integer is valid"); } else { echo("Integer is not valid"); } ?>
  • 24. Validate an IP Address <?php $ip = "127.0.0.1"; if (!filter_var($ip, FILTER_VALIDATE_IP) === false) { echo("$ip is a valid IP address"); } else { echo("$ip is not a valid IP address"); } ?>