Secure Application Development with the Zend Framework



Similar documents
Check list for web developers

Intrusion detection for web applications

Criteria for web application security check. Version

Offensive Security. Advanced Web Attacks and Exploitation. Mati Aharoni Devon Kearns. v. 1.0

Web application security

Hack Proof Your Webapps

Advanced Web Technology 10) XSS, CSRF and SQL Injection 2

Cyber Security Challenge Australia 2014

DIPLOMA IN WEBDEVELOPMENT

Project 2: Web Security Pitfalls

Is Drupal secure? A high-level perspective on web vulnerabilities, Drupal s solutions, and how to maintain site security

Magento Security and Vulnerabilities. Roman Stepanov

COURSE CONTENT FOR WINTER TRAINING ON Web Development using PHP & MySql

OWASP TOP 10 ILIA

Certified Secure Web Application Security Test Checklist

REDCap Technical Overview

ASP.NET MVC Secure Coding 4-Day hands on Course. Course Syllabus

Advanced Web Security, Lab

CSE598i - Web 2.0 Security OWASP Top 10: The Ten Most Critical Web Application Security Vulnerabilities

FINAL DoIT v.4 PAYMENT CARD INDUSTRY DATA SECURITY STANDARDS APPLICATION DEVELOPMENT AND MAINTENANCE PROCEDURES

REDCap General Security Overview

What about MongoDB? can req.body.input 0; var date = new Date(); do {curdate = new Date();} while(curdate-date<10000)

Executive Summary On IronWASP

WEB SECURITY CONCERNS THAT WEB VULNERABILITY SCANNING CAN IDENTIFY

How to break in. Tecniche avanzate di pen testing in ambito Web Application, Internal Network and Social Engineering

APPLICATION SECURITY AND ITS IMPORTANCE

Hacking de aplicaciones Web

Enterprise Application Security Workshop Series

Security vulnerabilities in new web applications. Ing. Pavol Lupták, CISSP, CEH Lead Security Consultant

State of The Art: Automated Black Box Web Application Vulnerability Testing. Jason Bau, Elie Bursztein, Divij Gupta, John Mitchell

Adobe Systems Incorporated

Ruby on Rails Security. Jonathan Weiss, Peritor Wissensmanagement GmbH

Ruby on Rails Secure Coding Recommendations

Hardening Moodle. Concept and Realization of a Security Component in Moodle. a project by

Web Application Security. Vulnerabilities, Weakness and Countermeasures. Massimo Cotelli CISSP. Secure

Prevent Cross-site Request Forgery: PCRF

EVALUATING COMMERCIAL WEB APPLICATION SECURITY. By Aaron Parke

Secure Coding. External App Integrations. Tim Bach Product Security Engineer salesforce.com. Astha Singhal Product Security Engineer salesforce.

The purpose of this report is to educate our prospective clients about capabilities of Hackers Locked.

Web applications. Web security: web basics. HTTP requests. URLs. GET request. Myrto Arapinis School of Informatics University of Edinburgh

ASL IT SECURITY BEGINNERS WEB HACKING AND EXPLOITATION

Web Application Vulnerability Testing with Nessus

Cross Site Scripting in Joomla Acajoom Component

PHP Authentication Schemes

Acunetix Website Audit. 5 November, Developer Report. Generated by Acunetix WVS Reporter (v8.0 Build )

(WAPT) Web Application Penetration Testing

Chapter 22 How to send and access other web sites

Develop PHP mobile apps with Zend Framework

Webapps Vulnerability Report

JOOMLA SECURITY. ireland website design. by Oliver Hummel. ADDRESS Unit 12D, Six Cross Roads Business Park, Waterford City

Detecting Web Application Vulnerabilities Using Open Source Means. OWASP 3rd Free / Libre / Open Source Software (FLOSS) Conference 27/5/2008

Hardening Joomla! (MNI)

Web Application Guidelines

Attack Vector Detail Report Atlassian

Annex B - Content Management System (CMS) Qualifying Procedure

CS 558 Internet Systems and Technologies

Web Security - Hardening estudy

What is Web Security? Motivation

Security Testing with Selenium

OWASP Application Security Building and Breaking Applications

Implementation of Web Application Firewall

Web Application Report

Sichere Software- Entwicklung für Java Entwickler

Web Application Security Guidelines for Hosting Dynamic Websites on NIC Servers

Where every interaction matters.

