zoukankan
html css js c++ java
检查一个文件是不是可执行文件(Win32 PE)
Code
Option
Explicit
Private
Declare
Sub
CopyMemory Lib
"
kernel32
"
Alias
"
RtlMoveMemory
"
(Destination
As
Any, Source
As
Any, ByVal Length
As
Long
)
Private
Declare
Function
CreateFile Lib
"
kernel32
"
Alias
"
CreateFileA
"
(ByVal lpFileName
As
String
, ByVal dwDesiredAccess
As
Long
, ByVal dwShareMode
As
Long
, lpSecurityAttributes
As
Any, ByVal dwCreationDisposition
As
Long
, ByVal dwFlagsAndAttributes
As
Long
, ByVal hTemplateFile
As
Long
)
As
Long
Private
Declare
Function
GetFileSize Lib
"
kernel32
"
(ByVal hFile
As
Long
, lpFileSizeHigh
As
Long
)
As
Long
Private
Declare
Function
ReadFile Lib
"
kernel32
"
(ByVal hFile
As
Long
, lpBuffer
As
Any, ByVal nNumberOfBytesToRead
As
Long
, lpNumberOfBytesRead
As
Long
, lpOverlapped
As
Any)
As
Long
Private
Declare
Function
SetFilePointer Lib
"
kernel32
"
(ByVal hFile
As
Long
, ByVal lDistanceToMove
As
Long
, lpDistanceToMoveHigh
As
Long
, ByVal dwMoveMethod
As
Long
)
As
Long
Private
Declare
Function
CloseHandle Lib
"
kernel32
"
(ByVal hObject
As
Long
)
As
Long
'
DOS .EXE头部
Private
Type IMAGE_DOS_HEADER
e_magic
As
Integer
'
魔术字
e_cblp
As
Integer
'
文件最后页的字节数
e_cp
As
Integer
'
文件页数
e_crlc
As
Integer
'
重定义元素个数
e_cparhdr
As
Integer
'
头部尺寸,以段落为单位
e_minalloc
As
Integer
'
所需的最小附加段
e_maxalloc
As
Integer
'
所需的最大附加段
e_ss
As
Integer
'
初始的SS值(相对偏移量)
e_sp
As
Integer
'
初始的SP值
e_csum
As
Integer
'
校验和
e_ip
As
Integer
'
初始的IP值
e_cs
As
Integer
'
初始的CS值(相对偏移量)
e_lfarlc
As
Integer
'
重分配表文件地址
e_ovno
As
Integer
'
覆盖号
e_res(
0
To
3
)
As
Integer
'
保留字
e_oemid
As
Integer
'
OEM标识符(相对e_oeminfo)
e_oeminfo
As
Integer
'
OEM信息
e_res2(
0
To
9
)
As
Integer
'
保留字
e_lfanew
As
Long
'
新exe头部的文件地址
End
Type
Private
Const
GENERIC_READ
=
&
H80000000
Private
Const
FILE_SHARE_READ
=
&
H1
Private
Const
OPEN_EXISTING
As
Long
=
3
Private
Const
FILE_BEGIN
=
0
Private
Const
FILE_CURRENT
=
1
'
函数:检查一个文件是不是可执行文件(Win32 PE)
'
如果是Win32 PE文件,返回 True,否则返回 False
Public
Function
CheckPEFile(ByVal strFileName
As
String
)
As
Boolean
On
Error
Resume
Next
Dim
hFile
As
Long
Dim
lngApiRet
As
Long
Dim
lngRet
As
Long
Dim
ReadBuf(
4
)
As
Byte
hFile
=
CreateFile(strFileName, ByVal (GENERIC_READ
Or
FILE_SHARE_READ),
0
, ByVal
0
, OPEN_EXISTING,
0
, ByVal
0
)
If
hFile
>
0
Then
Dim
PEDosHeader
As
IMAGE_DOS_HEADER
lngApiRet
=
ReadFile(hFile, PEDosHeader, ByVal
Len
(PEDosHeader), lngRet, ByVal
0
)
If
lngApiRet
>
0
And
lngRet
=
64
Then
'
因为有些人喜欢鼓捣些很小的PE文件,那么这里改成:
'
If GetFileSize(hFile, 0) < 68 Then
If
GetFileSize(hFile,
0
)
<
424
Then
'
其实不止吧 呵呵
CloseHandle hFile
Exit
Function
End
If
CopyMemory ReadBuf(
0
), PEDosHeader.e_magic,
2
If
(
Chr
(ReadBuf(
0
))
&
Chr
(ReadBuf(
1
))
=
"
MZ
"
)
Then
lngApiRet
=
SetFilePointer(hFile, PEDosHeader.e_lfanew,
0
, FILE_BEGIN)
If
lngApiRet
>
0
Then
lngApiRet
=
ReadFile(hFile, ReadBuf(
0
),
4
, lngRet, ByVal
0
)
If
lngApiRet
>
0
And
lngRet
=
4
Then
If
(
Chr
(ReadBuf(
0
))
&
Chr
(ReadBuf(
1
))
=
"
PE
"
)
And
(ReadBuf(
2
)
=
0
)
And
(ReadBuf(
3
)
=
0
)
Then
CheckPEFile
=
True
CloseHandle hFile
Exit
Function
End
If
End
If
End
If
End
If
End
If
CloseHandle hFile
End
If
End Function
查看全文
相关阅读:
MFC中实现LISTCRTL控件选中多行进行删除操作
如何使属性值为“只读”(readonly)的EDIT控件在获取焦点后不显示光标?
crm 使用stark组件
ModelForm组件
自定义admin管理工具(stark组件)
Django-admin管理工具
Django-session中间件源码简单分析
基于角色的权限管理
ajax参数补充
datetime模块
原文地址:https://www.cnblogs.com/sysdzw/p/1243785.html
最新文章
unittest用例的执行顺序
unittest参数化
解决问题 mysql_config not found
解决问题 Parallels Desktop虚拟机提示网络适配器不存在
解决问题:Django admin页面样式丢失
unittest笔记
解决问题 xcrun: error: invalid active developer path
CentOS修改yum源为阿里云的源
【转】python 连接 Oracle 乱码问题(cx_Oracle)
Leetcode-Valid Parentheses
热门文章
Leetcode-Length of Last Word
Leetcode-Minimum Depth of Binary Tree
Leetcode-Unique Paths
Leetcode-Rotate Image
Putty配置
常见排序算法(一) MergeSort
Mysql学习(一)文件
Maven配置web项目
Leetcode 算法题--ReverseWordsInString
新人报道,写的东西还请大神们多指导!也希望能让和我一样的同事少走弯路。
Copyright © 2011-2022 走看看