Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNew Line / Multiple Lines Pin
tekhead
22:08 12 Jan '09  
GeneralRe: New Line / Multiple Lines Pin
rongchaua
10:58 17 Apr '09  
Questioni cant insert instruction Pin
sujithkumarsl
19:38 17 Apr '08  
GeneralGodd stuff Pin
hhir1
0:05 22 Oct '07  
QuestionReplace Method??? Pin
rongchaua
13:40 15 Jul '07  
AnswerRe: Replace Method??? Pin
jonbratz
22:23 18 Jul '07  
GeneralRe: Replace Method??? Pin
jonbratz
6:31 24 Jul '07  
GeneralTry Reflexil Pin
jonbratz
23:01 2 Jul '07  
Generalmeta-data corruption? Pin
Philox
3:06 6 Jun '07  
GeneralRe: meta-data corruption? Pin
Fco. Javier Marin
3:09 6 Jun '07  
GeneralRe: meta-data corruption? Pin
Philox
23:43 6 Jun '07  
GeneralMore Function Please Pin
rongchaua
8:55 1 Jun '07  
GeneralRe: More Function Please Pin
Fco. Javier Marin
6:33 2 Jun '07  
GeneralPatching signed assemblies ? Pin
Vertyg0
21:57 31 May '07  
GeneralRe: Patching signed assemblies ? Pin
Fco. Javier Marin
0:59 1 Jun '07  
GeneralSecuring my Application Pin
jfdoubell
20:50 31 May '07  
GeneralRe: Securing my Application Pin
Fco. Javier Marin
0:54 1 Jun '07  
GeneralNice work Pin
David Sleeckx
12:21 31 May '07  
GeneralRe: Nice work [modified] Pin
Fco. Javier Marin
12:24 31 May '07  


Last Updated 31 May 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009