ProMiNick
Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
|
DEFAULT_SECURITY_COOKIE = $BB40
__security_init_cookie:
mov eax, [___security_cookie]
test eax, eax
jz .go_init
cmp eax, DEFAULT_SECURITY_COOKIE
jnz .done
.go_init:
sub esp, 8
invoke GetSystemTimeAsFileTime, esp
pop eax
xor [esp], eax
invoke GetCurrentProcessId
xor [esp], eax
invoke GetCurrentThreadId
xor [esp], eax
invoke GetTickCount
xor [esp], eax
sub esp, 8
invoke QueryPerformanceCounter, esp
pop eax
xor [esp], eax
pop eax
xor [esp], eax
pop eax
and eax, $FFFF
jnz .skip_default
mov eax, DEFAULT_SECURITY_COOKIE
.skip_default:
mov [___security_cookie], eax
.done:
not eax
mov [___security_cookie_complement], eax
retn
__check_security_cookie:
cmp ecx, [___security_cookie]
jnz __report_gsfailure
retn
__report_gsfailure:
sub esp, sizeof.X86_CONTEXT - X86_CONTEXT._Eip
push ebp
push eax
push ecx
push edx
push ebx
push esi
push edi
push ds
push es
push fs
push gs
add esp, X86_CONTEXT.ExtendedRegisters-X86_CONTEXT.SegGs
push ss
lea ebx, [esp+sizeof.X86_CONTEXT-X86_CONTEXT.SegSs]
push ebx
pushfd
push cs
mov ebx, [ebx]
push ebx
sub esp, X86_CONTEXT._Eip-X86_CONTEXT._Dr0
push CONTEXT_CONTROL
mov ebp, esp
push [___security_cookie]
push [___security_cookie_complement]
mov ecx, sizeof.EXCEPTION_RECORD32/4
lea esp, [esp-sizeof.EXCEPTION_RECORD32]
xor eax, eax
mov edi, esp
rep stosb
mov eax, esp
mov [esp+EXCEPTION_RECORD32.ExceptionAddress], ebx
mov [esp+EXCEPTION_RECORD32.ExceptionCode], STATUS_STACK_BUFFER_OVERRUN
push ebp
push eax
invoke SetUnhandledExceptionFilter, NULL
invoke UnhandledExceptionFilter, esp
invoke GetCurrentProcess
invoke TerminateProcess, eax, $502
use case:
; DialogBox example
format PE GUI 4.0
entry start
include 'win32a.inc'
include 'os_specs/windows/equates/exsup.inc'
include 'os_specs/windows/structs/x32/exsup.inc'
include 'os_specs/windows/structs/contexts.inc'
CONTEXT_CONTROL = $10001
STATUS_STACK_BUFFER_OVERRUN = $C0000409
ID_CAPTION = 101
ID_MESSAGE = 102
ID_ICONERROR = 201
ID_ICONINFORMATION = 202
ID_ICONQUESTION = 203
ID_ICONWARNING = 204
ID_TOPMOST = 301
section '.text' code readable executable
proc start
call __security_init_cookie
invoke DialogBoxParam,PE_IMAGE_BASE,37,HWND_DESKTOP,DialogProc,0
ret
endp
proc DialogProc,hwnddlg,msg,wparam,lparam
;push [___security_cookie]
locals
message rb 100h
caption rb 10h
security_cookie dd ?
endl
push ebx
push esi
push edi
cmp [msg],WM_INITDIALOG
je .wminitdialog
cmp [msg],WM_COMMAND
je .wmcommand
cmp [msg],WM_CLOSE
je .wmclose
xor eax,eax
jmp .finish
.wminitdialog:
invoke CheckRadioButton,[hwnddlg],ID_ICONERROR,ID_ICONWARNING,ID_ICONINFORMATION
jmp .processed
.wmcommand:
cmp [wparam],BN_CLICKED shl 16 + IDCANCEL
je .wmclose
cmp [wparam],BN_CLICKED shl 16 + IDOK
jne .processed
mov eax, [___security_cookie]
mov [security_cookie], eax
invoke GetDlgItemText,[hwnddlg],ID_CAPTION,addr caption,$40
invoke GetDlgItemText,[hwnddlg],ID_MESSAGE,addr message,$100
mov ecx, [security_cookie]
;mov ecx,[esp-$100-$40]
call __check_security_cookie
mov [flags],MB_OK
invoke IsDlgButtonChecked,[hwnddlg],ID_ICONERROR
cmp eax,BST_CHECKED
jne .iconerror_ok
or [flags],MB_ICONERROR
.iconerror_ok:
invoke IsDlgButtonChecked,[hwnddlg],ID_ICONINFORMATION
cmp eax,BST_CHECKED
jne .iconinformation_ok
or [flags],MB_ICONINFORMATION
.iconinformation_ok:
invoke IsDlgButtonChecked,[hwnddlg],ID_ICONQUESTION
cmp eax,BST_CHECKED
jne .iconquestion_ok
or [flags],MB_ICONQUESTION
.iconquestion_ok:
invoke IsDlgButtonChecked,[hwnddlg],ID_ICONWARNING
cmp eax,BST_CHECKED
jne .iconwarning_ok
or [flags],MB_ICONWARNING
.iconwarning_ok:
invoke IsDlgButtonChecked,[hwnddlg],ID_TOPMOST
cmp eax,BST_CHECKED
jne .topmost_ok
or [flags],MB_TOPMOST
.topmost_ok:
invoke MessageBox,HWND_DESKTOP,addr message,addr caption,[flags]
invoke EndDialog,[hwnddlg],1
jmp .processed
.wmclose:
invoke EndDialog,[hwnddlg],0
.processed:
mov eax,1
.finish:
pop edi
pop esi
pop ebx
;add esp,4
ret
endp
include 'os_specs/windows/hllstuff/cpp/pattern/x86/cookie.inc'
section '.bss' readable writeable
flags dd ?
___security_cookie dd ?
___security_cookie_complement dd ?
section '.idata' import data readable writeable
library kernel,'KERNEL32.DLL',\
user,'USER32.DLL'
import kernel,\
GetCurrentProcess,'GetCurrentProcess',\
GetCurrentProcessId,'GetCurrentProcessId',\
GetCurrentThreadId,'GetCurrentThreadId',\
GetSystemTimeAsFileTime,'GetSystemTimeAsFileTime',\
GetTickCount,'GetTickCount',\
QueryPerformanceCounter,'QueryPerformanceCounter',\
SetUnhandledExceptionFilter,'SetUnhandledExceptionFilter',\
TerminateProcess,'TerminateProcess',\
UnhandledExceptionFilter,'UnhandledExceptionFilter'
import user,\
DialogBoxParam,'DialogBoxParamA',\
CheckRadioButton,'CheckRadioButton',\
GetDlgItemText,'GetDlgItemTextA',\
IsDlgButtonChecked,'IsDlgButtonChecked',\
MessageBox,'MessageBoxA',\
EndDialog,'EndDialog'
section '.rsrc' resource data readable
directory RT_DIALOG,dialogs
resource dialogs,\
37,LANG_ENGLISH+SUBLANG_DEFAULT,demonstration
dialog demonstration,'Create message box',70,70,190,175,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME
dialogitem 'STATIC','&Caption:',-1,10,10,70,8,WS_VISIBLE
dialogitem 'EDIT','',ID_CAPTION,10,20,170,13,WS_VISIBLE+WS_BORDER+WS_TABSTOP
dialogitem 'STATIC','&Message:',-1,10,40,70,8,WS_VISIBLE
dialogitem 'EDIT','',ID_MESSAGE,10,50,170,13,WS_VISIBLE+WS_BORDER+WS_TABSTOP+ES_AUTOHSCROLL
dialogitem 'BUTTON','&Icon',-1,10,70,80,70,WS_VISIBLE+BS_GROUPBOX
dialogitem 'BUTTON','&Error',ID_ICONERROR,20,82,60,13,WS_VISIBLE+BS_AUTORADIOBUTTON+WS_TABSTOP+WS_GROUP
dialogitem 'BUTTON','I&nformation',ID_ICONINFORMATION,20,95,60,13,WS_VISIBLE+BS_AUTORADIOBUTTON
dialogitem 'BUTTON','&Question',ID_ICONQUESTION,20,108,60,13,WS_VISIBLE+BS_AUTORADIOBUTTON
dialogitem 'BUTTON','&Warning',ID_ICONWARNING,20,121,60,13,WS_VISIBLE+BS_AUTORADIOBUTTON
dialogitem 'BUTTON','&Style',-1,100,70,80,70,WS_VISIBLE+BS_GROUPBOX
dialogitem 'BUTTON','&Top most',ID_TOPMOST,110,82,60,13,WS_VISIBLE+WS_TABSTOP+BS_AUTOCHECKBOX
dialogitem 'BUTTON','OK',IDOK,85,150,45,15,WS_VISIBLE+WS_TABSTOP+BS_DEFPUSHBUTTON
dialogitem 'BUTTON','C&ancel',IDCANCEL,135,150,45,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON
enddialog
As we see caption is 10h in size, while from dialogitem it could became 40h string which will erase ret address & cookie.
So we will se:
The exeption unknown software exception (0xc0000409) occured in the application at location 0x004010b4.
...
when string from dialogitem will be less than 11h chars will be normal behavior from dialog demo.
_________________ I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
|