10 Tips for Building a Secure PHP Application. Tip 1: Use Proper Error Reporting/Handling  The development process of the application can become very.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Nick Feamster CS 6262 Spring 2009
Web Security Never, ever, trust user inputs Supankar.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Understand Database Security Concepts
By Brian Vees.  SQL Injection  Username Enumeration  Cross Site Scripting (XSS)  Remote Code Execution  String Formatting Vulnerabilities.
It’s always better live. MSDN Events Security Best Practices Part 2 of 2 Reducing Vulnerabilities using Visual Studio 2008.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
Information Networking Security and Assurance Lab National Chung Cheng University The Ten Most Critical Web Application Security Vulnerabilities Ryan J.W.
CROSS SITE SCRIPTING..! (XSS). Overview What is XSS? Types of XSS Real world Example Impact of XSS How to protect against XSS?
Handling Security Threats in Kentico CMS Karol Jarkovsky Sr. Solution Architect Kentico Software
Chapter 6: Hostile Code Guide to Computer Network Security.
Web Application Attacks ECE 4112 Fall 2007 Group 9 Zafeer Khan & Simmon Yau.
Web-based Document Management System By Group 3 Xinyi Dong Matthew Downs Joshua Ferguson Sriram Gopinath Sayan Kole.
Introduction to InfoSec – Recitation 10 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
PHP Security.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Cosc 4765 Server side Web security. Web security issues From Cenzic Vulnerability report
+ Websites Vulnerabilities. + Content Expand of The Internet Use of the Internet Examples Importance of the Internet How to find Security Vulnerabilities.
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Web Application Access to Databases. Logistics Test 2: May 1 st (24 hours) Extra office hours: Friday 2:30 – 4:00 pm Tuesday May 5 th – you can review.
INTERNET APPLICATION DEVELOPMENT For More visit:
1-Vulnerabilities 2-Hackers 3-Categories of attacks 4-What a malicious hacker do? 5-Security mechanisms 6-HTTP Web Servers 7-Web applications attacks.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
© All rights reserved. Zend Technologies, Inc. PHP Security Kevin Schroeder Zend Technologies.
Lecture 16 Page 1 CS 236 Online SQL Injection Attacks Many web servers have backing databases –Much of their information stored in a database Web pages.
PHP meets MySQL.
CGI Security COEN 351. CGI Security Security holes are exploited by user input. We need to check user input against Buffer overflows etc. that cause a.
Feedback #2 (under assignments) Lecture Code:
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
PHP Workshop ‹#› PHP Security. PHP Workshop ‹#› Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less obvious.. $_SERVER 2.ESCAPE.
OWASP Top Ten #1 Unvalidated Input. Agenda What is the OWASP Top 10? Where can I find it? What is Unvalidated Input? What environments are effected? How.
PHP2010/11 : [‹#›] PHP Security. PHP2010/11 : [‹#›] Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less obvious.. $_SERVER.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
SQL INJECTIONS Presented By: Eloy Viteri. What is SQL Injection An SQL injection attack is executed when a web page allows users to enter text into a.
SecurityPHPApril 2010 : [‹#›] PHP Security. SecurityPHPApril 2010 : [‹#›] Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less.
Web Applications Testing By Jamie Rougvie Supported by.
By Sean Rose and Erik Hazzard.  SQL Injection is a technique that exploits security weaknesses of the database layer of an application in order to gain.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Form Handling IDIA 618 Fall 2014 Bridget M. Blodgett.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
PHP Error Handling Section :I Source: 1.
Secure Authentication. SQL Injection Many web developers are unaware of how SQL queries can be tampered with SQL queries are able to circumvent access.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Writing secure Flex applications  MXML tags with security restrictions  Disabling viewSourceURL  Remove sensitive information from SWF files  Input.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
Group 18: Chris Hood Brett Poche
Building Secure ColdFusion Applications
CHAPTER 5 SERVER SIDE SCRIPTING
Web Application Vulnerabilities, Detection Mechanisms, and Defenses
Chapter 7: Identifying Advanced Attacks
SQL Injection Attacks Many web servers have backing databases
PHP: Security issues FdSc Module 109 Server side scripting and
CSC 495/583 Topics of Software Security Intro to Web Security
Web Programming Language
PHP Forms and Databases.
Unit 32 Every class minute counts! 2 assignments 3 tasks/assignment
Cross Site Request Forgery (CSRF)
Presentation transcript:

10 Tips for Building a Secure PHP Application

Tip 1: Use Proper Error Reporting/Handling  The development process of the application can become very cumbersome when the errors are not handled properly. In other words if there are no error reports enabled then identifying the minor mistakes like spell checks, incorrect functions usage and many more mistakes can become very difficult. It is a great practice to enable error reporting before even starting the development process. Once the website goes live, just hide error reporting from displaying.  Set Below Code in PHP.ini file Log_errors = On Display_errors = Off  Set Below Code in Configuration file define('DEBUG',true); if(DEBUG ==true) { ini_set('display_errors','On'); error_reporting(E_ALL); } else { ini_set('display_errors','Off'); error_reporting(0); }

Tip 2: Validate Input  The inputs that are coming from the users needs to be validated from server side as well as client side. The inputs come in the form of POST or GET. Always use regular expressions in validation to avoid blank entries in the database.  Check the ‘type’ of the data  Check range of numbers  Check length of strings  Check s, urls, dates to be valid  Ensure that data does not contain un allowed characters. For Example,if Month value is not valid if ( ! preg match( "/^[0-9]{1,2}$/", $_GET['month'] ) ) { echo “”; // handle error }

Tip 3: Protecting Against Sql Injection  To perform your database queries, one should be using PHP Data Objects(PDO). With parameterized queries and prepared statements (Store Procedure), you can prevent SQL injection.  Take a look at the following example: <?php $sql = "SELECT * FROM users WHERE name=:name and age=:age"; $stmt = $db->prepare($sql); $stmt->execute(array(":name" => $name, ":age" => $age)); ?>  The code given above has two parameters named :name and :age. Prepare() is the method which informs the database engine to pre-compile the query and attach the values to the named parameters later. When execute() is called, the query is executed with the actual values of the named parameters. By coding this way, the attacker on the SQL wont be able to inject a malicious query because the queries are already precompiled and the database will not accept it. Hence a secure database can be achieved.  Mysql real escape string :- The mysql real escape string() function escapes special characters in a string for use in an SQL statement

Tip 4: Disable PHP’s Bad Features  Global Variables (Register Globals)  Using the PHP feature ‘Register Globals’ can hamper the objective of maintaining programming safety. As soon as this feature is activated in the PHP configuration file, even an uninitialized variable can lead to a damaging security flaw and the height is almost anyone can seize administrative control. To deal with this situation, disable Register Globals, ensure that you initialize variables as well as use localized variables too within the program.  If the application is running with register globals ON, a user could just place access=1 into a query string, and would then have access to whatever the script is running.  Unfortunately, we cannot disable register globals from the script side (using ini set, like we normally might), but we can use an.htaccess files to do this.  Set Below Code in.htaccessfile for disabling php flagregister globals 0  Set Below Code in php.ini file (if you have access for the same) for disabling register_globals = Off

Tip 5: Protect Against XSS Attacks  Cross Site Scripting has to be protected in order to protect a very simple attack on the website. PHP Application which allows the user inputs may come across a situation where the user placed a malicious script as per the example below into your application.  Here is an example of what an XSS attacker might submit to an application: window.location.href='  What the script means is, it will hijack every user who visits that output page and send them to an unwanted page. This type of attack can be eliminated by using proper techniques to validate user input data and not allowing specific types of data.  Few functions to filter/validate data : htmlentities(),strip_tags (), utf8_decode (), htmlspecialchars(), ctype_digit(), ctype_alnum(), stripslashes(), str_replace()

Tip 6: Avoid Short tags  <? and <?= are called short open tags, and are not always enabled.  PHP 5.3.0, they are disabled by default, however if they are enabled Set Below Code in PHP.ini file short_open_tag = Off  Your Application will not work if they are not enabled. Tip 7: Protect Against CSRF Attacks  CSRF stands for Cross Site Request Forgery. The attacker is the remote machine which is trying to access the cookies or some other means of a normal legitimate user. For example when the user is trying to comment on the website, the login information is primarily stored in the cookies and there is every possibility that the cookies can be accessed by remote server who is a malicious user. This is why it is imperative to use filters when requesting for random information.  Lets say a certain url in the application performs some database changes, update_info.php?id=123 delete_record.php?id=123

Tip 8: Securing the session  A hacker can setup a webpage with the following piece of code   Ask the user to open this webpage. Now since the user is logged into the application the url will be triggered and whatever action necessary would be taken by the script.So basically a hacker has made the request through the user. This is “request forgery”.  Solution is to, enable the server to identify each request with a key/random value.  Regenerate Session ID ( function:— session_regenerate_id(); ) Lock the user agent during a session  //Function to check if user is logged in or not functioncheck_login_status() { if($_SESSION['logged'] == true and$_SESSION['old_user_agent'] == $_SERVER['HTTP_USER_AGENT'])

{returntrue;} returnfalse; } if(!check_login_status()) { logout(); }  Lock the IP of a session $_SERVER['HTTP_ACCEPT_CHARSET']. $_SERVER['HTTP_ACCEPT_ENCODING']. $_SERVER['HTTP_ACCEPT_LANGUAGE']. $_SERVER['HTTP_USER_AGENT']. $_SERVER['REMOTE_ADDR']);  Store sessions in database  By default sessions are stored in files. Many applications are hosted on shared hosting environments where the session files are saved to /tmp directory. This directory may be readable to other users as well. If unencrypted the session information will be plain text in the file : userName|s:5:”ngood”;accountNumber|s:9:” ″;  Store sessions in database. Sessions stored inside database are not visible like files. They are only available to the application using it.

Tip 9: Setup correct directory permissions  Directories should have proper permissions with regard to the need of being writable or not. Keep a separate directory for temp files, cache files and other resource files and mark them writable as needed. Also directories (like temp) which can contain resource files, or files with other information should be guarded well and be totally inaccessible to the outside web.  Use htaccess to block all access to such directories( deny from all ) Tip 10: Password Security $salt = 'SUPER_SALTY'; $hash = md5($password. $salt); Original Source URL : building-a-secure-php-application/ building-a-secure-php-application/

Thank You Techtic Solutions PHP Development Company India Mail. USA: UK: AUS: