views:

209

answers:

5

I have a program i frequently use that is made with .NET. This program has a small bug that is very annoying and the developer to the app is nowhere to be found.

I have found the location of the problem in reflector and just want to add a single if-statement here and then recompile the program.

What is the easiest way to do this?

I have tried using the export-function in reflector but it doesn't seem to work perfectly. For example alot of using-directives are missing and enums that are cast back and forth to ints are also not exported properly. I believe i can fix this with some work but i'm wondering if there are any easier way to do this.

Update: Solved it by doing this:

The program is a single file executable

  1. Find the bug in reflector with C#-view. Then switch to IL-view to see what you should look for in step 5.
  2. Open the program you want to change with ildasm
  3. Press dump and select an empty folder, default settings worked for me.
  4. Find the .il file in this folder and open with any texteditor
  5. Find the code from step 1.
  6. Replace it with new code, in my case i just had to replace all the contents of a function with a simple return (IL_0000: ret). I don't think all fixes will be this easy.
  7. Open a commandwindow in the folder from step 3
  8. Run ilasm, i also had to include some resources from the original exe. These are automatically output with ildasm. I used this ilasm like this "C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe" test.il /resource=test.res /output=FixedProgram.exe

Done! I've only used the fixed program some minutes right now but it seems to work just like before except for the bug :)

I also ran into some problems with the application settings. In %appdata%\ProgramName there was a folder called something like *ProgramName.exe_Url_qa5i3p42aomuvoxpuxuficvbmngksgjs* where all settings are stored. My new executable created a new folder like this with different random letters at the end and with the default settings. To copy the settings you used before just copy all the contents of the old folder to the new one.

+2  A: 

I don't think there is an easier way to do this than what you already mentioned. I actually decompiled a program with ILDASM for something similar and it took quite a lot of tinkering.

If there is an easier method I'd like to know it too.

rslite
+1 - you were faster ;)
Lucero
+2  A: 

You can use ILDASM and ILASM to make a roundtrip. While this is impractical for large changes, I think that it should do the trick in your case.

Lucero
+1  A: 

If the program is simple, in theory you could use reflector to reverse the entire program back to C#, make the change and recompile it.

Alternatively you could try a tool like ILDasm, which should allow you to extract the IL, make your change (in MSIL), and re assemble (using ILAsm). Obviously to do this you'll have to figure out how to write your change in IL, so you'll probably have to do some experimenting to see how if statements compile.

I suspect both will take quite a bit of messing around to get the recompile to work.

Good luck.

If you get it to work, post back and let me know, I'd be quite interested in how you did this.

Simon P Stevens
Thanks for accepting this as the answer Erik, it sounds like it's what worked for you, but for other people reading this definitely check out Reflexil (http://sebastien.lebreton.free.fr/reflexil/) (as mentioned in Jb Evain's answer) as this will all you to directly make the change to the assembly with C# code without having to worry about the IL, and save a patched version of the assembly, all from reflector really easily. I strongly recommend checking it out, it's a great tool.
Simon P Stevens
+1  A: 

Try this .NET Reflector addin.

This has worked for me. It sometimes creates "Complier Generated" tags which cannot be complied. But you can easily fix this manually.

Ganesh R.
+8  A: 

I suggest you use the Reflexil plugin for Reflector. It allows you to easily modify assemblies, and even replace the body of a method by your own C# code.

Jb Evain
Now if that works like it says it does on the website that is fantastic. +1. Cecil looks awesome. "Modify assemblies on the fly". Wow! What more can I say. Keep up the great work Jb.
Simon P Stevens
Thanks Simon. All the credits for Reflexil go to my friend Sébastien Lebreton who did a truly great job at bridging Cecil and Reflector.
Jb Evain
See the following for an example. I actually found it quite difficult locating the options to re-save the assembly. http://www.cumps.be/reverse-engineering-with-reflector-and-reflexil/
Ian