make exception by masm

来源:互联网 发布:网上挂号预约软件 编辑:程序博客网 时间:2024/04/30 13:40

前言

自己造几种异常,然后自己拦住,只拦住自己做的异常.
除0异常
访问异常
单步异常
断点异常

知识点

判断异常筛选函数是否执行正确
比较异常发生地址的2种方法

Demo片段

; file hw_helper.asm; brief 工具函数.386.model flat, stdcalloption casemap:noneinclude windows.incinclude hw_macro.asminclude user32.incincludelib User32.libextern g_hWnd:DWORDpublic g_sz_title_okpublic g_sz_title_err; 初始化数据数据段.data.const    g_szExp_EXCEPTION_INT_DIVIDE_BY_ZERO db 'process EXCEPTION_INT_DIVIDE_BY_ZERO', 0    g_szExp_EXCEPTION_FLT_DIVIDE_BY_ZERO db 'process EXCEPTION_FLT_DIVIDE_BY_ZERO', 0    g_szExp_EXCEPTION_ACCESS_VIOLATION db 'process EXCEPTION_ACCESS_VIOLATION', 0    g_szExp_EXCEPTION_SINGLE_STEP db 'process EXCEPTION_SINGLE_STEP', 0    g_szExp_EXCEPTION_BREAKPOINTN db 'process EXCEPTION_BREAKPOINTN', 0    g_szEnter_fnUnhandledExceptionFilter db '>> fnUnhandledExceptionFilter', 0    g_sz_normal_code_stream db 'normal_code_stream', 0 ; 正常的代码流程    g_sz_title_ok db 'ok', 0 ; title-ok    g_sz_title_err db 'error', 0 ; title-err; 代码段    .codefnTest proc x:DWORD, y:DWORD    SAVE_EBP_ESP    SAVE_REGS_NO_EAX    mov eax, x    add eax, y    RESTORE_REGS_NO_EAX    RESTORE_EBP_ESP    return eaxfnTest endpfnUnhandledExceptionFilter proc pExceptionInfo:DWORD    SAVE_REGS_NO_EAX    mov esi, pExceptionInfo    assume esi:ptr EXCEPTION_POINTERS    mov edi, [esi].pExceptionRecord    assume edi:ptr EXCEPTION_RECORD    mov eax, [edi].ExceptionCode    push eax    invoke MessageBox, g_hWnd, offset g_szEnter_fnUnhandledExceptionFilter, offset g_sz_title_err, MB_OK    pop eax@@1:        cmp eax, EXCEPTION_BREAKPOINT    jnz @@2    ; process exp 1    mov edi, [esi].ContextRecord    assume edi:ptr CONTEXT    mov eax, CONTEXT_ALL    mov [edi].ContextFlags, eax    mov eax, [edi].regEip    add eax, 1 ; 0xcc 是一个字节    mov [edi].regEip, eax    invoke MessageBox, g_hWnd, offset g_szExp_EXCEPTION_BREAKPOINTN, offset g_sz_title_ok, MB_OK    mov eax, EXCEPTION_CONTINUE_EXECUTION         jmp @@ret @@2:         cmp eax, EXCEPTION_ACCESS_VIOLATION    jnz @@3    ; process exp 2    mov edi, [esi].ContextRecord    assume edi:ptr CONTEXT    ; only process the exception made by me    .if [edi].regEip == MY_EXCEPTION_ADDR_BEGIN_EXCEPTION_ACCESS_VIOLATION        mov eax, CONTEXT_ALL        mov [edi].ContextFlags, eax        mov eax, [edi].regEip        add eax, 2 ; 现在看到的引起C05的指令是2个字节        mov [edi].regEip, eax        invoke MessageBox, g_hWnd, offset g_szExp_EXCEPTION_ACCESS_VIOLATION, offset g_sz_title_ok, MB_OK        mov eax, EXCEPTION_CONTINUE_EXECUTION    .else        mov eax, EXCEPTION_CONTINUE_SEARCH    .endif                 jmp @@ret @@3:    cmp eax, EXCEPTION_INT_DIVIDE_BY_ZERO    jnz @@4    ; process exp 3    mov edi, [esi].pExceptionRecord    assume edi:ptr EXCEPTION_RECORD    ; only process the exception made by me    .if [edi].ExceptionAddress == MY_EXCEPTION_ADDR_BEGIN_EXCEPTION_INT_DIVIDE_BY_ZERO        mov edi, [esi].ContextRecord        assume edi:ptr CONTEXT        mov eax, CONTEXT_ALL        mov [edi].ContextFlags, eax        mov eax, [edi].regEip        add eax, 2        mov [edi].regEip, eax        invoke MessageBox, g_hWnd, offset g_szExp_EXCEPTION_INT_DIVIDE_BY_ZERO, offset g_sz_title_ok, MB_OK        mov eax, EXCEPTION_CONTINUE_EXECUTION         .else        mov eax, EXCEPTION_CONTINUE_SEARCH    .endif     jmp @@ret @@4:    cmp eax, EXCEPTION_SINGLE_STEP    mov eax, EXCEPTION_EXECUTE_HANDLER         jnz @@ret    ; process exp 4    mov edi, [esi].ContextRecord    assume edi:ptr CONTEXT    mov eax, CONTEXT_ALL    mov [edi].ContextFlags, eax    invoke MessageBox, g_hWnd, offset g_szExp_EXCEPTION_SINGLE_STEP, offset g_sz_title_ok, MB_OK    mov eax, EXCEPTION_CONTINUE_EXECUTION     @@ret:        RESTORE_REGS_NO_EAX    return eaxfnUnhandledExceptionFilter endpfnGenExp1 proc    mov eax, 0ffffffffh    mov ebx, 0ffffffffh MY_EXCEPTION_ADDR_BEGIN_EXCEPTION_ACCESS_VIOLATION::        mov [ebx], eax ; make EXCEPTION_ACCESS_VIOLATION    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    ; make a exception, our don't process        mov eax, 0ffffffffh    mov ebx, 0ffffffffh     mov [ebx], eax ; make EXCEPTION_ACCESS_VIOLATION    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    xor eax, eax    return eaxfnGenExp1 endpfnGenExp2 proc    pushf    pop eax    or eax, 100h    push eax    popf ; make EXCEPTION_SINGLE_STEP    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    xor eax, eax    return eaxfnGenExp2 endpfnGenExp3 proc    xor eax, eaxMY_EXCEPTION_ADDR_BEGIN_EXCEPTION_INT_DIVIDE_BY_ZERO::        div eax ; make EXCEPTION_INT_DIVIDE_BY_ZERO    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    ; make a excepiton, don't process by me    xor eax, eax    div eax ; make EXCEPTION_INT_DIVIDE_BY_ZERO    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    xor eax, eax    return eaxfnGenExp3 endpfnGenExp4 proc    int 3h ; make EXCEPTION_BREAKPOINT    invoke MessageBox, g_hWnd, offset g_sz_normal_code_stream, offset g_sz_title_ok, MB_OK    xor eax, eax    return eaxfnGenExp4 endpEND
.386.model flat,stdcalloption casemap:noneinclude hw.incinclude hw_helper.incinclude hw_macro.asm.const    g_szErrSetUnhandledExceptionFilter db 'err :SetUnhandledExceptionFilter', 0    g_szOkSetUnhandledExceptionFilter db 'ok :SetUnhandledExceptionFilter', 0.codestart:    invoke GetModuleHandle,NULL    mov    g_hInstance,eax    invoke GetCommandLine    mov     g_pszCommandLine,eax    invoke InitCommonControls    invoke WinMain,g_hInstance,NULL,g_pszCommandLine,SW_SHOWDEFAULT    invoke ExitProcess,eaxWinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD    LOCAL   wc:WNDCLASSEX    LOCAL   msg:MSG    mov     wc.cbSize,sizeof WNDCLASSEX    mov     wc.style,CS_HREDRAW or CS_VREDRAW    mov     wc.lpfnWndProc,offset WndProc    mov     wc.cbClsExtra,NULL    mov     wc.cbWndExtra,DLGWINDOWEXTRA    push    hInst    pop     wc.hInstance    mov     wc.hbrBackground,COLOR_BTNFACE+1    mov     wc.lpszMenuName,IDM_MENU    mov     wc.lpszClassName,offset ClassName    invoke LoadIcon,NULL,IDI_APPLICATION    mov     wc.hIcon,eax    mov     wc.hIconSm,eax    invoke LoadCursor,NULL,IDC_ARROW    mov     wc.hCursor,eax    invoke RegisterClassEx,addr wc    invoke CreateDialogParam,g_hInstance,IDD_DIALOG,NULL,addr WndProc,NULL    invoke ShowWindow,g_hWnd,SW_SHOWNORMAL    invoke UpdateWindow,g_hWnd    .while TRUE        invoke GetMessage,addr msg,NULL,0,0      .BREAK .if !eax        invoke TranslateMessage,addr msg        invoke DispatchMessage,addr msg    .endw    mov     eax,msg.wParam    retWinMain endpWndProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM    mov     eax,uMsg    .if eax==WM_INITDIALOG        m2m g_hWnd, hWin        ; do SetUnhandledExceptionFilter        lea eax, fnUnhandledExceptionFilter        invoke SetUnhandledExceptionFilter, eax         lea eax, fnUnhandledExceptionFilter        invoke SetUnhandledExceptionFilter, eax        mov ebx, eax        lea eax, fnUnhandledExceptionFilter        .if eax != ebx            invoke MessageBox, NULL, offset g_szErrSetUnhandledExceptionFilter, offset g_sz_title_err, MB_OK        .else             invoke MessageBox, NULL, offset g_szOkSetUnhandledExceptionFilter, offset g_sz_title_ok, MB_OK        .endif    .elseif eax==WM_COMMAND        LOWORD wParam        .if eax==IDM_FILE_EXIT            invoke SendMessage,hWin,WM_CLOSE,0,0        .elseif eax==IDM_HELP_ABOUT            invoke ShellAbout,hWin,addr AppName,addr AboutMsg,NULL        .elseif eax == IDC_BTN_GEN_EXP1            invoke fnGenExp1        .elseif eax == IDC_BTN_GEN_EXP2            invoke fnGenExp2        .elseif eax == IDC_BTN_GEN_EXP3            invoke fnGenExp3        .elseif eax == IDC_BTN_GEN_EXP4            invoke fnGenExp4        .endif    .elseif eax==WM_CLOSE        invoke DestroyWindow,hWin    .elseif uMsg==WM_DESTROY        invoke PostQuitMessage,NULL    .else        invoke DefWindowProc,hWin,uMsg,wParam,lParam        ret    .endif    xor    eax,eax    retWndProc endpend start
0 0
原创粉丝点击