introduction of reverse engineering

33
Reverse Engineering 逆向工程入門 Y.C. Ling All Rights Reserved 2010/09/24

Upload: yc-ling

Post on 24-Jun-2015

1.381 views

Category:

Technology


4 download

DESCRIPTION

My presentation related to Reverse Engineering at 2010/09. Traditional Chinese.

TRANSCRIPT

Page 1: Introduction of Reverse Engineering

Reverse Engineering逆向工程入門

Y.C. LingAll Rights Reserved

2010/09/24

Page 2: Introduction of Reverse Engineering

Agenda

Part 1: 逆向工程入門Part 2: 從組合語言到除錯

Page 3: Introduction of Reverse Engineering

Part 1 分享內容

中研院上課心得自由軟體技術充電站-「軟體除錯分析-C語言逆向入門」

Website Link

Page 4: Introduction of Reverse Engineering

軟體工程 V.S. 逆向工程

軟體工程人 => 機器

逆向工程機器 => 人 

Page 5: Introduction of Reverse Engineering

逆向工程的應用

研究分析AV的最愛也是外掛的最愛

漏洞發掘黑盒白盒

軟體除錯我們有源始碼啊?鬼打牆時的最後一招

Page 6: Introduction of Reverse Engineering

Why C?

Why C? 較為貼近Machine CodeMachine Code易與Source Code一一對應

What about C++? �Much more complicated

ClassMember functionMember dataVirtual FunctionTemplate.....etc

Page 7: Introduction of Reverse Engineering

Machine Code

Page 8: Introduction of Reverse Engineering

助記符號

Page 9: Introduction of Reverse Engineering

分析方法

靜態分析IDA proobjdump

動態分析Ollydbggdb

Page 10: Introduction of Reverse Engineering

IA32 Assembly Language

您懂組語?略懂略懂

mov a, blea a, b

add a, bsub a, b

inc adec a

Page 11: Introduction of Reverse Engineering

IA32 Assembly Language

cmp a, b

jmp addrje addrjne addrj?? addr

push apop a

call addrret

Page 12: Introduction of Reverse Engineering

Example 1: Hello World

Main FunctionThree arguments緊臨exit() 

Let's Demo....

Page 13: Introduction of Reverse Engineering

Example 2: Function Call

Calling Conventionstdcall, cdecl: 參數由右至左推入堆疊

StackESP: 堆疊頂端EBP: 堆疊底端

int sub(a, b) { return a-b; }int result = sub(2, 1); 轉成組合語言....push 1push 2call submov result, eax

Demo Time!!

Page 14: Introduction of Reverse Engineering

Example 3: If Condition

通常長的像....cmp a, bj?? addr

廢話不多說,直接看Demo....

Page 15: Introduction of Reverse Engineering

Example 4: For Loop應該長的像....

可是....編譯器會....

甚至....唉....Demo吧....

Page 16: Introduction of Reverse Engineering

Example 5: Password

實戰題大約是CrackMe或WarGame最簡單難度Demo Demo Demo!!

Page 17: Introduction of Reverse Engineering

只要有心....

天才?世界上沒有那麼多天才啦前輩:

逆向 = 經驗 + 耐心不懂破解,別妄談保護

Just Try It!!

Page 18: Introduction of Reverse Engineering

Just Try It回家做作業啦

看雪學院http://www.pediy.com/

Wargame http://wargame.cs.nctu.edu.tw/

CrackmeBeware of Virus & TrojanSandbox!!

VMWareVirtualBox

Page 19: Introduction of Reverse Engineering

NCTU Wargame

Page 20: Introduction of Reverse Engineering
Page 21: Introduction of Reverse Engineering

Any Question?還有Part 2....

Page 22: Introduction of Reverse Engineering

Reverse EngineeringPart 2

從組合語言到除錯

Y.C. LingAll Rights Reserved

2010/09/24

Page 23: Introduction of Reverse Engineering

從組合語言到除錯

核心精神了解你的C++ Code會被編譯成怎樣的機器碼

組合語言的低階知識,的確數次幫我找到很難找的問題但這招其實很難得用的出來

投資報酬率低XD不是天天遇到需要看組語的鬼打牆Bug

