7

I want to modify an exe a little bit.

With ILSpy I see all the code that I need but I don't know how to modify the code.

I tried "save code" on ILSpy that exports a .cs file, but when I open the .cs file in Visual Studio and change the code I can't compile or run the modified code.

Is there a way to do that?

P.S. I read that I can change the code in assembly but I don't know assembly so I have to do that at high level, if there is a way.

3
  • you cant modify exe file using ILSpy , you should rather manipulate CFF explorer and patch new addresses of CS and DS in header each time you dilate or shorten the code for more informations)
    – Abr001am
    Jun 20, 2015 at 14:06
  • Thanks for you comment but I'm a newbie in this field and I'm only able to modify if the decompiler show me all the code with a high-level language such as C#. I'm trying with .Net Reflector and Reflexil but I have some trouble as show in beyond comments.
    – sivlab
    Jun 21, 2015 at 0:40
  • There are 2 Save as and Save Code youtube.com/watch?v=ZhancUbQN2k Jul 18, 2022 at 14:06

4 Answers 4

7

For reference to those still looking for an answer to this question, I would recommend trying dnSpy - it allows you to do exactly what the requester asked - modify the high level code and recompile.

It isn't perfect, but I've been able to make small changes to the in-house exe's we have where we didn't have the latest source code (disgruntled employees, don't ask).

https://github.com/dnSpy/dnSpy

5

You may work according to the following pattern:

  • Save code in ILSpy (or in Reflector) as .cs files (as you already described)
  • Try to create a Visual Studio project from that code

  • Make all modifications in Visual Studio.

  • If VS compiles the code, it should open as well in ILSpy and/or Reflector.

  • If it doesn't it is most probably not complete and/or not correct. In that case you might simplify the code until you get something compilable, to identify the "missing links".

  • In this way I have been able to recompile also more complicated
    software. First do everything statically, until you have something
    where code is created in VS. Then test it and expand it.

Code parts which do not recompile in ILSpy or Reflector (each has its own strengths and weaknesses) can be exported in IL and perhaps manually rearranged to recompile in the tools, then further processed in Visual Studio. Unfortunately, VS does not allow for inline IL assembled code.

For instance, Reflector protects itself (among other means like obfuscation) against recompilation with useless jumps confusing the recompiler. ILSpy mostly is able to cope with this.

ILSpy for instance is not able to recompile subclasses (i.e. classes within a class). They just not show up in the class tree, when in C# view. In IL view, you see all of them.

2
  • Thanks for the reply. You hit the point: ILSpy doesn't generate a complete/correct project so in VS I saw the code that I want to edit but in the compiling VS generates a lot of errors. Unfortunately I don't know the complete "desing" of the software so I don't know which part I have to rearrange.
    – sivlab
    Jul 19, 2015 at 12:36
  • ILSpy in IL view - as well as Reflector in IL view or ILDASM output - should give you the complete "design". It may be - in particular for non-trivial assemblies - very well possible to identify the causes of errors of a VS project and link them to the IL view. The missing or erroneous parts may be reconstructed in C# from a manual conversion of IL code to C#. It is a similar but simpler procedure than the step from assembly to HL in other languages, Understanding a "design" is very much easier in a HL view like C#.
    – josh
    Jul 19, 2015 at 14:48
2

Currently, there's no way to modify the binary directly with ILSpy. The only solution is the one you described, export the source and recompile it.

However, the feature you are looking for is included with .NET Reflector in the Reflexil plugin.

2
  • I tried .NET Reflector with Reflexil plugin and I had some improvements. But once I found the method that I want to modify I go with "Replace all with code" but Reflexil show me it empty. So I can't modify it. Some help?
    – sivlab
    Jun 21, 2015 at 6:45
  • 1
    Reflexil is an excellent tool. To learn it, you may look at the tutorial in codeproject.com created by the inventor of Reflexil. The "Replace all with code" intentionally offers you an empty method intended to replace a given method completely with your own one.
    – josh
    Jul 19, 2015 at 14:52
1

I used piece of software a few years ago that allowed limited editing of the generated code directly within the tool itself, but when I say 'limited', I really mean limited. In fact, this feature was actually removed from more recent releases of this software. The old version that had this feature is no longer available, but it's still a decent and free .NET decompiler so if you want to check it out, it's called DotNet Resolver.

There's also the Reflexil plugin for Reflector, which has already been mentioned, but it's also pretty unreliable and limited.

If you really want to get things done, however, I'd recommend using the ILDASM and ILASM tools which are installed with Visual Studio.

I know you want to be able to just edit the high level code, but that's just not very feasible. You can use Reflector to export the source code generated from a disassembled .NET application as a project, but then you have errors, missing dependencies, and the like to deal with.

With ILDASM and ILASM you would be editing the MSIL directly, but it's really the best way to go about modifying a .NET application. MSIL is actually pretty simple and you won't have to deal with the source code produced by tools like Reflector, which are often riddled with errors. Moreover, you generally won't have to worry about obfuscation. In 99% of cases you will always be able to disassemble .NET applications down to MSIL and then reassemble them without any issue.

There are plenty of resources online to aid you with both editing and understanding MSIL if you're not familiar with it. Good luck!

1
  • Thanks a lot. As I wrote in above comment, yeah, the decompiler generates the code that I interested in but when I export it to VS the compilation failed due to the several errors. And without the complete knowledge of the software it is impossible to correct the errors (at least for me). I will try the tool in VS that you have mentioned modifying MSIL directly.
    – sivlab
    Jul 19, 2015 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.