天天看点

C#逆向反编译工具:JetBrains dotPeek

C#逆向反编译工具:JetBrains dotPeek

《论道篇》

一天,戈戈来到江边静坐凝思,一手放着风筝,一手钓着鱼。微风徐徐,水面粼粼,柳枝招摇。

C#逆向反编译工具:JetBrains dotPeek

戈戈:《论语》读了许多天可有心得?

狄狄:错误要么是“过了”,要么是“不及”,重要的就是把握住度。

戈戈:程朱理学常说格物致知。那些红绿线平时可格出了些什么?

狄狄:随机漫步的傻瓜,价值,习惯遗忘。

戈戈:你未看此花时,此花与汝心同归于寂。你来看此花时,则此花颜色一时明白起来。便知此花不在你的心外。

狄狄:修道了嚒,神神叨叨的…

C#逆向反编译工具:JetBrains dotPeek

戈戈:最近在研读稻盛大师的几本洗脑大作。

狄狄:人家怎么就洗脑了?

戈戈:专门给人灌鸡汤。

狄狄:适度鸡汤也可以的,不要刻意。

戈戈:稻盛心学教人一心一意磨炼心灵,到达真我,瞬间明白一切真理。可对?

狄狄:不知道是不是跟文化有点关系。

戈戈:周末来加班吧?

狄狄:加班费提前预知一下哈。

戈戈:稻盛大师可不是这样的!

狄狄:年轻人没有家人要养活吗?

C#逆向反编译工具:JetBrains dotPeek

狄狄:最近累死了。

戈戈:丁元英告诉年轻人不要轻易说这个词,说完就真的马上要倒下。想干成点事,别把别人看得太轻,别把自己看得太重。

戈戈:我想找个井沿扒着看看,哪有?

狄狄:农村!

狄狄:这盘菜不是人人都能吃得,扒上了饱了眼福,再掉下去可就跌入了地狱。

戈戈:你注意到书上那句“见路不走”吗?

狄狄:见路非路,即见因果。

C#逆向反编译工具:JetBrains dotPeek

狄狄:大佬您再写一篇经文大概需要几个时辰?

戈戈:佛曰: 不可说,不可说,一说就得背锅。

戈戈:放假前交代的功课做了没?

狄狄:说不清,道不明,杂事一堆。

戈戈:人不立志,天下无有可成之事。

狄狄:师父今天很闲啊。

戈戈:最近大家都在说躺平。

狄狄:其实我也想。

戈戈:有人说,孙悟空头上的有形紧箍最后内化为他心里的无形紧箍了。

狄狄:师父,我先回去了。

C#逆向反编译工具:JetBrains dotPeek

幕落。

文章目录

  • ​​1、dotPeek​​
  • ​​1.1 功能简介​​
  • ​​1.2 文件格式支持​​
  • ​​1.3 使用测试​​
  • ​​2、ILSpy​​
  • ​​3、dnSpy​​
  • ​​4、.NET Reflector​​
  • ​​5、ildasm​​
  • ​​6、获取.Net程序依赖项(C#代码实现)​​
  • ​​结语​​

1、dotPeek

官网地址:​​https://www.jetbrains.com/decompiler/​​ dotPeek 是 JetBrains 开发的一款.Net反编译工具,是.Net工具套件中的一个,而且免费使用。

C#逆向反编译工具:JetBrains dotPeek
C#逆向反编译工具:JetBrains dotPeek

1.1 功能简介

To assist you in working with compiled assemblies, dotPeek provides lots of features for:

  1. Exploring .NET assemblies
  2. Working with decompiled code, original source code, and IL code
  3. Navigation and search
  4. Debugging compiled code
C#逆向反编译工具:JetBrains dotPeek
  • 将 .NET 程序集反编译为 C#

dotPeek 是一款基于 ReSharper 捆绑反编译器的免费独立工具。 它可以可靠地将任意 .NET 程序集反编译为对等的 C# 或 IL 代码。

这款反编译器支持包括库 (.dll)、可执行文件 (.exe) 和 Windows 元数据文件 (.winmd) 在内的多种格式。

  • 将反编译代码导出至 Visual Studio 项目

在反编译完程序集后,您可以将其另存为 Visual Studio 项目 (.csproj)。 如果您需要从旧程序集恢复丢失的源代码,此举可以为您节省大量的时间。

  • 下载源代码并调试第三方代码

dotPeek 可以根据 PDB 文件识别本地源代码,或者从 Microsoft Reference Source Center 或 SymbolSource 等源服务器中获取源代码。

dotPeek 还可以作为符号服务器运行,为 Visual Studio 调试器提供调试程序集代码所需的信息。

C#逆向反编译工具:JetBrains dotPeek

1.2 文件格式支持

dotPeek decompiles any .NET assemblies and presents them as C# or IL code. Supported file types include:

  • Libraries (.dll)
  • Executable files (.exe)
  • Windows 8 metadata files (.winmd)
  • Archives (.zip)
  • NuGet packages (.nupkg)
  • Microsoft Visual Studio Extensions packages (.vsix)
C#逆向反编译工具:JetBrains dotPeek

1.3 使用测试

以下是使用dotPeek反编译作者写的C#程序的.exe(ProjectManager.exe):

C#逆向反编译工具:JetBrains dotPeek

下面来一张动图来简单说明一下反编译的整个流程:

2、ILSpy

官网地址:​​https://github.com/icsharpcode/ILSpy​​

ILSpy is the open-source .NET assembly browser and decompiler.

.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!

C#逆向反编译工具:JetBrains dotPeek

以下是使用ILSpy反编译作者写的C#程序的.exe(ProjectManager.exe):

C#逆向反编译工具:JetBrains dotPeek

3、dnSpy

官网地址:​​https://github.com/dnSpy/dnSpy​​

dnSpy is a debugger and .NET assembly editor. You can use it to edit and debug assemblies even if you don’t have any source code available.

dnSpy是一款开源的基于ILSpy发展而来的.net程序集的编辑,反编译,调试神器。

主要功能:1、程序集编辑;2、反编译器;3、调试工具;4、Tabs及tabs分组;5、提供多主题。

C#逆向反编译工具:JetBrains dotPeek

以下是使用dnSpy反编译作者写的C#程序的.exe(ProjectManager.exe):

C#逆向反编译工具:JetBrains dotPeek

4、.NET Reflector

官网地址:​​https://www.red-gate.com/products/dotnet-development/reflector/​​

Decompile, understand, and fix any .NET code, even if you don’t have the source.

C#逆向反编译工具:JetBrains dotPeek
  • Look inside any .NET code

    Debug your application

    Follow bugs through your application to see where the problem is – your own code, third-party libraries, or components used by your application.

  • Understand how applications work

    Inherited an application with no documentation and no comments? Use .NET Reflector to understand how the code runs and avoid bugs.

  • Look inside APIs, SharePoint, and other third-party platforms

    Third-party platforms aren’t always well-documented. Use .NET Reflector to look inside their assemblies, and see how they work and which APIs you can call.

以下是使用Reflector反编译作者写的C#程序的.exe(ProjectManager.exe):

C#逆向反编译工具:JetBrains dotPeek

5、ildasm

官网地址:​​https://docs.microsoft.com/zh-cn/dotnet/framework/tools/ildasm-exe-il-disassembler​​

IL 反汇编程序是 IL 汇编程序 (Ilasm.exe) 的配套工具。 Ildasm.exe 可利用包含中间语言 (IL) 代码的可移植可执行 (PE) 文件,并创建适合输入到 Ilasm.exe 的文本文件 。

此工具会自动随 Visual Studio 一起安装。 若要运行该工具,请使用 Visual Studio 开发人员命令提示或 Visual Studio 开发人员 PowerShell。

以下是使用ildasm反编译作者写的C#程序的.exe(ProjectManager.exe):

C#逆向反编译工具:JetBrains dotPeek

查看C#的dll或exe所依赖.Net版本。

Microsoft SDK自带的ildasm.exe工具, 是一个反编译工具, 可以查看编译好后的dll的文件。

双击ildasm.exe, 把你要识别的.dll文件拖进来, 就会反编译了. 接着在ildasm里, 双击第一行的MANIFEST, 前面五行会类似如下显示, 注意一定要是mscorlib。

当你看到.ver 1:0:5000:0, 说明它是在.net1.1的版本下编译的;

如果看到的是.ver 2:0:0:0, 说明它是.net2.0版本下编译的。

这里看到的是.ver 4:0:0:0,说明它是.net4.0版本下编译的。

C#逆向反编译工具:JetBrains dotPeek

6、获取.Net程序依赖项(C#代码实现)

/*************************************************************************************
 *
 * 文 件 名:   Test_GetReferencedAssemblies.cs
 * 描    述:   获取当前程序引用的所有程序集
 * 版    本:  V1.0
 * 创 建 者:  爱看书的小沐
 * 创建时间:  2022-02-28
*************************************************************************************/
using System;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {
        /// <summary>
        ///  加载指定路径上的程序集文件的内容。
        /// </summary>
        /// <param name="filename">要加载的文件的完全限定路径。</param>  
        static public Assembly LoadAssemblyFromFile(String filename)
        {
            try
            {
                return Assembly.LoadFile(@filename);
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred: {0}", e.Message);
            }

            return null;
        }
        /// <summary>
        ///  打印指定程序集的所有依赖项的信息。
        /// </summary>
        /// <param name="filename">要打印的指定程序集。</param>  
        static public void PrintReferencedAssemblies(Assembly assemblyUser)
        {
            if (assemblyUser == null)
            {
                return;
            }

            try
            {
                AssemblyName[] assemblies = assemblyUser.GetReferencedAssemblies();

                if (assemblies.GetLength(0) > 0)
                {
                    foreach (var assembly in assemblies)
                    {
                        Console.WriteLine(assembly);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred: {0}", e.Message);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("\nAssembly.GetEntryAssembly:");
            PrintReferencedAssemblies(Assembly.GetEntryAssembly());

            Console.WriteLine("\nAssembly.GetExecutingAssembly:");
            PrintReferencedAssemblies(Assembly.GetExecutingAssembly());

            Console.WriteLine("\nAssembly.GetCallingAssembly:");
            PrintReferencedAssemblies(Assembly.GetCallingAssembly());

            Console.WriteLine("\nLoad .Net Dll:");
            PrintReferencedAssemblies(LoadAssemblyFromFile(@"d:\\DH.dll"));

            Console.WriteLine("\nLoad .Net Exe:");
            PrintReferencedAssemblies(LoadAssemblyFromFile(@"d:\\ProjectManager.exe"));

            Console.WriteLine("\nLoad Not .Net Dll:");
            PrintReferencedAssemblies(LoadAssemblyFromFile(@"d:\\glew.dll"));
        }
    }
}