If so, 程式碼的品質太差了就算鬼打牆,也不一定能從組語看出端倪

Page 24: Introduction of Reverse Engineering

從組合語言到除錯

常應用的場合:開啟Optimization造成Debugger資訊不完全Crash Dump資訊不完全時一行Code中有一連串的Function Call,不知道當在那個點

How to learn? 沒有捷徑C++基本功Detail knowledge of C++ Compiler組合語言基本功Also helps in optimization

以「對失效指標物件呼叫Member Function」為例

Page 25: Introduction of Reverse Engineering

靠腰,鬼打牆

void CPlayer::IncreaseHP(int iAmount){ // Do a lot of work.... m_iHP += iAmount; //有人問:當在這行是三小狀況?}m_pPlayer->IncreaseHP(10);

Access Violation是m_pPlayer指標錯了嗎?那為什麼Call IncreaseHP時不會當?

Page 26: Introduction of Reverse Engineering

您懂Access Violation?略懂略懂

程式什麼時候會當掉?Infinity loop, Deadlock, Invalid system call最常見的狀況之一: Access Violation(Segmentation Fault)

何謂Access Violation不合法的存取

讀寫未非法的Segment寫入唯讀的Segment

當物件指標不合法pPlayer->IncreaseHP(10); 如為null,則會讀寫到0附近的位置,造成當機怎麼當?何時當?為何有時Call Function就會當?有時執行Function Body才當?

Page 27: Introduction of Reverse Engineering

Non-Virtual Function

m_pPlayer->IncreaseHP(10)

機器碼行為類似這樣CPlayer::InvokeHP(m_pPlayer, 10);

組語長的像這樣push 10push m_pPlayercall CPlayer::InvokeHP

呼叫時沒有存取m_pPlayer指向之內容! 所以呼叫時不會當機,而當在函數

Page 28: Introduction of Reverse Engineering

Virtual Function

m_pPlayer->IncreaseHP(10)

組語行為像這樣mov eax, m_pPlayermov eax, DWORD PTR[eax] #當機add eax, offsetmov edx, DWORD PTR[eax]

push 10push m_pPlayer

call edx

Virtual Function在被呼叫物件為null時,必定在呼叫時當機

Page 29: Introduction of Reverse Engineering

Inline Function

沒有Function Call! 

m_pPlayer->IncreaseHP(10)行為其實像m_pPlayer->m_iHP += 10

Page 30: Introduction of Reverse Engineering

Crash when Function Invocation

Non-Virtual Function Virtual Function

Null Pointer當在Function中存取Member時

呼叫時當機

Dangling Pointer

若Segment被回收,可能馬上當機。

否則,視該記憶體位置是否已被其他資料佔用。若無的話,則可能(不幸地)沒有影響。若有的話,則可能造成資料錯亂。

若Segment被回收,可能馬上當機。

仍有機會完全不當機(指向Virtual Pointer Table的指標沒被動到)。

再者,可能在存取VPTable或call function時當機。

假設呼叫了一個IncreaseHP的函式,該函式僅簡單地改動一個Member Data

Virtual Function Revisited:

mov eax, m_pPlayermov eax, DWORD PTR[eax]add eax, offsetmov edx, DWORD PTR[eax]

push 10push m_pPlayer

call edx

Page 31: Introduction of Reverse Engineering

m_pGameStage->GetMainPlayer()->GetWeapon() ->GetSFX()->GetChannel(2)->SetSpeed(2.0f);

Access ViolationCrash Dump資訊不完全偶發當機,難以重現

int iDamage = pAttacker->GetDmg() * pAttacker->GetWeapon()->GetDmgBonus() / ( pDefender->GetDef() * pDefender->GetArmor()->GetDefBonus() );

.....Orz

Page 32: Introduction of Reverse Engineering

結語核心精神:

了解你的C++ Code會被編譯成怎樣的機器碼當Debugger資訊不足,Crash Dump資訊不完全時

在Register和Assembly中挖掘資訊,以拼湊問題點

夜路走多總會碰到鬼,C++寫多總會弄錯指標使用更高階的語言實作High Level Logic?讓VM與GC讓你解決這些問題

但你會碰到其他問題....

Page 33: Introduction of Reverse Engineering

Any Question?