判断是否为PE文件改进版
2016-06-07 22:30
260 查看
原来的程序一遇到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
.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
相关文章推荐
- Swift和Java关于字符串和字符的比较
- 将连接MySQL数据库的java项目程序打包成为.jar格式
- SQL case when
- 3、用私有构造器或者枚举类型强化SIngleton属性
- QSignalMapper简介
- MySQL索引简单总结
- Android N 对Doze(打盹)模式优化
- 219.[Leetcode]Contains Duplicate II
- 用一行简单的代码就能加速IE运行脚本的速度
- 第15周阅读程序(1)
- 使用spring mvc前台获取后台返回的List
- 设计模式——策略模式
- HDU 1426 - Sudoku Killer
- CodeForces 26D Tickets(概率)
- OpenWRT上判断客户端在线个数
- mybatis的动态sql之if test用法
- UVALive 2889 (数学)
- java应用cpu过高
- javaweb学习总结(四十四)——监听器(Listener)学习
- 94. Binary Tree Inorder Traversal