Click here to Skip to main content
6,293,171 members and growing! (11,593 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#.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0, Visual Studio, Dev
Posted:31 May 2007
Views:30,415
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
16 votes for this article.
Popularity: 5.36 Rating: 4.45 out of 5
1 vote, 6.3%
1

2
1 vote, 6.3%
3
2 votes, 12.5%
4
12 votes, 75.0%
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


Member
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 studying engineering of Telecommunications in the university of my city and I maintain some popular websites like Xuletas, Wikiteka and SayToMe

Apuntes, Resumenes, Trabajos, Examenes, Problemas
Occupation: Software Developer
Location: Spain Spain

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralNew Line / Multiple Lines Pinmembertekhead22:08 12 Jan '09  
GeneralRe: New Line / Multiple Lines Pinmemberrongchaua10:58 17 Apr '09  
Questioni cant insert instruction Pinmembersujithkumarsl19:38 17 Apr '08  
GeneralGodd stuff Pinmemberhhir10:05 22 Oct '07  
QuestionReplace Method??? Pinmemberrongchaua13:40 15 Jul '07  
AnswerRe: Replace Method??? Pinmemberjonbratz22:23 18 Jul '07  
GeneralRe: Replace Method??? Pinmemberjonbratz6:31 24 Jul '07  
GeneralTry Reflexil Pinmemberjonbratz23:01 2 Jul '07  
Generalmeta-data corruption? PinmemberPhilox3:06 6 Jun '07  
GeneralRe: meta-data corruption? PinmemberFco. Javier Marin3:09 6 Jun '07  
GeneralRe: meta-data corruption? PinmemberPhilox23:43 6 Jun '07  
GeneralMore Function Please Pinmemberrongchaua8:55 1 Jun '07  
GeneralRe: More Function Please PinmemberFco. Javier Marin6:33 2 Jun '07  
GeneralPatching signed assemblies ? PinmemberVertyg021:57 31 May '07  
GeneralRe: Patching signed assemblies ? PinmemberFco. Javier Marin0:59 1 Jun '07  
GeneralSecuring my Application Pinmemberjfdoubell20:50 31 May '07  
GeneralRe: Securing my Application PinmemberFco. Javier Marin0:54 1 Jun '07  
GeneralNice work PinmemberDavid Sleeckx12:21 31 May '07  
GeneralRe: Nice work [modified] PinmemberFco. 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-2009
Web12 | Advertise on the Code Project