Fix bugs in .NET program without access to source

2020-06-12 02:29发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

telerik Just Decompile (free) Then the reflexil plug in

2 min job with the above (I've tried all the other methods!!)