Click here to Skip to main content
15,896,348 members
Articles / Programming Languages / MSIL

Assembly Manipulation and C# / VB.NET Code Injection

Rate me:
Please Sign up or sign in to vote.
4.95/5 (200 votes)
26 Apr 2013MIT5 min read 927.4K   25K   486  
Reflexil is an assembly editor and runs as a plug-in for Reflector or JustDecompile. Reflexil is able to manipulate IL code and save the modified assemblies to disk. Reflexil also supports "on-the-fly" C#/VB.NET code injection.
/*
    Reflexil .NET assembly editor.
    Copyright (C) 2007 Sebastien LEBRETON

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#region " Imports "
using System;
using System.Windows.Forms;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Reflexil.Editors;
using Reflexil.Utils;
using Reflexil.Wrappers;
#endregion

namespace Reflexil.Forms
{
	
	public partial class InstructionForm 
	{
		
		#region " Fields "
		private MethodDefinition m_mdef;
        private Instruction m_selectedinstruction;
		#endregion
		
		#region " Properties "
        public MethodDefinition MethodDefinition
        {
            get
            {
                return m_mdef;
            }
        }

        public Instruction SelectedInstruction
        {
            get
            {
                return m_selectedinstruction;
            }
        }
		#endregion
		
		#region " Events "
		protected virtual void Operands_SelectedIndexChanged(object sender, EventArgs e)
		{
			OperandPanel.Controls.Clear();
			OperandPanel.Controls.Add((Control) Operands.SelectedItem);
            if (MethodDefinition != null)
			{
				((IGlobalOperandEditor) Operands.SelectedItem).Initialize(MethodDefinition);
			}
		}
		
		protected virtual void OpCodes_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            if (OpCodes.SelectedItem != null)
            {
                RtbOpCodeDesc.Text = DataManager.GetInstance().GetOpcodeDesc((OpCode)OpCodes.SelectedItem);
            }
		}

        private void OpCodes_TextChanged(object sender, EventArgs e)
        {
            if (OpCodes.SelectedItem == null)
            {
                RtbOpCodeDesc.Text = "Unknown opcode";
            }
        }
		#endregion
		
		#region " Methods "
        public InstructionForm() : base()
        {
            InitializeComponent();
        }

		public void FillControls(MethodDefinition mdef)
		{
			OpCodeBindingSource.DataSource = DataManager.GetInstance().GetAllOpCodes();
			OpCodes.SelectedIndex = 0;
			
			Operands.Items.Add(new NullOperandEditor());
			Operands.Items.Add(new ByteEditor());
			Operands.Items.Add(new SByteEditor());
			Operands.Items.Add(new IntegerEditor());
			Operands.Items.Add(new LongEditor());
			Operands.Items.Add(new SingleEditor());
			Operands.Items.Add(new DoubleEditor());
			Operands.Items.Add(new StringEditor());

            if (mdef.HasBody)
			{
                Operands.Items.Add(new InstructionReferenceEditor(mdef.Body.Instructions));
                Operands.Items.Add(new MultipleInstructionReferenceEditor(mdef.Body.Instructions));
                Operands.Items.Add(new VariableReferenceEditor(mdef.Body.Variables));
			}
			else
			{
				Operands.Items.Add(new GenericOperandReferenceEditor<Instruction, InstructionWrapper>(null));
				Operands.Items.Add(new MultipleInstructionReferenceEditor(null));
				Operands.Items.Add(new GenericOperandReferenceEditor<VariableDefinition, VariableWrapper>(null));
			}

            Operands.Items.Add(new ParameterReferenceEditor(mdef.Parameters));
			Operands.Items.Add(new FieldReferenceEditor());
			Operands.Items.Add(new MethodReferenceEditor());
			Operands.Items.Add(new GenericTypeReferenceEditor());
			Operands.Items.Add(new TypeReferenceEditor());
			Operands.Items.Add(new NotSupportedOperandEditor());
			
			Operands.SelectedIndex = 0;
		}
		
		public virtual DialogResult ShowDialog(MethodDefinition mdef, Instruction selected)
		{
            m_mdef = mdef;
            m_selectedinstruction = selected;
			return base.ShowDialog();
		}
		
		protected Instruction CreateInstruction()
		{
			try
			{
                if (OpCodes.SelectedItem != null)
                {
                    IGlobalOperandEditor editor = (IGlobalOperandEditor)Operands.SelectedItem;
                    Instruction ins = editor.CreateInstruction(MethodDefinition.Body.CilWorker, ((OpCode)OpCodes.SelectedItem));
                    return ins;
                }
                else
                {
                    MessageBox.Show("Unknown opcode");
                    return null;
                }
			}
			catch (Exception)
			{
				MessageBox.Show("Reflexil is unable to create this instruction, check coherence between the opcode and the operand");
				return null;
			}
		}
		#endregion

    }
	
}


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 MIT License


Written By
Software Developer (Senior) Microsoft
United States United States
Sebastien Lebreton is a Software Engineer at Microsoft.

He is particularly interested in optimization, reverse engineering and distributed objects technologies.

Comments and Discussions