Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

NetDasm - A tool to disassemble and patch .NET assemblies

Rate me:
Please Sign up or sign in to vote.
4.87/5 (27 votes)
22 Apr 2013CPOL1 min read 138.5K   8.3K   92   23
Disassemble and patch .NET assemblies using the Mono.Cecil library.

Screenshot - Screen.jpg

Introduction

This article illustrates a little tool for disassembling a .NET assembly IL code and making some patches.

Background

For my application, I have used Mono.Cecil, a very powerful library for modifying IL code. If you want more info, visit the Cecil homepage.

Another Cecil article I found to be helpful is the one by ronnyek: MonoCecilChapter1.asp.

Using the code

You only need to load an assembly and look at all the types that it contains. When you click on one type, its methods and some information appear in the right. You can delete a type, delete a method, or edit the code of a method. In the Edit Code form, you can watch the IL code, remove a sentence, or insert a new sentence.

The basic code for using Cecil is:

C#
AssemblyDefinition assembly = AssemblyFactory.GetAssembly("assembly.exe");
 
//Gets all types of the MainModule of the assembly

foreach(TypeDefinition type in assembly.MainModule.Types)
{
    if(type.Name != "<Module>")
    {
        //Gets all methods of the current type

        foreach(MethodDefinition method in type.Methods)
        {
            //Gets the CilWorker of the method for working with CIL instructions

            CilWorker worker = method.Body.CilWorker;
            //Creates the MSIL instruction for inserting the sentence

                   Instruction insertSentence = worker.Create(OpCodes.Ldstr, "Text");
 
 
            //Inserts the insertSentence instruction before the first //instruction

            method.Body.CilWorker.InsertBefore(method.Body.Instructions[0], 
                                               insertSentence);
        }
        //Import the modifying type into the AssemblyDefinition

        assembly.MainModule.Import(type);
    }    
} 
 
//Save the new assembly

AssemblyFactory.SaveAssembly(assembly, "new.exe");

Points of Interest

I want to improve the program and add some more options like methods insertion, feature to show C# code (like Reflector), and an improved instructions insertion.

History

  •  22/04/2013 - Created github repository 
  • 2/06/2007 - Update version
    • Code for IL tool added (only C# code)
    • Support for edit instructions
    • Filter for instructions
    • Some minor changes and bugs fixed
  • 1/06/2007 - Update version
    • Instructions documentation and auto-complete
    • Some interface changes
    • Multi-instructions remover
  • 31/05/2007 - First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Spain Spain
I'm a young entrepreneur from Cartagena (Spain). I'm the creator and owner of some popular websites like Chuletas and Wikiteka.

Currently, I'm working on Ideatic, a company for Internet and software develop.

Comments and Discussions

 
QuestionHow to specify the runtime version of the generated assembly Pin
Ant_22218-Sep-20 5:43
Ant_22218-Sep-20 5:43 
Question!!! Pin
alejandro29A22-Nov-12 2:08
alejandro29A22-Nov-12 2:08 
GeneralNew Line / Multiple Lines Pin
tekhead12-Jan-09 21:08
tekhead12-Jan-09 21:08 
GeneralRe: New Line / Multiple Lines Pin
rongchaua17-Apr-09 9:58
rongchaua17-Apr-09 9:58 
GeneralRe: New Line / Multiple Lines Pin
RandolphC27-Sep-11 21:54
RandolphC27-Sep-11 21:54 
Questioni cant insert instruction Pin
sujithkumarsl17-Apr-08 18:38
sujithkumarsl17-Apr-08 18:38 
GeneralGodd stuff Pin
hhir121-Oct-07 23:05
hhir121-Oct-07 23:05 
QuestionReplace Method??? Pin
rongchaua15-Jul-07 12:40
rongchaua15-Jul-07 12:40 
AnswerRe: Replace Method??? Pin
jonbratz18-Jul-07 21:23
jonbratz18-Jul-07 21:23 
GeneralRe: Replace Method??? Pin
jonbratz24-Jul-07 5:31
jonbratz24-Jul-07 5:31 
GeneralTry Reflexil Pin
jonbratz2-Jul-07 22:01
jonbratz2-Jul-07 22:01 
GeneralRe: Try Reflexil Pin
RandolphC27-Sep-11 21:53
RandolphC27-Sep-11 21:53 
Questionmeta-data corruption? Pin
Philox6-Jun-07 2:06
Philox6-Jun-07 2:06 
AnswerRe: meta-data corruption? Pin
Fco. Javier Marin6-Jun-07 2:09
Fco. Javier Marin6-Jun-07 2:09 
GeneralRe: meta-data corruption? Pin
Philox6-Jun-07 22:43
Philox6-Jun-07 22:43 
GeneralMore Function Please Pin
rongchaua1-Jun-07 7:55
rongchaua1-Jun-07 7:55 
GeneralRe: More Function Please Pin
Fco. Javier Marin2-Jun-07 5:33
Fco. Javier Marin2-Jun-07 5:33 
QuestionPatching signed assemblies ? Pin
Vertyg031-May-07 20:57
Vertyg031-May-07 20:57 
Is it possible to patch signed assembly and not to get next exception:

<br />
An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module.<br />
<br />
Additional information: Could not load file or assembly 'TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f20ee6ae21b7015e' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)<br />


Anyway really nice app ... you could add autocompleate to Opcode combobox in InsertInstruction ...
AnswerRe: Patching signed assemblies ? Pin
Fco. Javier Marin31-May-07 23:59
Fco. Javier Marin31-May-07 23:59 
GeneralSecuring my Application Pin
Jacquers31-May-07 19:50
Jacquers31-May-07 19:50 
GeneralRe: Securing my Application Pin
Fco. Javier Marin31-May-07 23:54
Fco. Javier Marin31-May-07 23:54 
GeneralNice work Pin
2sky31-May-07 11:21
2sky31-May-07 11:21 
GeneralRe: Nice work [modified] PinPopular
Fco. Javier Marin31-May-07 11:24
Fco. Javier Marin31-May-07 11:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.