Search code examples
c#.net-assemblyunsafemanaged

How to use Peverify.exe (PEVerify tool)?


I saw a couple of post(this post specifically) talking about using peverify.exe to check whether the .net exe(or dll) contains unsafe code. But no matter what I do I keep getting File not found or has bad headers error.

Here are my steps -

  1. Open developer power shell for 2022 - Note I did not change the path to where the exe is.
  2. peverify <complete path of the dll or exe>
  3. Enter

I keep getting File not found or has bad headers error. This should be simple yet there is something I am missing?


Other attempts with different command with same result is as follows

  1. peverify.exe <complete path of the exe/dll> /md /il
  2. Tried running the same in Developer Command Prompt for visual studio 2022

Solution

  • PEVerify is for the "old" Windows-specific .NET Framework. A newer tool was created for .NET 5+ and .NET Core (although it also works with the old .NET Framework), ILVerify.

    To install and run (copying from the README, so this information is here instead of susceptible to link rot):

    ILVerify is published as a global tool package. Install it by running:

    dotnet tool install --global dotnet-ilverify
    

    Example of use:

    C:\test>dotnet ilverify hello.dll -r "c:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.12\*.dll"
    All Classes and Methods in C:\test\hello.dll Verified.
    

    Note that ILVerify requires all dependencies of assembly that is being verified to be explicitly specified on the command line.

    The package itself is dotnet-ilverify on NuGet.org (here).