PHP: Security issues FdSc Module 109 Server side scripting and

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
-Ajay Babu.D y5cs022.. Contents Who is hacker? History of hacking Types of hacking Do You Know? What do hackers do? - Some Examples on Web application.
9/9/2005 Developing "Secure" Web Applications 1 Methods & Concepts for Developing “Secure” Web Applications Peter Y. Hammond, Developer Wasatch Front Regional.
Introduction The concept of “SQL Injection”
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.
SQL Injection Attacks Prof. Jim Whitehead CMPS 183: Spring 2006 May 17, 2006.
PHP Security.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
PHP : Hypertext Preprocessor
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
© 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.
Sofia, Bulgaria | 9-10 October Writing Secure Code for ASP.NET Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte CTO,
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
School of Computing and Information Systems CS 371 Web Application Programming Security Avoiding and Preventing Attacks.
PHP Workshop ‹#› PHP Security. PHP Workshop ‹#› Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less obvious.. $_SERVER 2.ESCAPE.
PHP2010/11 : [‹#›] PHP Security. PHP2010/11 : [‹#›] Two Golden Rules 1.FILTER external input Obvious.. $_POST, $_COOKIE, etc. Less obvious.. $_SERVER.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
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.
If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 9 1COMP9321, 15s2, Week.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
Preventing MySQL Injection Sonja Parson COSC 5010 Security Presentation April 26, 2005.
Secure Authentication. SQL Injection Many web developers are unaware of how SQL queries can be tampered with SQL queries are able to circumvent access.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
Example – SQL Injection MySQL & PHP code: // The next instruction prompts the user is to supply an ID $personID = getIDstringFromUser(); $sqlQuery = "SELECT.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
 Encryption provides confidentiality  Information is unreadable to anyone without knowledge of the key  Hashing provides integrity  Verify the integrity.
10 Tips for Building a Secure PHP Application. Tip 1: Use Proper Error Reporting/Handling  The development process of the application can become very.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
SQL Injection Attacks.
COMP9321 Web Application Engineering Semester 2, 2017
Database System Implementation CSE 507
Group 18: Chris Hood Brett Poche
Module: Software Engineering of Web Applications
CHAPTER 5 SERVER SIDE SCRIPTING
Web Application Vulnerabilities, Detection Mechanisms, and Defenses
Introduction to Dynamic Web Programming
World Wide Web policy.
SQL and SQL*Plus Interaction
CS 371 Web Application Programming
Example – SQL Injection
SQL Injection Attacks Many web servers have backing databases
Pengantar Keamanan Informasi
Web Programming Language
Computer Security Fundamentals
BASIC PHP and MYSQL Edward S. Flores.
Working with Forms and Regular Expressions
Transparent Data Encryption (TDE)
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 13 Security Methods Part 3.
Lecture 2 - SQL Injection
Web Programming Language
CS5123 Software Validation and Quality Assurance
Tutorial 10: Programming with javascript
Exercise: Hashing, Password security, And File Integrity
PHP Forms and Databases.
Web Programming Language
Presentation transcript:

PHP: Security issues FdSc Module 109 Server side scripting and Database design 2011

Rule Number One Never, Ever, Trust Your Users Assume: Every single piece of data your site collects from a user contains malicious code Always That includes data you think you have checked with client-side validation, for example using JavaScript because JavaScript can be disabled The data may not be coming from your form

SQL injection Using a security hole to run SQL queries on a database Typical login query: $check = mysql_query("SELECT Username, Password FROM Users WHERE Username = '".$_POST['username']."' and Password = '".$_POST['password']."'"); If the entered user name is ' OR 1=1 # then a different query is run

SQL injection SELECT Username, Password FROM Users WHERE Username = '' OR 1=1 #' and Password = '‘ This will return all the user names and passwords from the database! The first entry is likely to be the admin user!! How does this work? The # symbol tells MySQL that what follows is a comment, so it won’t execute As 1 always = 1 it evaluates as TRUE, so the OR part is TRUE and the statement effectively becomes: SELECT Username, Password FROM Users

SQL injection - solution mysql_real_escape_string This  escapes special characters in a string Escaping in PHP means that you place a \ in front of a character so that it is interpreted as text and not a command It will escape: \x00 \n \r \ ‘ “ \x1a

SQL injection - solution function make_safe($variable) { $variable = mysql_real_escape_string(trim($variable)); return $variable; } This will escape command characters and remove excess space characters $username = make_safe($_POST['username']); Makes the unsafe '' OR 1=1 #' become '\' OR 1=1 #' and very few people have a username of \' OR 1=1 #

Password protection Passwords must not be stored in clear text They must be protected by hashing Hash algorithms turn any amount of data into a fixed- length checksum that cannot be reversed hash("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1 fa7425e73043362938b9824 hash("hbllo") = 58756879c05c68dfac9866712fad6a93f8146f337 a69afe7dd238f3364946366 if the input changes by even a tiny bit, the resulting hash is COMPLETELY different

Password protection The password is hashed and stored in the database. At no point is the unhashed user's password ever stored When the user attempts to login, the hash of the password they entered is checked against the hash in the database. If the hashes match, the user is granted access. If not, the user is told they entered an incorrect password.

Password protection Hashed passwords are not secure Dictionary or brute force Try words or combinations until a match is found Lookup table Store each word:hash pair from a dictionary and then compare each hash Rainbow table A combination of lookup table and brute force

Password protection Top 10 passwords in 180,00 (hacked file) 123456 12345678 qwerty abc123 12345 monkey 111111 consumer letmein

Password protection Use a “salt” to add additional protection A salt is a string of random characters that gets appended to the password before hashing If the password is 123456, the hash is known If the password is 123456Uwqe2uXdSKpAA the hash is not known

Password protection The way it works is: Generate a long random salt Hash the password joined to the salt Save the hash and the salt in the database. To validate a password: Get the hash and salt for that user from the database. Compute the hash of the password they tried to login with joined with the salt Compare the two If they are EXACTLY the same, then the password is valid. If there is any difference, then the password is invalid.

Password protection Practical example using SHA (no salt) SHA() creates a one way encrypted string of exactly 40 characters Define the password field in a user table as CHAR(40) Assume a users table with fields for userID, firstname, lastname and password

Password protection Syntax: INSERT INTO users (firstname, lastname, password) VALUES ('Arthur', 'Smith', SHA('password'); SELECT userID FROM users WHERE firstname = 'Arthur' AND password = SHA('password');

Password protection To use a salt investigate: AES_ENCRYPT ('password', 'salt') AES-DECRYPT ('password', 'salt')

Input validation Firstly: Check that something has been entered Secondly Make sure it is the right type

Input validation Use the empty() function to check for a blank entry if(empty($_POST['userName'])==FALSE ) { $userName = $_POST['userName']; } else echo 'Username is not set'; exit();

Numeric validation Rating is required to be number between 1 and 5 is_numeric checks it is a number <?php if(isset($_POST[“lRating"])) { $number = $_POST[“Rating"]; if((is_numeric($number)) && ($number > 0) && ($number < 6)) echo "Selected rating: " . $number; // Write the rating to the database here } else echo "The rating has to be a number between 1 and 5!"; ?>

Cross site scripting The most basic form of cross site scripting (XSS) is to put some JavaScript in user- submitted content (eg a blog post or comment field) <SCRIPT>Dosomethingmalicious</SCRIPT> Steal the data in a user's cookie or session Use cookie and session data to impersonate the user

Cross site scripting The htmlentities function: <?php htmlentities($variable, ENT_QUOTES); ?> will convert HTML special characters into HTML literals So <STRONG>Bold Text</STRONG> normally displays as Bold Text But after using htmlentities displays as <STRONG>Bold Text</STRONG> which cannot be executed

Summary Security measures are: Counteract SQL injection Protect passwords Validate input Counteract cross site scripting Marks available in assignment 2 Validation techniques used (5%) Security methods implemented (5%)