Finding and Preventing Cross- Site Request Forgery. Tom Gallagher Security Test Lead, Microsoft

Sidste chance for Early Bird! Tilmeld dig før d. 30. juni og spar DKK. Læs mere og tilmeld dig på

Barracuda Web Application Firewall vs. Intrusion Prevention Systems (IPS) Whitepaper

Web Application Security

Working with forms in PHP

Cross Site Scripting (XSS) and PHP Security. Anthony Ferrara NYPHP and OWASP Security Series June 30, 2011

elearning for Secure Application Development

1. Introduction. 2. Web Application. 3. Components. 4. Common Vulnerabilities. 5. Improving security in Web applications

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 03 Major Subject

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

Unbreakable ABAP? Vulnerabilities in custom ABAP Code Markus Schumacher, Co-Founder Virtual Forge GmbH

OWASP and OWASP Top 10 (2007 Update) OWASP. The OWASP Foundation. Dave Wichers. The OWASP Foundation. OWASP Conferences Chair

WebCruiser Web Vulnerability Scanner User Guide

THE SMARTEST WAY TO PROTECT WEBSITES AND WEB APPS FROM ATTACKS

Columbia University Web Security Standards and Practices. Objective and Scope

Top Ten Web Application Vulnerabilities in J2EE. Vincent Partington and Eelco Klaver Xebia

Certified Secure Web Application Secure Development Checklist

Mavituna Security Ltd. Finance House, 522A Uxbridge Rd. Pinner. HA5 3PU / UK

ArcGIS Server Security Threats & Best Practices David Cordes Michael Young

ASL IT Security Advanced Web Exploitation Kung Fu V2.0

Recon and Mapping Tools and Exploitation Tools in SamuraiWTF Report section Nick Robbins

JVA-122. Secure Java Web Development

1. Building Testing Environment

Bld. du Roi Albert II, 27, B 1030 BRUSSELS Tel Fax Secure file upload in PHP web applications

Arrow ECS University 2015 Radware Hybrid Cloud WAF Service. 9 Ottobre 2015

Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

Threat Modeling/ Security Testing. Tarun Banga, Adobe 1. Agenda

Web Application Penetration Testing

Understanding Cross Site Scripting

Network Security Exercise #8

5 Mistakes to Avoid on Your Drupal Website

Application Security Testing. Erez Metula (CISSP), Founder Application Security Expert

HackMiami Web Application Scanner 2013 PwnOff

Statistics Whitepaper

Transcription:

Secure Application Development with the Zend Framework By Stefan Esser

Who? Stefan Esser from Cologne / Germany in IT security since 1998 PHP core developer since 2001 Month of PHP Bugs/Security and Suhosin Research and Development SektionEins GmbH 2 Secure Application Development with the Zend Framework

Part I Introduction 3 Secure Application Development with the Zend Framework

Introduction Zend-Framework gets more and more popular Growing demand of secure development guidelines for applications based on the Zend-Framework Books / Talks / Seminars focus on secure programming of PHP applications without a framework Usage of frameworks requires different security guidelines Frameworks often come with own security features 4 Secure Application Development with the Zend Framework

Topics Central Authentication Central Input Validation and Filtering SQL Security Cross Site Request Forgery (CSRF) Protection Session Management Security Cross Site Scripting (XSS) Protection New attacks with old vulnerabilities 5 Secure Application Development with the Zend Framework

Part II Central Authentication and Input Validation and Filtering 6 Secure Application Development with the Zend Framework

Traditional Applications vs. Zend Framework Traditional applicationen have a lot of entrypoints ZF applications usually use the MVC design with a dispatcher Traditional way is prone to errors ZF way allows to implement security tasks in a central place Input Validation and Filtering Authentication 7 Secure Application Development with the Zend Framework

Front Controller Plugin Adding functionality to Zend_Controller_Action No class extension required Suitable for central tasks like authentication and input validation/filtering $front = Zend_Controller_Front::getInstance(); $front->registerplugin(new MyPlugin()); $front->dispatch(); 8 Secure Application Development with the Zend Framework

Central Authentication class ForceAuthPlugin extends Zend_Controller_Plugin_Abstract { public function predispatch(zend_controller_request_abstract $request) { try { } } My_Auth::isLoggedIn(); } catch (My_Auth_UserNotLoggedInException $e) { } if (!in_array($request->getcontrollername(), array('login','index','error'))) { } $request->setmodulename('default') ->setcontrollername('login') ->setactionname('index') ->setdispatched(false); return; 9 Secure Application Development with the Zend Framework

