如何在Windows上检查一个二进制程序是32位还是64位?
有什么简单的方法可以在Windows上检查一个二进制程序是32位还是64位?我需要先检查一下,然后再把程序移到32位的机器上,遇到壮观的故障。
有什么简单的方法可以在Windows上检查一个二进制程序是32位还是64位?我需要先检查一下,然后再把程序移到32位的机器上,遇到壮观的故障。
在研究了Richard的回答的标题值后,我想出了一个快速、简单、只需要一个文本编辑器就能解决的方案。即使是Windows的默认的记事本.exe也可以使用。在文本编辑器中打开可执行文件。你可能需要拖放或使用编辑器的Open...
对话框,因为Windows在上下文菜单中不显示Open with...
选项。检查第一次出现PE
之后的第一个可打印字符。这部分很可能至少被一些白格包围(可能是很多),所以很容易直观地看到。
这里是你会发现:
PE L
PE d†
如果你需要在你无法安装任何额外软件的机器上检查一个文件,这个解决方案可能会很有用。签名是0x3C
(字母 “P "和 "E "后面是两个空字节),后面是两个字节的小尾巴机器类型。还有很多可能的值,但你可能永远不会遇到这些值,也无法在你的Windows PC上运行这样的可执行文件。
完整的机器类型列表,以及其余的.exe规范,可以在Microsoft PE和COFF规范 机器类型部分找到。
SDK工具dumpbin.exe
的/headers
选项中包含了这些信息,对比一下这两个(关键信息我加粗了)
PS [64] E:\ #4\> dumpbin /headers C:\Windows\system32\cmd.exe Microsoft (R) COFF/PE Dumper Version 10.00.40219.01 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file C:\Windows\system32\cmd.exe PE signature found File Type: EXECUTABLE IMAGE FILE HEADER VALUES **8664 machine (x64)** 6 number of sections 4CE798E5 time date stamp Sat Nov 20 09:46:13 2010 0 file pointer to symbol table 0 number of symbols F0 size of optional header 22 characteristics Executable Application can handle large (\>2GB) addresses [...]
和
PS [64] E:\ #5\> dumpbin /headers C:\Windows\syswow64\cmd.exe Microsoft (R) COFF/PE Dumper Version 10.00.40219.01 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file C:\Windows\syswow64\cmd.exe PE signature found File Type: EXECUTABLE IMAGE FILE HEADER VALUES **14C machine (x86)** 4 number of sections 4CE78E2B time date stamp Sat Nov 20 09:00:27 2010 0 file pointer to symbol table 0 number of symbols E0 size of optional header 102 characteristics Executable 32 bit word machine [...]
如果你没有或不需要整个Windows SDK或Visual Studio,你可以使用sigcheck.exe
from SysInternals:
sigcheck.exe C:\Windows\Notepad.exe
输出:
Sigcheck v2.1 - File version and signature viewer
Copyright (C) 2004-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\notepad.exe:
Verified: Signed
Signing date: 8:59 AM 8/22/2013
Publisher: Microsoft Windows
Description: Notepad
Product: Microsoft« Windows« Operating System
Prod version: 6.3.9600.16384
File version: 6.3.9600.16384 (winblue_rtm.130821-1623)
MachineType: 64-bit
我可以确认一下,file
实用程序(如cygwin的)会区分32位和64位的可执行文件。它们出现如下:
32.exe: PE32 executable (GUI) Intel 80386, for MS Windows
64.exe: PE32+ executable (console) x86-64, for MS Windows
正如你所看到的那样,很明显的是哪个是哪个。此外,它还能区分控制台和GUI可执行文件,也是很明显的。
很多人都安装了优秀的【7-zip】(http://www.7-zip.org/),并在`PATH`中加入了7-Zip文件夹。7-ZIP可以理解ZIP和RAR以外的文件格式,如MSI文件和PE可执行文件等。只需在PE文件(Exe或DLL)上使用命令行`7z.exe`:
7z l some.exe | more
7z l some.exe | findstr CPU
输出结果如下,其中CPU
行读为x86
或x64
,也就是这里所说的
Path = C:\Extra\AV\neroAacEnc.exe
Type = PE
CPU = x86
Characteristics = Executable 32-bit
Path = C:\Extra\AV\LAME\lame_enc.dll
Type = PE
CPU = x86
Characteristics = Executable DLL 32-bit
Path = C:\Extra\AV\FFmpeg\bin\ffmpeg.exe
Type = PE
CPU = x64
64-bit = +
Characteristics = Executable LargeAddress NoRelocs NoLineNums NoLocalSyms NoDebugInfo
Path = C:\Extra\AV\FFmpeg\bin\avcodec-56.dll
Type = PE
CPU = x64
64-bit = +
Characteristics = Executable DLL LargeAddress NoLineNums NoLocalSyms NoDebugInfo
64位版本的【进程资源管理器】(http://technet.microsoft.com/en-us/sysinternals/bb896653)可以告诉你。只要运行可执行程序,打开进程的属性窗口。在主选项卡上有一个 “Image:32位 "或 "Image:64位 "的条目,
最简单的方法*(当数据不保密时)
我发现Virustotal File detail
是最简单的方法,可以查出二进制是32位还是64位。
这里有一个Powershell的解决方案,没有外部依赖关系什么的。打开Powershell,将函数粘贴到那里(按两次回车键,这样你就可以返回到提示符),然后使用它,就像我下面的例子中的函数:
function Test-is64Bit {
param($FilePath=“$env:windir\notepad.exe”)
[int32]$MACHINE_OFFSET = 4
[int32]$PE_POINTER_OFFSET = 60
[byte[]]$data = New-Object -TypeName System.Byte[] -ArgumentList 4096
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList ($FilePath, 'Open', 'Read')
$stream.Read($data, 0, 4096) | Out-Null
[int32]$PE_HEADER_ADDR = [System.BitConverter]::ToInt32($data, $PE_POINTER_OFFSET)
[int32]$machineUint = [System.BitConverter]::ToUInt16($data, $PE_HEADER_ADDR + $MACHINE_OFFSET)
$stream.Close()
$result = "" | select FilePath, FileType, Is64Bit
$result.FilePath = $FilePath
$result.Is64Bit = $false
switch ($machineUint)
{
0 { $result.FileType = 'Native' }
0x014c { $result.FileType = 'x86' }
0x0200 { $result.FileType = 'Itanium' }
0x8664 { $result.FileType = 'x64'; $result.is64Bit = $true; }
}
$result
}
以下是输出的例子:
D:\> Test-is64bit
FilePath FileType Is64Bit
-------- -------- -------
C:\Windows\notepad.exe x64 True
D:\> Test-is64bit 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
FilePath FileType Is64Bit
-------- -------- -------
C:\Program Files (x86)\Mozilla Firefox\firefox.exe x86 False
即使是标注为32位的可执行文件,如果是.NET可执行文件可以作为32位或64位运行,也可以作为64位运行。更多信息请参见https://stackoverflow.com/questions/3782191/how-do-i-determine-if-a-net-application-is-32-or-64-bit ,其中有一个答案说,可以用CORFLAGS实用程序来确定.NET应用程序的运行方式。 EXE输出**
对于32位的可执行文件:
Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 0x3
ILONLY : 1
32BITREQ : 1
32BITPREF : 0
Signed : 0
对于64位的可执行文件:
Version : v2.0.50727
CLR Header: 2.5
PE : PE32+
CorFlags : 0x1
ILONLY : 1
32BITREQ : 0
32BITPREF : 0
Signed : 0
对于可以作为32位或64位运行的可执行文件,并且在可能的情况下会作为64位运行:
Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 0x1
ILONLY : 1
32BITREQ : 0
32BITPREF : 0
Signed : 0
对于可以作为32位或64位运行的可执行文件,但除非加载到64位进程中,否则会作为32位运行:
Version : v4.0.30319
CLR Header: 2.5
PE : PE32
CorFlags : 0x20003
ILONLY : 1
32BITREQ : 0
32BITPREF : 1
Signed : 0
我的两点建议是下载dependency walker,然后检查一下在可执行文件中使用了什么架构。 exe文件→选择,在底部反射扫描完成后,你会看到一个有数据的网格,其中有一列有 “架构 "的详细信息(x86, x64)
打开可执行文件,看到构建架构
你也可以在msys bundle of mingw 中使用file
工具。它的工作原理和unix命令一样。与GNUwin32中的file
工具的工作原理类似。
创建一个名为exetest. reg,并包含这行代码:
Windows Registry Editor Version 5.00
; What will appear in the contextual menu when right-clicking on a .exe file
[HKEY_CLASSES_ROOT\exefile\shell\command32_64]
@="32/64 bit test"
; What to do with it
; here, %1 is the file given as argument of the script
[HKEY_CLASSES_ROOT\exefile\shell\command32_64\command]
@="\"c:\temp\x86TestStart.bat\" \"%1\""
创建一个名为x86TestStart.bat
的文本文件,包含这行代码,并保存在C:\temp:
c:\temp\x86or64.vbs %1
创建一个名为x86or64.vbs
的文本文件,包含这行代码,并保存在C:\temp:
rem Reading binary file in VBScript: http://stackoverflow.com/questions/21249440/modify-first-two-bytes-of-a-file-using-vbscript
rem Info on executables: https://dmoj.ca/problem/exe
rem x86/64 signature is located dinamycally; its position is addressed
rem from bytes in 0x3C-0x3D position.
rem Possible signatures;
rem "PE..L" (hex code: 50.45.00.00.4C) = 32 bit
rem "PE..d†" (hex code: 50.45.00.00.64.86) = 64 bit
' ------------------------------------
' Source code by Jumpkack 2015
' ------------------------------------
' Read all arguments from command line:
Set args = Wscript.Arguments
' Store first argument (full path to file)
FileName = args(0)
' Find address of executable signature:
FirstChars = readBinary(FileName)
FirstChars = FirstChars
Addr1 = asc(mid(FirstChars,61,1))
Addr2 = asc(mid(FirstChars,62,1))
AddrFinal = Addr2*256 + Addr1 + 1
' Check signature:
if ucase(hex(asc(mid(FirstChars,AddrFinal+4,2)))) = "4C" then Wscript.Echo Filename & " is a 32 bit executable."
if ucase(hex(asc(mid(FirstChars,AddrFinal+4,2)))) = "64" then Wscript.Echo Filename & " is a 64 bit executable."
Function readBinary(path)
Dim a, fso, file, i, ts
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.getFile(path)
If isNull(file) Then
wscript.echo "File not found: " & path
Exit Function
End If
Set ts = file.OpenAsTextStream()
'a = makeArray(file.size)
a=""
i = 0
While (Not ts.atEndOfStream) and (i<60000)
'a(i) = ts.read(1)
a = a + ts.read(1)
i = i + 1
Wend
ts.close
readBinary = a
End Function
双击exetest. reg文件:在windows注册表中会添加一个新的密钥:
[HKEY_CLASSES_ROOT\exefile\shell\command32_64\command]
,在右键点击可执行文件后,在上下文菜单中会显示为 “32/64位测试"。
点击该项目将导致启动批处理文件`c:\temp\x86TestStart.bat# 如何在你的上下文菜单中添加32/64位测试
创建一个名为exetest. reg,并包含这行代码:
Windows Registry Editor Version 5.00
; What will appear in the contextual menu when right-clicking on a .exe file
[HKEY_CLASSES_ROOT\exefile\shell\command32_64]
@="32/64 bit test"
; What to do with it
; here, %1 is the file given as argument of the script
[HKEY_CLASSES_ROOT\exefile\shell\command32_64\command]
@="\"c:\temp\x86TestStart.bat\" \"%1\""
创建一个名为x86TestStart.bat
的文本文件,包含这行代码,并保存在C:\temp:
c:\temp\x86or64.vbs %1
创建一个名为x86or64.vbs
的文本文件,包含这行代码,并保存在C:\temp:
rem Reading binary file in VBScript: http://stackoverflow.com/questions/21249440/modify-first-two-bytes-of-a-file-using-vbscript
rem Info on executables: https://dmoj.ca/problem/exe
rem x86/64 signature is located dinamycally; its position is addressed
rem from bytes in 0x3C-0x3D position.
rem Possible signatures;
rem "PE..L" (hex code: 50.45.00.00.4C) = 32 bit
rem "PE..d†" (hex code: 50.45.00.00.64.86) = 64 bit
' ------------------------------------
' Source code by Jumpkack 2015
' ------------------------------------
' Read all arguments from command line:
Set args = Wscript.Arguments
' Store first argument (full path to file)
FileName = args(0)
' Find address of executable signature:
FirstChars = readBinary(FileName)
FirstChars = FirstChars
Addr1 = asc(mid(FirstChars,61,1))
Addr2 = asc(mid(FirstChars,62,1))
AddrFinal = Addr2*256 + Addr1 + 1
' Check signature:
if ucase(hex(asc(mid(FirstChars,AddrFinal+4,2)))) = "4C" then Wscript.Echo Filename & " is a 32 bit executable."
if ucase(hex(asc(mid(FirstChars,AddrFinal+4,2)))) = "64" then Wscript.Echo Filename & " is a 64 bit executable."
Function readBinary(path)
Dim a, fso, file, i, ts
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.getFile(path)
If isNull(file) Then
wscript.echo "File not found: " & path
Exit Function
End If
Set ts = file.OpenAsTextStream()
'a = makeArray(file.size)
a=""
i = 0
While (Not ts.atEndOfStream) and (i<60000)
'a(i) = ts.read(1)
a = a + ts.read(1)
i = i + 1
Wend
ts.close
readBinary = a
End Function
双击exetest. reg文件:在windows注册表中会添加一个新的密钥:
[HKEY_CLASSES_ROOT\exefile\shell\command32_64\command]
,在右键点击可执行文件后,在上下文菜单中会显示为 ”32/64位测试“。
点击该项目将导致启动批处理文件,启动VBscript文件x86or64.vbs
,读取exe签名并显示结果。
我的两点意见:作为一个C++开发者,依赖关系行者http://www.dependencywalker.com/ )的信息量非常大,不仅显示64/32位,而且每一个Dll涉及的Dll都会显示:
你可以在每个文件名的左边看到64位…..
.DMP
转储
–在那里你可以得到所有的细节
–检查进程架构。