security cookie的一點分析

2008-03-16 16:53

今天在分析visual studio下的程序的時候發現都有security cookie檢查,順便分析一下。。。

 

使用/GS選項編譯的程序,對函數的入口和出口代碼都添加了爭對security cookie操作的指令,這樣可以有效的防止緩衝區溢出,即使程序存在緩衝區溢出錯誤,/GS選項也能阻止惡意代碼通過覆蓋返回地址執行代碼進行攻擊。。。

 

這是反彙編出來的security cookie值初始化函數

___security_init_cookie proc near       ; CODE XREF: DllEntryPoint+B p
.text:50002153
.text:50002153 PerformanceCount= LARGE_INTEGER ptr -10h
.text:50002153 SystemTimeAsFileTime= _FILETIME ptr -8
.text:50002153
.text:50002153 ; FUNCTION CHUNK AT .text:501D128D SIZE 00000022 BYTES
.text:50002153
.text:50002153                 mov     edi, edi
.text:50002155                 push    ebp
.text:50002156                 mov     ebp, esp
.text:50002158                 sub     esp, 10h
.text:5000215B                 mov     eax, ___security_cookie                 ;初始化隨機值
.text:50002160                 and     [ebp+SystemTimeAsFileTime.dwLowDateTime], 0
.text:50002164                 and     [ebp+SystemTimeAsFileTime.dwHighDateTime], 0
.text:50002168                 push    ebx
.text:50002169                 push    edi
.text:5000216A                 mov     edi, 0BB40E64Eh
.text:5000216F                 mov     ebx, 0FFFF0000h
.text:50002174                 cmp     eax, edi
.text:50002176                 jz      short loc_50002180
.text:50002176
.text:50002178                 test    eax, ebx
.text:5000217A                 jnz     loc_501D128D
.text:5000217A
.text:50002180
.text:50002180 loc_50002180:                           ; CODE XREF: ___security_init_cookie+23 j
.text:50002180                 push    esi
.text:50002181                 lea     eax, [ebp+SystemTimeAsFileTime]
.text:50002184                 push    eax             ;                                    ;參數指向 lpSystemTimeAsFileTime 結構
.text:50002185                 call    ds:GetSystemTimeAsFileTime(x)            ;獲得系統時間
.text:5000218B                 mov     esi, [ebp+SystemTimeAsFileTime.dwHighDateTime]    ;
.text:5000218E                 xor     esi, [ebp+SystemTimeAsFileTime.dwLowDateTime]
.text:50002191                 call    ds:GetCurrentProcessId()         ;獲得進程標識

.text:50002197                 xor     esi, eax
.text:50002199                 call    ds:GetCurrentThreadId()         ;獲得線程標識
.text:5000219F                 xor     esi, eax
.text:500021A1                 call    ds:GetTickCount()                    ;獲得系統開機到現在的時間
.text:500021A7                 xor     esi, eax
.text:500021A9                 lea     eax, [ebp+PerformanceCount]
.text:500021AC                 push    eax                                               ; lpPerformanceCount
.text:500021AD                 call    ds:QueryPerformanceCounter(x)   
.text:500021B3                 mov     eax, dword ptr [ebp+PerformanceCount+4]
.text:500021B6                 xor     eax, dword ptr [ebp+PerformanceCount]
.text:500021B9                 xor     esi, eax
.text:500021BB                 cmp     esi, edi
.text:500021BD                 jz      loc_501D1299
.text:500021BD
.text:500021C3                 test    esi, ebx
.text:500021C5                 jz      loc_501D12A3
.text:500021C5
.text:500021CB
.text:500021CB loc_500021CB:                           ; CODE XREF: ___security_init_cookie+1CF14B j
.text:500021CB                                         ; ___security_init_cookie+1CF157 j
.text:500021CB                 mov     ___ ;security_cookie, esi                       ;最後生成的security_cookie
.text:500021D1                 not     esi
.text:500021D3                 mov     ___security_cookie_complement, esi
.text:500021D9                 pop     esi
.text:500021D9
.text:500021DA
.text:500021DA loc_500021DA:                           ; CODE XREF: ___security_init_cookie+1CF141 j
.text:500021DA                 pop     edi
.text:500021DB                 pop     ebx
.text:500021DC                 leave
.text:500021DD                 retn
.text:500021DD
.text:500021DD ___security_init_cookie endp

下面這是在函數結束的時候對security_cookie的檢查

 

.text:500049C9                 mov     ecx, [ebp+var_4]
.text:500049CC                 mov     eax, edi
.text:500049CE                 pop     edi
.text:500049CF                 pop     esi
.text:500049D0                 xor     ecx, ebp
.text:500049D2                 pop     ebx
.text:500049D3                 call    __security_check_cookie(x)
.text:500049D3
.text:500049D8                 leave

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章