Round-Tripping a simple .Net exe

by Jason Haley 6. June 2004 16:29

Great Resource: Inside Microsoft .Net IL Assembler by Serge Lidin
Pages 383-385

Round tripping is a two step process:
1.  Create an il file from your .net dll or exe (disassemble it)
2.  Compile the il code and resource files into a .net exe or dll (re-assemble it)

The fun part is to change something between steps 1 and 2.  In a simple example I change a message being written to the console from "Hello World" to a more appropriate "Hack the Planet!!!" message, I also wrote a version that was exactly the same except I signed the code with a key.  First one worked, second one fails the strong name validation.

1.  Create a C# console application, named rtConsole
2.  Write some code to write a message out to the console:

[STAThread]
static
void Main(string[] args)
{
      Console.WriteLine("Hello World");
}

3.  Build the application (Ctl+Shft+B)
4.  Run it to make sure it works (Ctl+F5)

When the simple console application is working, disassemble it.
1.  Open up a command prompt
2.  Go to the directory where your bin folder is and find rtConsole.exe
3.  Type the following and press enter when done:
   ildasm rtConsole.exe /out:rtConsole.il
4.  Open rtConsole.il up in a text editor
5.  Find and replace the text "Hello World" with what ever you want
6.  Save the editted rtConsole.il file

Now it is time to re-compile the exe
1.  Go back to your command prompt
2.  Type the following and press enter when done:
  ilasm /exe rtConsole.il /out:rtConsole.exe /res:rtConsole.res
3.  Now run rtConsole.exe and your new message should come out.

If you don't already have a key pair, create one by doing the following:

Create a Key Pair
1.  Go to command prompt
2.  Type the following and press enter when done (To find out more look into the documentation on the sn.exe utility.):
   sn -k myKey.snk

Now to find out what will happen if the assembly is signed....
1.  Go back to your Visual Studio solution and sign the project.
2.  In your Visual Studio solution, go to the AssemblyInfo.cs file and change the AssemblyKeyFile attribute to point to your key. For example:
   [assembly: AssemblyKeyFile("C:\\MyKey.snk")]
3.  Build the application (Ctl+Shft+B)
4.  Run it to make sure it works (Ctl+F5)

Repeat the sections above to disassemble, edit and re-compile the exe and see if it works this time.  You should get a strong name validation failed error message.  I guess this is a good reason to sign your assemblies...

Comments (0) | Post RSSRSS comment feed |

Categories:
Tags:

Comments are closed