Central Input Validation/Filtering (I) $filters['index']['index'] = array( '*' => 'StringTrim', 'month' => 'Digits' ); $filters['login']['index'] = array( 'login' => 'Alpha', 'pass' => 'Alpha' ); $validators['index']['index'] = array( 'month' => array( new Zend_Validate_Int(), new Zend_Validate_Between(1, 12) ) ); $validators['login']['index'] = array( 'login' => array( new My_Validate_Username() ), 'pass' => array( new My_Validate_Password() ), ); 10 Secure Application Development with the Zend Framework

Central Input Validation/Filtering (II) class FilterPlugin extends Zend_Controller_Plugin_Abstract { public function predispatch(zend_controller_request_abstract $request) { $params = $request->getparams(); $controller = $request->getcontrollername(); $action = $request->getactionname(); @$filter = $GLOBALS['filters'][$controller][$action]; @$validator = $GLOBALS['validators'][$controller][$action]; $input = new Zend_Filter_Input($filter, $validator, $params); } if (!$input->isvalid()) { $request->setmodulename('default') ->setcontrollername('error') ->setactionname('illegalparam') ->setdispatched(false); return; } 11 Secure Application Development with the Zend Framework

Central Integration of PHPIDS class Controller_Plugin_PHPIDS extends Zend_Controller_Plugin_Abstract { public function predispatch(zend_controller_request_abstract $request) { $request = array('get' => $request->getquery(), 'POST' => $request->getpost(), 'COOKIE' => $request->getcookie(), 'PARAMS' => $request->getuserparams()); $init = IDS_Init::init(APPLICATION_PATH.'/config/phpids.ini'); $ids = new IDS_Monitor($request, $init); } } $result = $ids->run(); if (!$result->isempty()) { $compositelog = new IDS_Log_Composite(); $compositelog->addlogger(ids_log_database::getinstance($init)); $compositelog->execute($result); } 12 Secure Application Development with the Zend Framework

Part III Formvalidation and -filtering 13 Secure Application Development with the Zend Framework

Input Validation/Filtering in Forms (I) ZF Forms use validators/filters automatically Validators can be added to Zend_Form_Element objects Validators can be chained arbitrarily // Create Name Element $name = $form->createelement('text', 'name', array('size' => 40, 'maxlength' => 40)); $name->addvalidator('alpha') ->addvalidator('stringlength', false, array(1, 40)) ->setlabel('name') ->setrequired(true); // Message Element erstellen $message = $form->createelement('textarea', 'message', array('rows' => 6, 'cols' => 40)); $message->setlabel('message') ->setrequired(true) ->addfilter('striptags'); // Create Submit Button $submit = $form->createelement('submit', 'send'); $submit->setlabel('absenden'); // Add all Elements to the Form $form->addelement($name)->addelement($message)->addelement($submit); 14 Secure Application Development with the Zend Framework

Input Validation/Filtering in Forms (II) Validation of form in the corresponding Action // Validation of formdata if (!$form->isvalid($this->getrequest()->getpost())) { // submit variables to view $this->view->form = $form; $this->view->title = "Form 1"; } // stop processing return $this->render('form'); 15 Secure Application Development with the Zend Framework

Part IV SQL-Security 16 Secure Application Development with the Zend Framework

SQL-Injection in Zend Framework Applications ZF comes with several classes for database access Methods usually support Prepared Statements <?php $sql = "SELECT id FROM _users WHERE lastname=? AND age=?"; $params = array('mueller', '18'); $res = $db->fetchall($sql, $params);?> Prep. Statements operating error allows SQL injection ZF also has escaping functions for dynamic SQL queries Lack of escaping leads to SQL injection 17 Secure Application Development with the Zend Framework

SQL-Injection + PDO_MySQL = Danger Traditionally MySQL allows only a single SQL query ext/mysql stops multi-queries completely ext/mysli has separate function mysql_multi_query() ATTENTION: PDO_MySQL doesn t have this limitation SQL injection in ZF Applicationen using PDO_MySQL is more dangerous than in applications using the traditional MySQL interfaces 18 Secure Application Development with the Zend Framework

Zend_Db Escaping function quote($value, $type = null) Always the right escaping one function instead of many ATTENTION: strings are put between single quotes function quoteidentifier($ident, $auto=false) Escaping function for identifiers Traditional PHP applications have to implement their own ATTENTION: strings are put between single quotes 19 Secure Application Development with the Zend Framework

Zend_Db_Select To create somewhat dynamic SQL SELECT queries Uses Prepared Statements internally as much as possible SQL injection possible if wrongly used ATTENTION especially at WHERE and ORDER BY // Build this query: // SELECT product_id, product_name, price // FROM "products" // WHERE (price < 100.00 OR price > 500.00) // AND (product_name = 'Apple') $minimumprice = 100; $maximumprice = 500; $prod = 'Apple'; $select = $db->select() ->from('products', array('product_id', 'product_name', 'price')) ->where("price < $minimumprice OR price > $maximumprice") ->where('product_name =?', $prod); 20 Secure Application Development with the Zend Framework

Part V Cross Site Request Forgery (CSRF) Protection 21 Secure Application Development with the Zend Framework

Cross Site Request Forgery (CSRF) Protection CSRF protections are usually based on secret, session dependent form tokens Zend-Framework has Zend_Form_Element_Hash which is such a token with built-in validator Forms can be safeguarded against CSRF by adding the form element $form->addelement('hash', 'csrf_token', array('salt' => 's3cr3ts4ltg%ek@on9!')); 22 Secure Application Development with the Zend Framework

Automatic CSRF Protection Protection has to be applied manually By extending the Zend_Form class it is possible to create a new form class with automatic CSRF protection <?php class My_Form extends Zend_Form { function construct() { parent:: construct(); $this->addelement('hash', 'csrf_token', array('salt' => get_class($this). 's3cr3t%ek@on9!')); } }?> 23 Secure Application Development with the Zend Framework

Part VI Session Management Security 24 Secure Application Development with the Zend Framework

Session Management Configuration Configuration has big impact on session security SSL applications muse be secured with the secure flag Own session name / session storage for each application Hardening against XSS with the httponly flag Setting the maximum lifetime <?php Zend_Session::setOptions(array( /* if SSL server */ 'cookie_secure' => true, /* own session name */ 'name' => 'myssl', /* own session storage */ 'save_path' => '/sessions/myssl', /* hardening against XSS */ 'cookie_httponly' => true, /* short lifetime */ 'gc_maxlifetime' => 15 * 60 )); Zend_Session::start();?> 25 Secure Application Development with the Zend Framework

Session Fixation and Session Hijacking Session Fixation Gets slightly harder with session validation/strict session handling Stopped by regenerating a new session id on each status change session_regenerate_id(true); Best implemented in the login/logout module Session Hijacking Only stoppable by using SSL (to stop network sniffing) httponly cookies protect against session id theft by XSS Session validation only of limited use 26 Secure Application Development with the Zend Framework

Session Validation (I) Recognizes valid sessions by checking additional information Often recommended to stop session fixation / hijacking but only limited usefullness Zend Framework supports session validators Zend_Session_Validator_HttpUserAgent <?php try { Zend_Session::start(); } catch (Zend_Session_Exception $e) { // Zend_Session::regenerate_id() support is broken session_regenerate_id(true); } Zend_Session::registerValidator(new Zend_Session_Validator_HttpUserAgent());?> 27 Secure Application Development with the Zend Framework

Session Validation (II) Be careful when checking additional information fields User-Agent HTTP header checks problematic since atleast Microsoft Internet Explorer 8 Accept HTTP header checks already a problem with earlier versions of Internet Explorer Checking the client IP not possible for users of big proxies / companies / ISPs Limit check to class C / B / A network Better compatibility with SSL sites 28 Secure Application Development with the Zend Framework

Session Validation Valididating the Client IP class Zend_Session_Validator_RemoteAddress extends Zend_Session_Validator_Abstract { /** * Setup() - this method will get the client's remote address and store * it in the session as 'valid data' * * @return void */ public function setup() { $this->setvaliddata( (isset($_server['remote_addr'])? $_SERVER['REMOTE_ADDR'] : null) ); } /** * Validate() - this method will determine if the client's remote addr * matches the remote address we stored when we initialized this variable. * * @return bool */ public function validate() { $currentbrowser = (isset($_server['remote_addr'])? $_SERVER['REMOTE_ADDR'] : null); } } return $currentbrowser === $this->getvaliddata(); 29 Secure Application Development with the Zend Framework

Part VI Cross Site Scripting (XSS) Protection 30 Secure Application Development with the Zend Framework

XSS in Zend Framework Applications Symfony comes with an automatic output escaping Zend Framework comes without such things stopping XSS is task of the programmer XSS vulnerabilities occur in the view <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title><?php echo $this->title;?></title> </head> <body> <h2><?php echo $this->headline;?></h2> <ul> <li><a href="<?php echo $this->link;?>">link 1</a></li> </ul> </body> </html> 31 Secure Application Development with the Zend Framework

Protection against XSS (I) Traditionally: Two alternatives Outputting only clean values <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title><?php echo $this->escape($this->title);?></title> </head> <body> <h2><?php echo $this->escape($this->headline);?></h2> <ul> <li><a href="<?php echo urlprepare($this->link);?>">link 1</a> </li> </ul> </body> </html> 32 Secure Application Development with the Zend Framework

Protection against XSS (II) Traditionally: Two alternatives Assigning only clean values $entityfilter = new Zend_Filter_HtmlEntities(); $urlfilter = new My_Filter_Url(); $this->view->title = $this->escape("seite 1"); $this->view->headline = $entitiyfilter->filter($this->getrequest() ->getpost('link')); $this->view->link = $urlfilter->filter($this->getrequest() ->getpost('link')); 33 Secure Application Development with the Zend Framework

Safeguarding with Zend_View_Helper Traditional solutions prone to erros any mistake == XSS Automatic scanning for failure to apply filters is hard Prohibit direct output of variables Output only via Zend_View_Helper XSS protection becomes task of Zend_View_Helper <form action="action.php" method="post"> <p><label>your Email: <?php echo $this->formtext('email', 'you@example.com', array('size' => 32))?> </label></p> <p><label>your Country: <?php echo $this->formselect('country', 'us', null, $this->countries)?> </label></p> <p><label>would you like to opt in? <?php echo $this->formcheckbox('opt_in', 'yes', null, array('yes', 'no'))?> </label></p> </form> 34 Secure Application Development with the Zend Framework

Part VII unserialize() and User Input 35 Secure Application Development with the Zend Framework

unserialize() and User Input Never use unserialize() on user input! Properties can be filled arbitrarily even private ones destruct() and wakeup() methods will be executed autoloader allows loading arbitrary objects Zend Framework comes with a lot of classes combination of classes allow hijacking the control flow 36 Secure Application Development with the Zend Framework

unserialize() Example Exploit Classes of Zend-Framework allow Upload of arbitrary files Execution of arbitrary PHP Code (ZF >= 1.8.0) Sending SPAM Emails (ZF >= 1.8.0) Inclusion of arbitrary files (ZF >= 1.9.0) 37 Secure Application Development with the Zend Framework

Zend_Queue_Adapter_Activemq class Zend_Queue_Adapter_Activemq extends Zend_Queue_Adapter_AdapterAbstract {... /** * Close the socket explicitly when destructed * * @return void */ public function destruct() { // Gracefully disconnect $frame = $this->_client->createframe(); $frame->setcommand('disconnect'); $this->_client->send($frame); unset($this->_client); } Zend_Queue_Adapter_Activemq _client 38 Secure Application Development with the Zend Framework

Zend_Queue_Stomp_Client_Connection class Zend_Queue_Stomp_Client_Connection implements Zend_Queue_Stomp_Client_ConnectionInterface {... public function getframeclass() { return isset($this->_options['frameclass'])? $this->_options['frameclass'] : 'Zend_Queue_Stomp_Frame'; } public function createframe() { $class = $this->getframeclass(); if (!class_exists($class)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($class); } $frame = new $class();... Zend_Queue_Stomp_Client_Connection _options[frameclass] 39 Secure Application Development with the Zend Framework

Combined Zend_Queue_Adapter_Activemq _client Zend_Queue_Stomp_Client_Connection _options[frameclass] = /var/www/malicious O:27:"Zend_Queue_Adapter_Activemq":1:{s: 36:"\0Zend_Queue_Adapter_Activemq\0_client";O: 34:"Zend_Queue_Stomp_Client_Connection":1:{s:11:"\0*\0_options";a:1: {s:10:"frameclass";s:18:"/var/www/malicious";}}} 40 Secure Application Development with the Zend Framework

Questions? Get Audited by Web Security Experts http://www.sektioneins.com 41 Secure Application Development with the Zend Framework