C#检测特定进程的CPU架构
我用 C# 编写代码。 我的代码将在 Any CPU
模式下运行并提升。
我的目标是使用 Process.GetProcesses() 枚举计算机中的所有进程,并为每个进程检测其 CPU 架构:x86、x64 或IA64。
我正在 C# 中实现代码注入,需要检测目标进程的体系结构来决定注入哪些操作码。
怎么做呢?
谢谢。
I writing code in C#.
My code gonna run in Any CPU
mode and elevated.
My goal is enumerating all processes in the machine with Process.GetProcesses()
, and for each process detect its CPU architecture: x86, x64 or IA64.
I'm implementing code injection in C# and need to detect the architecture of the target process to decide what opcodes to inject.
How to do that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
定义:
GetNativeSystemInfo 将返回有关正在运行的计算机的信息。
GetSystemInfo 将返回有关正在运行的虚拟化环境的信息(如果没有,则与 GetNativeSystemInfo 相同)。
IE:
在 32 位 Windows 上,您将始终拥有 wProcessorArchitecture == ProcessorArchitectureIntel。
在 64 位 Windows 上,如果您作为 32 位进程运行,则 GetSystemInfo 将为 wProcessorArchitecture == ProcessorArchitectureIntel,但 GetNativeSystemInfo 将为 wProcessorArchitecture == ProcessorArchitectureAmd64。
如果您是 64 位 Windows 上的 64 位进程,那么它们显然都是 ProcessorArchitectureAmd64。
Define:
GetNativeSystemInfo will return you info about the machine you're running on.
GetSystemInfo will return you info about the virtualised environment you're running within (which will be the same as GetNativeSystemInfo if there isn't one).
I.e:
On 32 bit Windows, you will have wProcessorArchitecture == ProcessorArchitectureIntel always.
On 64 bit Windows, you will have wProcessorArchitecture == ProcessorArchitectureIntel for GetSystemInfo, but wProcessorArchitecture == ProcessorArchitectureAmd64 for GetNativeSystemInfo, if you are running as a 32 bit process.
They will obviously both be ProcessorArchitectureAmd64 if you're a 64 bit process on 64 bit Windows.
您必须调用 Win32 才能获取此信息:
为每个进程调用
IsWow64Process(process)
将告诉您它是否是 64 位。我还没有遇到过确定进程是 x64 还是 IA64 的方法,只是确定它的“位数”。You have to call out to Win32 to get this information:
Calling
IsWow64Process(process)
for each process will tell you whether is it 64-bit or not. I've not come across a method to determine whether a process is x64 or IA64, just its "bitness".您可以 p/invoke
QueryFullProcessImageName
或GetProcessImageFileName
,然后读取 .exe 文件的 PE 标头。You could p/invoke
QueryFullProcessImageName
orGetProcessImageFileName
and then read the PE header of the .exe file.Alastair 是对的,您不能只调用 IsWow64Process,但也不需要直接调用另一个 WinApi 函数。这是一个更短的解决方案。
Alastair is right in that you cannot just call IsWow64Process, but you also dont need to call another WinApi function directly. Here's a shorter solution.
您可以尝试 P/Invoke:
You can try to P/Invoke:
我认为你们都在正确的轨道上,但返回值缺少一个非运算符。如果您使用的是 64 位计算机并且进程是 WOW64,那么它就是 32 位进程(请参阅 IsWow64Process 上面的注释)。此外,IsWow64Process 返回错误的可能性也没有得到处理。这是一个固定版本:
I think y'all are on the right track, but the return value is missing a not operator. If you're on a 64-bit machine and the process is WOW64, then it's a 32-bit process (see comments above IsWow64Process). Also, the possibility of IsWow64Process returning an error is not being handled. Here's a fixed version: