天天看點

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"));
        }
    }
}