判断是否为PE文件改进版

原来的程序一遇到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 " xbin1981",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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值