©2009 Justin C. Klein Keane PHP Code Auditing Session 4.2 – File Include Vulnerabilities Justin C. Klien Keane

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Error HandlingPHPMay-2007 : [#] PHP Error Handling.
©Copyright Justin C. Klein Keane PHP Vulnerability Potpourri File Include, Command Injection & Authentication Bypass Vulnerabilities.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
ARDUINO CLUB Session 1: C & An Introduction to Linux.
CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.
PHP Reusing Code and Writing Functions.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Types, exploitation, and prevention
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
©2009 Justin C. Klein Keane PHP Code Auditing Session 5 XSS & XSRF Justin C. Klein Keane
©2009 Justin C. Klein Keane PHP Code Auditing Session 4.3 – Information Disclosure & Authentication Bypass Justin C. Klein Keane
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
By Brian Vees.  SQL Injection  Username Enumeration  Cross Site Scripting (XSS)  Remote Code Execution  String Formatting Vulnerabilities.
NAVY Research Group Department of Computer Science Faculty of Electrical Engineering and Computer Science VŠB-TUO 17. listopadu Ostrava-Poruba.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
©2009 Justin C. Klein Keane PHP Code Auditing Session 4.1 – Command Injection Justin C. Klein Keane
©2009 Justin C. Klein Keane PHP Code Auditing Session 6 Auditing Strategies & Demonstration Justin C. Klein Keane
Copyright 2009 Justin C. Klein Keane PHP Code Auditing Session 1 – PHP Foundations Justin C. Klein Keane
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
Program Input and the Software Design Process ROBERT REAVES.
PHP Security.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
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.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Security Attacks CS 795. Buffer Overflow Problem Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program.
Aniket Joshi Justin Thomas. Agenda Introduction to SQL Injection SQL Injection Attack SQL Injection Prevention Summary.
System calls for Process management
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
Security Issues with PHP  PHP installation  PHP programming Willa Zhu & Eugene Burger.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
How to maintain state in a stateless web Shirley Cohen
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
PHP Security Ryan Dunn Jason Pack. Outline PHP Overview PHP Overview Common Security Issues Common Security Issues Advanced Security Issues Advanced Security.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
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.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
UNIX PROCESSES.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Attacking Back-End Components
Topics Introduction to File Input and Output
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Files.
Topics Introduction to File Input and Output
Presentation transcript:

©2009 Justin C. Klein Keane PHP Code Auditing Session 4.2 – File Include Vulnerabilities Justin C. Klien Keane

©2009 Justin C. Klein Keane File Include Vulnerabilities Arbitrary file includes (reading)  Local file includes  Remote file includes Directory traversal Writing arbitrary files

©2009 Justin C. Klein Keane Basic PHP File Includes Four common functions  include()  include_once()  require()  require_once() Difference is that require will die (with fatal E_ERROR) if the specified file is not found  Include() will produce an E_WARNING _once functions will not re-include the file if it has already been called

©2009 Justin C. Klein Keane How Includes Work When PHP includes a file it will parse any PHP code within that file Anything not delimited with the PHP delimiters (“ ”) will be treated as plain text Plain text will simply be rendered inline

©2009 Justin C. Klein Keane Typical Include <?php include_once('header.php'); include_once($_GET['action']. '.php'); include_once('footer.php'); ?>

©2009 Justin C. Klein Keane Problems with Includes Arbitrary local file includes triggered via malicious user input: If user supplies “../../../../../../../etc/passwd” as the 'action' URL variable that file will be rendered during page display!

©2009 Justin C. Klein Keane Incorrect Projection Schemes Some programmers will append a file extension to attempt to limit includes like /etc/passwd This fails for several reasons, one is because PHP is written in C

©2009 Justin C. Klein Keane Caveats of C C doesn't have a string type Instead strings are null terminated character arrays: char foo[3]; int main() { foo[0] = 'B'; foo[1] = 'A'; foo[2] = 'R'; foo[3] = '\0'; } Without the null at the end the “string” would have no end  C reads from the start of the string until it reaches the null character when printing strings

©2009 Justin C. Klein Keane Tricking PHP with C Conventions Using a null character triggers C constructs and defeats the prior example If user passes in: action=../../../../../../etc/passwd%00 then PHP executes: include('inc/../.././../../etc/passwd'); Because PHP terminates the string at the null bit (and ignores the appended '.php') Most PHP programmers are unaware of this!

©2009 Justin C. Klein Keane Other Include Strategies There are other ways around extension protections Attacker can provide the GET var: ?action=/path/to/other/php_file.php? renders the final “.php” as a GET var to the included php_file.php

©2009 Justin C. Klein Keane Other Dangers of Includes Often times include files are meant to be included Include files live on the filesystem though May contain vulnerabilities when called directly as variables could be redefined or arbitrarily defined Especially dangerous when register_globals is on!

©2009 Justin C. Klein Keane Example Main file: <?php $style_dir='images/'; include_once('header.php'); [...] Include file: Foo url(“ style.css”); What happens when an attacker calls:

©2009 Justin C. Klein Keane Remote File Include Rather than specifying a local resource, an attacker could specify a remote file for inclusion Remote files must be served as plain text, rather than compiled PHP Remote text is pulled for inclusion then the local PHP compiler interprets the text, rendering the PHP locally

©2009 Justin C. Klein Keane Remote File Include Requirements /etc/php.ini has parameters that define the ability of PHP to include files: ;;;;;;;;;;;;;;;;;; ; Fopen wrappers ; ;;;;;;;;;;;;;;;;;; ; Whether to allow the treatment of URLs (like or ftp://) as files. allow_url_fopen = On

©2009 Justin C. Klein Keane If allow_url_fopen is On Attackers can include remote files: Attacker can call ?action=

©2009 Justin C. Klein Keane Other Include Strategies Attackers can use includes to bypass direct access restrictions such as.htaccess  This could be used to expose files like config.ini files Attackers can include Apache files like.htpasswd or.htaccess files which are included as plain text, exposing their contents Attackers can subvert program flow by calling files that are normally not included Attackers can call files readable by Apache, such as files in /tmp which may contain sensitive data (like session cookies or malicious uploads)

©2009 Justin C. Klein Keane Writing Files PHP functionality used to write files include:  File upload functions built into an application (such as image uploads)  Utilizing PHP filesystem commands such as fwrite()

©2009 Justin C. Klein Keane Typical Image Upload Handler $upload_dir = "files/"; $filename = basename($_FILES['form_filename']['name']); $target = $upload_dir. $filename; if(move_uploaded_file($_FILES['form_filename']['tmp_name'], $target)) { echo $filename. " has been uploaded"; } else{ echo "Error uploading file!"; }

©2009 Justin C. Klein Keane Common Upload Errors Collisions cause overwrites File type is not checked  Programmer may assume only image files are being uploaded, but this isn't enforced File type is checked inappropriately  Simply checking $_FILES['upload_file']['type'] is insufficient since this is a browser provided parameter Double extensions (and programmer only check the first one)

©2009 Justin C. Klein Keane Exploits for File Uploads Attacker uploads a PHP file which contains a backdoor or exposes other system files Attacker uploads a.htaccess file overwriting Apache rules Attacker overwrites existing files to insert a backdoor

©2009 Justin C. Klein Keane Fwrite() The fwrite() function is a built in function that allows Apache to write to file handles Often used in installers to write config files Also commonly used for logging For more information see: