Advertisement
Firex

AutoIt SEH - beta

Feb 10th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.81 KB | None | 0 0
  1. #include-once
  2.  
  3. ; #INDEX# =======================================================================================================================
  4. ; Title .........: AutoIt SEH
  5. ; AutoIt Version : 3.3.12.0 (x86)
  6. ; Description ...: Enables compiled script to gain control when events that normally terminate program execution occur.
  7. ; Version .......: 0.3.3 (beta)
  8. ; Author(s) .....: Firex
  9. ; Dll(s) ........: Kernel32.dll
  10. ; ===============================================================================================================================
  11.  
  12. ; #CURRENT# =====================================================================================================================
  13. ; __TRY
  14. ; __ENDTRY
  15. ; ===============================================================================================================================
  16.  
  17. ; #VARIABLES# ===================================================================================================================
  18. Global $__Au3SehCb[2]
  19. Global $__Au3SehBak[9]
  20. Global $SEH_INCLUDES_LENGTH
  21. ; ===============================================================================================================================
  22.  
  23. ; #EXAMPLE# =====================================================================================================================
  24. ;~#Include <SEH.au3>
  25. ;~
  26. ;~__TRY() ;Default: "__EXCEPT"
  27. ;~  MsgBox( 0, 'SEH - Example', 'Init' )
  28. ;~  For $Idx = 1 To 5 Step 1
  29. ;~      If Random( 0, 1, 1 ) Then
  30. ;~          $PlsError[1] = $PlsError[2]
  31. ;~      Else
  32. ;~          Local $PlsError[-1] = [[]] ;goto not work for this type of error, i will fix it
  33. ;~      EndIf
  34. ;~  Next
  35. ;~  If 1 == 0 Then
  36. ;~      MsgBox( 0, 'SEH - Example', 'Goto?' ) ;11 line
  37. ;~  EndIf
  38. ;~__ENDTRY()
  39. ;~
  40. ;~Func __EXCEPT( $pwErr, $pwScriptLine, $iLine )
  41. ;~  MsgBox( 0, '', _
  42. ;~      '$iLine=' & $iLine - $SEH_INCLUDES_LENGTH  & @LF & _
  43. ;~      '$pScriptLine=' & __wCharArray( $pwScriptLine ) & @LF & _
  44. ;~      '$pErrMsg=' & __wCharArray( $pwErr ) )
  45. ;~  ; ---
  46. ;~  Return $SEH_INCLUDES_LENGTH + 11 ;[goto 11 line]
  47. ;~  ;Change to:
  48. ;~      ;RET==0 : [ignore]
  49. ;~      ;RET<<0 : [terminate]
  50. ;~      ;RET>>0 : [goto_line]
  51. ;~EndFunc
  52. ;~
  53. ;~Func __wCharArray( $pwStr )
  54. ;~  Local $aRes, $tStr
  55. ;~  ; ---
  56. ;~  $aRes = DllCall("kernel32.dll", "int", "lstrlenW", "ptr", $pwStr)
  57. ;~  If @Error Or Not $aRes[0] Then _
  58. ;~      Return ''
  59. ;~
  60. ;~  $tStr = DllStructCreate( "wchar[" & $aRes[0] & "]", $pwStr )
  61. ;~  If @Error Then _
  62. ;~      Return ''
  63. ;~  ; ---
  64. ;~  Return DllStructGetData( $tStr, 1 )
  65. ;~EndFunc
  66. ; ===============================================================================================================================
  67.  
  68.  
  69. ; #FUNCTION# ====================================================================================================================
  70. ; Author ........: Firex
  71. ; Modified.......:
  72. ; ===============================================================================================================================
  73. Func __TRY( $sFunc = '__EXCEPT' )
  74.     If Not IsDeclared( "__Au3SehCb" ) Then _
  75.         Global $__Au3SehCb[2]
  76.  
  77.     If ( Not $__Au3SehCb[0] ) <> ( $sFunc <> '' ) Then _
  78.         Return SetError( -1 )
  79.     ; *
  80.     Local $aLib[2][9][3] = [ [ [1] ], _ ;* - ptr32
  81.         [ _
  82.             [ '0_3.3.12.0', 0x0004CD69, 0x0004CDD5 ], _
  83.             [ '#A_', '0BA2FBFF', '927FFBFF' ], _ ;__GetScriptLineByNum
  84.             [ 0x00, '0x743C' ], _
  85.             [ 0x20, '0xEB1C' ], _
  86.             [ 0x1E, '0x8B97F400000052E8#A_5250FF75BCB8*FFD083F8007C11744189EA8B12837A0C0075F8894208EB3231C04080BF1C010000008987FC00000074088B45080500F0FF7F8D4DDCE9C8FDFFFF' ] _
  87.         ] _
  88.     ], _
  89.     $Ver, $Vkp, $Const, $sDyn, $aRes, $hModule, $pOffset, $bVkp_Cur, $iVkp_Sz, $tPatch
  90.     ; ---
  91.     $aRes = DllCall( "kernel32.dll", "handle", "GetModuleHandle", "ptr", 0 )
  92.     If @Error Or Not $aRes[0] Then Return SetError( -2 )
  93.         $hModule = $aRes[0]
  94.  
  95.     For $Ver = 1 To $aLib[0][0][0] Step 1
  96.         If ( @AutoItX64 & "_" & @AutoItVersion ) <> $aLib[$Ver][0][0] Then _
  97.             ContinueLoop
  98.         ; *
  99.         $pOffset = $hModule + Ptr( $aLib[$Ver][0][1+@Compiled] )
  100.         If Not $__Au3SehCb[0] Then
  101.             $__Au3SehCb[0] = DllCallbackRegister( $sFunc, 'int', 'ptr;ptr;ulong' )
  102.             If Not $__Au3SehCb[0] Then _
  103.                 Return SetError( -3 )
  104.             ; -
  105.             $__Au3SehCb[1] = Hex( Binary( DllCallbackGetPtr( $__Au3SehCb[0] ) ) )
  106.             $__Au3SehCb[1] = StringLeft( $__Au3SehCb[1], 8 ) ;Fix for x64 (qdword=4 byte)
  107.             If Not IsDeclared( '__Au3SehBak' ) Then _
  108.                 Global $__Au3SehBak[9]
  109.         Else
  110.             $__Au3SehCb[1] = 0
  111.         EndIf
  112.         For $Vkp = 1 To 8 Step 1
  113.             $sDyn = $aLib[$Ver][$Vkp][0]
  114.             Select ;Preproc vkp
  115.                 Case $sDyn == ''
  116.                     If Not $__Au3SehCb[1] Then
  117.                         DllCallbackFree( $__Au3SehCb[0] )
  118.                         $__Au3SehCb[0] = 0
  119.                     EndIf
  120.                     ExitLoop
  121.                 Case IsString( $sDyn )
  122.                     For $Const = $Vkp To 8 Step 1
  123.                         If $aLib[$Ver][$Const][1] == '' Then _
  124.                             ExitLoop
  125.                         ; -
  126.                         $aLib[$Ver][$Const][1] = StringReplace( $aLib[$Ver][$Const][1], $sDyn, $aLib[$Ver][$Vkp][1+@Compiled] )
  127.                     Next
  128.                     ContinueLoop
  129.             EndSelect
  130.             ; -
  131.             $pOffset += Ptr( $sDyn )
  132.             $bVkp_Cur = $aLib[$Ver][$Vkp][1]
  133.             $iVkp_Sz = BinaryLen( StringReplace( $bVkp_Cur, '*', '00000000' ) )
  134.  
  135.             ; < Replace opcode region
  136.             $aRes = DllCall( "kernel32.dll", "bool", "VirtualProtect", "ptr", $pOffset, "ulong", $iVkp_Sz, "dword", 0x40, "dword*", "" )
  137.             If @Error Or Not $aRes[0] Then _
  138.                 ExitLoop 2
  139.  
  140.             $tPatch = DllStructCreate( "byte Code[" & $iVkp_Sz & "]", $pOffset )
  141.             If $__Au3SehCb[1] Then
  142.                 If Not $__Au3SehBak[$Vkp] Then _
  143.                     $__Au3SehBak[$Vkp] = DllStructGetData( $tPatch, 'Code' ) ;Save opcode
  144.                 ; -
  145.                 DllStructSetData( $tPatch, 'Code', StringReplace( $bVkp_Cur, '*', $__Au3SehCb[1] ) )
  146.             Else
  147.                 DllStructSetData( $tPatch, 'Code', $__Au3SehBak[$Vkp] ) ;Restore opcode
  148.             EndIf
  149.             DllCall( "kernel32.dll", "bool", "VirtualProtect", "ptr", $pOffset, "ulong", $iVkp_Sz, "dword", $aRes[4], "ptr", 0 )
  150.             ; < Replace opcode endregion
  151.         Next
  152.         ; -
  153.         Return True
  154.     Next
  155.     ; ---
  156.     Return False
  157. EndFunc
  158.  
  159. ; #FUNCTION# ====================================================================================================================
  160. ; Author ........: Firex
  161. ; Modified.......:
  162. ; ===============================================================================================================================
  163. Func __ENDTRY()
  164.     Return __TRY('')
  165. EndFunc
  166.  
  167.  
  168. ; #INTERLAN_USE_ONLY# ====================================================================================================================
  169. ; Description ...: Calculate real line number
  170. ; ========================================================================================================================================
  171. Func __SEH_InternalCb( $pwErr, $pwScriptLine, $iLine )
  172.     If Not IsDeclared( 'SEH_INCLUDES_LENGTH' ) Then _
  173.         Global $SEH_INCLUDES_LENGTH
  174.  
  175.     $SEH_INCLUDES_LENGTH = $iLine + 4
  176.     Return 0
  177. EndFunc
  178.  
  179. Func __SEH_GetIncludesLength()
  180.     If __TRY( '__SEH_InternalCb' ) Then
  181.         SEH_GetError
  182.  
  183.         __ENDTRY()
  184.     EndIf
  185. EndFunc
  186.  
  187. __SEH_GetIncludesLength()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement