440 likes | 623 Views
Introduction To Web Application Security in PHP. Security is Big And Often Difficult. PHP doesn’t make it any easier. What we’ll cover. What do we mean by security? Application Security Code Configuration OWASP OWASP Top Ten SQL Injection XSS Configuration. Application Security.
E N D
Security is Big And Often Difficult PHP doesn’t make it any easier
What we’ll cover • What do we mean by security? • Application Security • Code • Configuration • OWASP • OWASP Top Ten • SQL Injection • XSS • Configuration
Application Security Security in the SDLC as opposed to network security or data security or physical security
For our purposes we’ll just stick to this: Security in Code and in Deployment
OWASP An authority in Web Application Security
Open Web Application Security Project • Really, many projects are “under” OWASP • OWASP Top Ten • ESAPI • Development Guide • Cheat Sheets • Do not bring to exam • Testing Guide • More
Based on the statistics of a number of scanning tools OWASP Top Ten – Top Web Application Security Issues
OWASP Top 10-2013 – A1 Injection SQL Injection is the variant of this that we’ll cover here
SQL Injection Confusing the DBMS between logic (written by the developer) and data (provided by the user)
A common query: • $query = "SELECT * FROM user WHERE username = '" . $_POST["username"] . "' AND password = '" . $_POST["password"] . "';";
The intention • $query = "SELECT * FROM user WHERE username = 'sue' AND password = 'secret';";
The vulnerability: What if $_POST[“username”] is actually SQL Code
Let’s try this: ' OR 1 = 1 #
An SQL Injection $query = "SELECT * FROM user WHERE username = '' OR 1 = 1 #' AND password = '';”;
How to protect our code? Use Prepared Statements (available in all modern languages)
Prepared Statements • $stmt = $dbh->prepare("SELECT * FROM user WHERE username = ? and password = ?"); • $stmt->execute(array($_POST["username"], $_POST["password"]));
The Intention • $stmt = $dbh->prepare("SELECT * FROM user WHERE username = ? and password = ?"); • $stmt->execute(array("sue", ”secret"));
The Exploit Foiled • $stmt = $dbh->prepare("SELECT * FROM user WHERE username = ? and password = ?"); • $stmt->execute(array("' OR 1 = 1 #", "")); • // the logic is clearly separated • // in our code and in transmission • // to our database
Cross Site Scripting OWASP Top 10-2013 – A3 XSS
Three Variants of XSS • Reflected XSS • Stored XSS • DOM based XSS
Confusing the browser between the application’s HTML (structure) and Data. Cross Site Scripting
Commonly Used Display Code • <div><?php print $_GET["username"] ?></div>
The Intended Result • <div>sue</div>
The vulnerability: What if $_GET[“username”] is actually HTML and JavaScript?
Let’s try this: <script>alert("Hello World")</script>
Display Code With Injection • <div><?php print "<script>alert('hello world’)</script>" ?></div>
Display Code With Injection • <div><script>alert('hello world')</script></div>
Reflected XSS • The vulnerability is exploited only in response to a specific request. • Example • http://vulnerable.example.org/index.php?data=%3Cscript%3Ealert(%22hello%20world%22)%3Cscript%3E
DOM Based XSS • Also known as Type 0 XSS • Out of the scope of this course • Basically, tricking JavaScript to write out code
Protecting from XSS Encode user inputs
htmlentites() • $foo = “<script>”; • $foo = htmlentities($foo, ENT_QUOTES | ENT_HTML5); • print $foo; # <script>
html_entity_decode() • foo = "<script>"; • $foo = html_entity_decode($foo, ENT_QUOTES | ENT_HTML5); • print $foo; # "<script>”
When to encode? • Before reflecting • Before displaying information you just received • Choose either before you persist or after then be consistent. • Better yet do both but watch out for double encoding
Configuration • Your app is not secure if it’s running on a vulnerable server or otherwise deployed insecurely.
This is a topic in itself • Sources to look at: • http://php.net/manual/en/security.php • http://www.phptherightway.com/ • Google et al.
Patch! Your software is only as secure as your latest security patch
Hide your fingerprints • http://www.php.net/manual/en/security.hiding.php • http://httpd.apache.org/docs/current/mod/core.html#servertokens