判斷是否為PE檔案改進版

來源:互聯網
上載者:User

原來的程式一遇到0位元組的檔案就會掛掉,在這裡我添加了SEH錯誤處理代碼,完美解決了掛掉的問題!

 

.386
.model flat, stdcall
option casemap :none 
include    windows.inc
include    user32.inc
includelib  user32.lib
include    kernel32.inc
includelib  kernel32.lib

.data?
hFile    dd  ?
hMapFile  dd  ?
lpFile  dd  ?

.const
szErr    db  "不是有效32位程式!",0
szOK    db  "是可執行檔!",0
szNO    db  "開啟檔案失敗!",0
szName  db  "d: .exe",0
Copyright  db  " www.xbin.cn ",0

.code
_SEH  proc  _lpExceptionRecord,_lpSEH,_lpContext,_lpDispatcherContext
    pushad
    mov  esi,_lpExceptionRecord
    mov  edi,_lpContext
    assume  esi:ptr EXCEPTION_RECORD,edi:ptr CONTEXT
    mov  eax,_lpSEH
    push  [eax + 0ch]
    pop  [edi].regEbp
    push  [eax + 8]
    pop  [edi].regEip
    push  eax
    pop  [edi].regEsp
    assume  esi:nothing,edi:nothing
    popad
    mov  eax,ExceptionContinueExecution
    ret
_SEH  endp

Start:
;設定SEH
assume  fs:nothing
push  offset _ErrFormat
push  offset _SEH
push  fs:[0]
mov  fs:[0],esp
;開啟檔案
invoke CreateFile,offset szName,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
.if eax == INVALID_HANDLE_VALUE
  invoke MessageBox,NULL,offset szNO,NULL,MB_OK
  JMP _END
.endif
mov hFile,eax
;建立對應檔
invoke CreateFileMapping,hFile,NULL,PAGE_READONLY,0,0,NULL
mov hMapFile,eax
invoke MapViewOfFile,hMapFile,FILE_MAP_READ,0,0,0
mov lpFile,eax
;把對應檔的首地址給ESI
mov esi,eax
assume esi:ptr IMAGE_DOS_HEADER
;判斷MZ標誌
mov di,[esi].e_magic
mov bx,5a4dh
.if di != bx
  invoke MessageBox,NULL,offset szErr,NULL,MB_OK
  JMP _END
.endif
;判斷PE標誌
add esi,[esi].e_lfanew
assume esi:ptr IMAGE_NT_HEADERS
mov edi,[esi].Signature
.if edi != 00004550h
  invoke MessageBox,NULL,offset szErr,NULL,MB_OK
  JMP _END
.endif

invoke MessageBox,NULL,offset szOK,NULL,MB_OK
assume esi:nothing
JMP _END
_ErrFormat:
invoke MessageBox,NULL,offset szErr,NULL,MB_OK
pop  fs:[0]
add  esp,0ch
_END:
invoke UnmapViewOfFile,lpFile
invoke CloseHandle,hMapFile
invoke CloseHandle,hFile
invoke ExitProcess,0
end Start

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.