Click here to Skip to main content
15,881,687 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.8K   8.3K   92  
Disassemble and patch .NET assemblies using the Mono.Cecil library.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System.Reflection;
using NetDasm.Controls;

namespace NetDasm
{
    public partial class EditCode : Form
    {
        AssemblyDefinition assembly;
        MethodDefinition method;
        public EditCode(MethodDefinition metodo, AssemblyDefinition assembly)
        {
            InitializeComponent();
            this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);

            this.assembly = assembly;
            this.method = metodo;

            methodInstructions1.assembly = assembly;
            methodInstructions1.method = metodo;
            methodInstructions1.labelInfo = label1;
            methodInstructions1.Load();            
        }



        private void button1_Click(object sender, EventArgs e)
        {
            methodInstructions1.RemoveSelected();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            InsertInstruction insert = new InsertInstruction(method.Body.Instructions.Count - 1);
            insert.ShowDialog();

            if (insert.DialogResult == DialogResult.OK)
            {
                methodInstructions1.InsertInstruction(insert);
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            methodInstructions1.filter = textBox3.Text;
            methodInstructions1.Load();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (methodInstructions1.SelectedItem != null)
            {
                InsertInstruction insert = new InsertInstruction(method.Body.Instructions.Count - 1);
                insert.numericUpDown1.Value = methodInstructions1.SelectedIndex;
                insert.comboBox1.SelectedItem = methodInstructions1.SelectedInstruction.OpCode.Code.ToString();
                if(methodInstructions1.SelectedInstruction.Operand!=null)
                insert.textBox1.Text = methodInstructions1.SelectedInstruction.Operand.ToString();
            insert.button1.Text = "Modify";
                insert.ShowDialog();

                if (insert.DialogResult==DialogResult.OK)
                {
                    methodInstructions1.RemoveSelected();
                    methodInstructions1.InsertInstruction(insert);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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