Hello Folks, In my last article i discussed about protecting the IL code from unauthorised disassembling.
In this article i am going to demonstrate how we can manipulate the IL code to change the behaviour of how the .NET programme executes. "Main" method is supposed to be the entry point of an assembly, with a bit of manupilation in the IL code we can set some other method as the entry point, no fellas i m not trying to hack into the IL code but just exploring few things which we cannot do normally. I am again going to use the utility ILDASM utility to output a .il file from the assembly.
A brief explanation about the "Main" method.
When an assembly is compiled the Main method is fired as it is the entry point of the assembly but in reality the entry point for an assembly is the "_Main" method generated automatically and which in turn calls the Main method defined by us. This means that if we open up the IL and change the "_Main" method to call some other method than that method will act as a entry point for the assembly. In fact we could literally delete the section of the "Main" method without affecting the assembly.
Lets do it step by step
Step 1 Creating an assembly simple.vb
' Copy this line in a file simple.vb
' Compile it as vbc simple.vb to produce simple.exe
imports system
class simple
shared sub main
console.writeline("Hello From Main")
end sub
shared sub m2
console.writeline("Hi from m2")
end sub
end class
Step 2 Generating IL code from the assembly
The output of the code will be simple.exe which when executed would write "Hello From Main" on the console. Now lets open up the assembly and do some post mortem to change the entry point, but first we will have to generate IL code from the assembly it is done thru ILDASM util
ILDASM simple.exe /out=simple.il
The result will be simple.il file which we will mainpulate.
Step 3 Modifying simple.il file
Open the simple.il file from any editor and refer to the last section which looks something like this
.custom instance void [Microsoft.VisualBasic]Microsoft.VisualBasic.Globals/Globals$StandardModuleAttribute::.ctor() = ( 01 00 00 00 )
.method public static void _main(class System.String[] _s) il managed
{
.entrypoint
// Code size 6 (0x6)
.maxstack 8
IL_0000: call void t::main()
IL_0005: ret
} // end of method _vbProject::_main
} // end of class _vbProject
Modify the line IL_0000: call void t::main() to IL_0000: call void t::m2()
.custom instance void [Microsoft.VisualBasic]Microsoft.VisualBasic.Globals/Globals$StandardModuleAttribute::.ctor() = ( 01 00 00 00 )
.method public static void _main(class System.String[] _s) il managed
{
.entrypoint
// Code size 6 (0x6)
.maxstack 8
IL_0000: call void t::m2()
IL_0005: ret
} // end of method _vbProject::_main
} // end of class _vbProject
Save the file. The next step is to regenerate an executable assembly from modified simple.il
Step 4 Generating assembly from simple.il
We will use the utility ILASM provided by the .NET to generate an assembly from an .IL file.
Type on command line
ilasm simple.il
The result will be a simple.exe file which when fired will display "Hi from m2" on the console.
There are lot of other things u can do by altering the IL file. To protect the assembly from getting disassembled refer to the article Protecting IL Code from Unauthorized Disassembling.