home *** CD-ROM | disk | FTP | other *** search
/ Cracking 2 / Cracking II..iso / Texty / Structured Exception Handling (Seh) / SEH.inc
Encoding:
Text File  |  1998-11-16  |  11.5 KB  |  317 lines

  1. ;*******************************************************************************
  2.  
  3.  
  4. ;this file contains information, constants and structures needed for SEH coding
  5. ;it's designed for Borland Turbo Assembler (v4.0+, MASM mode)
  6.  
  7. ;Usage: INCLUDE SEH.inc
  8.  
  9.  
  10. ;----------------------------------documented-----------------------------------
  11.  
  12. _CONTEXT             STRUC
  13. cx_ContextFlags         DD ?
  14.  
  15. ;CONTEXT_DEBUG_REGISTERS
  16. cx_Dr0                DD ?         ;04
  17. cx_Dr1                DD ?         ;08
  18. cx_Dr2                DD ?         ;0C
  19. cx_Dr3                DD ?         ;10
  20. cx_Dr6                DD ?         ;14
  21. cx_Dr7                DD ?         ;18
  22.  
  23. ;CONTEXT_FLOATING_POINT
  24. cx_ControlWord            DD ?
  25. cx_StatusWord            DD ?
  26. cx_TagWord            DD ?
  27. cx_ErrorOffset            DD ?
  28. cx_ErrorSelector        DD ?
  29. cx_DataOffset            DD ?
  30. cx_DataSelector         DD ?
  31. SIZE_OF_80387_REGISTERS         EQU 80
  32. cx_RegisterArea         DB SIZE_OF_80387_REGISTERS DUP (?)
  33. cx_Cr0NpxState            DD ?
  34.  
  35. ;CONTEXT_SEGMENTS
  36. cx_SegGs            DD ?         ;8C
  37. cx_SegFs            DD ?         ;90
  38. cx_SegEs            DD ?         ;94
  39. cx_SegDs            DD ?         ;98
  40.  
  41. ;CONTEXT_INTEGER
  42. cx_Edi                DD ?         ;9C
  43. cx_Esi                DD ?         ;A0
  44. cx_Ebx                DD ?         ;A4
  45. cx_Edx                DD ?         ;A8
  46. cx_Ecx                DD ?         ;AC
  47. cx_Eax                DD ?         ;B0
  48.  
  49. ;CONTEXT_CONTROL
  50. cx_Ebp                DD ?         ;B4
  51. cx_Eip                DD ?         ;B8
  52. cx_SegCs            DD ?         ;BC
  53. cx_EFlags            DD ?         ;C0
  54. cx_Esp                DD ?         ;C4
  55. cx_SegSs            DD ?         ;C8
  56. _CONTEXT             ENDS
  57. ;size of CONTEXT is 0CCH bytes
  58.  
  59.  
  60. ;---cx_ContextFlags { used by _VWIN32_Set_Thread_Context(95) or NTCALL 13H(NT) }
  61. CONTEXT_i386            EQU 000010000H
  62. CONTEXT_i486            EQU 000010000H
  63. CONTEXT_CONTROL         EQU CONTEXT_i386+00000001H
  64. CONTEXT_INTEGER         EQU CONTEXT_i386+00000002H
  65. CONTEXT_SEGMENTS        EQU CONTEXT_i386+00000004H
  66. CONTEXT_FLOATING_POINT        EQU CONTEXT_i386+00000008H
  67. CONTEXT_DEBUG_REGISTERS     EQU CONTEXT_i386+00000010H
  68.  
  69. ;there is a bug (+ instead of OR) in W32Main.inc (from Walk32 package) in EQU
  70. ;for CONTEXT_FULL, so better (when you don't need to update debug registers
  71. ;and FPU) use
  72. CONTEXT_NORMAL                  EQU CONTEXT_CONTROL OR CONTEXT_INTEGER OR \
  73.                                     CONTEXT_SEGMENTS
  74.  
  75. CONTEXT_ALL                     EQU CONTEXT_CONTROL OR CONTEXT_INTEGER OR \
  76.                                     CONTEXT_SEGMENTS OR CONTEXT_FLOATING_POINT \
  77.                                     OR CONTEXT_DEBUG_REGISTERS  
  78. ;note that CONTEXT_ALL (01001FH) is in cx_ContextFlags by default!
  79.  
  80. ;-------------------------------------------------------------------------------
  81.  
  82. _EXCEPTION_POINTERS             STRUC ;parameter of top-level exception handler
  83.         ExceptionRecord         DD ?  ;pointer to _EXCEPTION_RECORD
  84.         ContextRecord           DD ?  ;pointer to _CONTEXT
  85. _EXCEPTION_POINTERS             ENDS
  86. ;size of _EXCEPTION_POINTERS is 008H bytes
  87.  
  88.  
  89. _EXCEPTION_RECORD        STRUC
  90.     ExceptionCode        DD ?
  91.     ExceptionFlags        DD ?             
  92.     NextExceptionRecord    DD ?  ;pointer (for nested exceptions)
  93.     ExceptionAddress    DD ?
  94.     NumberParameters    DD ?
  95.     EXCEPTION_MAXIMUM_PARAMETERS  EQU 15     
  96.     ExceptionInformation    DD EXCEPTION_MAXIMUM_PARAMETERS DUP (?)
  97. _EXCEPTION_RECORD        ENDS
  98. ;size of _EXCEPTION_RECORD is 014H..050H bytes
  99.  
  100. ;---ExceptionInformation for EXCEPTION_ACCESS_VIOLATION
  101. ;NumberParameters = 2
  102. ;ExceptionInformation[0] = ACCESS_VIOLATION_READ or ACCESS_VIOLATION_WRITE
  103. ;ExceptionInformation[1] = virtual address of inaccessible data
  104. ;                          (Win95 usually = -1)  
  105.  ACCESS_VIOLATION_READ             EQU 000000000H  ; for CMP instruction
  106.  ACCESS_VIOLATION_WRITE            EQU 000000001H
  107. ;of course it's true for NT only
  108. ;in 95 is all READ ! :(
  109.  
  110. ;---ExceptionInformation passed by RaiseException function
  111. ;it's up to you 
  112.  
  113. ;RaisedExceptionCode has bit 28 set to 0 by system, hence
  114. RaisedExceptionCodeMask            EQU 0EFFFFFFFH  ; for AND instruction
  115. ;of course it's true for NT only
  116. ;in 95 is not ExceptionCode changed ! :(
  117.  
  118. ;other exceptions have some "information" too, but you can't believe it
  119. ;(for example breakpoint(in 95) has 1 parameter (usually), while
  120. ; breakpoint(in NT) has 3 parameters; they contain trash)
  121.  
  122.  
  123. ;---ExceptionFlags   for TEST, AND  or CMP instructions
  124. EXCEPTION_CONTINUABLE              EQU 000000000H
  125. EXCEPTION_NONCONTINUABLE           EQU 000000001H
  126. UNWIND_STACK                       EQU 000000006H   ; ? 
  127.  
  128. ;---ExceptionCodes   for CMP  instruction
  129. EXCEPTION_WAIT_0           EQU 000000000H
  130. EXCEPTION_ABANDONED_WAIT_0        EQU 000000080H
  131. EXCEPTION_USER_APC            EQU 0000000C0H
  132. EXCEPTION_TIMEOUT           EQU 000000102H
  133. EXCEPTION_PENDING           EQU 000000103H
  134. EXCEPTION_SEGMENT_NOTIFICATION       EQU 040000005H
  135. EXCEPTION_GUARD_PAGE_VIOLATION       EQU 080000001H
  136. EXCEPTION_DATATYPE_MISALIGNMENT       EQU 080000002H
  137. EXCEPTION_BREAKPOINT           EQU 080000003H   ; exception 3
  138. EXCEPTION_SINGLE_STEP           EQU 080000004H   ; exception 1
  139. EXCEPTION_ACCESS_VIOLATION        EQU 0C0000005H   ; typically exception 13
  140. EXCEPTION_IN_PAGE_ERROR           EQU 0C0000006H
  141. EXCEPTION_NO_MEMORY           EQU 0C0000017H
  142. EXCEPTION_ILLEGAL_INSTRUCTION       EQU 0C000001DH
  143. EXCEPTION_NONCONTINUABLE_EXCEPTION EQU 0C0000025H
  144. EXCEPTION_INVALID_DISPOSITION       EQU 0C0000026H
  145. EXCEPTION_ARRAY_BOUNDS_EXCEEDED       EQU 0C000008CH   ; exception 5
  146. EXCEPTION_FLOAT_DENORMAL_OPERAND   EQU 0C000008DH   
  147. EXCEPTION_FLT_DENORMAL_OPERAND     EQU 0C000008DH   
  148. EXCEPTION_FLOAT_DIVIDE_BY_ZERO       EQU 0C000008EH
  149. EXCEPTION_FLT_DIVIDE_BY_ZERO       EQU 0C000008EH
  150. EXCEPTION_FLOAT_INEXACT_RESULT       EQU 0C000008FH
  151. EXCEPTION_FLT_INEXACT_RESULT       EQU 0C000008FH
  152. EXCEPTION_FLOAT_INVALID_OPERATION  EQU 0C0000090H
  153. EXCEPTION_FLT_INVALID_OPERATION    EQU 0C0000090H
  154. EXCEPTION_FLOAT_OVERFLOW           EQU 0C0000091H
  155. EXCEPTION_FLT_OVERFLOW             EQU 0C0000091H
  156. EXCEPTION_FLOAT_STACK_CHECK       EQU 0C0000092H
  157. EXCEPTION_FLT_STACK_CHECK       EQU 0C0000092H
  158. EXCEPTION_FLOAT_UNDERFLOW       EQU 0C0000093H
  159. EXCEPTION_FLT_UNDERFLOW               EQU 0C0000093H
  160. EXCEPTION_INTEGER_DIVIDE_BY_ZERO   EQU 0C0000094H   ; exception 0
  161. EXCEPTION_INT_DIVIDE_BY_ZERO       EQU 0C0000094H   
  162. EXCEPTION_INTEGER_OVERFLOW        EQU 0C0000095H   ; exception 4
  163. EXCEPTION_INT_OVERFLOW                EQU 0C0000095H   
  164. EXCEPTION_PRIVILEGED_INSTRUCTION   EQU 0C0000096H   ; typically exception 13
  165. EXCEPTION_PRIV_INSTRUCTION         EQU 0C0000096H   
  166. EXCEPTION_STACK_OVERFLOW       EQU 0C00000FDH
  167. EXCEPTION_CONTROL_C_EXIT       EQU 0C000013AH
  168.  
  169. ; there are many other STATUSes (watch code in NTice)
  170.  
  171.  
  172. ;---SEH API functions
  173. ; AbnormalTermination              ;doesn't exist, interpreted as C++ keyword
  174. ; GetExceptionCode                 ;doesn't exist, interpreted as C++ keyword
  175. ; GetExceptionInformation          ;doesn't exist, interpreted as C++ keyword
  176. ; UnhandledExceptionFilter         ;exists; called from kernel xFrame.filter
  177.  
  178. EXTRN STDCALL RaiseException:PROC                   ; has 4 parameters:
  179. ; 1st parameter is ExceptionCode   ;see _EXCEPTION_RECORD
  180. ; 2nd parameter is ExceptionFlags
  181. ; 3rd parameter is NumberParameters
  182. ; 4th parameter points to ExceptionInformation 
  183.  
  184. EXTRN STDCALL SetUnhandledExceptionFilter:PROC      ; has 1 parameter:
  185. ; 1st parameter points to top-level exception handler
  186.  
  187. ; additional:
  188. EXTRN STDCALL SetErrorMode:PROC                     ; has 1 parameter:
  189. SEM_FAILCRITICALERRORS             EQU 000000001H
  190. SEM_NOGPFAULTERRORBOX              EQU 000000002H   ; quiet exit vs. ErrorBox
  191. SEM_NOALIGNMENTFAULTEXCEPT         EQU 000000004H   ; no effect on x86
  192. SEM_NOOPENFILEERRORBOX             EQU 080000000H
  193. ; default ErrorMode is 0 
  194.  
  195. ;---return codes for top-level exception handler   (EAX)
  196. EXCEPTION_CONTINUE_EXECUTION       EQU -1
  197. EXCEPTION_CONTINUE_SEARCH          EQU  0 
  198. EXCEPTION_EXECUTE_HANDLER          EQU  1 
  199.  
  200. ;---return codes for try-except exception handler  (EAX)
  201. ExceptionContinueExecution         EQU  0
  202. ExceptionContinueSearch            EQU  1
  203. ExceptionNestedException           EQU  2
  204. ExceptionCollidedUnwind            EQU  3
  205.  
  206.  
  207.  
  208.  
  209. ;----------------------------------my research----------------------------------
  210.  
  211. _xFrame_RECORD            STRUC 
  212.        NextxFrame        DD ?  ; pointer or -1 for last member
  213.        xHandler         DD ?  ; pointer to code
  214.        xTable            DD ?  ; pointer to xTableArray
  215.        xScope            DD ?  ; 1st index of _xTable_RECORD or -1 for end
  216. _xFrame_RECORD            ENDS
  217. ; size of _xFrame_RECORD = 010H bytes
  218.  
  219. ; xTableArray            _xTable_RECORD  MAX_xTable_RECORDs  DUP (?)
  220.  
  221. _xTable_RECORD            STRUC
  222.        NextxScope        DD ?  ; index of next xTable_RECORD or -1 for end
  223.        filter            DD ?  ; pointer to code
  224.        handler            DD ?  ; pointer to code
  225. _xTable_RECORD            ENDS
  226. ; size of _xTable_RECORD = 00CH bytes
  227.  
  228. ; xTable, xScope and _xTable_RECORD are used by kernel try-except exception
  229. ; handler only (or by C startup codes)
  230.  
  231. ; for new thread is constructed NEW kernel xFrame with xScope=0; 
  232. ; xTable_RECORD's filter  calls UnhandledExceptionFilter and  handler 
  233. ; calls ExitProcess or ExitThread
  234.  
  235. ;---constants for xScope, NextxScope and NextxFrame
  236. ;searching for filter function stops when xScope or NextxScope has this value:
  237. xScopeEnd                       EQU -1
  238. ; when xTable points to nonexisting region (i.e. xTable = -2)
  239. ; softice's xframe says : (local unwind) 
  240.  
  241. ;searching for xFrame stops when NextxFrame has this value:
  242. xFrameEnd                       EQU -1
  243.  
  244. ;---pointer to the 1st xFrame_RECORD
  245. xFrameHead                      EQU DWORD PTR FS:0
  246.  
  247.  
  248. ;---procedure types
  249. ; try-except exception handler  (=xHandler)
  250.   xHandler_PROC  PROCTYPE  C    :DWORD, :DWORD, :DWORD, :DWORD
  251. ; 1st parameter points to _EXCEPTION_RECORD
  252. ; 2nd parameter points to _xFrame_RECORD (belonging to this xHandler)
  253. ; 3rd parameter points to _CONTEXT
  254. ; 4th parameter points to _DISPATCHER_CONTEXT (various content, unusable on x86) 
  255.  
  256. ; top-level exception handler  (called by UnhandledExceptionFilter)
  257.   TopLevelHandler_PROC  PROCTYPE  STDCALL  :DWORD
  258. ; 1st parameter points to _EXCEPTION_POINTERS
  259.  
  260. COMMENT $
  261. Comparison of try-except and top-level handlers
  262.  
  263.            Thing              try-except                top-level
  264. Number of parameters              4                         1
  265. Calling convention              C (ret)               STDCALL (ret 4)
  266. API dependent                     no                       yes
  267. Relationship                    father                     son
  268. Must preserve regs         EBP, EBX, ESI, EDI            EBP, ESI
  269. Called from                     kernel            UnhandledExceptionFilter
  270. Where can be found        everywhere in system,         sometimes
  271.                           in C startup codes..           in apps
  272.  
  273. In C++ startup codes is used copy of kernel xHandler to provide keywords:
  274. try, except, finally, GetExceptionCode, GetExceptionInformation,
  275. AbnormalTermination,..
  276. $
  277.  
  278. ;---lame macros
  279. Inst_xFrame   MACRO ___PxHndl, ___PxTbl, ___PxSc
  280.     PUSH ___PxSc OFFSET ___PxTbl OFFSET ___PxHndl xFrameHead
  281.     MOV  xFrameHead, ESP
  282.               ENDM
  283.  
  284. Rest_xFrame   MACRO
  285.     POP  xFrameHead
  286.     ADD  ESP, 4*3
  287.               ENDM
  288.  
  289. Inst_TopLevel MACRO ___PTLhandler, ___OldTLhandler
  290.     CALL SetUnhandledExceptionFilter, OFFSET ___PTLhandler
  291.     MOV  DWORD PTR ___OldTLhandler, EAX
  292.               ENDM
  293.  
  294. Rest_TopLevel MACRO ___PTLhandler, ___OldTLhandler
  295.     CALL SetUnhandledExceptionFilter, DWORD PTR ___OldTLhandler
  296.               ENDM
  297.  
  298. BeginxHandler MACRO ___Nomen, __P1, __P2, __P3, __P4
  299.     ___Nomen  PROC  C __P1, __P2, __P3, __P4
  300.               USES  EBX, ESI, EDI
  301.               ENDM
  302.  
  303. EndxHandler   MACRO ___Nomen
  304.     ___Nomen  ENDP
  305.               ENDM
  306.  
  307. BeginTopLevelHandler MACRO ___Nomen, __P1
  308.     ___Nomen  PROC   STDCALL __P1
  309.               USES   ESI
  310.               ENDM
  311.  
  312. EndTopLevelHandler   MACRO ___Nomen
  313.     ___Nomen  ENDP
  314.               ENDM
  315.  
  316. ;*******************************************************************************
  317.