Skip to content

Instantly share code, notes, and snippets.

@tahadraidia
Last active December 23, 2021 11:39
Show Gist options
  • Save tahadraidia/e95d104ba54b20f3b2ff17a381268bcd to your computer and use it in GitHub Desktop.
Save tahadraidia/e95d104ba54b20f3b2ff17a381268bcd to your computer and use it in GitHub Desktop.
Tiny ASM program that access ExceptionList and print some pointers out as educational purpose, with the idea to understand better SEH implementation in X86. This code was written along with this blog post: https://tahadraidia.com/posts/a_brief_dive_into_windows_structured_exception_handeling/
format PE console 4.0
include 'win32ax.inc'
section '.text' code readable executable
entry start
macro PrintPointer reg,string
{
xor eax,eax
mov dword eax,string
cinvoke printf,eax,reg
}
start:
xor edx,edx
xor ebx,ebx
xor ecx,ecx
mov ecx,[fs:ecx] ; ecx holds ExceptionList
push ecx ; save it for later
PrintPointer ecx,exception_address_string
;mov ebx,[ecx] ; ebx now holds Next (execption_registration_record) pointer
pop ebx ; fetch ExceptionList address from stack.
mov ebx, [ebx] ; Dereference the pointer (_Exception_Registration_Record struct) Next.
PrintPointer ebx,exception_record ; ebx contains the value of Next.
mov ebx,[ebx+0x4] ; ebx now ecx _exception_hander pointer (Handler)
PrintPointer ebx,exception_handler
xor ecx,ecx
mov ecx,[fs:0x4 ] ; ecx now holds StackBase
PrintPointer ecx,stackbase_string
mov ecx,[fs:0x8 ] ; ecx now holds StackLimit
PrintPointer ecx,stacklimit_string
jmp finish
finish:
invoke TerminateProcess, 0xffffffff,0x00
section '.data' data readable writable
exception_address_string db "ExceptionList: %p",10,0
exception_record db "Exception Record: %p",10,0
exception_handler db "Exception Handler: %p",10,0
stackbase_string db "StackBase: %p",10,0
stacklimit_string db "StackLimit: %p",10,0
section '.idata' import data readable writeable
library kernel32, 'KERNEL32.DLL',\
msvcrt, 'msvcrt.dll'
import msvcrt,\
printf, 'printf'
include 'api\kernel32.inc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment