5,442,164 members and growing! (23,605 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Advanced License: The Code Project Open License (CPOL)

NetDasm - A tool to disassemble and patch .NET assemblies

By Fco. Javier Marin

Disassemble and patch .NET assemblies using the Mono.Cecil library.
C#Windows, .NET, .NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0, Visual Studio, Dev

Posted: 31 May 2007
Updated: 31 May 2007
Views: 18,352
Bookmarked: 33 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 5.22 Rating: 4.44 out of 5
1 vote, 6.7%
1
0 votes, 0.0%
2
1 vote, 6.7%
3
2 votes, 13.3%
4
11 votes, 73.3%
5

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:

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

  • 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)

About the Author

Fco. Javier Marin


I am a young student of Cartagena, Spain. I like much the programming, I program since I was 14, and I dominate languages like C #, PHP, Javascript and C.

At the moment, I'm study Bachillerato and next year I will initiate the studies of an engineering of Telecommunications in the university of my city.
Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Subject  Author Date 
Questioni cant insert instructionmembersujithkumarsl19:38 17 Apr '08  
GeneralGodd stuffmemberhhir10:05 22 Oct '07  
QuestionReplace Method???memberrongchaua13:40 15 Jul '07  
AnswerRe: Replace Method???memberjonbratz22:23 18 Jul '07  
GeneralRe: Replace Method???memberjonbratz6:31 24 Jul '07  
GeneralTry Reflexilmemberjonbratz23:01 2 Jul '07  
Generalmeta-data corruption?memberPhilox3:06 6 Jun '07  
GeneralRe: meta-data corruption?memberFco. Javier Marin3:09 6 Jun '07  
GeneralRe: meta-data corruption?memberPhilox23:43 6 Jun '07  
GeneralMore Function Pleasememberrongchaua8:55 1 Jun '07  
GeneralRe: More Function PleasememberFco. Javier Marin6:33 2 Jun '07  
GeneralPatching signed assemblies ?memberVertyg021:57 31 May '07  
GeneralRe: Patching signed assemblies ?memberFco. Javier Marin0:59 1 Jun '07  
GeneralSecuring my Applicationmemberjfdoubell20:50 31 May '07  
GeneralRe: Securing my ApplicationmemberFco. Javier Marin0:54 1 Jun '07  
GeneralNice workmemberDavid Sleeckx12:21 31 May '07  
GeneralRe: Nice workmemberFco. Javier Marin12:24 31 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 May 2007
Editor: Smitha Vijayan
Copyright 2007 by Fco. Javier Marin
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project